-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (69 loc) · 1.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author liupeng ([email protected]) @kefong
*/
import view from './view'
var Toast = {
vm: null,
body: null,
opt: {
type: 'success',//error,
delay: 3000,
top: 5,
}
}
Toast.install = function(Vue, options){
// Vue
Toast.vm = Vue;
// init
for (var property in options) {
Toast.opt[property] = options[property];
}
// create box
if(Toast.body === null){
Toast.body = document.createElement('div');
Toast.body.className = 'kefong-vue-toast';
document.body.appendChild(Toast.body);
}
Vue.prototype.$toast = function(message, opt){
// set message | type
if(typeof(opt) === 'string'){
Toast.opt.type = opt;
}else if(typeof(opt) === 'object'){
for (var property in opt) {
Toast.opt[property] = opt[property];
}
}
// create div and show
var toastView = Vue.component('toast-view', view);
var v = new toastView();
v.message = message;
v.type = 'kefong-toast-'+ Toast.opt.type;
v.delay = Toast.opt.delay;
v.top = Toast.opt.top;
Toast.body.appendChild(v.$mount().$el);
}
// error
Vue.prototype.$toast.error = function(message, opt){
if(typeof(opt) === 'undefined'){
opt = {type: 'error'}
}else if(typeof(opt) === 'object'){
opt.type = 'error';
}else if(typeof(opt) === 'string'){
opt = 'error';
}
Vue.prototype.$toast(message, opt);
}
// success
Vue.prototype.$toast.success = function(message, opt){
if(typeof(opt) === 'undefined'){
opt = {type: 'success'}
}else if(typeof(opt) === 'object'){
opt.type = 'success';
}else if(typeof(opt) === 'string'){
opt = 'success';
}
Vue.prototype.$toast(message, opt);
}
}
export default Toast;