loading.js
1.45 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
/**
* 加载组件
*
* @author: Aiden Xu <aiden.xu@yoho.cn>
* @date: 2016/07/18
*/
'use strict';
const $ = require('yoho-jquery');
const Overlay = require('./overlay');
const template = require('components/loading.hbs');
const _ = require('lodash');
const AJAX_LOADING_ENABLED = false; // 全局控制
if (!Overlay) {
throw new Error('Required dependency "Overlay" not found!');
}
class Loading {
constructor(opts) {
this.defaults = {};
this.settings = Object.assign({}, this.defaults, opts);
this.elem = $(template());
}
/**
* 显示
*/
show() {
if (!this.isVisible) {
this.isVisible = true;
if (this.elem.parent().length === 0) {
this.elem.appendTo('body');
}
this.overlay = new Overlay({
animation: 'fade',
clickToClose: false
});
this.overlay.show();
}
}
/**
* 关闭
*/
hide() {
if (this.isVisible) {
this.overlay.hide();
this.elem.detach();
this.isVisible = false;
}
}
}
const instance = new Loading();
if (AJAX_LOADING_ENABLED) {
$(document).ajaxStart(()=> {
_.debounce(()=> {
instance.show();
}, 100)();
});
$(document).ajaxStop(()=> {
_.debounce(()=> {
instance.hide();
}, 500)();
});
}
module.exports = instance;