[483] | 1 | define(['./has'], function(has){ |
---|
| 2 | var global = this, |
---|
| 3 | doc = document, |
---|
| 4 | readyStates = { 'loaded': 1, 'complete': 1 }, |
---|
| 5 | fixReadyState = typeof doc.readyState != "string", |
---|
| 6 | ready = !!readyStates[doc.readyState], |
---|
| 7 | readyQ = [], |
---|
| 8 | recursiveGuard; |
---|
| 9 | |
---|
| 10 | function domReady(callback){ |
---|
| 11 | // summary: |
---|
| 12 | // Plugin to delay require()/define() callback from firing until the DOM has finished loading. |
---|
| 13 | readyQ.push(callback); |
---|
| 14 | if(ready){ processQ(); } |
---|
| 15 | } |
---|
| 16 | domReady.load = function(id, req, load){ |
---|
| 17 | domReady(load); |
---|
| 18 | }; |
---|
| 19 | |
---|
| 20 | // Export queue so that ready() can check if it's empty or not. |
---|
| 21 | domReady._Q = readyQ; |
---|
| 22 | domReady._onQEmpty = function(){ |
---|
| 23 | // summary: |
---|
| 24 | // Private method overridden by dojo/ready, to notify when everything in the |
---|
| 25 | // domReady queue has been processed. Do not use directly. |
---|
| 26 | // Will be removed in 2.0, along with domReady._Q. |
---|
| 27 | }; |
---|
| 28 | |
---|
| 29 | // For FF <= 3.5 |
---|
| 30 | if(fixReadyState){ doc.readyState = "loading"; } |
---|
| 31 | |
---|
| 32 | function processQ(){ |
---|
| 33 | // Calls all functions in the queue in order, unless processQ() is already running, in which case just return |
---|
| 34 | |
---|
| 35 | if(recursiveGuard){ return; } |
---|
| 36 | recursiveGuard = true; |
---|
| 37 | |
---|
| 38 | while(readyQ.length){ |
---|
| 39 | try{ |
---|
| 40 | (readyQ.shift())(doc); |
---|
| 41 | }catch(err){ |
---|
| 42 | console.log("Error on domReady callback: " + err); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | recursiveGuard = false; |
---|
| 47 | |
---|
| 48 | // Notification for dojo/ready. Remove for 2.0. |
---|
| 49 | // Note that this could add more tasks to the ready queue. |
---|
| 50 | domReady._onQEmpty(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | if(!ready){ |
---|
| 54 | var tests = [], |
---|
| 55 | detectReady = function(evt){ |
---|
| 56 | evt = evt || global.event; |
---|
| 57 | if(ready || (evt.type == "readystatechange" && !readyStates[doc.readyState])){ return; } |
---|
| 58 | |
---|
| 59 | // For FF <= 3.5 |
---|
| 60 | if(fixReadyState){ doc.readyState = "complete"; } |
---|
| 61 | |
---|
| 62 | ready = 1; |
---|
| 63 | processQ(); |
---|
| 64 | }, |
---|
| 65 | on = function(node, event){ |
---|
| 66 | node.addEventListener(event, detectReady, false); |
---|
| 67 | readyQ.push(function(){ node.removeEventListener(event, detectReady, false); }); |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | if(!has("dom-addeventlistener")){ |
---|
| 71 | on = function(node, event){ |
---|
| 72 | event = "on" + event; |
---|
| 73 | node.attachEvent(event, detectReady); |
---|
| 74 | readyQ.push(function(){ node.detachEvent(event, detectReady); }); |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | var div = doc.createElement("div"); |
---|
| 78 | try{ |
---|
| 79 | if(div.doScroll && global.frameElement === null){ |
---|
| 80 | // the doScroll test is only useful if we're in the top-most frame |
---|
| 81 | tests.push(function(){ |
---|
| 82 | // Derived with permission from Diego Perini's IEContentLoaded |
---|
| 83 | // http://javascript.nwbox.com/IEContentLoaded/ |
---|
| 84 | try{ |
---|
| 85 | div.doScroll("left"); |
---|
| 86 | return 1; |
---|
| 87 | }catch(e){} |
---|
| 88 | }); |
---|
| 89 | } |
---|
| 90 | }catch(e){} |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | on(doc, "DOMContentLoaded"); |
---|
| 94 | on(global, "load"); |
---|
| 95 | |
---|
| 96 | if("onreadystatechange" in doc){ |
---|
| 97 | on(doc, "readystatechange"); |
---|
| 98 | }else if(!fixReadyState){ |
---|
| 99 | // if the ready state property exists and there's |
---|
| 100 | // no readystatechange event, poll for the state |
---|
| 101 | // to change |
---|
| 102 | tests.push(function(){ |
---|
| 103 | return readyStates[doc.readyState]; |
---|
| 104 | }); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | if(tests.length){ |
---|
| 108 | var poller = function(){ |
---|
| 109 | if(ready){ return; } |
---|
| 110 | var i = tests.length; |
---|
| 111 | while(i--){ |
---|
| 112 | if(tests[i]()){ |
---|
| 113 | detectReady("poller"); |
---|
| 114 | return; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | setTimeout(poller, 30); |
---|
| 118 | }; |
---|
| 119 | poller(); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return domReady; |
---|
| 124 | }); |
---|