forked from marko-js/marko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ready.js
143 lines (122 loc) · 4.5 KB
/
ready.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
jQuery's doc.ready/$(function(){}) should
you wish to use a cross-browser domReady solution
without opting for a library.
Demo: http://jsfiddle.net/zKLpb/
usage:
$(function(){
// your code
});
Parts: jQuery project, Diego Perini, Lucent M.
Previous version from Addy Osmani (https://raw.github.com/addyosmani/jquery.parts/master/jquery.documentReady.js)
This version: Patrick Steele-Idem
- Converted to CommonJS module
- Code cleanup
- Fixes for IE <=10
*/
/* globals window */
var isReady = false;
var readyBound = false;
var defaultWindow = typeof window != 'undefined' && window;
var defaultDocument = typeof document != 'undefined' && document;
var listeners = [];
function domReadyCallback() {
for (var i = 0, len = listeners.length; i < len; i++) {
var listener = listeners[i];
listener[0].call(listener[1]);
}
listeners = null;
}
function bindReady(doc) {
var toplevel = false;
var win = doc.defaultView || defaultWindow || doc;
// Handle when the DOM is ready
function domReady() {
// Make sure that the DOM is not already loaded
if (!isReady) {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if (!doc.body) {
return setTimeout(domReady, 1);
}
// Remember that the DOM is ready
isReady = true;
// If there are functions bound, to execute
domReadyCallback();
// Execute all of them
}
} // /ready()
// The ready event handler
function domContentLoaded() {
if (doc.addEventListener) {
doc.removeEventListener("DOMContentLoaded", domContentLoaded, false);
doc.removeEventListener("load", domContentLoaded, false);
} else {
// we're here because readyState !== "loading" in oldIE
// which is good enough for us to call the dom ready!
doc.detachEvent("onreadystatechange", domContentLoaded);
doc.detachEvent("onload", domContentLoaded);
}
domReady();
}
// The DOM ready check for Internet Explorer
function doScrollCheck() {
if (isReady) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
doc.documentElement.doScroll("left");
} catch (error) {
setTimeout(doScrollCheck, 1);
return;
}
// and execute any waiting functions
domReady();
}
// Catch cases where $ is called after the
// browser event has already occurred. IE <= 10 has a bug that results in 'interactive' being assigned
// to the readyState before the DOM is really ready
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
// We will get here if the browser is IE and the readyState === 'complete' or the browser
// is not IE and the readyState === 'interactive' || 'complete'
domReady(doc);
} else if (doc.addEventListener) { // Standards-based browsers support DOMContentLoaded
// Use the handy event callback
doc.addEventListener("DOMContentLoaded", domContentLoaded, false);
// A fallback to win.onload, that will always work
win.addEventListener("load", domContentLoaded, false);
// If IE event model is used
} else if (doc.attachEvent) {
// ensure firing before onload,
// maybe late but safe also for iframes
doc.attachEvent("onreadystatechange", domContentLoaded);
// A fallback to win.onload, that will always work
win.attachEvent("onload", domContentLoaded);
// If IE and not a frame
// continually check to see if the document is ready
try {
toplevel = win.frameElement == null;
} catch (e) {}
if (doc.documentElement.doScroll && toplevel) {
doScrollCheck();
}
}
}
function ready(callback, thisObj, doc) {
if (isReady) {
return callback.call(thisObj);
}
listeners.push([callback, thisObj]);
if (!readyBound) {
readyBound = true;
bindReady(doc || defaultDocument);
}
}
module.exports = ready;
module.exports.patchWidget = function() {
require('./widgets/Widget').prototype.ready = function (callback) {
var document = this.el.ownerDocument;
ready(callback, this, document);
};
};