grass-prompt.js
2.9 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {isArray} from 'lodash';
export default {
install(Vue) {
let promptVM = null;
let promptEle = null;
let timer = null;
function creatPromptVM(self) {
let tpl = Vue.extend({
data: function () {
return {
type: '',
img: '',
title: '',
desc: '',
onClick: null,
classNames: []
}
},
render: function(h) {
let classNames = ['yoho-grass-prompt-wrap', ...this.classNames];
return h('div', {class: ['yoho-grass-prompt', `yoho-grass-prompt-${this.type}`]}, [
h(this.url ? 'router-link' : 'div', {
class: classNames,
on: {
click: () => {
this.onClick && this.onClick();
}
}
}, [
h('ImageFormat', {
class: ['prompt-thumb'],
props: {
src: this.img,
lazy: false,
width: 100,
height: 100
}
}),
h('div', {class: ['prompt-info']}, [
h('p', {
class: ['prompt-info-title'],
domProps: {
innerHTML: this.title
}
}),
h('p', {
class: ['prompt-info-desc'],
domProps: {
innerHTML: this.desc
}
})
]),
h('span', {class: ['iconfont', 'icon-right', 'prompt-icon']})
])
])
}
});
promptVM = new tpl();
promptEle = promptVM.$mount().$el;
document.body.appendChild(promptEle);
}
Vue.prototype.$grassPrompt = function(data = {}, time) {
let {img, title, desc, type, className, onClick} = data;
if (!promptVM) {
creatPromptVM(this);
}
promptVM.img = img || '';
promptVM.title = title;
promptVM.desc = desc;
promptVM.type = type || 'bottom';
if (className && !isArray(className)) {
className = [className];
}
promptVM.classNames = className || [];
if (typeof onClick === 'function') {
promptVM.onClick = onClick;
}
let res = {
show() {
timer && clearTimeout(timer);
timer = setTimeout(function() {
promptVM.classNames.push('yoho-grass-prompt-enter-active');
timer = setTimeout(function() {
promptVM.classNames.push('yoho-grass-prompt-leave-active');
}, time || 8000);
}, 100);
},
hide() {
timer && clearTimeout(timer);
promptVM.classNames = [];
}
}
res.show();
return res;
}
}
}