-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
116 lines (91 loc) · 2.48 KB
/
index.ts
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
import Vue, { VueConstructor } from 'vue';
import hypernova, { load } from 'hypernova';
import { findNode, getData } from 'nova-helpers';
import { CombinedVueInstance } from 'vue/types/vue';
type HypernovaPayload = {
node: HTMLElement;
data: any;
}
type VueInstance = CombinedVueInstance<Vue, object, object, object, object>
type VueWithStoreInstance =
CombinedVueInstance<Vue, object, object, object, object> & { $store: any };
export { default as Vue } from 'vue';
export { load } from 'hypernova';
export const mountComponent = (
Component: VueConstructor,
node: HTMLElement,
data: any,
): VueInstance => {
const vm = new Component({
propsData: data,
});
if (!node.childElementCount) {
node.appendChild(document.createElement('div'));
}
vm.$mount(node.children[0]);
return vm;
};
export const renderInPlaceholder = (
name: string,
Component: VueConstructor,
id: string,
): VueInstance => {
const node: HTMLElement = findNode(name, id);
const data: any = getData(name, id);
if (node && data) {
return mountComponent(Component, node, data);
}
return null;
};
export const loadById = (name: string, id: string): HypernovaPayload => {
const node = findNode(name, id);
const data = getData(name, id);
if (node && data) {
return {
node,
data,
};
}
return null;
};
export const renderVue = (name: string, Component: VueConstructor): void => hypernova({
server() {
throw new Error('Use hypernova-vue/server instead');
},
client() {
const payloads = load(name);
if (payloads) {
payloads.forEach((payload: HypernovaPayload) => {
const { node, data: propsData } = payload;
mountComponent(Component, node, propsData);
});
}
return Component;
},
});
export const renderVuex = (
name: string,
ComponentDefinition: any,
createStore: Function,
): void => hypernova({
server() {
throw new Error('Use hypernova-vue/server instead');
},
client() {
const payloads = load(name);
if (payloads) {
payloads.forEach((payload: HypernovaPayload) => {
const { node, data } = payload;
const { propsData, state } = data;
const store = createStore();
store.replaceState(state);
const Component: VueConstructor = Vue.extend({
...ComponentDefinition,
store,
});
const vm = mountComponent(Component, node, propsData) as VueWithStoreInstance;
});
}
return ComponentDefinition;
},
});