Lungo.Boot.Resources.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Load Resources
  3. *
  4. * @namespace Lungo.Boot
  5. * @class Resources
  6. *
  7. * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  8. */
  9. Lungo.Boot.Resources = (function(lng, $$, undefined) {
  10. var ELEMENT = lng.Constants.ELEMENT;
  11. var ERROR = lng.Constants.ERROR;
  12. var RESOURCE = {
  13. SECTION: 'sections',
  14. TEMPLATE: 'templates',
  15. SCRIPT: 'scripts'
  16. };
  17. /**
  18. * Start loading async sections (local & remote)
  19. *
  20. * @method start
  21. *
  22. */
  23. var start = function() {
  24. var resources = lng.App.get('resources');
  25. for (resource_key in resources) {
  26. _loadResources(resource_key, resources[resource_key]);
  27. }
  28. _cacheDOMElements();
  29. };
  30. var _loadResources = function(resource_key, resources, callback) {
  31. for (index in resources) {
  32. var url = _parseUrl(resources[index], resource_key);
  33. try {
  34. var response = _loadAsyncResource(url);
  35. _factoryResources(resource_key, response);
  36. } catch(error) {
  37. lng.Core.log(3, error.message);
  38. }
  39. }
  40. };
  41. var _cacheDOMElements = function() {
  42. lng.Element.sections = lng.dom(ELEMENT.SECTION);
  43. lng.Element.asides = lng.dom(ELEMENT.ASIDE);
  44. // lng.Element.toolbars = lng.dom(ELEMENT.ASIDE);
  45. };
  46. var _parseUrl = function(section_url, folder) {
  47. return (/http/.test(section_url)) ? section_url : 'app/' + folder + '/' + section_url;
  48. };
  49. var _loadAsyncResource = function(url) {
  50. return $$.ajax({
  51. url: url,
  52. async: false,
  53. dataType: 'html',
  54. error: function() {
  55. throw new Error(ERROR.LOADING_RESOURCE + url);
  56. }
  57. });
  58. };
  59. var _factoryResources = function(resource_key, response) {
  60. switch(resource_key) {
  61. case RESOURCE.SECTION:
  62. _pushSectionInLayout(response);
  63. break;
  64. case RESOURCE.TEMPLATE:
  65. _createTemplate(response);
  66. break;
  67. case RESOURCE.SCRIPT:
  68. break;
  69. }
  70. };
  71. var _pushSectionInLayout = function(section) {
  72. if (lng.Core.toType(section) === 'string') {
  73. lng.dom(ELEMENT.BODY).append(section);
  74. }
  75. };
  76. var _createTemplate = function(markup) {
  77. var div = document.createElement(ELEMENT.DIV);
  78. div.innerHTML = markup;
  79. var template_id = lng.dom(div.firstChild).data('template');
  80. if (template_id) {
  81. lng.View.Template.create(template_id, markup);
  82. }
  83. };
  84. return {
  85. start: start
  86. };
  87. })(Lungo, Quo);