handlebars_helper.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. define([
  2. 'locales',
  3. 'handlebars',
  4. 'diffMatchPatch'
  5. ], function(locale, Handlebars, DiffMatchPatch) {
  6. /**
  7. * Return a text as markdown.
  8. * Currently only a little helper to replace apidoc-inline Links (#Group:Name).
  9. * Should be replaced with a full markdown lib.
  10. * @param string text
  11. */
  12. Handlebars.registerHelper('markdown', function(text) {
  13. if ( ! text ) {
  14. return text;
  15. }
  16. text = text.replace(/((\[(.*?)\])?\(#)((.+?):(.+?))(\))/mg, function(match, p1, p2, p3, p4, p5, p6) {
  17. var link = p3 || p5 + '/' + p6;
  18. return '<a href="#api-' + p5 + '-' + p6 + '">' + link + '</a>';
  19. });
  20. return text;
  21. });
  22. /**
  23. * set paramater type.
  24. */
  25. Handlebars.registerHelper("setInputType", function(text) {
  26. if (text === "File") {
  27. return "file";
  28. }
  29. return "text";
  30. });
  31. /**
  32. * start/stop timer for simple performance check.
  33. */
  34. var timer;
  35. Handlebars.registerHelper('startTimer', function(text) {
  36. timer = new Date();
  37. return '';
  38. });
  39. Handlebars.registerHelper('stopTimer', function(text) {
  40. console.log(new Date() - timer);
  41. return '';
  42. });
  43. /**
  44. * Return localized Text.
  45. * @param string text
  46. */
  47. Handlebars.registerHelper('__', function(text) {
  48. return locale.__(text);
  49. });
  50. /**
  51. * Console log.
  52. * @param mixed obj
  53. */
  54. Handlebars.registerHelper('cl', function(obj) {
  55. console.log(obj);
  56. return '';
  57. });
  58. /**
  59. * Replace underscore with space.
  60. * @param string text
  61. */
  62. Handlebars.registerHelper('underscoreToSpace', function(text) {
  63. return text.replace(/(_+)/g, ' ');
  64. });
  65. /**
  66. *
  67. */
  68. Handlebars.registerHelper('assign', function(name) {
  69. if(arguments.length > 0) {
  70. var type = typeof(arguments[1]);
  71. var arg = null;
  72. if(type === 'string' || type === 'number' || type === 'boolean') arg = arguments[1];
  73. Handlebars.registerHelper(name, function() { return arg; });
  74. }
  75. return '';
  76. });
  77. /**
  78. *
  79. */
  80. Handlebars.registerHelper('nl2br', function(text) {
  81. return _handlebarsNewlineToBreak(text);
  82. });
  83. /**
  84. *
  85. */
  86. Handlebars.registerHelper('if_eq', function(context, options) {
  87. var compare = context;
  88. // Get length if context is an object
  89. if (context instanceof Object && ! (options.hash.compare instanceof Object))
  90. compare = Object.keys(context).length;
  91. if (compare === options.hash.compare)
  92. return options.fn(this);
  93. return options.inverse(this);
  94. });
  95. /**
  96. *
  97. */
  98. Handlebars.registerHelper('if_gt', function(context, options) {
  99. var compare = context;
  100. // Get length if context is an object
  101. if (context instanceof Object && ! (options.hash.compare instanceof Object))
  102. compare = Object.keys(context).length;
  103. if(compare > options.hash.compare)
  104. return options.fn(this);
  105. return options.inverse(this);
  106. });
  107. /**
  108. *
  109. */
  110. var templateCache = {};
  111. Handlebars.registerHelper('subTemplate', function(name, sourceContext) {
  112. if ( ! templateCache[name])
  113. templateCache[name] = Handlebars.compile($('#template-' + name).html());
  114. var template = templateCache[name];
  115. var templateContext = $.extend({}, this, sourceContext.hash);
  116. return new Handlebars.SafeString( template(templateContext) );
  117. });
  118. /**
  119. *
  120. */
  121. Handlebars.registerHelper('toLowerCase', function(value) {
  122. return (value && typeof value === 'string') ? value.toLowerCase() : '';
  123. });
  124. /**
  125. *
  126. */
  127. Handlebars.registerHelper('splitFill', function(value, splitChar, fillChar) {
  128. var splits = value.split(splitChar);
  129. return new Array(splits.length).join(fillChar) + splits[splits.length - 1];
  130. });
  131. /**
  132. * Convert Newline to HTML-Break (nl2br).
  133. *
  134. * @param {String} text
  135. * @returns {String}
  136. */
  137. function _handlebarsNewlineToBreak(text) {
  138. return ('' + text).replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
  139. }
  140. /**
  141. *
  142. */
  143. Handlebars.registerHelper('each_compare_list_field', function(source, compare, options) {
  144. var fieldName = options.hash.field;
  145. var newSource = [];
  146. if (source) {
  147. source.forEach(function(entry) {
  148. var values = entry;
  149. values['key'] = entry[fieldName];
  150. newSource.push(values);
  151. });
  152. }
  153. var newCompare = [];
  154. if (compare) {
  155. compare.forEach(function(entry) {
  156. var values = entry;
  157. values['key'] = entry[fieldName];
  158. newCompare.push(values);
  159. });
  160. }
  161. return _handlebarsEachCompared('key', newSource, newCompare, options);
  162. });
  163. /**
  164. *
  165. */
  166. Handlebars.registerHelper('each_compare_keys', function(source, compare, options) {
  167. var newSource = [];
  168. if (source) {
  169. var sourceFields = Object.keys(source);
  170. sourceFields.forEach(function(name) {
  171. var values = {};
  172. values['value'] = source[name];
  173. values['key'] = name;
  174. newSource.push(values);
  175. });
  176. }
  177. var newCompare = [];
  178. if (compare) {
  179. var compareFields = Object.keys(compare);
  180. compareFields.forEach(function(name) {
  181. var values = {};
  182. values['value'] = compare[name];
  183. values['key'] = name;
  184. newCompare.push(values);
  185. });
  186. }
  187. return _handlebarsEachCompared('key', newSource, newCompare, options);
  188. });
  189. /**
  190. *
  191. */
  192. Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
  193. return _handlebarsEachCompared('field', source, compare, options);
  194. });
  195. /**
  196. *
  197. */
  198. Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
  199. return _handlebarsEachCompared('title', source, compare, options);
  200. });
  201. /**
  202. *
  203. */
  204. Handlebars.registerHelper('reformat', function(source, type){
  205. if (type == 'json')
  206. try {
  207. return JSON.stringify(JSON.parse(source.trim()),null, " ");
  208. } catch(e) {
  209. }
  210. return source
  211. });
  212. /**
  213. *
  214. */
  215. Handlebars.registerHelper('showDiff', function(source, compare, options) {
  216. var ds = '';
  217. if(source === compare) {
  218. ds = source;
  219. } else {
  220. if( ! source)
  221. return compare;
  222. if( ! compare)
  223. return source;
  224. var d = diffMatchPatch.diff_main(stripHtml(compare), stripHtml(source));
  225. diffMatchPatch.diff_cleanupSemantic(d);
  226. ds = diffMatchPatch.diff_prettyHtml(d);
  227. ds = ds.replace(/&para;/gm, '');
  228. }
  229. if(options === 'nl2br')
  230. ds = _handlebarsNewlineToBreak(ds);
  231. return ds;
  232. });
  233. /**
  234. *
  235. */
  236. function _handlebarsEachCompared(fieldname, source, compare, options)
  237. {
  238. var dataList = [];
  239. var index = 0;
  240. if(source) {
  241. source.forEach(function(sourceEntry) {
  242. var found = false;
  243. if (compare) {
  244. compare.forEach(function(compareEntry) {
  245. if(sourceEntry[fieldname] === compareEntry[fieldname]) {
  246. var data = {
  247. typeSame: true,
  248. source: sourceEntry,
  249. compare: compareEntry,
  250. index: index
  251. };
  252. dataList.push(data);
  253. found = true;
  254. index++;
  255. }
  256. });
  257. }
  258. if ( ! found) {
  259. var data = {
  260. typeIns: true,
  261. source: sourceEntry,
  262. index: index
  263. };
  264. dataList.push(data);
  265. index++;
  266. }
  267. });
  268. }
  269. if (compare) {
  270. compare.forEach(function(compareEntry) {
  271. var found = false;
  272. if (source) {
  273. source.forEach(function(sourceEntry) {
  274. if(sourceEntry[fieldname] === compareEntry[fieldname])
  275. found = true;
  276. });
  277. }
  278. if ( ! found) {
  279. var data = {
  280. typeDel: true,
  281. compare: compareEntry,
  282. index: index
  283. };
  284. dataList.push(data);
  285. index++;
  286. }
  287. });
  288. }
  289. var ret = '';
  290. var length = dataList.length;
  291. for (var index in dataList) {
  292. if(index == (length - 1))
  293. dataList[index]['_last'] = true;
  294. ret = ret + options.fn(dataList[index]);
  295. }
  296. return ret;
  297. }
  298. var diffMatchPatch = new DiffMatchPatch();
  299. /**
  300. * Overwrite Colors
  301. */
  302. DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
  303. var html = [];
  304. var pattern_amp = /&/g;
  305. var pattern_lt = /</g;
  306. var pattern_gt = />/g;
  307. var pattern_para = /\n/g;
  308. for (var x = 0; x < diffs.length; x++) {
  309. var op = diffs[x][0]; // Operation (insert, delete, equal)
  310. var data = diffs[x][1]; // Text of change.
  311. var text = data.replace(pattern_amp, '&amp;').replace(pattern_lt, '&lt;')
  312. .replace(pattern_gt, '&gt;').replace(pattern_para, '&para;<br>');
  313. switch (op) {
  314. case DIFF_INSERT:
  315. html[x] = '<ins>' + text + '</ins>';
  316. break;
  317. case DIFF_DELETE:
  318. html[x] = '<del>' + text + '</del>';
  319. break;
  320. case DIFF_EQUAL:
  321. html[x] = '<span>' + text + '</span>';
  322. break;
  323. }
  324. }
  325. return html.join('');
  326. };
  327. /**
  328. * Fixes html after comparison (#506, #538, #616, #825)
  329. */
  330. function stripHtml(html){
  331. var div = document.createElement("div");
  332. div.innerHTML = html;
  333. return div.textContent || div.innerText || "";
  334. }
  335. // Exports
  336. return Handlebars;
  337. });