Helpers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. define([ "require", "handlebars" ], function(require, Handlebars, localization) {
  2. var HHelpers = {};
  3. return HHelpers.nl2br = function(text) {
  4. text = Handlebars.Utils.escapeExpression(text);
  5. var nl2br = (text + "").replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, "$1<br>$2");
  6. return new Handlebars.SafeString(nl2br);
  7. }, Handlebars.registerHelper("nl2br", HHelpers.nl2br), Handlebars.registerHelper("toHumanDate", function(val) {
  8. return val ? val : "";
  9. }), Handlebars.registerHelper("tt", function(str) {
  10. return str;
  11. }), Handlebars.registerHelper("ifCond", function(v1, operator, v2, options) {
  12. switch (operator) {
  13. case "==":
  14. return v1 == v2 ? options.fn(this) : options.inverse(this);
  15. case "===":
  16. return v1 === v2 ? options.fn(this) : options.inverse(this);
  17. case "!=":
  18. return v1 !== v2 ? options.fn(this) : options.inverse(this);
  19. case "!==":
  20. return v1 !== v2 ? options.fn(this) : options.inverse(this);
  21. case "<":
  22. return v1 < v2 ? options.fn(this) : options.inverse(this);
  23. case "<=":
  24. return v1 <= v2 ? options.fn(this) : options.inverse(this);
  25. case ">":
  26. return v1 > v2 ? options.fn(this) : options.inverse(this);
  27. case ">=":
  28. return v1 >= v2 ? options.fn(this) : options.inverse(this);
  29. case "isEmpty":
  30. return _.isEmpty(v1) ? options.fn(this) : options.inverse(this);
  31. case "has":
  32. return _.has(v1, v2) ? options.fn(this) : options.inverse(this);
  33. default:
  34. return options.inverse(this);
  35. }
  36. }), Handlebars.registerHelper("arithmetic", function(val1, operator, val2, commaFormat, options) {
  37. var v1 = val1 && parseInt(val1.toString().replace(/\,/g, "")) || 0, v2 = val2 && parseInt(val2.toString().replace(/\,/g, "")) || 0, val = null;
  38. switch (operator) {
  39. case "+":
  40. val = v1 + v2;
  41. break;
  42. case "-":
  43. val = v1 - v2;
  44. break;
  45. case "/":
  46. val = v1 / v2;
  47. break;
  48. case "*":
  49. val = v1 * v2;
  50. break;
  51. case "%":
  52. val = v1 % v2;
  53. break;
  54. default:
  55. val = 0;
  56. }
  57. return commaFormat === !1 ? val : _.numberFormatWithComma(val);
  58. }), Handlebars.registerHelper("lookup", function(obj, field, defaulValue) {
  59. return obj[field] ? obj[field] : defaulValue ? defaulValue : "";
  60. }), Handlebars.registerHelper("eachlookup", function(obj, field, options) {
  61. return Handlebars.helpers.each(obj[field] ? obj[field] : null, options);
  62. }), Handlebars.registerHelper("callmyfunction", function(functionObj, param, options) {
  63. var argumentObj = _.extend([], arguments);
  64. return argumentObj.shift(), functionObj.apply(this, argumentObj);
  65. }), HHelpers;
  66. });