-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery-stub-async.js
60 lines (46 loc) · 1.62 KB
/
jquery-stub-async.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
/**
* jQuery Stub for inline jQuery()/.ready capture based on https://github.com/style-tools/async
*
* Copyright (C) 2019 Style.Tools
* @link https://github.com/style-tools/jquery-stub
*/
(function(win, doc){
var CALLBACKS = [];
// push to queue
function JQUERY_STUB_PUSH_QUEUE(handler, fn) {
fn = ((handler === "ready") ? fn : handler);
var dependency = 'jquery'; // $async( ... {"ref":"jquery"})
var index = CALLBACKS.push(function() {
jQuery(fn);
});
index--;
// example: alternative dependency based on script
// var fn_text = fn.toString();
// if (fn_text.indexOf('.owlCarousel(') !== -1) {
// dep = ['jquery','owl'];
// }
$async.dependencies(dep, CALLBACKS[index]);
};
// setup stub if jQuery is not yet loaded
if (!win.jQuery) {
// 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) {
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);
}
};
// mark stub
win.$.isStub = win.jQuery.isStub = true;
}
}(window, document));