選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

assets.js 2.8 KiB

14年前
14年前
14年前
14年前
14年前
14年前
14年前
14年前
14年前
14年前
14年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  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. localStorage._last_load = new Date();
  39. localStorage._version_number = window._version_number;
  40. },
  41. // check if the asset exists in
  42. // localstorage
  43. exists: function(src) {
  44. if('localStorage' in window
  45. && localStorage.getItem(src) && (wn.boot ? !wn.boot.developer_mode : true))
  46. return true;
  47. },
  48. // add the asset to
  49. // localstorage
  50. add: function(src, txt) {
  51. if('localStorage' in window) {
  52. try {
  53. localStorage.setItem(src, txt);
  54. } catch(e) {
  55. // if quota is exceeded, clear local storage and set item
  56. localStorage.clear();
  57. console.log("Local Storage cleared");
  58. localStorage.setItem(src, txt);
  59. }
  60. }
  61. },
  62. get: function(src) {
  63. return localStorage.getItem(src);
  64. },
  65. extn: function(src) {
  66. if(src.indexOf('?')!=-1) {
  67. src = src.split('?').slice(-1)[0];
  68. }
  69. return src.split('.').slice(-1)[0];
  70. },
  71. // load an asset via
  72. load: function(src) {
  73. // this is virtual page load, only get the the source
  74. // *without* the template
  75. var t = src;
  76. wn.set_loading();
  77. $.ajax({
  78. url: t,
  79. data: {
  80. q: Math.floor(Math.random()*1000)
  81. },
  82. dataType: 'text',
  83. success: function(txt) {
  84. // add it to localstorage
  85. wn.assets.add(src, txt);
  86. },
  87. async: false
  88. });
  89. wn.done_loading();
  90. },
  91. // pass on to the handler to set
  92. execute: function(src) {
  93. if(!wn.assets.exists(src)) {
  94. wn.assets.load(src);
  95. }
  96. var type = wn.assets.extn(src);
  97. if(wn.assets.handler[type]) {
  98. wn.assets.handler[type](wn.assets.get(src), src);
  99. wn.assets.executed_[src] = 1;
  100. }
  101. },
  102. // handle types of assets
  103. // and launch them in the
  104. // app
  105. handler: {
  106. js: function(txt, src) {
  107. wn.dom.eval(txt);
  108. },
  109. css: function(txt, src) {
  110. wn.dom.set_style(txt);
  111. }
  112. }
  113. };