errors.jquery.js | |
---|---|
This jQuery plugin adds functions that make it easy to display friendly tips to a user based on Slowparse errors. For more information on Slowparse errors, see the error specification. For examples of this plugin in use, consult its test suite. | (function(jQuery) {
var $ = jQuery;
|
jQuery Extensions |
jQuery.extend({ |
jQuery.errorTemplates is a selection that contains all our error message templates. |
errorTemplates: $(), |
jQuery.loadErrors(basePath, names, cb) loads a set of error message templates.
| loadErrors: function(basePath, names, cb) {
var reqs = names.map(function(name) {
var url = basePath + "errors." + name + ".html";
return jQuery.get(url);
});
jQuery.when.apply(jQuery, reqs).then(function() {
reqs.forEach(function(req) {
var div = $('<div></div>').html(req.responseText);
$.errorTemplates = $.errorTemplates.add($(".error-msg", div));
});
cb(null);
}, function() {
cb("ERROR: At least one template file did not load.");
});
}
});
jQuery.fn.extend({ |
jQuery.fn.errorHighlightInterval() returns an object
containing | errorHighlightInterval: function() {
var interval = $(this).attr("data-highlight").split(",");
var start = parseInt(interval[0]);
var end = interval[1] ? parseInt(interval[1]) : undefined;
return {start: start, end: end};
},
|
jQuery.fn.eachErrorHighlight(cb) calls the given callback on
every element with a | eachErrorHighlight: function(cb) {
$("[data-highlight]", this).each(function(i) {
var interval = $(this).errorHighlightInterval();
cb.call(this, interval.start, interval.end, i);
});
return this;
},
|
jQuery.fn.fillError(error [, templates]) fills the current selection with the friendly error message for the given error object. For more information on error objects, see the error specification. Optionally, a second argument containing a selection of all
available error templates can be provided. If not present, the
global selection of templates from | fillError: function(error, templates) {
var selector = ".error-msg." + error.type;
var template = (templates || $.errorTemplates).filter(selector);
if (template.length == 0)
throw new Error("Error template not found for " + error.type);
this.html(_.template(template.html(), error, mustacheSettings)).show();
return this;
}
}); |
Underscore.js TemplatingWe define a private subset of underscore.js that includes only
The | var _ = (function createUnderscoreTemplating() { |
Certain characters need to be escaped so that they can be put into a string literal. | var escapes = {
'\\': '\\',
"'": "'",
'r': '\r',
'n': '\n',
't': '\t',
'u2028': '\u2028',
'u2029': '\u2029'
};
for (var p in escapes) escapes[escapes[p]] = p;
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
var _ = { |
Escape a string for HTML interpolation. | escape: function escape(string) {
return (''+string)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g,'/');
},
|
JavaScript micro-templating, similar to John Resig's implementation. Underscore templating handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. | template: function template(text, data, settings) { |
Compile the template source, taking care to escape characters that cannot be included in a string literal and then unescape them in code blocks. | var source = "__p+='" + text
.replace(escaper, function(match) {
return '\\' + escapes[match];
})
.replace(settings.escape || noMatch, function(match, code) {
return "'+\n((__t=(" + unescape(code) +
"))==null?'':_.escape(__t))+\n'";
}) + "';\n"; |
If a variable is not specified, place data values in local scope. | if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'')};\n" +
source + "return __p;\n";
var render = new Function(settings.variable || 'obj', '_', source);
if (data) return render(data, _);
var template = function(data) {
return render.call(this, data, _);
}; |
Provide the compiled function source as a convenience for precompilation. | template.source = 'function(' + (settings.variable || 'obj') +
'){\n' + source + '}';
return template;
}
};
return _;
})();
|
We want to use "mustache"-style templating, e.g. | var mustacheSettings = {
escape: /\{\{(.+?)\}\}/g
};
})(jQuery);
|