rs.load.js
3.43 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
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
/**
* rs_load - Runsa Plugin
*
* author:yijun
*
* Dependencies:
*
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
function initWrap(container){
var opts = $.data(container, 'rs_load').options;
var cc=$(container);
if(opts.cls){
cc.addClass(opts.cls);
}
if(opts.position){
cc.css("position",opts.position);
}
initGhost(container);
};
function initGhost(container){
var cc=$(container);
var opts = $.data(container, 'rs_load').options;
var ghost,center,icon,label;
ghost=cc.children(".load-ghost");
if(ghost.length==0||!cc.hasClass('rs-load')){
cc.addClass("rs-load");
ghost = $('<div class="load-ghost"></div>').appendTo(cc);
}
ghost.children().remove();
if($(document.body).find(".rs-size-calc").length==0){
$(document.body).append('<div class="rs-size-calc"></div>');
}
center=$('<div></div>').appendTo(ghost);
if(opts.location=="center"){
center.addClass("load-center");
}else if(opts.location=="topleft"){
center.addClass("load-topleft");
}
icon=$('<div class="load-icon icon-loading"></div>').appendTo(center);
if(opts.iconCls){
icon.addClass(opts.iconCls);
}
if(opts.label){
label=$('<div class="load-label"></div>').appendTo(center);
label.html(opts.label);
if(opts.labelCls){
label.addClass(opts.labelCls);
}
}else{
center.height(icon.height());
}
return ghost;
}
function resize(container){
var cc=$(container);
var opts = $.data(container, 'rs_load').options;
var ghost=cc.children(".load-ghost");
var center=ghost.children(".load-center");
var label=center.children(".load-label");
var labelGhost=$(document.body).find(".rs-size-calc");
labelGhost.css({
fontFamily:label.css("font-family"),
fontSize:label.css("font-size"),
fontWeight:label.css("font-weight")
})
labelGhost.html(opts.label);
var top,left;
top=Math.floor(center.outerHeight()/2);
left=Math.floor(labelGhost.outerWidth()/2);
center.css({
marginTop:-top,
marginLeft:-left
});
}
function show(container) {
var cc = $(container);
var ghost = cc.children(".load-ghost");
if (ghost.length == 0) {
ghost=initGhost(container);
}
ghost.show();
resize(container);
}
function hide(container){
var cc=$(container);
var ghost=cc.children(".load-ghost");
setTimeout(function(){
ghost.hide();
},200);
}
function destroy(container){
$.removeData(container, 'rs_load');
}
$.fn.rs_load = function(options, param){
if (typeof options == 'string') {
return $.fn.rs_load.methods[options](this, param);
}
options = options || {};
return this.each(function(){
var state = $.data(this, 'rs_load');
if (state) {
$.extend(state.options, options);
} else {
var opts=$.extend({},$.fn.rs_load.defaults,options);
$.data(this, 'rs_load', {options:opts});
}
initWrap(this);
});
};
$.fn.rs_load.methods={
show:function(jq){
return jq.each(function() {
show(this);
});
},
hide:function(jq){
return jq.each(function() {
hide(this);
});
},
destroy:function(jq){
return jq.each(function() {
destroy(this);
});
}
};
$.fn.rs_load.defaults={
location:'center',
cls:null,
iconCls:null,
label:'Loading,please wait...',
labelCls:null,
position:null
};
}));