Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

114 рядки
3.1 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. // library to mange assets (js, css, models, html) etc in the app.
  23. // will try and get from localStorge if latest are available
  24. // depends on wn.versions to manage versioning
  25. wn.assets = {
  26. // keep track of executed assets
  27. executed_ : {},
  28. // check if the asset exists in
  29. // localstorage
  30. exists: function(src) {
  31. if('localStorage' in window
  32. && localStorage.getItem(src))
  33. return true
  34. },
  35. // add the asset to
  36. // localstorage
  37. add: function(src, txt) {
  38. if('localStorage' in window) {
  39. localStorage.setItem(src, txt);
  40. }
  41. },
  42. get: function(src) {
  43. return localStorage.getItem(src);
  44. },
  45. extn: function(src) {
  46. if(src.indexOf('?')!=-1) {
  47. src = src.split('?').slice(-1)[0];
  48. }
  49. return src.split('.').slice(-1)[0];
  50. },
  51. // load an asset via
  52. load: function(src) {
  53. // this is virtual page load, only get the the source
  54. // *without* the template
  55. var t = src;
  56. $.ajax({
  57. url: t,
  58. data: {
  59. q: Math.floor(Math.random()*1000)
  60. },
  61. dataType: 'text',
  62. success: function(txt) {
  63. // add it to localstorage
  64. wn.assets.add(src, txt);
  65. },
  66. async: false
  67. })
  68. },
  69. // pass on to the handler to set
  70. execute: function(src) {
  71. if(!wn.assets.exists(src)) {
  72. wn.assets.load(src);
  73. }
  74. var type = wn.assets.extn(src);
  75. if(wn.assets.handler[type]) {
  76. wn.assets.handler[type](wn.assets.get(src), src);
  77. wn.assets.executed_[src] = 1;
  78. }
  79. },
  80. // handle types of assets
  81. // and launch them in the
  82. // app
  83. handler: {
  84. js: function(txt, src) {
  85. wn.dom.eval(txt);
  86. },
  87. css: function(txt, src) {
  88. var se = document.createElement('style');
  89. se.type = "text/css";
  90. if (se.styleSheet) {
  91. se.styleSheet.cssText = txt;
  92. } else {
  93. se.appendChild(document.createTextNode(txt));
  94. }
  95. document.getElementsByTagName('head')[0].appendChild(se);
  96. },
  97. cgi: function(txt, src) {
  98. // dynamic content, will return content as
  99. // javascript
  100. wn.dom.eval(txt)
  101. }
  102. }
  103. }