dialoger.js
4.61 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
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
var tipTmpl = '<div class="tip {{tipClassName}}"><div class="title">{{title}}</div><div class="content">{{content}}</div><a class="button {{buttonClass}}">{{button}}</a></div>'
var toastTmpl = '<div style="{{style}}" class="feature-toast-content">{{content}}</div>';
var frameRate = 75; // 动画帧率
var showTip = function (data) {
var tipNode = null;
data = data || {
title: '',
content: '',
close: true
};
data.tipClassName = data.tipClassName || '';
if (data.close) {
data.buttonClass = 'close';
data.button = '返回'
} else {
data.buttonClass = 'refresh';
data.button = '刷新'
}
if (document.getElementById('yo-tip')) {
tipNode = document.getElementById('yo-tip');
var tip = tipNode.querySelector('.tip');
tip.setAttribute('class', 'tip ' + data.tipClassName);
tip.querySelector('.title').innerHTML = data.title;
tip.querySelector('.content').innerHTML = data.content;
var button = tip.querySelector('.button');
button.setAttribute('class', 'button ' + data.buttonClass);
button.innerHTML = data.button;
} else {
for (var key in data) {
tipTmpl = tipTmpl.replace(new RegExp("\\{\\{" + key + "\\}\\}", "g"), data[key]);
}
tipNode = document.createElement('div');
tipNode.setAttribute('id', 'yo-tip');
tipNode.setAttribute('class', 'featuretip tip-wrap');
tipNode.innerHTML = tipTmpl;
document.body.appendChild(tipNode);
tipNode.addEventListener('click',function (e) {
if ('featuretip tip-wrap' === e.target.className) {
fadeOut(e.currentTarget,300);
}
})
}
if (data.close) {
tipNode.querySelector('.close').onclick = function (e) {
fadeOut(tipNode,300)
}
} else {
tipNode.querySelector('.refresh').onclick = function (e) {
location.reload();
}
}
fadeIn(tipNode,300);
};
var toast = function (content, options) {
if (typeof content === 'undefined') {
return;
}
var obj = Object.assign({
bottom: '10%',
paddingX: 6,
paddingY: 10,
radius: 3
}, options || {});
var param = {
style: 'bottom:' + obj.bottom + ';padding:' + obj.paddingY + 'px ' + obj.paddingX + ';border-radius:' + obj.radius + 'px;',
content: content
};
var toastNode
if (document.getElementById('yo-toast')) {
toastNode = document.getElementById('yo-toast');
var toast = toastNode.querySelector('.feature-toast-content');
toast.setAttribute('style', param.style);
toast.innerHTML = param.content;
} else {
toastNode = document.createElement('div');
toastNode.setAttribute('class', 'feature-toast feature-toast-wrap');
toastNode.setAttribute('id', 'yo-toast');
var _toastTmpl;
for (var key in param) {
_toastTmpl = toastTmpl.replace(new RegExp("\\{\\{" + key + "\\}\\}", "g"), param[key]);
}
toastNode.innerHTML = _toastTmpl;
document.body.appendChild(toastNode);
}
toastNode.style.display = 'block';
this.toastTimer && clearTimeout(this.toastTimer);
this.toastTimer = setTimeout(function () {
toastNode.style.display = 'none';
}, (obj.duration && obj.duration > 0) ? obj.duration : 2000);
};
function fadeIn(el,time){
var mt = 1000 / frameRate,
opacityStep = mt / time;
if(el.style.opacity === ""){
el.style.opacity = 0;
}
if(el.style.display === "" || el.style.display === 'none'){
el.style.display = 'block';
}
el.timer && clearInterval(el.timer);
el.timer = setInterval(function(){
if(el.style.opacity < 1 - opacityStep){
el.style.opacity = parseFloat(el.style.opacity) + opacityStep;
}
else{
clearInterval(el.timer);
el.style.opacity = 1;
}
}, mt);
}
function fadeOut(el,time){
var mt = 1000 / frameRate,
opacityStep = mt / time;
if(el.style.opacity === ""){
el.style.opacity = 1;
}
if(el.style.display === "" || el.style.display === 'none'){
el.style.display = 'block';
}
el.timer && clearInterval(el.timer);
el.timer = setInterval(function(){
if(el.style.opacity > opacityStep){
el.style.opacity = parseFloat(el.style.opacity) - mt / time;
}
else{
clearInterval(el.timer);
el.style.opacity = 0;
el.style.display = 'none'
}
}, mt);
}
module.exports = {
showTip: showTip,
toast: toast
};