-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.js
55 lines (50 loc) · 1.31 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
import { h, ref, watchEffect, onMounted, onBeforeUnmount } from 'vue';
const ctors = {
Highcharts: 'chart',
Highstock: 'stockChart',
Highmaps: 'mapChart',
HighchartsGantt: 'ganttChart',
};
function render() {
return h('div', { ref: 'highchartsRef' });
}
function createHighcharts(name, Highcharts) {
const ctor = Highcharts[ctors[name]];
if (!ctor) {
return Highcharts.win
? null
// When running in server, Highcharts will not be instanced,
// so there're no constructors in Highcharts,
// to avoid unmated content during SSR, it returns minimum component.
: { render };
}
return {
name,
props: ['options'],
render,
setup(props) {
const highchartsRef = ref(null);
const chart = ref(null);
onMounted(() => {
watchEffect(() => {
chart.value = ctor(highchartsRef.value, props.options);
});
});
onBeforeUnmount(() => {
if (highchartsRef.value) {
chart.value.destroy();
}
});
return { highchartsRef, chart };
},
};
}
export { createHighcharts };
export default function install(app, options) {
Object.keys(ctors).forEach((name) => {
const component = createHighcharts(name, options.Highcharts);
if (component) {
app.component(name, component);
}
});
}