templates.js 2.4 KB

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