Skip to content

Commit 3c2b8e9

Browse files
committed
Add width and newline configuration
1 parent 10a8f72 commit 3c2b8e9

File tree

4 files changed

+505
-309
lines changed

4 files changed

+505
-309
lines changed

README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# vue2-toast
2-
A mobile toast plugin for vue2.(can't work in Nuxt.js/SSR)
2+
A mobile toast plugin for vue2.
33

44
<p>
55
<a href="https://www.npmjs.com/package/vue2-toast"><img src="https://img.shields.io/npm/dm/vue2-toast.svg" alt="Downloads"></a>
@@ -20,6 +20,17 @@ import 'vue2-toast/lib/toast.css';
2020
import Toast from 'vue2-toast';
2121
Vue.use(Toast);
2222
```
23+
or
24+
```javascript
25+
import 'vue2-toast/lib/toast.css';
26+
import Toast from 'vue2-toast';
27+
Vue.use(Toast, {
28+
defaultType: 'center',
29+
duration: 3000,
30+
wordWrap: true,
31+
width: '150px'
32+
});
33+
```
2334

2435
Use in component:
2536

@@ -74,8 +85,10 @@ build: {
7485

7586
Vue.use(Toast, [options])
7687

77-
- defaultType : position of Toast. | default: 'bottom' | possible 'top, center,bottom'
78-
- duration : default 2500ms
88+
- defaultType : position of Toast. | String | default: 'bottom' | possible 'top, center,bottom'
89+
- duration : Number | default 2500ms
90+
- wordWrap : word wrap. | Boolean | default: false
91+
- width : width of Toast. | String | default: 'auto'
7992

8093
## source code
8194
download in [Github](https://github.com/lin-xin/vue-toast).

lib/index.js

+47-35
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,78 @@
11
/**
2-
* Updated by linxin on 2017/7/5.
2+
* Updated by linxin on 2017/7/27.
33
*/
44
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节点元素
89

910
Toast.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

Comments
 (0)