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
151
import { Http } from '@/utils';
import { Subscription } from 'rxjs';
/**
* 按钮loadding状态服务
*
* @export
* @class ButtonLoadingService
*/
export class ButtonLoadingService {
/**
* 单例变量声明
*
* @private
* @static
* @type {ButtonLoadingService}
* @memberof ButtonLoadingService
*/
private static ButtonLoadingService: ButtonLoadingService;
/**
* loadding状态事件
*
* @public
* @type {(Subscription | undefined)}
* @memberof ButtonLoadingService
*/
public buttonLoadingStateEvent: Subscription | null = null;
/**
* @description dom映射对象
* @type {Map<string, any>}
* @memberof ButtonLoadingService
*/
public domMap: Map<string, any> = new Map();
/**
* 获取 ButtonLoadingService 单例对象
*
* @static
* @returns {ButtonLoadingService}
* @memberof ButtonLoadingService
*/
public static getInstance(): ButtonLoadingService {
if (!ButtonLoadingService.ButtonLoadingService) {
ButtonLoadingService.ButtonLoadingService = new ButtonLoadingService();
}
return this.ButtonLoadingService;
}
/**
* 初始化指令所绑定的元素状态
*
* @param {any} el 指令所绑定的元素
* @param {any} binding 指令附加参数
* @memberof ButtonLoadingService
*/
public initElement(el:any, binding:any){
if(binding && binding.arg){
// 工具栏按钮
if(Object.is(binding.arg,'i-button')){
if(el.getElementsByTagName('i') && el.getElementsByTagName('i').length >0){
let iconElement:any = el.getElementsByTagName('i')[0];
iconElement.setAttribute('ownclassname',iconElement.className);
const uuid: string = String(el.__vue__._uid);
el.setAttribute('uuid',uuid);
this.domMap.set(uuid, iconElement);
}
}
}
}
/**
* 设置loadding状态
*
* @param {any} el 指令所绑定的元素
* @param {any} binding 指令附加参数
* @memberof ButtonLoadingService
*/
public setLoadState(el:any, binding:any){
if (!this.buttonLoadingStateEvent) {
this.buttonLoadingStateEvent = Http.getInstance().getNotifyObject().subscribe((result:any) =>{
if(result && result.action && Object.is(result.action,'setloadstate')){
if(result && result.state){
this.addLoadState();
}else{
this.removeLoadState();
}
}
})
}
}
/**
* 添加loadding状态
*
* @param {any} el 指令所绑定的元素
* @param {any} binding 指令附加参数
* @memberof ButtonLoadingService
*/
public addLoadState(){
this.domMap.forEach((dom: any, key: string) => {
dom.className = "el-icon-loading";
})
}
/**
* 移除loadding状态
*
* @param {any} el 指令所绑定的元素
* @param {any} binding 指令附加参数
* @memberof ButtonLoadingService
*/
public removeLoadState(){
this.domMap.forEach((dom: any, key: string) => {
dom.className = dom.getAttribute('ownclassname');
})
}
/**
* 清除资源(取消订阅)
*
* @param {any} el 指令所绑定的元素
* @param {any} binding 指令附加参数
* @memberof ButtonLoadingService
*/
public clearResource(el:any, binding:any){
if(this.buttonLoadingStateEvent && this.domMap.size == 0){
this.buttonLoadingStateEvent.unsubscribe();
this.buttonLoadingStateEvent = null;
}
const uuid: string = el.getAttribute('uuid');
if (uuid && this.domMap.has(uuid)) {
this.domMap.delete(uuid);
}
}
}
export default {
bind(el:any, binding:any) {
ButtonLoadingService.getInstance().initElement(el, binding);
},
inserted(el:any, binding:any) {
ButtonLoadingService.getInstance().setLoadState(el, binding);
},
unbind(el:any, binding:any) {
ButtonLoadingService.getInstance().clearResource(el,binding);
}
}