grass-prompt.js
3.01 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
112
113
114
115
116
117
118
import {get, isArray} from 'lodash';
import {createStore} from '../store';
export default {
install(Vue) {
let promptVM = null;
let promptEle = null;
let timer = null;
function createPromptVM(parent) {
let tpl = Vue.extend({
store: parent.$store,
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 ? 'a' : 'div', {
class: classNames,
attrs: {
href: this.url || '',
},
on: {
click: () => {
this.onClick && this.onClick();
}
}
}, [
h('ImageFormat', {
class: ['prompt-thumb'],
props: {
src: this.img,
lazy: false,
mode: 1,
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, url, type, className, onClick} = data;
if (!promptVM) {
createPromptVM(this);
}
promptVM.img = img || '';
promptVM.title = title || '';
promptVM.desc = desc || '';
promptVM.url = url;
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;
}
}
}