11/**
2- * Updated by linxin on 2017/7/5 .
2+ * Updated by linxin on 2017/7/27 .
33 */
44var 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节点元素
89
910Toast . install = function ( Vue , options ) {
11+
1012 var opt = {
11- defaultType :'bottom' ,
12- duration :'2500'
13+ defaultType : 'bottom' ,
14+ duration : '2500' ,
15+ wordWrap : false
1316 } ;
14- for ( var property in options ) {
17+ for ( var property in options ) {
1518 opt [ property ] = options [ property ] ;
1619 }
17- Vue . prototype . $toast = function ( tips , type ) {
20+
21+ Vue . prototype . $toast = function ( tips , type ) {
1822
1923 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>' ;
2027
21- if ( showToast ) {
28+ if ( showToast ) {
2229 // 如果toast还在,则不再执行
2330 return ;
2431 }
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 ;
3850
3951 setTimeout ( function ( ) {
40- vm . show = showToast = false ;
52+ toastVM . show = showToast = false ;
4153 } , opt . duration )
4254 } ;
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 )
4658 }
4759 } ) ;
4860
49- Vue . prototype . $loading = function ( tips , type ) {
50- if ( type == 'close' ) {
61+ Vue . prototype . $loading = function ( tips , type ) {
62+ if ( type == 'close' ) {
5163 loadNode . show = showLoad = false ;
52- } else {
53- if ( showLoad ) {
64+ } else {
65+ if ( showLoad ) {
5466 // 如果loading还在,则不再执行
5567 return ;
5668 }
5769 var loadTpl = Vue . extend ( {
58- data : function ( ) {
70+ data : function ( ) {
5971 return {
6072 show : showLoad
6173 }
6274 } ,
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>'
6476 } ) ;
6577 loadNode = new loadTpl ( ) ;
6678 var tpl = loadNode . $mount ( ) . $el ;
@@ -70,9 +82,9 @@ Toast.install = function (Vue, options) {
7082 }
7183 } ;
7284
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 )
7688 }
7789 } ) ;
7890}
0 commit comments