-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
91 lines (85 loc) · 2.39 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
import "./src/styles/index.scss";
import render from "./src/render";
import { optionType, resultType } from "./src/type";
import { storeSteward } from "./src/store/index";
import { className} from "./src/util"
let store = new storeSteward([]);
// Exposure to message objects is not recommended, and is postEvented for more powerful boxes
class MessageClass {
defaultOption = {
type: "info",
durationTime: 1000, //ms
postEvent: null,
class: "",
center:true
};
option = {} as optionType;
seed: number = 0;
isContainer: boolean = false;
containerDom: HTMLElement;
constructor(option) {}
//class function🚩
alteration(option) {
this.option = Object.assign({}, this.defaultOption, option);
this.establish();
}
establish() {
let { seed, option } = this;
this.seed++
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: "i",
attr: {
class: `iconfont nan-icon icon-${option.type}`,
},
},
{
tag: "span",
children: option.content,
},
],
attr: {
class:className(`alert-${option.type} nan-alert enter ${option.class} ${
option.center ? "center" : ""
}`) ,
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 * 100+ (typeof option.content ==="string"?option.content.length*6:0);
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