-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-morph.js
89 lines (73 loc) · 2.37 KB
/
vue-morph.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
/* eslint-disable */
import Btn from './src/components/Btn.vue';
import CheckBox from './src/components/CheckBox.vue';
import EditText from './src/components/EditText.vue';
import LinearLayout from './src/components/LinearLayout.vue';
import ScrollView from './src/components/ScrollView.vue';
import TextView from './src/components/TextView.vue';
import RadioGroup from './src/components/RadioGroup.vue';
// TODO, better way than adding to window?
// An array of components uids mapped to their classes
// e.g. [{ uid: 5, component: {...} }]
window.uidToVueComponentMap = [];
/**
* Build a JSON object of the DOM
* @param {VNode} root - Parse form here and down
*/
window.buildVNodeTree = (root) => {
const children = root.$children;
let tag;
const uid = root._uid;
let description;
if (root.$vnode) {
tag = root.$vnode.componentOptions.tag;
if (root.$vnode.componentInstance.describe) {
description = root.$vnode.componentInstance.describe();
}
} else if (root._vnode) {
tag = root._vnode.componentOptions.tag;
if (root._vnode.componentInstance.describe) {
description = root._vnode.componentInstance.describe();
}
}
window.uidToVueComponentMap.push({
uid,
component: root,
});
return {
tag,
...description,
children: children ? children.map(c => ({ ...this.buildVNodeTree(c) })) : [],
};
};
/**
* Event Dispatchers,
* There functions allow DOM/ Vue Component Events to be called Natively
*/
window.dispatchOnClick = function (uid) {
const map = window.uidToVueComponentMap.filter(component => component.uid == uid)[0];
map.component.onClick();
};
window.dispatchOnChange = function (params) {
const uid = params.split(', ')[0];
const value = params.split(', ')[1];
const map = window.uidToVueComponentMap.filter(component => component.uid == uid)[0];
map.component.setValue(value);
};
const VueMorph = {
install(Vue) {
Vue.component(Btn.name, Btn);
Vue.component(CheckBox.name, CheckBox);
Vue.component(EditText.name, EditText);
Vue.component(LinearLayout.name, LinearLayout);
Vue.component(ScrollView.name, ScrollView);
Vue.component(TextView.name, TextView);
Vue.component(RadioGroup.name, RadioGroup);
Vue.prototype.$vueMorph = {
setSideNavItems: (items) => {
Android.setSideNavItems(JSON.stringify(items));
},
};
},
};
export default VueMorph;