-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomComponent.js
More file actions
205 lines (176 loc) · 5.18 KB
/
Copy pathdomComponent.js
File metadata and controls
205 lines (176 loc) · 5.18 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
201
202
203
204
205
export class DomComponent {
static className;
element;
invokeStatic(methodName, ...args) {
return this.constructor[methodName](...args);
}
static create(...args) {
const element = this.createElement(...args);
if (!element.classList.contains(this.className)) {
element.classList.add(this.className);
}
const dom = new this(element);
if (!element.id) {
element.id = `${this.className}-${dom.generateId(...args)}`;
}
dom.init(...args);
dom.registerEventListeners(...args);
return dom;
}
static createElement(...args) {
throw new Error('Not implemented');
}
init(...args) {
throw new Error('Not implemented');
}
registerEventListeners(...args) {
throw new Error('Not implemented');
}
static of(id) {
return this.newOrNull(document.getElementById(`${this.className}-${id}`));
}
static ofEvent(event) {
return this.newOrNull(event.target);
}
static newOrNull(element) {
try {
return new this(element);
} catch {
return null;
}
}
constructor(element) {
this.element = element.closest(`.${this.constructor.className}`);
if (!this.element) {
throw new Error(`Element ${element} is not a ${this.constructor.className}`);
}
}
/**
* Detach the component's root element from the DOM.
*/
detach() {
this.element?.remove();
}
/**
* Get the prefix of the path to allow for dynamic endpoint reference.
*/
getPathPrefix() {
return document.querySelector('meta[name=path-prefix]').content;
}
/**
* Gets a nested element or throws an Error.
*/
getElement(selector) {
const found = this.element.querySelector(selector);
if (!found) {
throw new Error(`Element ${selector} not found in ${this.constructor.className} ${this.getId()}`);
}
return found
}
/**
* Find nested elements (if any) that match a selector.
*/
getElements(selector) {
return this.element.querySelectorAll(selector);
}
/**
* Set a dataset attribute.
*/
setData(key, value) {
this.element.dataset[key] = value;
}
/**
* Get a dataset attribute.
*/
getData(key) {
return this.element.dataset[key];
}
/**
* Add a CSS class.
*/
setClass(className) {
this.element.classList.add(className);
}
/**
* Remove a CSS class.
*/
unsetClass(className) {
this.element.classList.remove(className);
}
/**
* Check for a CSS class.
*/
hasClass(className) {
return this.element.classList.contains(className);
}
/**
* Generates a unique ID for the element.
*
* This will be a random UUID unless overridden with something more meaningful.
*/
generateId(...args) {
return Math.random().toString(36).slice(2);
}
/**
* Get the ID associated with the element.
*/
getId() {
return this.element.id.slice(this.constructor.className.length + '-'.length);
}
}
export const Draggable = Base => class extends Base {
static create(...args) {
const dom = super.create(...args);
dom.element.draggable = true;
dom.registerDragEventListeners();
return dom;
}
registerDragEventListeners() {
this.element.addEventListener('dragstart', this.handleDragStart.bind(this));
this.element.addEventListener('dragover', this.handleDragOver.bind(this));
this.element.addEventListener('dragleave', this.handleDragLeave.bind(this));
this.element.addEventListener('drop', this.handleDrop.bind(this));
this.element.addEventListener('dragend', this.handleDragEnd.bind(this));
}
handleDragStart(e) {
this.setClass('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('id', this.getId());
e.dataTransfer.setData('text/html', this.element.outerHTML);
e.dataTransfer.setDragImage(this.element, 0, 0);
}
handleDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
if (!this.hasClass('dragging')) {
this.setClass('drag-over');
}
}
handleDragLeave(e) {
if (!this.element.contains(e.relatedTarget)) {
this.unsetClass('drag-over');
}
}
handleDrop(e) {
e.preventDefault();
const droppedOn = e.dataTransfer.getData('id')
if (droppedOn !== this.getId()) {
this.onDrop(e, this.constructor.of(droppedOn));
this.unsetClass('drag-over');
}
}
onDrop(e, dragged) {
throw new Error('Not implemented');
}
handleDragEnd(e) {
this.unsetClass('dragging');
document.querySelectorAll('.drag-over').forEach(item => {
item.classList.remove('drag-over');
});
}
};
export function htmlToElement(html) {
const template = document.createElement('template');
template.innerHTML = html.trim();
return template.content.firstElementChild;
}