forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.disableWhileLoading.js
More file actions
94 lines (86 loc) · 3.83 KB
/
Copy pathjquery.disableWhileLoading.js
File metadata and controls
94 lines (86 loc) · 3.83 KB
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
/*
will make the element semi-transparent and disable any :inputs untill a defferred completes.
example:
$('#some_form').disableWhileLoading($.ajaxJSON(...), {buttons: ['.send_button' : 'Sending...'}});
or
var promise = $.ajaxJSON(...);
$('#form').disableWhileLoading(promise, {
buttons: {
'.send_button' : 'Sending...'
}
});
*/
define([
'i18n!instructure',
'compiled/util/objectCollection',
'jquery' /* $ */,
'jquery.ajaxJSON' /* ajaxJSON */,
'spin.js/jquery.spin' /* /\.spin/ */
], function(I18n, objectCollection, $) {
$.fn.disableWhileLoading = function(deferred, options) {
return this.each(function() {
var opts = $.extend(true, {}, $.fn.disableWhileLoading.defaults, options),
$this = $(this),
data = $this.data(),
thingsToWaitOn = data.disabledWhileLoadingDeferreds || (data.disabledWhileLoadingDeferreds = objectCollection([])),
myDeferred = $.Deferred();
$.when.apply($, thingsToWaitOn).done(function() {
var dataKey = 'disabled_' + $.guid++,
$disabledArea = $this.add($this.nextAll('.ui-dialog-buttonpane')),
// todo: replace .andSelf with .addBack when JQuery is upgraded.
$inputsToDisable = $disabledArea.find('*').andSelf().filter(':input').not(':disabled,[type=file]'),
$foundSpinHolder = $this.find('.spin_holder'),
$spinHolder = $foundSpinHolder.length ? $foundSpinHolder : $this,
previousSpinHolderDisplay = $spinHolder.css('display'),
disabled = false;
var disabler = setTimeout(function() {
disabled = true;
$inputsToDisable.prop('disabled', true);
$spinHolder.show().spin(options);
$($spinHolder.data().spinner.el).css({'max-width':'100px'});
$disabledArea.css('opacity', function(i, currentOpacity){
$(this).data(dataKey+'opacityBefore', this.style.opacity || 1);
return opts.opacity;
});
$.each(opts.buttons, function(selector, text) {
//if you pass an array to $.each the first arg is indexInArray, we need second arg
if ($.isArray(opts.buttons)){ selector = text, text = null }
$disabledArea.find(selector).text(function(i, currentText) {
$(this).data(dataKey, currentText);
return text ||
$(this).data('textWhileLoading') ||
( $(this).is('.ui-button-text') && $(this).closest('.ui-button').data('textWhileLoading') ) ||
// if nothing was passed in as the text value or if they pass an array for opts.buttons,
// just use a default loading... text.
I18n.t('loading', 'Loading...');
});
});
}, 13);
$.when(deferred).always(function() {
clearTimeout(disabler);
if (disabled) {
$spinHolder.css('display', previousSpinHolderDisplay).spin(false); // stop spinner
$disabledArea.css('opacity', function(){
return $(this).data(dataKey+'opacityBefore') || 1;
});
$inputsToDisable.prop('disabled', false);
$.each(opts.buttons, function(selector, text) {
if(typeof selector === 'number') var selector = ''+this; // for arrays
$disabledArea.find(selector).text(function() { return $(this).data(dataKey) });
});
thingsToWaitOn.erase(myDeferred); //speed up so that $.when doesn't have to look at myDeferred any more
myDeferred.resolve();
if (opts.onComplete) {
opts.onComplete();
}
}
});
});
thingsToWaitOn.push(myDeferred);
});
};
$.fn.disableWhileLoading.defaults = {
opacity: 0.5,
buttons: ['button[type="submit"], .ui-dialog-buttonpane .ui-button .ui-button-text']
};
});