handlebars_helper.js 9.5 KB

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