[483] | 1 | define([ |
---|
| 2 | "dojo/_base/kernel", |
---|
| 3 | "dojo/_base/array", |
---|
| 4 | "dojo/_base/config", |
---|
| 5 | "dojo/_base/window", |
---|
| 6 | "dojo/_base/Deferred", |
---|
| 7 | "dojo/ready" |
---|
| 8 | ], function(dojo, array, config, win, Deferred, ready){ |
---|
| 9 | |
---|
| 10 | // module: |
---|
| 11 | // dojox/mobile/lazyLoadUtils |
---|
| 12 | |
---|
| 13 | var LazyLoadUtils = function(){ |
---|
| 14 | // summary: |
---|
| 15 | // Utilities to lazy-loading of Dojo widgets. |
---|
| 16 | |
---|
| 17 | this._lazyNodes = []; |
---|
| 18 | var _this = this; |
---|
| 19 | if(config.parseOnLoad){ |
---|
| 20 | ready(90, function(){ |
---|
| 21 | var lazyNodes = array.filter(win.body().getElementsByTagName("*"), // avoid use of dojo.query |
---|
| 22 | function(n){ return n.getAttribute("lazy") === "true" || (n.getAttribute("data-dojo-props")||"").match(/lazy\s*:\s*true/); }); |
---|
| 23 | var i, j, nodes, s, n; |
---|
| 24 | for(i = 0; i < lazyNodes.length; i++){ |
---|
| 25 | array.forEach(["dojoType", "data-dojo-type"], function(a){ |
---|
| 26 | nodes = array.filter(lazyNodes[i].getElementsByTagName("*"), |
---|
| 27 | function(n){ return n.getAttribute(a); }); |
---|
| 28 | for(j = 0; j < nodes.length; j++){ |
---|
| 29 | n = nodes[j]; |
---|
| 30 | n.setAttribute("__" + a, n.getAttribute(a)); |
---|
| 31 | n.removeAttribute(a); |
---|
| 32 | _this._lazyNodes.push(n); |
---|
| 33 | } |
---|
| 34 | }); |
---|
| 35 | } |
---|
| 36 | }); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | ready(function(){ |
---|
| 40 | for(var i = 0; i < _this._lazyNodes.length; i++){ /* 1.8 */ |
---|
| 41 | var n = _this._lazyNodes[i]; |
---|
| 42 | array.forEach(["dojoType", "data-dojo-type"], function(a){ |
---|
| 43 | if(n.getAttribute("__" + a)){ |
---|
| 44 | n.setAttribute(a, n.getAttribute("__" + a)); |
---|
| 45 | n.removeAttribute("__" + a); |
---|
| 46 | } |
---|
| 47 | }); |
---|
| 48 | } |
---|
| 49 | delete _this._lazyNodes; |
---|
| 50 | |
---|
| 51 | }); |
---|
| 52 | |
---|
| 53 | this.instantiateLazyWidgets = function(root, requires, callback){ |
---|
| 54 | // summary: |
---|
| 55 | // Instantiates dojo widgets under the root node. |
---|
| 56 | // description: |
---|
| 57 | // Finds DOM nodes that have the dojoType or data-dojo-type attributes, |
---|
| 58 | // requires the found Dojo modules, and runs the parser. |
---|
| 59 | var d = new Deferred(); |
---|
| 60 | var req = requires ? requires.split(/,/) : []; |
---|
| 61 | var nodes = root.getElementsByTagName("*"); // avoid use of dojo.query |
---|
| 62 | var len = nodes.length; |
---|
| 63 | for(var i = 0; i < len; i++){ |
---|
| 64 | var s = nodes[i].getAttribute("dojoType") || nodes[i].getAttribute("data-dojo-type"); |
---|
| 65 | if(s){ |
---|
| 66 | req.push(s); |
---|
| 67 | var m = nodes[i].getAttribute("data-dojo-mixins"), |
---|
| 68 | mixins = m ? m.split(/, */) : []; |
---|
| 69 | req = req.concat(mixins); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | if(req.length === 0){ return true; } |
---|
| 73 | |
---|
| 74 | if(dojo.require){ |
---|
| 75 | array.forEach(req, function(c){ |
---|
| 76 | dojo["require"](c); |
---|
| 77 | }); |
---|
| 78 | dojo.parser.parse(root); |
---|
| 79 | if(callback){ callback(root); } |
---|
| 80 | return true; |
---|
| 81 | }else{ |
---|
| 82 | req = array.map(req, function(s){ return s.replace(/\./g, "/"); }); |
---|
| 83 | require(req, function(){ |
---|
| 84 | dojo.parser.parse(root); |
---|
| 85 | if(callback){ callback(root); } |
---|
| 86 | d.resolve(true); |
---|
| 87 | }); |
---|
| 88 | } |
---|
| 89 | return d; |
---|
| 90 | } |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | // Return singleton. (TODO: can we replace LazyLoadUtils class and singleton w/a simple hash of functions?) |
---|
| 94 | return new LazyLoadUtils(); |
---|
| 95 | }); |
---|
| 96 | |
---|