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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { VNode } from 'vue';
import moment from 'moment';
import './notification-signal.less';
import { TimerUtil } from 'ibiz-core';
/**
* 通知信号指令
*
* @export
* @class NotificationSignal
*/
export const NotificationSignal: any = {
/**
* 指定初始化
*
* @param {HTMLDivElement} el
* @param {*} binding
* @param {VNode} vNode
* @param {VNode} oldVNode
*/
bind(el: HTMLDivElement, binding: any, vNode: VNode, oldVNode: VNode) {
nsc.init(el);
},
/**
* 指令更新
*
* @param {HTMLDivElement} el
* @param {*} binding
* @param {VNode} vNode
* @param {VNode} oldVNode
*/
componentUpdated(el: HTMLDivElement, binding: any, vNode: VNode, oldVNode: VNode) {
if(binding.value != binding.oldValue){
if(binding.value == true){
nsc.loading();
}else if(binding.value == false){
nsc.loadingEnd();
}
}
}
};
/**
* 信号闪烁控制器
*
* @export
* @class NotificationSignal
*/
export class NotificationSignalController {
/**
* 唯一实例
*
* @private
* @static
* @memberof NotificationSignalController
*/
private static readonly instance = new NotificationSignalController();
/**
* 容器
*
* @protected
* @type {HTMLDivElement}
* @memberof NotificationSignalController
*/
protected el!: HTMLDivElement;
/**
* 加载计数
*
* @protected
* @type {number}
* @memberof NotificationSignalController
*/
protected loadingCount: number = 0;
/**
* 加载动画定时器
*
* @protected
* @type {*}
* @memberof NotificationSignalController
*/
protected loadingInterval: any;
/**
* 加载结束定时器
*
* @protected
* @type {*}
* @memberof NotificationSignalController
*/
protected loadingEndTimer: any;
/**
* 加载动画宽度百分比
*
* @protected
* @type {number}
* @memberof NotificationSignalController
*/
protected loadingWidth: number = 0;
/**
* 是否加载动画中
*
* @protected
* @type {boolean}
* @memberof NotificationSignalController
*/
protected isLoading: boolean = false;
/**
* Creates an instance of NotificationSignalController.
* @memberof NotificationSignalController
*/
private constructor() {
if (NotificationSignalController.instance) {
return NotificationSignalController.instance;
}
}
/**
* 初始化
*
* @param {HTMLDivElement} el
* @memberof NotificationSignalController
*/
public init(el: HTMLDivElement): void {
el.classList.add('nsc-container');
const div: HTMLDivElement = document.createElement('div');
div.classList.add('nsc-content');
el.appendChild(div);
this.el = div;
}
/**
* 成功
*
* @param {number} [num=1500]
* @memberof NotificationSignalController
*/
public success(num: number = 1500): void {
this.active();
this.el.classList.add('success');
TimerUtil.timeout(
() => {
this.inactive();
this.el.classList.remove('success');
},
num,
'nsc-success'
);
}
/**
* 警告
*
* @param {number} [num=1500]
* @memberof NotificationSignalController
*/
public waring(num: number = 1500): void {
this.active();
this.el.classList.add('waring');
TimerUtil.timeout(
() => {
this.inactive();
this.el.classList.remove('waring');
},
num,
'nsc-waring'
);
}
/**
* 错误
*
* @param {number} [num=1500]
* @memberof NotificationSignalController
*/
public error(num: number = 1500): void {
this.active();
this.el.classList.add('error');
TimerUtil.timeout(
() => {
this.inactive();
this.el.classList.remove('error');
},
num,
'nsc-error'
);
}
/**
* 激活态
*
* @protected
* @memberof NotificationSignalController
*/
protected active(): void {
this.el.classList.add('active');
this.el.classList.remove('inactive');
}
/**
* 无活动态
*
* @protected
* @memberof NotificationSignalController
*/
protected inactive(): void {
this.el.classList.add('inactive');
this.el.classList.remove('active');
}
/**
* 显示加载态
*
* @memberof NotificationSignalController
*/
public loading(): void {
if (this.loadingCount === 0 && this.el) {
if (!this.isLoading) {
this.active();
this.loadingWidth = 0;
this.el.classList.add('loading');
this.setElWidth(this.loadingWidth);
this.isLoading = true;
this.loadingChange();
this.loadingInterval = setInterval(() => {
this.loadingChange();
}, 300);
} else {
this.loadingCount++;
this.loadingEnd();
return;
}
}
this.loadingCount++;
}
/**
* 加载动画变更
*
* @protected
* @memberof NotificationSignalController
*/
protected loadingChange(): void {
this.loadingWidth += Math.abs(Math.random() * 5);
if (this.loadingWidth >= 99) {
this.loadingWidth = 99;
this.clearLoadingInterval();
}
this.setElWidth(this.loadingWidth);
}
/**
* 结束加载态
*
* @memberof NotificationSignalController
*/
public loadingEnd(): void {
if (this.loadingCount > 0) {
this.loadingCount--;
} else {
this.loadingCount = 0;
}
if (this.loadingCount === 0 && this.el) {
this.clearLoadingInterval();
this.setElWidth(100);
this.clearLoadingEndTimer();
this.loadingEndTimer = setTimeout(() => {
this.isLoading = false;
this.inactive();
this.el.classList.remove('loading');
}, 300);
}
}
/**
* 清除加载动画定时器
*
* @protected
* @memberof NotificationSignalController
*/
protected clearLoadingInterval(): void {
if (this.loadingInterval) {
clearInterval(this.loadingInterval);
this.loadingInterval = null;
}
}
/**
* 清除结束定时器
*
* @protected
* @memberof NotificationSignalController
*/
protected clearLoadingEndTimer(): void {
if (this.loadingEndTimer) {
clearTimeout(this.loadingEndTimer);
this.loadingEndTimer = null;
}
}
/**
* 设置容器宽度百分比
*
* @protected
* @param {number} width
* @memberof NotificationSignalController
*/
protected setElWidth(width: number): void {
this.el.style.width = width + '%';
}
/**
* 获取当前时间
*
* @protected
* @returns {string}
* @memberof NotificationSignalController
*/
protected getLocalDate(): string {
return moment().format('MM-DD HH:mm:ss');
}
/**
* 获取唯一实例
*
* @static
* @returns {NotificationSignalController}
* @memberof NotificationSignalController
*/
public static getInstance(): NotificationSignalController {
return NotificationSignalController.instance;
}
}
// 导出服务
export const nsc: NotificationSignalController = NotificationSignalController.getInstance();