-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-component.js
156 lines (150 loc) · 4.31 KB
/
vue-component.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
'use strict'
import Vue from 'vue'
import Case from 'case'
let VueComponent = function() {
const PROPERTY_RADIX = 'data-component--',
REGEXP_PROPERTY = new RegExp(`^${PROPERTY_RADIX}`),
VUE_EVENT_BUS = new Vue()
let $mainVue = null,
$templateNames = [],
$templateProperties = [],
$templateCache = [],
$parser = new DOMParser()
// utils
let generateUID = (name = '', strength = 12, b = '', a = 1) => {
while (a < strength) {
b +=
(a * 51) & 52
? (a ^ 15 ? 8 ^ (Math.random() * (a ^ 20 ? 16 : 4)) : 4).toString(
16
)
: '-'
a++
}
return `${name}${name.length > 0 ? '--' : ''}${b}`
},
trimValues = (value = '') => `${value}`.trim(),
concat = function(...list) {
return list.map(trimValues).join(' ')
},
// hooks
getMainVue = () => $mainVue,
getEventBus = () => VUE_EVENT_BUS,
// build
register = function(components, options = null) {
const PROMISES = []
.concat(components)
.map(component => registerOneComponent(`${component}`)) // as per @laruiss review
return Promise.all(PROMISES)
.then(_templates => {
for (let _html of _templates) {
let { _template, _props } = getTemplateProperties(_html)
prepare(`${_template}`, _props)
}
})
.then(() => {
let _v = options !== null ? set(options) : null
fireEvent('global.init')
return _v
})
},
isolateComponentName = function(componentName) {
if (/\?noscript$/i.test(componentName) === true) {
return [componentName.replace(/^(.*)\?noscript$/i, '$1'), '?noscript=1']
} else {
return [componentName, '']
}
},
registerOneComponent = function(componentName) {
let _splitComponentName = isolateComponentName(componentName),
$index = $templateNames.findIndex(
(name, index) => name === _splitComponentName[0]
)
if ($index === -1) {
return fetchTemplate(_splitComponentName[0], _splitComponentName[1])
} else {
return build($templateCache[$index], name, $templateProperties[$index])
}
},
fetchTemplate = (componentName, noscript = '') =>
System['import'](`/api/get-component/${componentName}${noscript}!text`),
getTemplateProperties = function(template = '') {
let _node = $parser.parseFromString(template, 'text/html'),
_body = _node.getElementsByTagName('body').item(0),
_scriptNode = null,
_target = _body.childNodes.item(0),
_propertyName = '',
_props = {
name: '',
properties: [],
methods: {}
}
for (let _prop of Array.from(_target.attributes)) {
if (_prop != 'prototype' && _prop != 'length') {
if (REGEXP_PROPERTY.test(_prop.name) === true) {
_propertyName = _prop.name.substring(PROPERTY_RADIX.length)
if (_propertyName === 'name') {
_props.name = _prop.value
} else {
_props.properties.push(Case.toCamelCase(_propertyName))
}
}
}
}
_scriptNode = _node !== null ? _node.getElementsByTagName('script') : null
if (_scriptNode && _scriptNode[0]) {
_props.methods = eval(_scriptNode[0].innerHTML).make(
getMainVue,
getEventBus
)
}
return { _template: _target.outerHTML, _props }
},
prepare = function(template, _props) {
$templateNames.push(_props.name)
$templateProperties.push(_props.properties)
$templateCache.push(template)
build(template, _props.name, _props.properties, _props.methods)
},
build = function(template, name, props = {}, methods = {}) {
methods = { ...methods, ...{ generateUID, concat } } // utilities
let _component = Vue.extend({
template,
props,
methods,
data: () => ({ uniqueId: generateUID(name) })
})
Vue.component(name, _component)
},
set = function(options) {
try {
return new Vue(options)
} catch (err) {
console.error(err)
return null
}
},
addInitListener = _callback => addEventListener('global.init', _callback),
addEventListener = (_eventName, _callback) =>
VUE_EVENT_BUS.$on(_eventName, _callback),
fireEvent = (_eventName, ...args) =>
VUE_EVENT_BUS.$emit(_eventName, ...args),
track = _vue => {
$mainVue = _vue
$mainVue.$nextTick(() => {
for (let component of $mainVue.$children) {
if (component.init) component.init()
}
})
}
return {
register,
set,
addInitListener,
addEventListener,
fireEvent,
track,
generateUID
}
}
module.exports = new VueComponent()