templates.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. *
  3. * @author: Malishev Dmitry <dima.malishev@gmail.com>
  4. */
  5. App.Templates.html = {
  6. WEB: {
  7. hint: [''],
  8. notification: [
  9. '<li class="top-bar-notification-item ~!:UNSEEN~!" id="notification-~!:ID~!">\
  10. <div class="top-bar-notification-header">\
  11. <p class="top-bar-notification-title">~!:TOPIC~!</p>\
  12. <a href="#" class="top-bar-notification-delete js-delete-notification">\
  13. <i class="fas fa-xmark"></i>\
  14. </a>\
  15. </div>\
  16. ~!:NOTICE~!\
  17. <p class="top-bar-notification-timestamp">~!:TIME~! ~!:DATE~!</p>\
  18. </li>',
  19. ],
  20. notification_empty: [
  21. '<li class="top-bar-notification-item empty">\
  22. <i class="fas fa-bell-slash status-icon dim"></i><p>' +
  23. App.Constants.NOTIFICATIONS_EMPTY +
  24. '</p>\
  25. </li>',
  26. ],
  27. notification_mark_all: [
  28. '<li>\
  29. <a href="#" class="top-bar-notification-mark-all js-mark-all-notifications">\
  30. <i class="fas fa-check"></i>' +
  31. App.Constants.NOTIFICATIONS_DELETE_ALL +
  32. '</a>\
  33. </li>',
  34. ],
  35. },
  36. };
  37. // Internals
  38. var Tpl = App.Templates;
  39. var Templator = function () {
  40. var init = function () {
  41. Templator.splitThemAll();
  42. Templator.freezeTplIndexes();
  43. };
  44. /**
  45. * Split the tpl strings into arrays
  46. */
  47. (Templator.splitThemAll = function () {
  48. jQuery.each(App.Templates.html, function (o) {
  49. //try{
  50. var tpls = App.Templates.html[o];
  51. jQuery.each(tpls, function (t) {
  52. tpls[t] = tpls[t][0].split('~!');
  53. });
  54. //}catch(e){fb.error('%o %o', o, e);}
  55. });
  56. }),
  57. /**
  58. * Iterates tpls
  59. */
  60. (Templator.freezeTplIndexes = function () {
  61. jQuery.each(App.Templates.html, Templator.cacheTplIndexes);
  62. }),
  63. /**
  64. * Grab the tpl group key and process it
  65. */
  66. (Templator.cacheTplIndexes = function (key) {
  67. var tpls = App.Templates.html[key];
  68. jQuery.each(tpls, function (o) {
  69. var tpl = tpls[o];
  70. Templator.catchIndex(key, o, tpl);
  71. });
  72. }),
  73. /**
  74. * Set the indexes
  75. */
  76. (Templator.catchIndex = function (key, ref_key, tpl) {
  77. 'undefined' == typeof App.Templates._indexes[key]
  78. ? (App.Templates._indexes[key] = {})
  79. : false;
  80. 'undefined' == typeof App.Templates._indexes[key][ref_key]
  81. ? (App.Templates._indexes[key][ref_key] = {})
  82. : false;
  83. jQuery(tpl).each(function (index, o) {
  84. if (':' == o.charAt(0)) {
  85. App.Templates._indexes[key][ref_key][o.toString()] = index;
  86. }
  87. });
  88. });
  89. /**
  90. * Get concrete templates
  91. */
  92. init();
  93. return Templator;
  94. };
  95. Templator.getTemplate = function (ns, key) {
  96. return [App.Templates._indexes[ns][key], App.Templates.html[ns][key].slice(0)];
  97. };
  98. // init templator
  99. Tpl.Templator = Templator();
  100. Tpl.get = function (key, group) {
  101. return Tpl.Templator.getTemplate(group, key);
  102. };