-
Notifications
You must be signed in to change notification settings - Fork 17
/
html.js
292 lines (254 loc) · 9.39 KB
/
html.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { MathUtils } from 'three';
/**
* Check if an html element belong to another one recursively
*
* @param {HTMLElement} child - html child
* @param {HTMLElement} parent - html parent
* @returns {boolean} - true if child belong to parent
*/
export function checkParentChild(child, parent) {
let currentNode = child;
let isChild = false;
while (currentNode.parentNode) {
if (currentNode == parent) {
isChild = true;
break;
} else {
currentNode = currentNode.parentNode;
}
}
return isChild;
}
/**
* Create a button to toggle visibility of the content in a container
*
* @param {string} label - label of the container
* @returns {{parent:HTMLElement,container:HTMLElement}} - parent is the element to add container is the element to fill
*/
export function createDisplayable(label) {
const parent = document.createElement('div');
const displayableButton = document.createElement('button');
parent.appendChild(displayableButton);
const container = document.createElement('div');
parent.appendChild(container);
displayableButton.innerText = 'Display ' + label;
container.hidden = true;
displayableButton.onclick = () => {
container.hidden = !container.hidden;
if (container.hidden) {
displayableButton.innerText = 'Display ' + label;
} else {
displayableButton.innerText = 'Hide ' + label;
}
};
return { parent: parent, container: container };
}
/**
*
* @param {string} labelText - label text
* @param {string} inputType - input type
* @returns {{parent:HTMLElement,input:HTMLElement}} - parent is the element to add input is the input element
*/
export function createLabelInput(labelText, inputType) {
const parent = document.createElement('div');
const label = document.createElement('label');
label.innerText = labelText;
const input = document.createElement('input');
const uuid = MathUtils.generateUUID();
input.setAttribute('id', uuid);
input.setAttribute('type', inputType);
label.htmlFor = uuid;
parent.appendChild(label);
parent.appendChild(input);
return { parent: parent, input: input, label: label };
}
/**
*
* @param {string} labelText - text of the label
* @returns {{parent:HTMLElement,inputStartDate:HTMLElement,inputEndDate:HTMLElement}} - date interval object
*/
export function createDateIntervalInput(labelText) {
const parent = document.createElement('div');
const label = document.createElement('label');
label.innerText = labelText;
parent.appendChild(label);
const createLabelInputDate = (dateLabelText) => {
const dateLabel = document.createElement('div');
dateLabel.innerText = dateLabelText;
parent.appendChild(dateLabel);
const inputDate = document.createElement('input');
inputDate.setAttribute('type', 'date');
parent.appendChild(inputDate);
return inputDate;
};
return {
parent: parent,
inputStartDate: createLabelInputDate('From'),
inputEndDate: createLabelInputDate('To'),
};
}
/**
* A custom HTML element that represents a vector input with three numeric
* values (x, y, z) and allows for event handling when the values are changed.
*/
export class Vector3Input extends HTMLElement {
/**
* A custom element with three input fields for x, y and z values, and
* dispatches a 'change' event when any of the input values are changed.
*
* @param {string} labelVector - A string that represents the label for the
* vector input.
* @param {number} step - A number that specifies the increment or decrement value for
* the input fields.
* @param {number} value - The initial value that will be set for all three input
* fields (`x`, `y` and `z`).
* @param {Array<string>} [labels] - An optional array that contains the labels for the
* input fields. By default, it is set to `['x', 'y', 'z']`.
*/
constructor(labelVector, step, value, labels = ['x', 'y', 'z']) {
super();
this.labelVector = document.createElement('label');
this.labelVector.innerText = labelVector;
this.appendChild(this.labelVector);
this.x = createLabelInput(labels[0], 'number');
this.y = createLabelInput(labels[1], 'number');
this.z = createLabelInput(labels[2], 'number');
this.x.input.step = step;
this.y.input.step = step;
this.z.input.step = step;
this.x.input.value = value;
this.y.input.value = value;
this.z.input.value = value;
this.appendChild(this.x.parent);
this.appendChild(this.y.parent);
this.appendChild(this.z.parent);
this.x.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.y.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.z.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
}
/**
* The function returns an array of input elements.
*
* @returns {Array<HTMLInputElement>} Containing the input elements for the x, y, and z properties.
*/
get inputElements() {
return [this.x.input, this.y.input, this.z.input];
}
}
window.customElements.define('vector3-input', Vector3Input); // mandatory to extends HTMLElement
/**
* A custom HTML element that represents a vector input with two numeric
* values (x, y) and allows for event handling when the values are changed.
*/
export class Vector2Input extends HTMLElement {
/**
* A custom element with two input fields for x and y values, and
* dispatches a 'change' event when any of the input values are changed.
*
* @param {string} labelVector - A string that represents the label for the
* vector input.
* @param {number} step - A number that specifies the increment or decrement value for
* the input fields.
* @param {number} value - The initial value that will be set for all two input
* fields (`x` and `y`).
* @param {Array<string>} [labels] - An optional array that contains the labels for the
* input fields. By default, it is set to `['x', 'y']`.
*/
constructor(labelVector, step, value, labels = ['x', 'y']) {
super();
this.labelVector = document.createElement('label');
this.labelVector.innerText = labelVector;
this.appendChild(this.labelVector);
this.x = createLabelInput(labels[0], 'number');
this.y = createLabelInput(labels[1], 'number');
this.x.input.step = step;
this.y.input.step = step;
this.x.input.value = value;
this.y.input.value = value;
this.appendChild(this.x.parent);
this.appendChild(this.y.parent);
this.x.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.y.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
}
/**
* The function returns an array of input elements.
*
* @returns {Array<HTMLInputElement>} Containing the input elements for the x and y properties.
*/
get inputElements() {
return [this.x.input, this.y.input];
}
}
window.customElements.define('vector2-input', Vector2Input); // mandatory to extends HTMLElement
/**
* A custom HTML element that represents a vector input with four numeric
* values (x, y, z, w) and allows for event handling when the values are changed.
*/
export class Vector4Input extends HTMLElement {
/**
* A custom element with four input fields for x, y, z and w values, and
* dispatches a 'change' event when any of the input values are changed.
*
* @param {string} labelVector - A string that represents the label for the
* vector input.
* @param {number} step - A number that specifies the increment or decrement value for
* the input fields.
* @param {number} value - The initial value that will be set for all four input
* fields (`x`, `y`, `z` and `w`).
* @param {Array<string>} [labels] - An optional array that contains the labels for the
* input fields. By default, it is set to `['x', 'y', 'z', 'w']`.
*/
constructor(labelVector, step, value, labels = ['x', 'y', 'z', 'w']) {
super();
this.labelVector = document.createElement('label');
this.labelVector.innerText = labelVector;
this.appendChild(this.labelVector);
this.x = createLabelInput(labels[0], 'number');
this.y = createLabelInput(labels[1], 'number');
this.z = createLabelInput(labels[2], 'number');
this.w = createLabelInput(labels[3], 'number');
this.x.input.step = step;
this.y.input.step = step;
this.z.input.step = step;
this.w.input.step = step;
this.x.input.value = value;
this.y.input.value = value;
this.z.input.value = value;
this.w.input.value = value;
this.appendChild(this.x.parent);
this.appendChild(this.y.parent);
this.appendChild(this.z.parent);
this.appendChild(this.w.parent);
this.x.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.y.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.z.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
this.w.input.addEventListener('change', () =>
this.dispatchEvent(new Event('change'))
);
}
/**
* The function returns an array of input elements.
*
* @returns {Array<HTMLInputElement>} Containing the input elements for the x, y, z and w properties.
*/
get inputElements() {
return [this.x.input, this.y.input, this.z.input, this.w.input];
}
}
window.customElements.define('vector4-input', Vector4Input); // mandatory to extends HTMLElement