5 ], function(locale, Handlebars, DiffMatchPatch) {
8 * Return a text as markdown.
9 * Currently only a little helper to replace apidoc-inline Links (#Group:Name).
10 * Should be replaced with a full markdown lib.
13 Handlebars.registerHelper('markdown', function(text) {
17 text = text.replace(/((\[(.*?)\])?\(#)((.+?):(.+?))(\))/mg, function(match, p1, p2, p3, p4, p5, p6) {
18 var link = p3 || p5 + '/' + p6;
19 return '<a href="#api-' + p5 + '-' + p6 + '">' + link + '</a>';
27 Handlebars.registerHelper("setInputType", function(text) {
28 if (text === "File") {
35 * start/stop timer for simple performance check.
38 Handlebars.registerHelper('startTimer', function(text) {
43 Handlebars.registerHelper('stopTimer', function(text) {
44 console.log(new Date() - timer);
49 * Return localized Text.
52 Handlebars.registerHelper('__', function(text) {
53 return locale.__(text);
60 Handlebars.registerHelper('cl', function(obj) {
66 * Replace underscore with space.
69 Handlebars.registerHelper('underscoreToSpace', function(text) {
70 return text.replace(/(_+)/g, ' ');
76 Handlebars.registerHelper('assign', function(name) {
77 if(arguments.length > 0) {
78 var type = typeof(arguments[1]);
80 if(type === 'string' || type === 'number' || type === 'boolean') arg = arguments[1];
81 Handlebars.registerHelper(name, function() { return arg; });
89 Handlebars.registerHelper('nl2br', function(text) {
90 return _handlebarsNewlineToBreak(text);
96 Handlebars.registerHelper('if_eq', function(context, options) {
97 var compare = context;
98 // Get length if context is an object
99 if (context instanceof Object && ! (options.hash.compare instanceof Object))
100 compare = Object.keys(context).length;
102 if (compare === options.hash.compare)
103 return options.fn(this);
105 return options.inverse(this);
111 Handlebars.registerHelper('if_gt', function(context, options) {
112 var compare = context;
113 // Get length if context is an object
114 if (context instanceof Object && ! (options.hash.compare instanceof Object))
115 compare = Object.keys(context).length;
117 if(compare > options.hash.compare)
118 return options.fn(this);
120 return options.inverse(this);
126 var templateCache = {};
127 Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
128 if ( ! templateCache[name])
129 templateCache[name] = Handlebars.compile($('#template-' + name).html());
131 var template = templateCache[name];
132 var templateContext = $.extend({}, this, sourceContext.hash);
133 return new Handlebars.SafeString( template(templateContext) );
139 Handlebars.registerHelper('toLowerCase', function(value) {
140 return (value && typeof value === 'string') ? value.toLowerCase() : '';
146 Handlebars.registerHelper('splitFill', function(value, splitChar, fillChar) {
147 var splits = value.split(splitChar);
148 return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
152 * Convert Newline to HTML-Break (nl2br).
154 * @param {String} text
157 function _handlebarsNewlineToBreak(text) {
158 return ('' + text).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
164 Handlebars.registerHelper('each_compare_list_field', function(source, compare, options) {
165 var fieldName = options.hash.field;
168 source.forEach(function(entry) {
170 values['key'] = entry[fieldName];
171 newSource.push(values);
177 compare.forEach(function(entry) {
179 values['key'] = entry[fieldName];
180 newCompare.push(values);
183 return _handlebarsEachCompared('key', newSource, newCompare, options);
189 Handlebars.registerHelper('each_compare_keys', function(source, compare, options) {
192 var sourceFields = Object.keys(source);
193 sourceFields.forEach(function(name) {
195 values['value'] = source[name];
196 values['key'] = name;
197 newSource.push(values);
203 var compareFields = Object.keys(compare);
204 compareFields.forEach(function(name) {
206 values['value'] = compare[name];
207 values['key'] = name;
208 newCompare.push(values);
211 return _handlebarsEachCompared('key', newSource, newCompare, options);
217 Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
218 return _handlebarsEachCompared('field', source, compare, options);
224 Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
225 return _handlebarsEachCompared('title', source, compare, options);
231 Handlebars.registerHelper('reformat', function(source, type){
234 return JSON.stringify(JSON.parse(source.trim()),null, " ");
244 Handlebars.registerHelper('showDiff', function(source, compare, options) {
246 if(source === compare) {
255 var d = diffMatchPatch.diff_main(stripHtml(compare), stripHtml(source));
256 diffMatchPatch.diff_cleanupSemantic(d);
257 ds = diffMatchPatch.diff_prettyHtml(d);
258 ds = ds.replace(/¶/gm, '');
260 if(options === 'nl2br')
261 ds = _handlebarsNewlineToBreak(ds);
269 function _handlebarsEachCompared(fieldname, source, compare, options)
274 source.forEach(function(sourceEntry) {
277 compare.forEach(function(compareEntry) {
278 if(sourceEntry[fieldname] === compareEntry[fieldname]) {
282 compare: compareEntry,
304 compare.forEach(function(compareEntry) {
307 source.forEach(function(sourceEntry) {
308 if(sourceEntry[fieldname] === compareEntry[fieldname])
315 compare: compareEntry,
325 var length = dataList.length;
326 for (var index in dataList) {
327 if(index == (length - 1))
328 dataList[index]['_last'] = true;
329 ret = ret + options.fn(dataList[index]);
334 var diffMatchPatch = new DiffMatchPatch();
339 DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
341 var pattern_amp = /&/g;
342 var pattern_lt = /</g;
343 var pattern_gt = />/g;
344 var pattern_para = /\n/g;
345 for (var x = 0; x < diffs.length; x++) {
346 var op = diffs[x][0]; // Operation (insert, delete, equal)
347 var data = diffs[x][1]; // Text of change.
348 var text = data.replace(pattern_amp, '&').replace(pattern_lt, '<')
349 .replace(pattern_gt, '>').replace(pattern_para, '¶<br>');
352 html[x] = '<ins>' + text + '</ins>';
355 html[x] = '<del>' + text + '</del>';
358 html[x] = '<span>' + text + '</span>';
362 return html.join('');
366 * Fixes html after comparison (#506, #538, #616, #825)
368 function stripHtml(html){
369 var div = document.createElement("div");
370 div.innerHTML = html;
371 return div.textContent || div.innerText || "";