You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 line
3.0 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. // library to mange assets (js, css, models, html) etc in the app.
  4. // will try and get from localStorge if latest are available
  5. // depends on wn.versions to manage versioning
  6. wn.require = function(items) {
  7. if(typeof items === "string") {
  8. items = [items];
  9. }
  10. var l = items.length;
  11. for(var i=0; i< l; i++) {
  12. var src = items[i];
  13. //if(!(src in wn.assets.executed_)) {
  14. // check if available in localstorage
  15. wn.assets.execute(src);
  16. //}
  17. }
  18. };
  19. wn.assets = {
  20. // keep track of executed assets
  21. executed_ : {},
  22. check: function() {
  23. // if version is different then clear localstorage
  24. if(window._version_number != localStorage.getItem("_version_number")) {
  25. localStorage.clear();
  26. console.log("Cleared App Cache.");
  27. }
  28. if(localStorage._last_load) {
  29. var not_updated_since = new Date() - new Date(localStorage._last_load);
  30. if(not_updated_since < 10000 || not_updated_since > 86400000) {
  31. localStorage.clear();
  32. console.log("Cleared localstorage");
  33. }
  34. } else {
  35. localStorage.clear();
  36. console.log("Cleared localstorage");
  37. }
  38. wn.assets.init_local_storage();
  39. },
  40. init_local_storage: function() {
  41. localStorage._last_load = new Date();
  42. localStorage._version_number = window._version_number;
  43. if(wn.boot) localStorage.metadata_version = wn.boot.metadata_version;
  44. },
  45. // check if the asset exists in
  46. // localstorage
  47. exists: function(src) {
  48. if('localStorage' in window
  49. && localStorage.getItem(src) && (wn.boot ? !wn.boot.developer_mode : true))
  50. return true;
  51. },
  52. // add the asset to
  53. // localstorage
  54. add: function(src, txt) {
  55. if('localStorage' in window) {
  56. try {
  57. localStorage.setItem(src, txt);
  58. } catch(e) {
  59. // if quota is exceeded, clear local storage and set item
  60. localStorage.clear();
  61. console.log("Local Storage cleared");
  62. localStorage.setItem(src, txt);
  63. }
  64. }
  65. },
  66. get: function(src) {
  67. return localStorage.getItem(src);
  68. },
  69. extn: function(src) {
  70. if(src.indexOf('?')!=-1) {
  71. src = src.split('?').slice(-1)[0];
  72. }
  73. return src.split('.').slice(-1)[0];
  74. },
  75. // load an asset via
  76. load: function(src) {
  77. // this is virtual page load, only get the the source
  78. // *without* the template
  79. var t = src;
  80. wn.set_loading();
  81. $.ajax({
  82. url: t,
  83. data: {
  84. q: Math.floor(Math.random()*1000)
  85. },
  86. dataType: 'text',
  87. success: function(txt) {
  88. // add it to localstorage
  89. wn.assets.add(src, txt);
  90. },
  91. async: false
  92. });
  93. wn.done_loading();
  94. },
  95. // pass on to the handler to set
  96. execute: function(src) {
  97. if(!wn.assets.exists(src)) {
  98. wn.assets.load(src);
  99. }
  100. var type = wn.assets.extn(src);
  101. if(wn.assets.handler[type]) {
  102. wn.assets.handler[type](wn.assets.get(src), src);
  103. wn.assets.executed_[src] = 1;
  104. }
  105. },
  106. // handle types of assets
  107. // and launch them in the
  108. // app
  109. handler: {
  110. js: function(txt, src) {
  111. wn.dom.eval(txt);
  112. },
  113. css: function(txt, src) {
  114. wn.dom.set_style(txt);
  115. }
  116. }
  117. };