templates.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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="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" style="font-size: 4rem;"></i><br><br>\
  17. '+App.Constants.NOTIFICATIONS_EMPTY+'\</span></li>'
  18. ]
  19. },
  20. };
  21. // Internals
  22. var Tpl = App.Templates;
  23. var Templator = function()
  24. {
  25. var init = function()
  26. {
  27. fb.info('Templator work');
  28. Templator.splitThemAll();
  29. Templator.freezeTplIndexes();
  30. };
  31. /**
  32. * Split the tpl strings into arrays
  33. */
  34. Templator.splitThemAll = function(){
  35. fb.info('splitting tpls');
  36. jQuery.each(App.Templates.html, function(o){
  37. //try{
  38. var tpls = App.Templates.html[o];
  39. jQuery.each(tpls, function(t){
  40. tpls[t] = tpls[t][0].split('~!');
  41. });
  42. //}catch(e){fb.error('%o %o', o, e);}
  43. });
  44. },
  45. /**
  46. * Iterates tpls
  47. */
  48. Templator.freezeTplIndexes = function(){
  49. fb.info('freezing tpl keys');
  50. jQuery.each(App.Templates.html, Templator.cacheTplIndexes);
  51. },
  52. /**
  53. * Grab the tpl group key and process it
  54. */
  55. Templator.cacheTplIndexes = function(key)
  56. {
  57. var tpls = App.Templates.html[key];
  58. jQuery.each(tpls, function(o)
  59. {
  60. var tpl = tpls[o];
  61. Templator.catchIndex(key, o, tpl);
  62. });
  63. },
  64. /**
  65. * Set the indexes
  66. */
  67. Templator.catchIndex = function(key, ref_key, tpl)
  68. {
  69. 'undefined' == typeof App.Templates._indexes[key] ? App.Templates._indexes[key] = {} : false;
  70. 'undefined' == typeof App.Templates._indexes[key][ref_key] ?
  71. App.Templates._indexes[key][ref_key] = {} : false;
  72. jQuery(tpl).each(function(index, o) {
  73. if (':' == o.charAt(0)) {
  74. App.Templates._indexes[key][ref_key][o.toString()] = index;
  75. }
  76. });
  77. }
  78. /**
  79. * Get concrete templates
  80. */
  81. init();
  82. return Templator;
  83. };
  84. Templator.getTemplate = function(ns, key){
  85. return [
  86. App.Templates._indexes[ns][key],
  87. App.Templates.html[ns][key].slice(0)
  88. ];
  89. }
  90. // init templator
  91. Tpl.Templator = Templator();
  92. Tpl.get = function(key, group){
  93. return Tpl.Templator.getTemplate(group, key);
  94. }