-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathutil.js
171 lines (157 loc) · 3.5 KB
/
util.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as Vue from 'vue';
import vuescroll from 'src/entry-mix-mode';
export { vuescroll };
// https://github.com/ElemeFE/element/blob/dev/test/unit/util.js#L60
let id = 0;
const createElm = function () {
const elm = document.createElement('div');
elm.id = 'app' + ++id;
document.body.appendChild(elm);
return elm;
};
/**
* Crate a vue instance
*
* @export
* @param {any} Compo
* @param {boolean} [mounted=false]
* @returns
*/
export function createVue(Compo, mounted = false) {
if (Object.prototype.toString.call(Compo) === '[object String]') {
Compo = { template: Compo };
}
if (
Compo.data &&
Object.prototype.toString.call(Compo.data) === '[object Object]'
) {
const temp = Compo.data;
Compo.data = function () {
return temp;
};
}
return Vue.createApp(Compo)
.use(vuescroll)
.mount(mounted === false ? null : createElm());
}
/**
* mount a component instance
*
* @export
* @param {any} Compo
* @param {any} [propsData={}]
* @param {boolean} [mounted=false]
* @returns
*/
export function createTest(Compo, propsData = {}, mounted = false) {
if (propsData === true || propsData === false) {
mounted = propsData;
propsData = {};
}
const elm = createElm();
const Ctor = Vue.extend(Compo);
return new Ctor({ propsData }).$mount(mounted === false ? null : elm);
}
export function destroyVM(vm) {
vm.$refs.vs && vm.$refs.vs.destroy && vm.$refs.vs.destroy();
vm.$el && vm.$el.parentNode && vm.$el.parentNode.removeChild(vm.$el);
}
/**
* trigger an event
*/
export function trigger(elm, name, ...opts) {
let evt = null;
if (/^mouse|click/.test(name)) {
evt = new MouseEvent(name, ...opts);
} else if (/^key/.test(name)) {
evt = new KeyboardEvent(name, ...opts);
} else {
evt = new Event(name, ...opts);
}
elm.dispatchEvent ? elm.dispatchEvent(evt) : elm.fireEvent('on' + name, evt);
return elm;
}
export function makeTemplate(
child,
parent,
templateAttribute = null,
num = 1,
extraTmpl = ''
) {
return `
<div style="width:${parent.w}px;height:${parent.h}px;position:relative">
<vue-scroll ref="vs" ${templateAttribute || ''} :ops="ops">
<template v-slot:default>
<div
v-for="i in ${num}"
:key="i"
:id="'d' + i"
style="width:${child.w}px;height:${child.h}px">
</div>
</template>
${extraTmpl}
</vue-scroll>
</div>
`;
}
export function startSchedule(time = 0) {
let queue = [];
function wait(time = 0) {
queue.push({
type: 'wait',
value: time
});
return {
then,
wait
};
}
function then(cb = () => {}) {
queue.push({
type: 'then',
value: cb
});
return {
then,
wait
};
}
function loopSchedule() {
if (!queue.length) {
return;
}
let current = queue.shift();
if (current.type == 'wait') {
setTimeout(() => {
loopSchedule();
}, current.value);
} else if (current.type == 'then') {
if (current.value.length > 0) {
let timeId = setTimeout(() => {
loopSchedule();
}, 5000); // timeout 5s
current.value(() => {
clearTimeout(timeId);
setTimeout(() => {
loopSchedule();
}, 0);
});
} else {
current.value();
setTimeout(() => {
loopSchedule();
}, 0);
}
}
}
queue.push({
type: 'wait',
value: time
});
loopSchedule();
return {
then,
wait
};
}
export { Vue };