-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi-checkbox.js
444 lines (418 loc) · 17.1 KB
/
multi-checkbox.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* @file multi-checkbox.js
* @fileOverview Multi-checkbox vanilla javascript component. Provides
* an input with a dropdown of multiple checkboxes that are concatenated.
* in the input when selected. See the README file for usage.
* @version 0.1.11
* @author Stephen Montanus <[email protected]>
* @copyright ©2021 Stephen Montanus Software Engineering.
* @license MIT
*/
/** @constant {Object} */
const template = document.createElement('template');
template.innerHTML = `
<style>
.multi-checkbox {
display: var(--mc-display, inline-block);
margin: var(--mc-margin, 0);
width: initial;
z-index: var(--mc-z-index, 0);
}
.checkbox-container {
background: var(--mc-dropdown-background, #efefef);
border-bottom: var(--mc-border, 1px solid #000000);
border-left: var(--mc-border, 1px solid #000000);
border-right: var(--mc-border, 1px solid #000000);
border-top: none;
border-bottom-left-radius: var(--mc-border-radius, 2px);
border-bottom-right-radius: var(--mc-border-radius, 2px);
box-sizing: border-box;
box-shadow: var(--mc-dropdown-box-shadow, 3px 3px 5px 1px rgba(0,0,0,0.35));
-webkit-box-shadow: var(--mc-dropdown-box-shadow, 3px 3px 5px 1px rgba(0,0,0,0.35));
-moz-box-shadow: var(--mc-dropdown-box-shadow, 3px 3px 5px 1px rgba(0,0,0,0.35));
display: block;
max-height: var(--mc-dropdown-max-height, 100px);
position: absolute;
text-align: var(--mc-dropdown-text-align, left);
transform: scale(1, 0);
transform-origin: top left;
transition: transform 0.5s cubic-bezier(0.65, 0, 0.35, 1);
overflow-y: scroll;
width: var(--mc-dropdown-width, fit-content);
z-index: var(--mc-z-index, 0);
}
input {
background: var(--mc-target-background, #efefef);
border: var(--mc-border, 1px solid #000000);
border-bottom-left-radius: var(--mc-border-radius, 2px);
border-top-left-radius: var(--mc-border-radius, 2px);
color: var(--mc-target-color, #000000);
cursor: var(--mc-target-cursor, default);
font: var(--mc-font, 400 0.9em Arial);
height: var(--mc-target-height, 28px);
margin: var(--mc-target-margin, 0);
max-height: var(--mc-target-max-height);
min-height: var(--mc-target-min-height);
max-width: var(--mc-target-max-width);
min-width: var(--mc-target-min-width);
padding: var(--mc-target-padding, 0px 0px 0px 3px);
outline: var(--mc-target-outline, none);
vertical-align: var(--mc-vertical-align, middle);
width: var(--mc-target-width, 175px);
z-index: var(--mc-z-index, 0);
}
#measure-test {
left: 0;
position: absolute;
visibility: hidden;
white-space: nowrap;
}
svg {
vertical-align: middle;
}
::slotted(ul) {
color: var(--mc-dropdown-color, #000000);
font: var(--mc-font, 400 0.9em Arial) !important;
line-height: var(--mc-dropdown-line-height, 2em);
list-style-type: none;
margin: var(--mc-ul-margin, 5px);
padding: var(--mc-ul-padding, 0);
overflow-wrap: break-word;
}
#toggle-button {
background: var(--mc-toggle-button-background, #ffffff);
border: var(--mc-border, 1px solid #000000);
border-bottom-right-radius: var(--mc-border-radius, 2px);
border-top-right-radius: var(--mc-border-radius, 2px);
color: var(--mc-toggle-button-color, #000000);
cursor: var(--mc-toggle-button-cursor, pointer);
height: var(--mc-toggle-button-height, 30px);
margin-left: -5px;
outline: var(--mc-toggle-button-outline, none);
vertical-align: var(--mc-vertical-align, middle);
width: var(--mc-toggle-button-width, 30px);
z-index: var(--mc-z-index, 0);
}
#close-button {
background: var(--mc-close-button-background, #efefef);
border: var(--mc-close-button-border, none);
border-radius: var(--mc-close-button-border-radius, default);
color: var(--mc-close-button-color, #000000);
cursor: var(--mc-close-button-cursor, pointer);
display: var(--mc-close-button-display, block);
height: var(--mc-close-button-height, 22px);
margin: var(--mc-close-button-margin, 5px auto);
outline: var(--mc-close-button-outline, none);
width: var(--mc-close-button-width, 22px);
}
</style>
<div class="multi-checkbox">
<input type="text" readonly disabled />
<span id="measure-test"></span>
<button id="toggle-button">
<svg x="0px" y="0px" viewBox="0 0 330 330">
<path fill="currentColor" d="M325.607,79.393c-5.857-5.857-15.355-5.858-21.213,
0.001l-139.39,139.393L25.607,79.393c-5.857-5.857-15.355-5.858-21.213,0.001c-5.858,
5.858-5.858,15.355,0,21.213l150.004,150c2.813,2.813,6.628,4.393,10.606,4.393
s7.794-1.581,10.606-4.394l149.996-150C331.465,94.749,331.465,85.251,
325.607,79.393z"/>
</svg>
</button>
<div class="checkbox-container">
<div><slot name="check-values" /></div>
<button id="close-button">
<svg x="0px" y="0px" viewBox="0 0 330 330">
<path fill="currentColor" d="M324.001,209.25L173.997,96.75c-5.334-4-12.667-4-18,
0L6.001,209.25c-6.627,4.971-7.971,14.373-3,21c2.947,3.93,7.451,6.001,12.012,
6.001c3.131,0,6.29-0.978,8.988-3.001L164.998,127.5l141.003,105.75c6.629,
4.972,16.03,3.627,21-3C331.972,223.623,330.628,214.221,324.001,209.25z"/>
</svg>
</button>
</div>
</div>`;
/**
* Creates a new MultiCheckbox custom HTML element. Utilized in HTML as the
* <multi-checkbox> tag.
* @name MultiCheckbox
* @class
* @extends HTMLElement
*/
export class MultiCheckbox extends HTMLElement {
/**
* @description Specify observed attributes so that attributeChangedCallback will work.
* @return {String[]} String array of attributes to be observed.
*/
static get observedAttributes() {
return ['separator', 'value'];
}
/**
* @description Create a MultiCheckbox element. Fires when an instance of
* the element is created or updated.
*/
constructor() {
super();
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(template.content.cloneNode(true));
// Component variables.
this.dropDownVisible = false;
this.inputRadius = this.shadowRoot.querySelector('input')
.style.borderBottomLeftRadius;
this.toggleRadius = this.shadowRoot.querySelector('#toggle-button')
.style.borderBottomRightRadius;
this.items = [];
}
/**
* @description Get the MultiCheckbox element value.
* @return {string} The value.
*/
get value() {
return this.getAttribute('value');
}
/**
* @description Set the MultiCheckbox element value.
* @param {string} newValue The new value of the element.
*/
set value(newValue) {
this.setAttribute('value', newValue);
}
/**
* @description Get the MultiCheckbox item separator value.
* @return {string} The separator value.
*/
get separator() {
return this.getAttribute('separator');
}
/**
* @descriptionSet the MultiCheckbox item separator value.
* @param {string} newValue The new value of the item separator.
*/
set separator(newValue) {
this.setAttribute('separator', newValue);
}
/**
* @name updateItems
* @description Update the item list when slot content changes.
* @return {void}
*/
updateItems() {
// Remove original slot data.
const itemList = this.shadowRoot.querySelector('slot')
.assignedNodes({flatten: true})[0];
if (itemList.childNodes[itemList.childNodes.length - 1].nodeName == 'UL') {
itemList.removeChild(itemList.childNodes[itemList.childNodes.length - 1]);
}
// Update slot with new elements.
this.items = this.shadowRoot.querySelector('slot')
.assignedElements({flatten: true})[0]
.children;
// Add checkboxes to the component list items.
this.addCheckBoxes();
// Clear the input and custom element value.
this.shadowRoot.querySelector('input').value = '';
this.setAttribute('value', '');
}
/**
* @name addCheckBoxes
* @description Add check box inputs to the elements of the items list.
* @return {void}
*/
addCheckBoxes() {
for (let i = 0; i < this.items.length; i++) {
this.items[i].innerHTML = '<input type="checkbox" style="' +
'height: var(--mc-checkbox-height, auto); ' +
'vertical-align: middle; ' +
'width: var(--mc-checkbox-width, auto);" ' +
'id="item-' + i + '" value="' + this.items[i].innerHTML + '" /> ' +
this.items[i].innerHTML;
}
}
/**
* @name updateValue
* @description Update the custom element value based on checked boxes.
* @return {void}
*/
updateValue() {
// Only run this when checkboxes are being clicked, not dynamically changed.
if (this.dropDownVisible == true) {
// Get the separator string.
const sep = this.getAttribute('separator');
// Initialize the string to store the new element value.
let newVal = '';
// Iterate over the list items, adding the values of checked checkboxes to
// the new string.
for (let i = 0; i < this.items.length; i++) {
const chkBox = this.items[i].querySelector('input[type="checkbox"]');
if (chkBox.checked) {
// Concatenate the checked value.
newVal = newVal + chkBox.value + sep;
}
}
// Remove the last separator.
newVal = newVal.slice(0, -sep.length);
// Update the value attribute to the new string.
this.setAttribute('value', newVal);
}
}
/**
* @name showDropDown
* @description Show the drop down item list.
* @return {void}
*/
showDropDown() {
this.shadowRoot.querySelector('.checkbox-container').style.transform = 'scale(1, 1)';
this.shadowRoot.querySelector('input').style.borderBottomLeftRadius = '0px';
this.shadowRoot.querySelector('#toggle-button').style.borderBottomRightRadius = '0px';
this.shadowRoot.querySelector('#toggle-button').innerHTML =
`<svg x="0px" y="0px" viewBox="0 0 330 330">
<path fill="currentColor" d="M324.001,209.25L173.997,96.75c-5.334-4-12.667-4-18,
0L6.001,209.25c-6.627,4.971-7.971,14.373-3,21c2.947,3.93,7.451,6.001,12.012,
6.001c3.131,0,6.29-0.978,8.988-3.001L164.998,127.5l141.003,105.75c6.629,
4.972,16.03,3.627,21-3C331.972,223.623,330.628,214.221,324.001,209.25z"/>
</svg>`;
}
/**
* @name hideDropDown
* @description Hide the drop down item list.
* @return {void}
*/
hideDropDown() {
this.shadowRoot.querySelector('.checkbox-container').style.transform = 'scale(1, 0)';
this.shadowRoot.querySelector('input').style.borderBottomLeftRadius = this.inputRadius;
this.shadowRoot.querySelector('#toggle-button')
.style.borderBottomRightRadius = this.toggleRadius;
this.shadowRoot.querySelector('#toggle-button').innerHTML =
`<svg x="0px" y="0px" viewBox="0 0 330 330">
<path fill="currentColor" d="M325.607,79.393c-5.857-5.857-15.355-5.858-21.213,
0.001l-139.39,139.393L25.607,79.393c-5.857-5.857-15.355-5.858-21.213,0.001c-5.858,
5.858-5.858,15.355,0,21.213l150.004,150c2.813,2.813,6.628,4.393,10.606,4.393
s7.794-1.581,10.606-4.394l149.996-150C331.465,94.749,331.465,85.251,
325.607,79.393z"/>
</svg>`;
}
/**
* @name toggleDropDown
* @description Toggle display of the drop down item list.
* @return {void}
*/
toggleDropDown() {
this.dropDownVisible = !this.dropDownVisible;
if (this.dropDownVisible) {
this.showDropDown();
} else {
this.hideDropDown();
}
}
/**
* @name measureText
* @description Measure the element text in pixels compared to input element
* width.
* @return {boolean}
*/
measureText() {
const valuePixelWidth = this.shadowRoot.querySelector('#measure-test')
.getBoundingClientRect().width;
const inputPixelWidth = this.shadowRoot.querySelector('input')
.getBoundingClientRect().width;
if (valuePixelWidth > inputPixelWidth) {
return true;
} else {
return false;
}
}
/**
* @name connectedCallback
* @description Fires when an instance is inserted into the document.
* @return {void}
*/
connectedCallback() {
// Add button event listeners.
this.shadowRoot.querySelector('#toggle-button')
.addEventListener('click', () => this.toggleDropDown());
this.shadowRoot.querySelector('#close-button')
.addEventListener('click', () => this.toggleDropDown());
// Add slot change event listener
this.shadowRoot.addEventListener('slotchange', () => this.updateItems());
// Add checkbox event listener.
this.shadowRoot.addEventListener('click', (event) => {
if (event.target && event.target.id.split('-')[0] == 'item') {
this.updateValue();
}
});
// Add event listener to hide the dropdown when click is outside the component.
document.addEventListener('click', (event) => {
if (!event.composedPath().includes(this)) {
this.dropDownVisible = false;
this.hideDropDown();
}
});
}
/**
* @name disconnectedCallback
* @description Fires when an instance was removed from the document.
* @return {void}
*/
disconnectedCallback() {
// Remove button event listeners.
this.shadowRoot.querySelector('#toggle-button')
.removeEventListener('click', () => this.toggleDropDown());
this.shadowRoot.querySelector('#close-button')
.removeEventListener('click', () => this.toggleDropDown());
// Remove slot event listener.
this.shadowRoot.querySelector('slot')
.removeEventListener('slotchange', () => this.updateItems());
}
/**
* @name attributeChangedCallback
* @description Fires when an attribute was added, removed, or updated.
* @param {string} attrName The element attribute name.
* @param {string} oldVal The old value of the attribute.
* @param {string} newVal The new value of the attribute.
* @return {void}
*/
attributeChangedCallback(attrName, oldVal, newVal) {
switch (attrName) {
case 'value':
// Update the text measurement hidden span element.
this.shadowRoot.querySelector('#measure-test').textContent = newVal;
// Update the input.
if (this.measureText()) { // Value string is wider than input width.
// Get the separator string.
const sep = this.getAttribute('separator');
// Split newVal into array using separator and get length.
const newValCount = newVal.split(sep).length;
this.shadowRoot.querySelector('input').value = newValCount + ' Selected';
} else { // Value string is narrower than input width.
this.shadowRoot.querySelector('input').value = newVal;
}
if (this.dropDownVisible == false) {
// Get the separator string.
const sep = this.getAttribute('separator');
// Split newVal into array using separator
const newVals = newVal.split(sep);
// Update the checkboxes.
for (let i = 0; i < this.items.length; i++) {
const chkBox = this.items[i].querySelector('input[type="checkbox"]');
if (chkBox) { // Filter out nulls on page load.
if (newVals.includes(chkBox.value)) {
chkBox.checked = true;
} else {
chkBox.checked = false;
}
}
}
}
break;
case 'separator':
if (newVal !== oldVal) {
let valueString = this.getAttribute('value');
// Replace the separator string.
valueString = valueString.split(oldVal).join(newVal);
// Update the input and custom element value.
this.shadowRoot.querySelector('input').value = valueString;
this.setAttribute('value', valueString);
}
break;
}
}
}
customElements.define('multi-checkbox', MultiCheckbox);