|
- // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
- //
- // MIT License (MIT)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a
- // copy of this software and associated documentation files (the "Software"),
- // to deal in the Software without restriction, including without limitation
- // the rights to use, copy, modify, merge, publish, distribute, sublicense,
- // and/or sell copies of the Software, and to permit persons to whom the
- // Software is furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
- // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
- // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
-
- // library to mange assets (js, css, models, html) etc in the app.
- // will try and get from localStorge if latest are available
- // depends on wn.versions to manage versioning
-
- wn.assets = {
- // keep track of executed assets
- executed_ : {},
-
- // check if the asset exists in
- // localstorage
- exists: function(src) {
- if('localStorage' in window
- && localStorage.getItem(src))
- return true
- },
-
- // add the asset to
- // localstorage
- add: function(src, txt) {
- if('localStorage' in window) {
- localStorage.setItem(src, txt);
- }
- },
-
- get: function(src) {
- return localStorage.getItem(src);
- },
-
- extn: function(src) {
- if(src.indexOf('?')!=-1) {
- src = src.split('?').slice(-1)[0];
- }
- return src.split('.').slice(-1)[0];
- },
-
- // load an asset via
- load: function(src) {
- // this is virtual page load, only get the the source
- // *without* the template
- var t = src;
-
- $.ajax({
- url: t,
- data: {
- q: Math.floor(Math.random()*1000)
- },
- dataType: 'text',
- success: function(txt) {
- // add it to localstorage
- wn.assets.add(src, txt);
- },
- async: false
- })
- },
-
- // pass on to the handler to set
- execute: function(src) {
- if(!wn.assets.exists(src)) {
- wn.assets.load(src);
- }
- var type = wn.assets.extn(src);
- if(wn.assets.handler[type]) {
- wn.assets.handler[type](wn.assets.get(src), src);
- wn.assets.executed_[src] = 1;
- }
- },
-
- // handle types of assets
- // and launch them in the
- // app
- handler: {
- js: function(txt, src) {
- wn.dom.eval(txt);
- },
- css: function(txt, src) {
- var se = document.createElement('style');
- se.type = "text/css";
- if (se.styleSheet) {
- se.styleSheet.cssText = txt;
- } else {
- se.appendChild(document.createTextNode(txt));
- }
- document.getElementsByTagName('head')[0].appendChild(se);
- },
- cgi: function(txt, src) {
- // dynamic content, will return content as
- // javascript
- wn.dom.eval(txt)
- }
- }
- }
|