-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathloading.js
390 lines (340 loc) · 10 KB
/
loading.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import $ from "jquery";
import "./loading.css";
const Loading = function(element, options) {
this.element = element;
this.settings = $.extend({}, Loading.defaults, options);
this.settings.fullPage = this.element.is("body");
this.init();
if (this.settings.start) {
this.start();
}
};
Loading.defaults = {
/**
* jQuery element to be used as overlay
* If not defined, a default overlay will be created
*/
overlay: undefined,
/**
* z-index to be used by the default overlay
* If not defined, a z-index will be calculated based on the
* target's z-index
* Has no effect if a custom overlay is defined
*/
zIndex: undefined,
/**
* Message to be rendered on the overlay content
* Has no effect if a custom overlay is defined
*/
message: "Loading...",
/**
* Theme to be applied on the loading element
*
* Some default themes are implemented on `jquery.loading.css`, but you can
* define your own. Just add a `.loading-theme-my_awesome_theme` selector
* somewhere with your custom styles and change this option
* to 'my_awesome_theme'. The class is applied to the parent overlay div
*
* Has no effect if a custom overlay is defined
*/
theme: "light",
/**
* Class(es) to be applied to the overlay element when the loading state is started
*/
shownClass: "loading-shown",
/**
* Class(es) to be applied to the overlay element when the loading state is stopped
*/
hiddenClass: "loading-hidden",
/**
* Set to true to stop the loading state if the overlay is clicked
* This options does NOT override the onClick event
*/
stoppable: false,
/**
* Set to false to not start the loading state when initialized
*/
start: true,
/**
* Function to be executed when the loading state is started
* Receives the loading object as parameter
*
* The function is attached to the `loading.start` event
*/
onStart: function(loading) {
loading.overlay.fadeIn(150);
},
/**
* Function to be executed when the loading state is stopped
* Receives the loading object as parameter
*
* The function is attached to the `loading.stop` event
*/
onStop: function(loading) {
loading.overlay.fadeOut(150);
},
/**
* Function to be executed when the overlay is clicked
* Receives the loading object as parameter
*
* The function is attached to the `loading.click` event
*/
onClick: function() {}
};
/**
* Extend the Loading plugin default settings with the user options
* Use it as `$.Loading.setDefaults({ ... })`
*
* @param {Object} options Custom options to override the plugin defaults
*/
Loading.setDefaults = function(options) {
Loading.defaults = $.extend({}, Loading.defaults, options);
};
$.extend(Loading.prototype, {
/**
* Initializes the overlay and attach handlers to the appropriate events
*/
init: function() {
this.isActive = false;
this.overlay = this.settings.overlay || this.createOverlay();
this.resize();
this.attachMethodsToExternalEvents();
this.attachOptionsHandlers();
},
/**
* Return a new default overlay
*
* @return {jQuery} A new overlay already appended to the page's body
*/
createOverlay: function() {
var overlay = $(
'<div class="loading-overlay loading-theme-' +
this.settings.theme +
'"><div class="loading-overlay-content">' +
this.settings.message +
"</div></div>"
)
.addClass(this.settings.hiddenClass)
.hide()
.appendTo("body");
var elementID = this.element.attr("id");
if (elementID) {
overlay.attr("id", elementID + "_loading-overlay");
}
return overlay;
},
/**
* Attach some internal methods to external events
* e.g. overlay click, window resize etc
*/
attachMethodsToExternalEvents: function() {
var self = this;
// Add `shownClass` and remove `hiddenClass` from overlay when loading state
// is activated
self.element.on("loading.start", function() {
self.overlay
.removeClass(self.settings.hiddenClass)
.addClass(self.settings.shownClass);
});
// Add `hiddenClass` and remove `shownClass` from overlay when loading state
// is stopped
self.element.on("loading.stop", function() {
self.overlay
.removeClass(self.settings.shownClass)
.addClass(self.settings.hiddenClass);
});
// Attach the 'stop loading on click' behaviour if the `stoppable` option is set
if (self.settings.stoppable) {
self.overlay.on("click", function() {
self.stop();
});
}
// Trigger the `loading.click` event if the overlay is clicked
self.overlay.on("click", function() {
self.element.trigger("loading.click", self);
});
// Bind the `resize` method to `window.resize`
$(window).on("resize", function() {
self.resize();
});
// Bind the `resize` method to `document.ready` to guarantee right
// positioning and dimensions after the page is loaded
$(function() {
self.resize();
});
},
/**
* Attach the handlers defined on `options` for the respective events
*/
attachOptionsHandlers: function() {
var self = this;
self.element.on("loading.start", function(event, loading) {
self.settings.onStart(loading);
});
self.element.on("loading.stop", function(event, loading) {
self.settings.onStop(loading);
});
self.element.on("loading.click", function(event, loading) {
self.settings.onClick(loading);
});
},
/**
* Calculate the z-index for the default overlay element
* Return the z-index passed as setting to the plugin or calculate it
* based on the target's z-index
*/
calcZIndex: function() {
if (this.settings.zIndex !== undefined) {
return this.settings.zIndex;
} else {
return (
(parseInt(this.element.css("z-index")) || 0) +
1 +
this.settings.fullPage
);
}
},
/**
* Reposition the overlay on the top of the target element
* This method needs to be called if the target element changes position
* or dimension
*/
resize: function() {
var self = this;
var element = self.element,
totalWidth = element.outerWidth(),
totalHeight = element.outerHeight();
if (this.settings.fullPage) {
totalHeight = "100%";
totalWidth = "100%";
}
this.overlay.css({
position: self.settings.fullPage ? "fixed" : "absolute",
zIndex: self.calcZIndex(),
top: element.offset().top,
left: element.offset().left,
width: totalWidth,
height: totalHeight
});
},
/**
* Trigger the `loading.start` event and turn on the loading state
*/
start: function() {
this.isActive = true;
this.resize();
this.element.trigger("loading.start", this);
},
/**
* Trigger the `loading.stop` event and turn off the loading state
*/
stop: function() {
this.isActive = false;
this.element.trigger("loading.stop", this);
},
/**
* Check whether the loading state is active or not
*
* @return {Boolean}
*/
active: function() {
return this.isActive;
},
/**
* Toggle the state of the loading overlay
*/
toggle: function() {
if (this.active()) {
this.stop();
} else {
this.start();
}
},
/**
* Destroy plugin instance.
*/
destroy: function() {
this.overlay.remove();
}
});
/**
* Name of the data attribute where the plugin object will be stored
*/
var dataAttr = "jquery-loading";
/**
* Initializes the plugin and return a chainable jQuery object
*
* @param {Object} [options] Initialization options. Extends `Loading.defaults`
* @return {jQuery}
*/
$.fn.loading = function(options) {
return this.each(function() {
// (Try to) retrieve an existing plugin object associated with element
var loading = $.data(this, dataAttr);
if (!loading) {
// First call. Initialize and save plugin object
if (
options === undefined ||
typeof options === "object" ||
options === "start" ||
options === "toggle"
) {
// Initialize it just if argument is undefined, a config object
// or a direct call to 'start' or 'toggle' methods
$.data(this, dataAttr, new Loading($(this), options));
}
} else {
// Already initialized
if (options === undefined) {
// $(...).loading() call. Call the 'start' by default
loading.start();
} else if (typeof options === "string") {
// $(...).loading('method') call. Execute 'method'
loading[options].apply(loading);
} else {
// $(...).loading({...}) call. New configurations. Reinitialize
// plugin object with new config options and start the plugin
// Also, destroy the old overlay instance
loading.destroy();
$.data(this, dataAttr, new Loading($(this), options));
}
}
});
};
/**
* Return the loading object associated to the element or initialize it
* This method is interesting if you need the plugin object to access the
* internal API
* Example: `$('#some-element').Loading().toggle()`
*
* @param {Object} [options] Initialization options. If new options are given
* to a previously initialized object, the old ones are overriden and the
* plugin restarted
* @return {Loading}
*/
$.fn.Loading = function(options) {
var loading = $(this).data(dataAttr);
if (!loading || options !== undefined) {
$(this).data(dataAttr, (loading = new Loading($(this), options)));
}
return loading;
};
/**
* Create the `:loading` jQuery selector
* Return all the jQuery elements with the loading state active
*
* Using the `:not(:loading)` will return all jQuery elements that are not
* loading, even the ones with the plugin not attached.
*
* Examples of usage:
* `$(':loading')` to get all the elements with the loading state active
* `$('#my-element').is(':loading')` to check if the element is loading
*/
$.expr[":"].loading = function(element) {
var loadingObj = $.data(element, dataAttr);
if (!loadingObj) {
return false;
}
return loadingObj.active();
};
$.Loading = Loading;