|
2 | 2 | * Updated by linxin on 2017/7/27.
|
3 | 3 | */
|
4 | 4 | var Toast = {};
|
5 |
| -var showToast = false, // 存储toast显示状态 |
6 |
| - showLoad = false, // 存储loading显示状态 |
| 5 | +var toastTimer = false, // 存储toast定时器id |
7 | 6 | toastVM = null, // 存储toast vm
|
| 7 | + showLoad = false, // 存储loading显示状态 |
8 | 8 | loadNode = null; // 存储loading节点元素
|
9 | 9 |
|
10 | 10 | Toast.install = function (Vue, options) {
|
11 | 11 |
|
12 | 12 | var opt = {
|
13 |
| - defaultType: 'bottom', |
| 13 | + type: 'bottom', |
14 | 14 | duration: '2500',
|
15 |
| - wordWrap: false |
| 15 | + wordWrap: false, |
| 16 | + width: 'auto' |
16 | 17 | };
|
17 | 18 | for (var property in options) {
|
18 | 19 | opt[property] = options[property];
|
19 | 20 | }
|
20 |
| - |
21 |
| - Vue.prototype.$toast = function (tips, type) { |
22 |
| - |
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>'; |
27 |
| - |
28 |
| - if (showToast) { |
29 |
| - // 如果toast还在,则不再执行 |
30 |
| - return; |
| 21 | + Vue.prototype.$toast = function (tip, config) { |
| 22 | + let options = JSON.parse(JSON.stringify(opt)); |
| 23 | + if(typeof config === 'object'){ |
| 24 | + for (var property in config) { |
| 25 | + options[property] = config[property]; |
| 26 | + } |
| 27 | + }else if(config){ |
| 28 | + options['type'] = config |
| 29 | + } |
| 30 | + if (toastTimer) { |
| 31 | + // 如果toast还在,则取消上次消失时间 |
| 32 | + clearTimeout(toastTimer); |
| 33 | + toastVM.show = false; |
31 | 34 | }
|
32 | 35 | if (!toastVM) {
|
33 | 36 | var toastTpl = Vue.extend({
|
34 | 37 | data: function () {
|
35 | 38 | return {
|
36 |
| - show: showToast, |
37 |
| - tip: tips, |
38 |
| - type: 'lx-toast-' + curType |
| 39 | + show: false, |
| 40 | + tip: tip, |
| 41 | + wordWrap: options.wordWrap, |
| 42 | + type: options.type, |
| 43 | + extStyle: { |
| 44 | + width: options.width |
| 45 | + }, |
39 | 46 | }
|
40 | 47 | },
|
41 |
| - template: tmp |
| 48 | + render(h) { |
| 49 | + if(!this.show) return |
| 50 | + return h( |
| 51 | + 'div', |
| 52 | + { |
| 53 | + class: ['lx-toast', 'lx-toast-' + this.type, this.wordWrap?'lx-word-wrap':''], |
| 54 | + style: this.extStyle, |
| 55 | + show: this.show, |
| 56 | + domProps: { |
| 57 | + innerHTML: this.tip |
| 58 | + } |
| 59 | + } |
| 60 | + ) |
| 61 | + } |
42 | 62 | });
|
43 | 63 | toastVM = new toastTpl()
|
44 | 64 | var tpl = toastVM.$mount().$el;
|
45 | 65 | document.body.appendChild(tpl);
|
46 | 66 | }
|
47 |
| - toastVM.type = 'lx-toast-' + curType; |
48 |
| - toastVM.tip = tips; |
49 |
| - toastVM.show = showToast = true; |
50 | 67 |
|
51 |
| - setTimeout(function () { |
52 |
| - toastVM.show = showToast = false; |
53 |
| - }, opt.duration) |
| 68 | + toastVM.tip = tip; |
| 69 | + toastVM.wordWrap = options.wordWrap; |
| 70 | + toastVM.type = options.type; |
| 71 | + toastVM.extStyle.width = options.width; |
| 72 | + toastVM.show = true; |
| 73 | + |
| 74 | + toastTimer = setTimeout(function () { |
| 75 | + toastVM.show = toastTimer = false; |
| 76 | + }, options.duration) |
54 | 77 | };
|
55 | 78 | ['bottom', 'center', 'top'].forEach(function (type) {
|
56 |
| - Vue.prototype.$toast[type] = function (tips) { |
57 |
| - return Vue.prototype.$toast(tips, type) |
| 79 | + Vue.prototype.$toast[type] = function (tip, config) { |
| 80 | + config.type = type |
| 81 | + return Vue.prototype.$toast(tip, config) |
58 | 82 | }
|
59 | 83 | });
|
60 | 84 |
|
61 |
| - Vue.prototype.$loading = function (tips, type) { |
| 85 | + Vue.prototype.$loading = function (tip, type) { |
62 | 86 | if (type == 'close') {
|
63 |
| - loadNode.show = showLoad = false; |
64 |
| - var markNode = document.querySelector('.lx-load-mark'); |
65 |
| - if (markNode) { |
66 |
| - if (typeof markNode.remove === 'function') { |
67 |
| - markNode.remove(); |
68 |
| - } else { // IE have not remove |
69 |
| - markNode.parentNode.removeChild(markNode); |
70 |
| - } |
71 |
| - } |
| 87 | + if(loadNode) loadNode.show = showLoad = false; |
72 | 88 | } else {
|
73 |
| - if (showLoad) { |
74 |
| - // 如果loading还在,则不再执行 |
| 89 | + if (showLoad && loadNode) { |
| 90 | + loadNode.tip = tip |
75 | 91 | return;
|
76 | 92 | }
|
77 | 93 | var loadTpl = Vue.extend({
|
78 | 94 | data: function () {
|
79 | 95 | return {
|
80 |
| - show: showLoad |
| 96 | + show: false, |
| 97 | + tip: tip |
81 | 98 | }
|
82 | 99 | },
|
83 |
| - 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>' |
| 100 | + render(h) { |
| 101 | + if(!this.show) return |
| 102 | + return h( |
| 103 | + 'div', |
| 104 | + { |
| 105 | + attrs: { |
| 106 | + 'class': 'lx-load-mark' |
| 107 | + }, |
| 108 | + show: this.show |
| 109 | + }, |
| 110 | + [ |
| 111 | + h( |
| 112 | + 'div', |
| 113 | + { |
| 114 | + attrs: { |
| 115 | + 'class': 'lx-load-box' |
| 116 | + } |
| 117 | + },[ |
| 118 | + h( |
| 119 | + 'div', |
| 120 | + { |
| 121 | + attrs: { |
| 122 | + 'class': 'lx-loading' |
| 123 | + } |
| 124 | + }, Array.apply(null, {length: 12}).map(function(value, index){ |
| 125 | + return h('div', {attrs: {'class': 'loading_leaf loading_leaf_' + index}}) |
| 126 | + }) |
| 127 | + ), |
| 128 | + h( |
| 129 | + 'div', |
| 130 | + { |
| 131 | + attrs: { |
| 132 | + 'class': 'lx-load-content' |
| 133 | + }, |
| 134 | + domProps: { |
| 135 | + innerHTML: this.tip |
| 136 | + } |
| 137 | + } |
| 138 | + ) |
| 139 | + ] |
| 140 | + ) |
| 141 | + ] |
| 142 | + ); |
| 143 | + } |
84 | 144 | });
|
85 | 145 | loadNode = new loadTpl();
|
86 | 146 | var tpl = loadNode.$mount().$el;
|
|
0 commit comments