forked from webvs2/Nanometer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
146 lines (140 loc) · 3.35 KB
/
index.ts
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
"use strict";
import "./src/css/index.scss";
import { cloneDeep } from "lodash";
import render from "./src/render";
class storeSteward {
store: any[] = [];
pastDue: any[] = [];
constructor(Store) {
this.store = Store;
// this.pastDue = [];
}
push(value) {
let index = this.pastDue.length;
this.pastDue.push(this.timebomb(value, index));
}
timebomb(data, index) {
let { source, dom } = data;
return setTimeout(() => {
let { pastDue } = this;
dom.className = "out " + dom.className;
dom.addEventListener(
"animationend",
function () {
source.postEvent?.();
document.body.removeChild(dom);
},
false
);
clearTimeout(pastDue[index]);
pastDue[index] = null;
}, source.durationTime);
}
closeAll() {
let { pastDue } = this;
pastDue.map((item, index) => {
clearTimeout(item);
item = null;
});
pastDue = [];
return new Promise((resolve, reject) => {
if (!pastDue.length) {
resolve(true);
} else {
reject(false);
}
});
}
}
let store = new storeSteward([]);
// Exposure to message objects is not recommended, and is postEvented for more powerful boxes
interface resultType {
dom: HTMLElement;
id: String;
domID: String;
source: any;
[propName: string]: any;
}
interface optionType {
type: string;
durationTime: number; //ms
class?: string;
content?: string;
postEvent: null;
}
class MessageClass {
defaultOption= {
type: "info",
durationTime: 1000, //ms
postEvent: null,
class: "",
};
option = {
} as optionType;
seed: number = 0;
isContainer: boolean = false;
containerDom: HTMLElement;
constructor(option) {}
alteration(option) {
this.option = Object.assign({},this.defaultOption, option);
this.establish();
}
establish() {
let { seed,option } = this;
if (!option.content)
throw '[message] If you use the object argument form, be aware!"content" is required';
let id = "message_" + seed++;
function MessageConstructor(data: {}): resultType {
const elem = render({
tag: "div",
children: [
{
tag: "span",
attr: {
class: "iconfont nan-icon icon-xiaoxi",
},
},
{
tag:'span',
children:option.content
}
],
// option.content,
attr: {
class: `alert-${option.type} nan-alert enter ${option.class} `,
id: id,
style: { zindex: 100 + seed },
},
});
return {
dom: elem,
id: id,
domID: "#" + id,
source: data,
} as resultType;
}
// Generate and add to the body...
let messageBox = MessageConstructor(option);
let { source, dom } = messageBox;
source.durationTime = source.durationTime + seed * 60;
store.push(messageBox);
document.body.appendChild(dom);
}
}
let MessageBox = new MessageClass({});
let message = (...data: any[]) => {
MessageBox.alteration(
data.length < 2
? data[0]
: {
type: data[0],
content: data[1],
}
);
};
new Array("success", "warning", "info", "error").map((item, index) => {
message[item] = (value: any) => {
MessageBox.alteration({ type: item, content: value });
};
});
export default message;