templates.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. *
  3. * @author: Malishev Dmitry <dima.malishev@gmail.com>
  4. */
  5. App.Templates.html = {
  6. WEB: {
  7. hint: ['']
  8. }
  9. };
  10. // Internals
  11. var Tpl = App.Templates;
  12. var Templator = function()
  13. {
  14. var init = function()
  15. {
  16. fb.info('Templator work');
  17. Templator.splitThemAll();
  18. Templator.freezeTplIndexes();
  19. };
  20. /**
  21. * Split the tpl strings into arrays
  22. */
  23. Templator.splitThemAll = function(){
  24. fb.info('splitting tpls');
  25. jQuery.each(App.Templates.html, function(o){
  26. //try{
  27. var tpls = App.Templates.html[o];
  28. jQuery.each(tpls, function(t){
  29. tpls[t] = tpls[t][0].split('~!');
  30. });
  31. //}catch(e){fb.error('%o %o', o, e);}
  32. });
  33. },
  34. /**
  35. * Iterates tpls
  36. */
  37. Templator.freezeTplIndexes = function(){
  38. fb.info('freezing tpl keys');
  39. jQuery.each(App.Templates.html, Templator.cacheTplIndexes);
  40. },
  41. /**
  42. * Grab the tpl group key and process it
  43. */
  44. Templator.cacheTplIndexes = function(key)
  45. {
  46. var tpls = App.Templates.html[key];
  47. jQuery.each(tpls, function(o)
  48. {
  49. var tpl = tpls[o];
  50. Templator.catchIndex(key, o, tpl);
  51. });
  52. },
  53. /**
  54. * Set the indexes
  55. */
  56. Templator.catchIndex = function(key, ref_key, tpl)
  57. {
  58. 'undefined' == typeof App.Templates._indexes[key] ? App.Templates._indexes[key] = {} : false;
  59. 'undefined' == typeof App.Templates._indexes[key][ref_key] ?
  60. App.Templates._indexes[key][ref_key] = {} : false;
  61. jQuery(tpl).each(function(index, o) {
  62. if (':' == o.charAt(0)) {
  63. App.Templates._indexes[key][ref_key][o.toString()] = index;
  64. }
  65. });
  66. }
  67. /**
  68. * Get concrete templates
  69. */
  70. init();
  71. return Templator;
  72. };
  73. Templator.getTemplate = function(ns, key){
  74. return [
  75. App.Templates._indexes[ns][key],
  76. App.Templates.html[ns][key].slice(0)
  77. ];
  78. }
  79. // init templator
  80. Tpl.Templator = Templator();
  81. Tpl.get = function(key, group){
  82. return Tpl.Templator.getTemplate(group, key);
  83. }