-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipleSelect.js
192 lines (153 loc) · 5.47 KB
/
MultipleSelect.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
import Container from './components/Container'
import Store from './store/Store'
import './scss/multiple-select.scss'
var selectMultipleContainerId = 0
class MultipleSelect {
constructor(elId, options) {
selectMultipleContainerId++
const defaultOptions = {
placeholder: 'Select',
customPlaceholder: false,
}
this.$elId = elId;
this.$store = new Store()
this.$options = { ...defaultOptions, ...options }
let { el, select, isMultiple, items, selectedItems } = this._buildRootElement(elId)
this.$el = el
this.$select = select
this.$store.isMultiple = isMultiple
this.$store.items = items
this.$store.selectedItems = selectedItems
this.$container = new Container({
root: this
})
// syncing with the actual select
this.$store.on('selectedItemsChange', (selectedItems) => {
this.$select.querySelectorAll('option').forEach(option => {
if (selectedItems.find(item => item.value === option.value)) {
option.setAttribute('selected', true)
} else {
option.removeAttribute('selected')
}
})
if (selectedItems.length < 1) {
this.$select.value = ''
}
const changeEvent = document.createEvent('HTMLEvents')
changeEvent.initEvent('change', true, true)
const inputEvent = document.createEvent('HTMLEvents')
inputEvent.initEvent('input', true, true)
this.$select.dispatchEvent(changeEvent)
this.$select.dispatchEvent(inputEvent)
})
// when the container is done rendered, the dropdown
// will automatically focused
this.$observer = new MutationObserver(() => {
if (this.$el.classList.contains('opened')) {
this.$container.$dropdownSelect.$input.focus()
}
})
this.$observer.observe(this.$el, { attributes: true, childList: true });
this.$store.emit('render', { el: this.$el, items: items });
}
reRender() {
this.$el.remove();
this.$observer.disconnect();
this.$store.removeAllListeners();
let { el, select, isMultiple, items, selectedItems } = this._buildRootElement(this.$elId)
this.$el = el
this.$select = select
this.$store.isMultiple = isMultiple
this.$store.items = items
this.$store.selectedItems = selectedItems
this.$container = new Container({
root: this
})
// syncing with the actual select
this.$store.on('selectedItemsChange', (selectedItems) => {
this.$select.querySelectorAll('option').forEach(option => {
if (selectedItems.find(item => item.value === option.value)) {
option.setAttribute('selected', true)
} else {
option.removeAttribute('selected')
}
})
if (selectedItems.length < 1) {
this.$select.value = ''
}
const changeEvent = document.createEvent('HTMLEvents')
changeEvent.initEvent('change', true, true)
const inputEvent = document.createEvent('HTMLEvents')
inputEvent.initEvent('input', true, true)
this.$select.dispatchEvent(changeEvent)
this.$select.dispatchEvent(inputEvent)
})
// when the container is done rendered, the dropdown
// will automatically focused
this.$observer = new MutationObserver(() => {
if (this.$el.classList.contains('opened')) {
this.$container.$dropdownSelect.$input.focus()
}
})
this.$observer.observe(this.$el, { attributes: true, childList: true });
this.$store.emit('render', { el: this.$el, items: items });
}
/**
* Creating `<div>` for root element right after the `<select>` element.
* And then hide the `<select>` element.
*
* @param {*} elId
* @returns {*}
* @memberof MultipleSelect
*/
_buildRootElement (elId) {
let select = document.querySelector(elId)
let root = document.createElement('div')
let items = []
let selectedItems = []
root.setAttribute('id', `multiple-select-container-${selectMultipleContainerId}`)
root.classList.add('multiple-select-container')
root.style.position = 'relative'
Array.from(select.options).forEach(option => {
items.push({
value: option.value,
label: option.innerText.trim(),
disabled: option.disabled
})
})
// get the already selected items
Array.from(select.selectedOptions).forEach(option => {
selectedItems.push({ value: option.value, label: option.innerText.trim() })
})
// add event listener when <select> element changed via js
select.addEventListener('change', e => {
let values = Array.from(select.selectedOptions).map(el => el.value)
let selectedItems = this.$store.selectedItems.map(item => item.value)
let isTheSameLength = values.length === selectedItems.length
function isTheSameComponents () {
let found = true
values.forEach(value => {
if (!selectedItems.find(item => item === value)) {
found = false
return
}
})
return found
}
if (!isTheSameLength || !isTheSameComponents()) {
// console.log('changed through js', values, selectedItems)
this.$store.selectedItems = this.$store.items.filter(item => {
return values.find(value => value === item.value)
})
}
})
select.insertAdjacentElement('afterend', root)
select.hidden = true
let isMultiple = select.multiple
return {
select, el: root, isMultiple, items, selectedItems
}
}
}
window.MultipleSelect = MultipleSelect
export default MultipleSelect