handlebars_helper.js 10 KB

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