templates.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. '+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. 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. {
  54. var tpls = App.Templates.html[key];
  55. jQuery.each(tpls, function(o)
  56. {
  57. var tpl = tpls[o];
  58. Templator.catchIndex(key, o, tpl);
  59. });
  60. },
  61. /**
  62. * Set the indexes
  63. */
  64. Templator.catchIndex = function(key, ref_key, tpl)
  65. {
  66. 'undefined' == typeof App.Templates._indexes[key] ? App.Templates._indexes[key] = {} : false;
  67. 'undefined' == typeof App.Templates._indexes[key][ref_key] ?
  68. App.Templates._indexes[key][ref_key] = {} : 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 [
  83. App.Templates._indexes[ns][key],
  84. App.Templates.html[ns][key].slice(0)
  85. ];
  86. }
  87. // init templator
  88. Tpl.Templator = Templator();
  89. Tpl.get = function(key, group){
  90. return Tpl.Templator.getTemplate(group, key);
  91. }