forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
66 lines (61 loc) · 1.97 KB
/
loader.js
File metadata and controls
66 lines (61 loc) · 1.97 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
/*显示loading*/
function showLoader(text, type, attributes) {
if (!type) {
type = "loader-default";
attributes = attributes || {'data-half': true}
}
var $body = document.getElementsByTagName('body');
$body = $body && $body[0];
if ($body) {
var $loader = document.getElementsByClassName('loader')[0];
if (!$loader) {
$loader = createLoader(type);
}
$loader.classList.add('is-active');
setLoaderText(text);
setAttributes(attributes);
}
}
/*设置loading文本,默认文本为"loading"*/
function setLoaderText(text, textAttributeField) {
if (text != null) {
var txtAttrField = textAttributeField != null ? textAttributeField : "data-text";
var attributes = {};
attributes[txtAttrField] = text;
setAttributes(attributes);
}
}
/*设置loader显示相关属性*/
function setAttributes(attributes) {
var $loader = document.getElementsByClassName('loader')[0];
if ($loader && attributes) {
for (var attr in attributes) {
$loader.setAttribute(attr, attributes[attr]);
}
}
}
/*移除loader*/
function removeLoader() {
var $loader = document.getElementsByClassName('loader')[0];
if ($loader) {
$loader.parentNode.removeChild($loader);
}
}
function createLoader(className) {
var $loader, $body = document.getElementsByTagName('body');
$body = $body && $body[0];
if ($body) {
$loader = document.createElement('div');
$loader.className = "loader " + className;
$body.insertBefore($loader, $body.children[0]);
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = ".loader.is-active{" +
"background-color: rgba(0, 0, 0, 0.4) !important;" +
"}";
if (document.getElementsByTagName('head')) {
document.getElementsByTagName('head')[0].appendChild(style);
}
}
return $loader;
}