UNPKG

5.58 kBJavaScriptView Raw
1// Toast 提示框
2var Toast = function Toast(params) {
3 /* --------------------
4 Model
5 -------------------- */
6 var defaults = {
7 mask: null,
8 parent: document.body, // 创建于哪个元素下
9
10 maskClass: 'mask toast-mask', // 加toast-propagation允许点击
11 maskActiveClass: 'active',
12
13 toastClass: 'toast bottom',
14 toastActiveClass: 'active',
15 wrapperClass: 'toast-wrapper',
16 captionClass: 'toast-caption',
17 iconClass: 'toast-icon',
18
19 duration: 300,
20 delay: 0,
21 html: '',
22 icon: '' // 传入icon的class
23
24 /* callbacks
25 onShowed(Toast)// 显示动画结束后回调
26 onHid(Toast)// 隐藏动画结束后回调
27 */
28 };
29 params = params || {};
30 for (var def in defaults) {
31 if (params[def] === undefined) {
32 params[def] = defaults[def];
33 }
34 }
35 var s = this;
36 s.params = params;
37 s.parent = typeof s.params.parent === 'string' ? document.querySelector(s.params.parent) : s.params.parent;
38 s.toast = null;
39 s.wrapper = null;
40 // Mask
41 s.createMask = function () {
42 var mask = document.createElement('div');
43 mask.setAttribute('class', s.params.maskClass);
44 return mask;
45 };
46 s.createToast = function () {
47 var toast = document.createElement('div');
48 toast.setAttribute('class', s.params.toastClass);
49 return toast;
50 };
51 s.createToastWrapper = function () {
52 var wrapper = document.createElement('div');
53 wrapper.setAttribute('class', s.params.wrapperClass);
54 return wrapper;
55 };
56 s.createToastIcon = function () {
57 var icon = document.createElement('span');
58 if (!s.params.icon) return icon;
59 icon.setAttribute('class', s.params.iconClass + (s.params.icon ? ' ' + s.params.icon : ''));
60 return icon;
61 };
62 s.createToastCaption = function () {
63 var caption = document.createElement('span');
64 caption.setAttribute('class', s.params.captionClass);
65 if (s.params.html) caption.innerHTML = s.params.html;
66 return caption;
67 };
68 s.create = function () {
69 s.mask = s.createMask();
70 s.toast = s.createToast();
71 s.wrapper = s.createToastWrapper();
72 s.caption = s.createToastCaption();
73 s.icon = s.createToastIcon();
74 if (s.icon) s.wrapper.appendChild(s.icon);
75 s.wrapper.appendChild(s.caption);
76 s.toast.appendChild(s.wrapper);
77 s.mask.appendChild(s.toast);
78 s.parent.appendChild(s.mask);
79 };
80 s.update = function () {
81 if (s.params.mask) s.mask = typeof s.params.mask === 'string' ? document.querySelector(s.params.mask) : s.params.mask;
82 if (s.mask) {
83 s.toast = s.mask.querySelector('.' + s.params.toastClass);
84 s.wrapper = s.mask.querySelector('.' + s.params.wrapperClass);
85 } else {
86 s.create();
87 }
88 s.toast.style.webkitTransitionDuration = s.params.duration + 'ms';
89 };
90 s.update();
91
92 /* --------------------
93 Method
94 -------------------- */
95 s.setMaskClassName = function (className) {
96 s.params.maskClass = className;
97 s.mask.setAttribute('class', s.params.maskClass);
98 };
99 s.setToastClassName = function (className) {
100 s.params.toastClass = className;
101 s.toast.setAttribute('class', s.params.toastClass);
102 };
103 s.setIcon = function (className) {
104 s.params.icon = className;
105 if (s.params.icon) {
106 s.icon.setAttribute('class', s.params.iconClass + (s.params.icon ? ' ' + s.params.icon : ''));
107 } else {
108 s.icon.setAttribute('class', '');
109 }
110 };
111 s.setDelay = function (delay) {
112 s.params.delay = delay;
113 };
114 s.setHTML = function (html) {
115 s.caption.innerHTML = html;
116 };
117
118 s.showMask = function () {
119 s.mask.classList.add(s.params.maskActiveClass);
120 };
121 s.hideMask = function () {
122 s.mask.classList.remove(s.params.maskActiveClass);
123 };
124 s.destroyMask = function () {
125 s.mask.parentNode.removeChild(s.mask);
126 };
127
128 s.showToast = function () {
129 s.toast.classList.add(s.params.toastActiveClass);
130 };
131 s.hideToast = function () {
132 s.toast.classList.remove(s.params.toastActiveClass);
133 };
134
135 s.isHid = true;
136 s.hide = function () {
137 s.isHid = true;
138 s.hideMask();
139 s.hideToast();
140 if (s.params.duration === 0) s.onTransitionEnd();
141 };
142 s.show = function () {
143 s.isHid = false;
144 s.showMask();
145 s.showToast();
146 if (s.params.duration === 0) s.onTransitionEnd();
147
148 // 显示数秒后,自动消失
149 if (s.params.delay) {
150 if (s.delayer) window.clearTimeout(s.delayer);
151 s.delayer = setTimeout(function () {
152 s.hide();
153 }, s.params.delay);
154 }
155 };
156 s.destroy = function () {
157 s.destroyMask();
158 };
159 /* --------------------
160 Controller
161 -------------------- */
162 s.events = function (detach) {
163 var target = s.toast;
164 var action = detach ? 'removeEventListener' : 'addEventListener';
165 target[action]('webkitTransitionEnd', s.onTransitionEnd, false);
166 // target[action]('webkitAnimationEnd',s.onAnimationEnd,false)
167 };
168 s.attach = function () {
169 s.events();
170 };
171 s.detach = function () {
172 s.events(false);
173 };
174 s.transitionFlag = true;
175 // Events Handler
176 s.onTransitionEnd = function (e) {
177 if (e.propertyName === 'visibility') return;
178 // 防止animationend和transitionend多次执行的问题
179 if (s.transitionFlag) {
180 s.transitionFlag = false;
181 if (s.isHid) {
182 // Callback onHid
183 if (s.params.onHid) s.params.onHid(s);
184 } else {
185 // Callback onShowed
186 if (s.params.onShowed) s.params.onShowed(s);
187 }
188 }
189 };
190 /* --------------------
191 Init
192 -------------------- */
193 s.init = function () {
194 s.attach();
195 };
196 s.init();
197};
198
199export default Toast;
\No newline at end of file