-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (96 loc) · 3.27 KB
/
index.js
File metadata and controls
114 lines (96 loc) · 3.27 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
/*
* Primary base class
*/
export class LightropeBase extends HTMLElement {
static get observedAttributes() {
// Automatically observe the attributes in the base class's static attributes object
return Object.keys(this.attributes || []);
}
constructor() {
super();
this.registeredEventListeners = [];
// Set up automatic value fetching functions from base class's static attributes object
for (const [valName, parseFn] of Object.entries(this.constructor.attributes)) {
this[valName] = () => tryParse(parseFn, this.getAttribute(valName));
}
}
connectedCallback() {
wireUpAllActions(this);
// Call subclass connect
this.connect();
}
disconnectedCallback() {
unwireAllActions(this);
// Call subclass disconnect
this.disconnect();
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
const methodName = name + 'Changed';
const method = this[methodName];
if (method) {
const parseFn = this.constructor.attributes[name] || (x => x);
method.call(this, tryParse(parseFn, newValue), tryParse(parseFn, oldValue));
}
}
}
/*
* Subclass API
*/
connect() { /* Override this in your base class */ }
disconnect() { /* Override this in your base class */ }
target(key) {
return this.querySelector(`[data-target="${key}"]`);
}
targets(key) {
return Array.from(this.querySelectorAll(`[data-target="${key}"]`));
}
} // END LightropeBase
/*
* Private functions
*/
function tryParse(parseFn, value) {
return value ? parseFn(value) : value;
}
function parseAction(s) {
const [eventName, tagMethodName] = s.split('->');
const [tag, methodName] = tagMethodName.split('.');
return { eventName, methodName, tag };
}
function createActionCallback(host, methodName) {
return function (event) {
if (host[methodName]) {
host[methodName].call(host, event)
} else {
const className = host.constructor.name || '(Anonymous)'
throw new Error(`Attempted to invoke action '${methodName}' on ${className} but it doesn't exist`);
}
}
}
function dropTagPrefix(tagName) {
return tagName.slice(tagName.indexOf('-') + 1);
}
function wireUpAction(host, el) {
const { action } = el.dataset;
const { eventName, methodName, tag } = parseAction(action);
if (dropTagPrefix(host.tagName).toLowerCase() === tag) {
const callback = createActionCallback(host, methodName);
el.addEventListener(eventName, callback, true);
host.registeredEventListeners.push([el, eventName, callback]);
}
}
function wireUpAllActions(host) {
// Wire up any actions placed on the host component
if (host.dataset.action) {
wireUpAction(host, host);
}
// Wire up child actions
const actionElements = host.querySelectorAll('[data-action]',);
actionElements.forEach(el => wireUpAction(host, el));
}
function unwireAllActions(host) {
host.registeredEventListeners.forEach(([el, eventName, callback]) => {
el.removeEventListener(eventName, callback);
});
host.registeredEventListeners = [];
}