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.

assets.js 2.8 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. wn.assets.execute(src);
  14. }
  15. };
  16. wn.assets = {
  17. // keep track of executed assets
  18. executed_ : {},
  19. check: function() {
  20. // if version is different then clear localstorage
  21. if(window._version_number != localStorage.getItem("_version_number")) {
  22. localStorage.clear();
  23. console.log("Cleared App Cache.");
  24. }
  25. if(localStorage._last_load) {
  26. var not_updated_since = new Date() - new Date(localStorage._last_load);
  27. if(not_updated_since < 10000 || not_updated_since > 86400000) {
  28. localStorage.clear();
  29. console.log("Cleared localstorage");
  30. }
  31. } else {
  32. localStorage.clear();
  33. console.log("Cleared localstorage");
  34. }
  35. wn.assets.init_local_storage();
  36. },
  37. init_local_storage: function() {
  38. localStorage._last_load = new Date();
  39. localStorage._version_number = window._version_number;
  40. if(wn.boot) localStorage.metadata_version = wn.boot.metadata_version;
  41. },
  42. // check if the asset exists in
  43. // localstorage
  44. exists: function(src) {
  45. if('localStorage' in window
  46. && localStorage.getItem(src) && (wn.boot ? !wn.boot.developer_mode : true))
  47. return true;
  48. },
  49. // add the asset to
  50. // localstorage
  51. add: function(src, txt) {
  52. if('localStorage' in window) {
  53. try {
  54. localStorage.setItem(src, txt);
  55. } catch(e) {
  56. // if quota is exceeded, clear local storage and set item
  57. localStorage.clear();
  58. console.log("Local Storage cleared");
  59. localStorage.setItem(src, txt);
  60. }
  61. }
  62. },
  63. get: function(src) {
  64. return localStorage.getItem(src);
  65. },
  66. extn: function(src) {
  67. if(src.indexOf('?')!=-1) {
  68. src = src.split('?').slice(-1)[0];
  69. }
  70. return src.split('.').slice(-1)[0];
  71. },
  72. // load an asset via
  73. load: function(src) {
  74. // this is virtual page load, only get the the source
  75. // *without* the template
  76. wn.set_loading();
  77. wn.call({
  78. method:"webnotes.client.get_js",
  79. args: {
  80. "src": src
  81. },
  82. callback: function(r) {
  83. wn.assets.add(src, r.message);
  84. },
  85. async: false
  86. })
  87. wn.done_loading();
  88. },
  89. // pass on to the handler to set
  90. execute: function(src) {
  91. if(!wn.assets.exists(src)) {
  92. wn.assets.load(src);
  93. }
  94. var type = wn.assets.extn(src);
  95. if(wn.assets.handler[type]) {
  96. wn.assets.handler[type](wn.assets.get(src), src);
  97. wn.assets.executed_[src] = 1;
  98. }
  99. },
  100. // handle types of assets
  101. // and launch them in the
  102. // app
  103. handler: {
  104. js: function(txt, src) {
  105. wn.dom.eval(txt);
  106. },
  107. css: function(txt, src) {
  108. wn.dom.set_style(txt);
  109. }
  110. }
  111. };