forked from WilliamDASILVA/vue-browserupdate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (48 loc) · 1.34 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
import browserUpdate from 'browser-update';
function addLoadEvent(func) {
const oldOnLoad = window.onload;
if (typeof window.onload !== 'function') {
window.onload = func;
} else {
window.onload = () => {
if (oldOnLoad) {
oldOnLoad();
}
func();
}
}
}
const VueBrowserUpdate = {};
VueBrowserUpdate.install = (Vue, opts) => {
const options = opts;
const funcs = {
show: [],
click: [],
close: [],
};
function dispatch(func, infos) {
funcs[func].forEach(f => f(infos));
}
addLoadEvent(() => {
options.options.onshow = infos => dispatch('show', infos);
options.options.onclick = infos => dispatch('click', infos);
options.options.onclose = infos => dispatch('close', infos);
if (typeof browserUpdate === 'undefined') {
throw new Error('The plugin "browser-update" could not be loaded.');
} else {
if (!options.containerAsync) {
browserUpdate(options.options, options.test);
}
}
});
Vue.browserUpdate = {
onshow: func => funcs.show.push(func),
onclick: func => funcs.click.push(func),
onclose: func => funcs.close.push(func),
appendContainer: (container) => {
if (container) options.options.container = container;
browserUpdate(options.options, options.test);
},
};
};
export default VueBrowserUpdate;