1
1
/**
2
- * Updated by linxin on 2017/7/5 .
2
+ * Updated by linxin on 2017/7/27 .
3
3
*/
4
4
var Toast = { } ;
5
- var showToast = false , // 存储toast显示状态
6
- showLoad = false , // 存储loading显示状态
7
- loadNode = null ; // 存储loading节点元素
5
+ var showToast = false , // 存储toast显示状态
6
+ showLoad = false , // 存储loading显示状态
7
+ toastVM = null , // 存储toast vm
8
+ loadNode = null ; // 存储loading节点元素
8
9
9
10
Toast . install = function ( Vue , options ) {
11
+
10
12
var opt = {
11
- defaultType :'bottom' ,
12
- duration :'2500'
13
+ defaultType : 'bottom' ,
14
+ duration : '2500' ,
15
+ wordWrap : false
13
16
} ;
14
- for ( var property in options ) {
17
+ for ( var property in options ) {
15
18
opt [ property ] = options [ property ] ;
16
19
}
17
- Vue . prototype . $toast = function ( tips , type ) {
20
+
21
+ Vue . prototype . $toast = function ( tips , type ) {
18
22
19
23
var curType = type ? type : opt . defaultType ;
24
+ var wordWrap = opt . wordWrap ? 'lx-word-wrap' : '' ;
25
+ var style = opt . width ? 'style="width: ' + opt . width + '"' : '' ;
26
+ var tmp = '<div v-show="show" :class="type" class="lx-toast ' + wordWrap + '" ' + style + '>{{tip}}</div>' ;
20
27
21
- if ( showToast ) {
28
+ if ( showToast ) {
22
29
// 如果toast还在,则不再执行
23
30
return ;
24
31
}
25
- var toastTpl = Vue . extend ( {
26
- data : function ( ) {
27
- return {
28
- show : showToast
29
- }
30
- } ,
31
- template : '<div v-show="show" class="lx-toast lx-toast-' + curType + '">' + tips + '</div>'
32
- } ) ;
33
- var vm = new toastTpl ( )
34
- var tpl = vm . $mount ( ) . $el ;
35
-
36
- document . body . appendChild ( tpl ) ;
37
- vm . show = showToast = true ;
32
+ if ( ! toastVM ) {
33
+ var toastTpl = Vue . extend ( {
34
+ data : function ( ) {
35
+ return {
36
+ show : showToast ,
37
+ tip : tips ,
38
+ type : 'lx-toast-' + curType
39
+ }
40
+ } ,
41
+ template : tmp
42
+ } ) ;
43
+ toastVM = new toastTpl ( )
44
+ var tpl = toastVM . $mount ( ) . $el ;
45
+ document . body . appendChild ( tpl ) ;
46
+ }
47
+ toastVM . type = 'lx-toast-' + curType ;
48
+ toastVM . tip = tips ;
49
+ toastVM . show = showToast = true ;
38
50
39
51
setTimeout ( function ( ) {
40
- vm . show = showToast = false ;
52
+ toastVM . show = showToast = false ;
41
53
} , opt . duration )
42
54
} ;
43
- [ 'bottom' , 'center' , 'top' ] . forEach ( function ( type ) {
44
- Vue . prototype . $toast [ type ] = function ( tips ) {
45
- return Vue . prototype . $toast ( tips , type )
55
+ [ 'bottom' , 'center' , 'top' ] . forEach ( function ( type ) {
56
+ Vue . prototype . $toast [ type ] = function ( tips ) {
57
+ return Vue . prototype . $toast ( tips , type )
46
58
}
47
59
} ) ;
48
60
49
- Vue . prototype . $loading = function ( tips , type ) {
50
- if ( type == 'close' ) {
61
+ Vue . prototype . $loading = function ( tips , type ) {
62
+ if ( type == 'close' ) {
51
63
loadNode . show = showLoad = false ;
52
- } else {
53
- if ( showLoad ) {
64
+ } else {
65
+ if ( showLoad ) {
54
66
// 如果loading还在,则不再执行
55
67
return ;
56
68
}
57
69
var loadTpl = Vue . extend ( {
58
- data : function ( ) {
70
+ data : function ( ) {
59
71
return {
60
72
show : showLoad
61
73
}
62
74
} ,
63
- template : '<div v-show="show" class="lx-load-mark"><div class="lx-load-box"><div class="lx-loading"><div class="loading_leaf loading_leaf_0"></div><div class="loading_leaf loading_leaf_1"></div><div class="loading_leaf loading_leaf_2"></div><div class="loading_leaf loading_leaf_3"></div><div class="loading_leaf loading_leaf_4"></div><div class="loading_leaf loading_leaf_5"></div><div class="loading_leaf loading_leaf_6"></div><div class="loading_leaf loading_leaf_7"></div><div class="loading_leaf loading_leaf_8"></div><div class="loading_leaf loading_leaf_9"></div><div class="loading_leaf loading_leaf_10"></div><div class="loading_leaf loading_leaf_11"></div></div><div class="lx-load-content">' + tips + '</div></div></div>'
75
+ template : '<div v-show="show" class="lx-load-mark"><div class="lx-load-box"><div class="lx-loading"><div class="loading_leaf loading_leaf_0"></div><div class="loading_leaf loading_leaf_1"></div><div class="loading_leaf loading_leaf_2"></div><div class="loading_leaf loading_leaf_3"></div><div class="loading_leaf loading_leaf_4"></div><div class="loading_leaf loading_leaf_5"></div><div class="loading_leaf loading_leaf_6"></div><div class="loading_leaf loading_leaf_7"></div><div class="loading_leaf loading_leaf_8"></div><div class="loading_leaf loading_leaf_9"></div><div class="loading_leaf loading_leaf_10"></div><div class="loading_leaf loading_leaf_11"></div></div><div class="lx-load-content">' + tips + '</div></div></div>'
64
76
} ) ;
65
77
loadNode = new loadTpl ( ) ;
66
78
var tpl = loadNode . $mount ( ) . $el ;
@@ -70,9 +82,9 @@ Toast.install = function (Vue, options) {
70
82
}
71
83
} ;
72
84
73
- [ 'open' , 'close' ] . forEach ( function ( type ) {
74
- Vue . prototype . $loading [ type ] = function ( tips ) {
75
- return Vue . prototype . $loading ( tips , type )
85
+ [ 'open' , 'close' ] . forEach ( function ( type ) {
86
+ Vue . prototype . $loading [ type ] = function ( tips ) {
87
+ return Vue . prototype . $loading ( tips , type )
76
88
}
77
89
} ) ;
78
90
}
0 commit comments