handlebars_helper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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('gen_body', function(context, options) {
  193. let strBody = {};
  194. context.forEach(element => {
  195. element.field = element.field.replace("]", "");
  196. switch (element.type.toLowerCase()) {
  197. case "string":
  198. if (element.field.includes('[')) {
  199. if (strBody[element.field.split("[")[0]] === undefined) {
  200. strBody[element.field.split("[")[0]] = {};
  201. }
  202. strBody[element.field.split("[")[0]][element.field.split("[")[1]] = (element.defaultValue || "");
  203. break;
  204. }
  205. strBody[element.field] = (element.defaultValue || "");
  206. break;
  207. case "number":
  208. if (element.field.includes('[')) {
  209. if (strBody[element.field.split("[")[0]] === undefined) {
  210. strBody[element.field.split("[")[0]] = {};
  211. }
  212. strBody[element.field.split("[")[0]][element.field.split("[")[1]] = (element.defaultValue || 0);
  213. break;
  214. }
  215. strBody[element.field] = (element.defaultValue || 0);
  216. break;
  217. case "object":
  218. if (strBody[element.field] === undefined) {
  219. strBody[element.field] = {};
  220. }
  221. break;
  222. default:
  223. strBody[element.field] = null;
  224. }
  225. });
  226. return JSON.stringify(strBody, null, 4);
  227. });
  228. /**
  229. *
  230. */
  231. Handlebars.registerHelper('each_compare_field', function(source, compare, options) {
  232. return _handlebarsEachCompared('field', source, compare, options);
  233. });
  234. /**
  235. *
  236. */
  237. Handlebars.registerHelper('each_compare_title', function(source, compare, options) {
  238. return _handlebarsEachCompared('title', source, compare, options);
  239. });
  240. /**
  241. *
  242. */
  243. Handlebars.registerHelper('reformat', function(source, type){
  244. if (type == 'json')
  245. try {
  246. return JSON.stringify(JSON.parse(source.trim()),null, " ");
  247. } catch(e) {
  248. }
  249. return source
  250. });
  251. /**
  252. *
  253. */
  254. Handlebars.registerHelper('showDiff', function(source, compare, options) {
  255. var ds = '';
  256. if(source === compare) {
  257. ds = source;
  258. } else {
  259. if( ! source)
  260. return compare;
  261. if( ! compare)
  262. return source;
  263. var d = diffMatchPatch.diff_main(stripHtml(compare), stripHtml(source));
  264. diffMatchPatch.diff_cleanupSemantic(d);
  265. ds = diffMatchPatch.diff_prettyHtml(d);
  266. ds = ds.replace(/&para;/gm, '');
  267. }
  268. if(options === 'nl2br')
  269. ds = _handlebarsNewlineToBreak(ds);
  270. return ds;
  271. });
  272. /**
  273. *
  274. */
  275. function _handlebarsEachCompared(fieldname, source, compare, options)
  276. {
  277. var dataList = [];
  278. var index = 0;
  279. if(source) {
  280. source.forEach(function(sourceEntry) {
  281. var found = false;
  282. if (compare) {
  283. compare.forEach(function(compareEntry) {
  284. if(sourceEntry[fieldname] === compareEntry[fieldname]) {
  285. var data = {
  286. typeSame: true,
  287. source: sourceEntry,
  288. compare: compareEntry,
  289. index: index
  290. };
  291. dataList.push(data);
  292. found = true;
  293. index++;
  294. }
  295. });
  296. }
  297. if ( ! found) {
  298. var data = {
  299. typeIns: true,
  300. source: sourceEntry,
  301. index: index
  302. };
  303. dataList.push(data);
  304. index++;
  305. }
  306. });
  307. }
  308. if (compare) {
  309. compare.forEach(function(compareEntry) {
  310. var found = false;
  311. if (source) {
  312. source.forEach(function(sourceEntry) {
  313. if(sourceEntry[fieldname] === compareEntry[fieldname])
  314. found = true;
  315. });
  316. }
  317. if ( ! found) {
  318. var data = {
  319. typeDel: true,
  320. compare: compareEntry,
  321. index: index
  322. };
  323. dataList.push(data);
  324. index++;
  325. }
  326. });
  327. }
  328. var ret = '';
  329. var length = dataList.length;
  330. for (var index in dataList) {
  331. if(index == (length - 1))
  332. dataList[index]['_last'] = true;
  333. ret = ret + options.fn(dataList[index]);
  334. }
  335. return ret;
  336. }
  337. var diffMatchPatch = new DiffMatchPatch();
  338. /**
  339. * Overwrite Colors
  340. */
  341. DiffMatchPatch.prototype.diff_prettyHtml = function(diffs) {
  342. var html = [];
  343. var pattern_amp = /&/g;
  344. var pattern_lt = /</g;
  345. var pattern_gt = />/g;
  346. var pattern_para = /\n/g;
  347. for (var x = 0; x < diffs.length; x++) {
  348. var op = diffs[x][0]; // Operation (insert, delete, equal)
  349. var data = diffs[x][1]; // Text of change.
  350. var text = data.replace(pattern_amp, '&amp;').replace(pattern_lt, '&lt;')
  351. .replace(pattern_gt, '&gt;').replace(pattern_para, '&para;<br>');
  352. switch (op) {
  353. case DIFF_INSERT:
  354. html[x] = '<ins>' + text + '</ins>';
  355. break;
  356. case DIFF_DELETE:
  357. html[x] = '<del>' + text + '</del>';
  358. break;
  359. case DIFF_EQUAL:
  360. html[x] = '<span>' + text + '</span>';
  361. break;
  362. }
  363. }
  364. return html.join('');
  365. };
  366. /**
  367. * Fixes html after comparison (#506, #538, #616, #825)
  368. */
  369. function stripHtml(html){
  370. var div = document.createElement("div");
  371. div.innerHTML = html;
  372. return div.textContent || div.innerText || "";
  373. }
  374. // Exports
  375. return Handlebars;
  376. });