blob: 68d41f664e537eceedc261978349de440fe561e1 [file] [log] [blame]
qiaoweif044a742019-07-10 16:04:20 +08001/**
2 * aui-popup.js
3 * @author 流浪男
4 * @todo more things to abstract, e.g. Loading css etc.
5 * Licensed under the MIT license.
6 * http://www.opensource.org/licenses/mit-license.php
7 */
8(function( window, undefined ) {
9 "use strict";
10 var auiToast = function() {
11 // this.create();
12 };
13 var isShow = false;
14 auiToast.prototype = {
15 create: function(params,callback) {
16 var self = this;
17 var toastHtml = '';
18 switch (params.type) {
19 case "success":
20 var iconHtml = '<i class="aui-iconfont aui-icon-correct"></i>';
21 break;
22 case "fail":
23 var iconHtml = '<i class="aui-iconfont aui-icon-close"></i>';
24 break;
25 case "custom":
26 var iconHtml = params.html;
27 break;
28 case "loading":
29 var iconHtml = '<div class="aui-toast-loading"></div>';
30 break;
31 }
32
33 var titleHtml = params.title ? '<div class="aui-toast-content">'+params.title+'</div>' : '';
34 toastHtml = '<div class="aui-toast">'+iconHtml+titleHtml+'</div>';
35 if(document.querySelector(".aui-toast"))return;
36 document.body.insertAdjacentHTML('beforeend', toastHtml);
37 var duration = params.duration ? params.duration : "2000";
38 self.show();
39 if(params.type == 'loading'){
40 if(callback){
41 callback({
42 status: "success"
43 });
44 };
45 }else{
46 setTimeout(function(){
47 self.hide();
48 }, duration)
49 }
50 },
51 show: function(){
52 var self = this;
53 document.querySelector(".aui-toast").style.display = "block";
54 document.querySelector(".aui-toast").style.marginTop = "-"+Math.round(document.querySelector(".aui-toast").offsetHeight/2)+"px";
55 if(document.querySelector(".aui-toast"))return;
56 },
57 hide: function(){
58 var self = this;
59 if(document.querySelector(".aui-toast")){
60 document.querySelector(".aui-toast").parentNode.removeChild(document.querySelector(".aui-toast"));
61 }
62 },
63 remove: function(){
64 if(document.querySelector(".aui-dialog"))document.querySelector(".aui-dialog").parentNode.removeChild(document.querySelector(".aui-dialog"));
65 if(document.querySelector(".aui-mask")){
66 document.querySelector(".aui-mask").classList.remove("aui-mask-out");
67 }
68 return true;
69 },
70 success: function(params,callback){
71 var self = this;
72 params.type = "success";
73 return self.create(params,callback);
74 },
75 fail: function(params,callback){
76 var self = this;
77 params.type = "fail";
78 return self.create(params,callback);
79 },
80 custom:function(params,callback){
81 var self = this;
82 params.type = "custom";
83 return self.create(params,callback);
84 },
85 loading:function(params,callback){
86 var self = this;
87 params.type = "loading";
88 return self.create(params,callback);
89 }
90 };
91 window.auiToast = auiToast;
92})(window);