forked from FrontendMatter/dom-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
200 lines (171 loc) · 5.19 KB
/
Copy pathhandler.js
File metadata and controls
200 lines (171 loc) · 5.19 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { factory } from '../factory'
const toCamelCase = (str) => str.replace(/(\-[a-z])/g, ($1) => $1.toUpperCase().replace('-', ''))
// The property name used to store the component config on a component reference
const CONFIG_PROPERTY = '__domFactoryConfig'
/**
* Component handler
* @type {Object}
*/
export const handler = {
// Registered components
_registered: [],
// Created component references
_created: [],
/**
* Register a component.
* @param {String} id A unique component ID.
* @param {Object} factory The component definition object.
*/
register (id, factory) {
const callbacks = []
let idChunks = id.split('-')
idChunks.splice(1, 0, 'js')
const cssClass = idChunks.join('-')
if (!this.findRegistered(id)) {
this._registered.push({
id,
cssClass,
callbacks,
factory
})
}
this.upgrade(id)
},
/**
* Register a callback to be called on component upgrade.
* @param {String} id The component ID.
* @param {Function} callback The callback function.
*/
registerUpgradedCallback (id, callback) {
const config = this.findRegistered(id)
if (config) {
config.callbacks.push(callback)
}
},
/**
* Get a registered component.
* @param {String} id The component ID.
* @return {Object} A configuration object.
*/
findRegistered (id) {
return this._registered.find(config => config.id === id)
},
/**
* Get a created component reference for an element.
* @param {HTMLElement} element
* @return {Object}
*/
findCreated (element) {
return this._created.filter(ref => ref.element === element)
},
/**
* Upgrade an element with a single component type or all of the registered components.
* @param {HTMLElement} element The element to upgrade.
* @param {String} id The component ID (optional).
*/
upgradeElement (element, id) {
if (id === undefined) {
this._registered.forEach(config => {
if (element.classList.contains(config.cssClass)) {
this.upgradeElement(element, config.id)
}
})
return
}
let upgraded = element.getAttribute('data-domfactory-upgraded')
const config = this.findRegistered(id)
if (config && (upgraded === null || upgraded.indexOf(id) === -1)) {
upgraded = upgraded === null ? [] : upgraded.split(',')
upgraded.push(id)
let component
try {
component = factory(config.factory(element), element)
}
catch (e) {
console.error(id, e.message, e.stack)
}
if (component) {
element.setAttribute('data-domfactory-upgraded', upgraded.join(','))
const instanceConfig = Object.assign({}, config)
delete instanceConfig.factory
component[CONFIG_PROPERTY] = instanceConfig
this._created.push(component)
Object.defineProperty(element, toCamelCase(id), {
configurable: true,
writable: false,
value: component
})
config.callbacks.forEach(cb => cb(element))
component.fire('domfactory-component-upgraded')
}
}
else if (config) {
let component = element[toCamelCase(id)]
if (typeof component._reset === 'function') {
component._reset()
}
}
},
/**
* Upgrade all elements matching a registered component ID.
* @param {String} id The component ID.
*/
upgrade (id) {
if (id === undefined) {
this.upgradeAll()
}
else {
const config = this.findRegistered(id)
if (config) {
const elements = [...document.querySelectorAll('.' + config.cssClass)]
elements.forEach(element => this.upgradeElement(element, id))
}
}
},
/**
* Upgrade all elements matching the registered components.
*/
upgradeAll () {
this._registered.forEach(config => this.upgrade(config.id))
},
/**
* Downgrade a component reference.
* @param {Object} component
*/
downgradeComponent (component) {
const index = this._created.indexOf(component)
this._created.splice(index, 1)
const upgrades = component.element.getAttribute('data-domfactory-upgraded').split(',')
const upgradeIndex = upgrades.indexOf(component[CONFIG_PROPERTY].id)
upgrades.splice(upgradeIndex, 1)
component.element.setAttribute('data-domfactory-upgraded', upgrades.join(','))
component.fire('domfactory-component-downgraded')
},
/**
* Downgrade an element.
* @param {HTMLElement} element
*/
downgradeElement (element) {
this.findCreated(element).forEach(this.downgradeComponent)
},
/**
* Downgrade all the created components.
*/
downgradeAll () {
this._created.forEach(this.downgradeComponent)
},
/**
* Downgrade a single element, a NodeList or an array of elements
* @param {Node|Array<Node>|NodeList} node
*/
downgrade (node) {
if (node instanceof Array || node instanceof NodeList) {
const nodes = node instanceof NodeList ? [...node] : node
nodes.forEach(element => this.downgradeElement(element))
}
else if (node instanceof Node) {
this.downgradeElement(node)
}
}
}
window.addEventListener('load', () => handler.upgradeAll())