Source js/bauplan.templates.js

  1. define([
  2. "handlebars"
  3. ], function (Handlebars) {
  4. /**
  5. * @module bauplan%templates
  6. * @description ## Templates
  7. *
  8. * Loads and compiles template files for use by Handlebars
  9. *
  10. * @return {instance} BauplanTemplates
  11. */
  12. var BauplanTemplates = {
  13. templates: {},
  14. raw: {},
  15. /**
  16. * @param {function} callback
  17. */
  18. init: function (callback) {
  19. this.load(this.Config.config.templates, callback);
  20. },
  21. /**
  22. * @method load
  23. * @instance
  24. * @description Load template files for app
  25. * @param {object} templates Template path key/value pairs
  26. * @param {function} callback
  27. */
  28. load: function BauplanTemplatesLoad (templates, callback) {
  29. var paths = this.Require.paths(templates, {
  30. suffix: "template",
  31. extension: "hbs"
  32. });
  33. var Templates = this;
  34. this.Require.load(paths, {
  35. plugin: "text",
  36. paths: paths,
  37. keys: paths.originalkeys,
  38. /**
  39. * @method filterTemplate
  40. * @inner
  41. * @param {string} template Raw template string
  42. * @description Turns controls into view helpers
  43. * @return {function} Filtered template string
  44. */
  45. filterTemplate: function BauplanFilterTemplate (template) {
  46. template = template.replace(/\{\{control ([^ \{}]+)( ([^\{]+?)){0,1}\}\}/g,
  47. function(m, m1, m2) {
  48. var controlname = m1.replace(/"/g, "");
  49. var controlclass = "control control-" + controlname;
  50. m2 = m2 || "";
  51. if (m2.indexOf("edit=") === -1) {
  52. m2 += " edit=edit";
  53. }
  54. if (m2.indexOf("display=") === -1) {
  55. m2 += " display=display";
  56. }
  57. if (m2.indexOf("control-model=") === -1) {
  58. m2 += " control-model=model";
  59. }
  60. var newtmpl = '{{view "control" class="' + controlclass + '" control-name=' + m1 + " " + m2 + '}}';
  61. return newtmpl;
  62. });
  63. return template;
  64. },
  65. /**
  66. * @method processValue
  67. * @inner
  68. * @param {string} value Template string
  69. * @param {object} bundle
  70. * @param {string} bundle.key
  71. * @param {array} bundle.keys
  72. * @param {string} bundle.value
  73. * @description Filters template and compiles string
  74. *
  75. * Invoked by {@link module:bauplan%require}
  76. * @return {function} Compiled template
  77. */
  78. processValue: function(value, bundle, options) {
  79. var template = bundle.key;
  80. if (value !== Templates.raw[template]) {
  81. Templates.raw[template] = value;
  82. value = this.filterTemplate(value);
  83. Templates.templates[template] = Handlebars.compile(value);
  84. Handlebars.templates[template] = Templates.templates[template];
  85. }
  86. return Templates.templates[template];
  87. },
  88. /**
  89. * @method callback
  90. * @inner
  91. * @param {object} templates Object of compiled templates
  92. * @return {object} Compiled templates
  93. */
  94. callback: function (templates) {
  95. callback(templates);
  96. }
  97. });
  98. }
  99. };
  100. return BauplanTemplates;
  101. });