forked from iloveitaly/jquery.loadmask.spin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.loadmask.spin.js
146 lines (118 loc) · 4.76 KB
/
jquery.loadmask.spin.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
/**
* Copyright (c) 2009 Sergiy Kovalchuk (serg472@gmail.com)
*
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Following code is based on Element.mask() implementation from ExtJS framework (http://extjs.com/)
*
*/
(function($){
/**
* Displays loading mask over selected element(s). Accepts both single and multiple selectors
* @param options the options to be used to display the mask
* @example $('el').mask({spinner: false, label: 'Some Text'});
* @example $('el').mask({spinner: { lines: 10, length: 4, width: 2, radius: 5}, delay: 100, label: 'Loading...'});
*/
$.fn.mask = function(options) {
options = $.extend({
spinner: { lines: 10, length: 4, width: 2, radius: 5},
spinnerPadding: 5,
label: "",
delay: 0,
overlayOpacity: 0.75,
overlaySize: false
}, options);
return $(this).each(function() {
var element = $(this);
if(options.delay > 0) {
element.data("_mask_timeout", setTimeout(function() { $.maskElement(element, options)}, options.delay));
} else {
$.maskElement(element, options);
}
});
};
/**
* Removes mask from the element(s). Accepts both single and multiple selectors.
*/
$.fn.unmask = function(){
return $(this).each(function() {
$.unmaskElement($(this));
});
};
/**
* Checks if a single element is masked. Returns false if mask is delayed or not displayed.
*/
$.fn.isMasked = function(){
return this.hasClass("masked");
};
$.maskElement = function(element, options) {
//if this element has delayed mask scheduled then remove it and display the new one
if (element.data("_mask_timeout") !== undefined) {
clearTimeout(element.data("_mask_timeout"));
element.removeData("_mask_timeout");
}
if(element.isMasked()) {
$.unmaskElement(element);
}
if(element.css("position") == "static") {
element.addClass("masked-relative");
}
element.addClass("masked");
var maskDiv = $('<div class="loadmask"></div>').css({opacity: 0 });
if(options.overlaySize !== false) {
if(options.overlaySize.height !== undefined)
maskDiv.height(options.overlaySize.height)
if(options.overlaySize.width !== undefined)
maskDiv.width(options.overlaySize.width)
}
// auto height fix for IE
if(navigator.userAgent.toLowerCase().indexOf("msie") > -1){
maskDiv.height(element.height() + parseInt(element.css("padding-top")) + parseInt(element.css("padding-bottom")));
maskDiv.width(element.width() + parseInt(element.css("padding-left")) + parseInt(element.css("padding-right")));
}
// fix for z-index bug with selects in IE6
if(navigator.userAgent.toLowerCase().indexOf("msie 6") > -1){
element.find("select").addClass("masked-hidden");
}
element.append(maskDiv);
if(options.label.length > 0 || options.spinner !== false) {
var maskMsgDiv = $('<div class="loadmask-msg" style="display:none;"></div>').css({'opacity':0});
if(options.spinner !== false)
maskMsgDiv.append(new Spinner(options.spinner).spin().el);
if(options.label.length > 0) {
var label = $('<div class="loadmask-label">' + options.label + '</div>')
maskMsgDiv.append(label)
}
element.append(maskMsgDiv);
// calculate center position
// TODO there must be cleaner way to do this; check newer jQuery methods and see if anything will fit
maskMsgDiv.css("top", Math.round(maskDiv.height() / 2 - (maskMsgDiv.height() - parseInt(maskMsgDiv.css("padding-top")) - parseInt(maskMsgDiv.css("padding-bottom"))) / 2)+"px");
maskMsgDiv.css("left", Math.round(maskDiv.width() / 2 - (maskMsgDiv.width() - parseInt(maskMsgDiv.css("padding-left")) - parseInt(maskMsgDiv.css("padding-right"))) / 2)+"px");
maskMsgDiv.show();
if(options.spinner !== false && options.label.length > 0) {
var spinnerSquare = options.spinner.radius * 2 + (options.spinner.width + options.spinner.length) * 2
// The center of the spinner is positioned at the top left corner of this DIV
// center the label text vertically, and align the label to the right of the spinner
label.css({
'margin-left': spinnerSquare / 2 + options.spinnerPadding,
'margin-top': -label.height() / 2
})
}
maskMsgDiv.fadeTo('slow', 1)
}
// TODO this should be customizable
maskDiv.fadeTo('slow', options.overlayOpacity);
};
$.unmaskElement = function(element){
//if this element has delayed mask scheduled then remove it
if (element.data("_mask_timeout") !== undefined) {
clearTimeout(element.data("_mask_timeout"));
element.removeData("_mask_timeout");
}
element.find(".loadmask-msg,.loadmask").remove();
element.removeClass("masked");
element.removeClass("masked-relative");
element.find("select").removeClass("masked-hidden");
};
})(jQuery);