-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinquery.js
144 lines (126 loc) · 4.36 KB
/
jinquery.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
143
144
var jInquery = (function() {
// The jqueries that have been loaded
var loadedStates = {};
// The requires queued to be called until jquery is ready
var requireCallbacks = {};
// The callbacks to be called with onLoad
var onLoadedCallbacks = [];
// The jQueries that we have loaded
var jQueries = {};
// Returns whether or not a version of jQuery has been loaded yet.
var isLoaded = function(version) {
if (!loadedStates.hasOwnProperty(version))
return false;
return loadedStates[version] === true;
};
// Internal - alerts all event subscribers that the version is ready.
var notifyLoaded = function(version) {
// Set the loaded state to true to indicate that requires should no longer be queued for this version
loadedStates[version] = true;
// Call any subscribers to the onLoaded event
for (var i = 0; i < onLoadedCallbacks.length; i++) {
onLoadedCallbacks[i](version, jQueries[version]);
}
// Call any queued requires
if (requireCallbacks.hasOwnProperty(version)) {
for (var i = 0; i < requireCallbacks[version].length; i++) {
requireCallbacks[version][i](jQueries[version]);
}
}
}
// Allows you to request a version of jQuery from the internal data store.
// If the version is not loaded, it will be queued until it has been loaded
// Note, this can be never!
// The callback provides a function of the signature:
// function(jQuery)
// jQuery - the instance of jQuery that was loaded
var require = function(version, callback) {
if (isLoaded(version))
callback(jQueries[version]);
else
{
if (!requireCallbacks.hasOwnProperty(version))
requireCallbacks[version] = [];
requireCallbacks[version].push(callback);
}
};
// Allows you to hook when a version of jQuery has been loaded.
// The callback provides a function of the signature:
// function(version, jQuery)
// version - the version that was loaded
// jQuery - the instance of jQuery that was loaded
var onLoaded = function(callback) {
onLoadedCallbacks.push(callback);
};
// Allows you to load a specific version of jQuery by providing the version number and the url where
// you want to download it from.
var fromUrl = function(version, url) {
// Generate a new script element
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.async = true;
// Define what happens when the script element has been loaded completely
var ready = function(k) {
jQueries[k] = $.noConflict(true);
notifyLoaded(k);
}.bind(this, version);
if (newScript.readyState) { //IE
newScript.onreadystatechange = function () {
if (newScript.readyState == "loaded" || newScript.readyState == "complete") {
newScript.onreadystatechange = null;
ready();
}
};
} else { //Others
newScript.onload = function () {
ready();
};
}
// Begin loading the script.
newScript.src = url;
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(newScript);
};
// Allows you to load many versions of jQuery by passing an object where the keys are the version numbers,
// and the values are the urls where the version can be downloaded from.
// Example: {
// '1_7' : 'https://code.jquery.com/jquery-1.7.min.js',
// '1_8_0' : 'https://code.jquery.com/jquery-1.8.0.min.js'
// }
var manyFromJson = function(data) {
// Just iterate over this
for (var key in data) {
loadedStates[key] = false;
var url = data[key];
fromUrl(key, url);
}
};
// Allows you to load many versions of jQuery by passing the url to a file that returns an object where the keys
// are the version numbers, and the values are the urls where the version can be downloaded from.
// see manyFromJson(data) for an example of the expected format.
var manyFromUrl = function(url) {
// Load the data
var req = new XMLHttpRequest();
req.open('GET', url);
req.send(null);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
var response = req.responseText;
var data = JSON.parse(response);
manyFromJson(data);
} else {
console.log(req.status);
}
}
};
};
// Return the public functions
return {
fromUrl: fromUrl,
manyFromUrl: manyFromUrl,
manyFromJson: manyFromJson,
isLoaded: isLoaded,
onLoaded: onLoaded,
require: require
};
}());