-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-stub.js
137 lines (113 loc) · 4.03 KB
/
jquery-stub.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
/**
* jQuery Stub for inline jQuery()/.ready capture.
*
* Copyright (C) 2019 Style.Tools
* @link https://github.com/style-tools/jquery-stub
*/
(function(win, doc){
// push to queue
function JQUERY_STUB_PUSH_QUEUE(handler, fn) {
if (handler === "ready") {
JQUERY_READY_QUEUE.push(fn);
} else {
JQUERY_READY_QUEUE.push(handler);
}
};
// setup stub if jQuery is not yet loaded
if (!win.jQuery) {
var JQUERY_LOADED; // jQuery loaded flag
// jQuery.ready queue
var JQUERY_READY_QUEUE = [];
var JQUERY_NOCONFLICT = false;
// define an alias object
var JQUERY_ALIAS_OBJECT = {
ready: JQUERY_STUB_PUSH_QUEUE,
bind: JQUERY_STUB_PUSH_QUEUE
};
// jQuery stub
win.$ = win.jQuery = function(handler) {
// reference access, pass to jQuery object
if (JQUERY_LOADED) {
return win.jQuery.apply(this, arguments);
}
if (handler === doc || handler === undefined) {
// Queue $(doc).ready(handler), $().ready(handler)
// and $(doc).bind("ready", handler) by returning
// an object with alias methods for JQUERY_STUB_PUSH_QUEUE
return JQUERY_ALIAS_OBJECT;
} else {
// Queue $(handler)
JQUERY_STUB_PUSH_QUEUE(handler);
}
};
// noConflict
win.$.noConflict = win.jQuery.noConflict = function() {
JQUERY_NOCONFLICT = true;
};
// mark stub
win.$.isStub = win.jQuery.isStub = true;
// object.watch polyfill
if (!Object.prototype.watch) {
Object.defineProperty(Object.prototype, "watch", {
enumerable: false,
configurable: true,
writable: false,
value: function(prop, handler) {
var
oldval = this[prop],
newval = oldval,
getter = function() {
return newval;
},
setter = function(val) {
oldval = newval;
return newval = handler.call(this, prop, oldval, val);
};
if (delete this[prop]) { // can't watch constants
Object.defineProperty(this, prop, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}
}
});
}
// object.unwatch
if (!Object.prototype.unwatch) {
Object.defineProperty(Object.prototype, "unwatch", {
enumerable: false,
configurable: true,
writable: false,
value: function(prop) {
var val = this[prop];
delete this[prop]; // remove accessors
this[prop] = val;
}
});
}
// wait for jQuery
win.watch('jQuery', function(id, oldval, jQuery) {
// Verify if valid jQuery
if (typeof jQuery !== 'function' || typeof jQuery.fn === 'undefined' || typeof jQuery.isStub !== 'undefined') {
return jQuery;
}
// jQuery.noConflict() has been called
if (JQUERY_NOCONFLICT) {
jQuery.noConflict();
}
// process jQuery.ready queue
jQuery.each(JQUERY_READY_QUEUE, function(index, handler) {
jQuery(handler);
});
// stop watching object
win.unwatch('jQuery');
// set global object
win.jQuery = jQuery;
// mark loaded
JQUERY_LOADED = true;
return jQuery;
});
}
}(window, document));