-
Notifications
You must be signed in to change notification settings - Fork 1
/
szn-select--button.js
159 lines (133 loc) · 3.75 KB
/
szn-select--button.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
'use strict'
;(global => {
const SznElements = global.SznElements = global.SznElements || {}
const CSS_STYLES = `
%{CSS_STYLES}%
`
const OPENING_POSITION = {
UP: 'OPENING_POSITION.UP',
DOWN: 'OPENING_POSITION.DOWN',
}
if (Object.freeze) {
Object.freeze(OPENING_POSITION)
}
SznElements['szn-select--button'] = class SznSelectButton {
constructor(rootElement) {
this.OPENING_POSITION = OPENING_POSITION
this._root = rootElement
this._select = null
this._label = null
this._observer = new MutationObserver(() => {
updateLabel(this)
updateDisabledStatus(this)
})
rootElement.OPENING_POSITION = OPENING_POSITION
rootElement.setSelectElement = this.setSelectElement.bind(this)
rootElement.setOpen = this.setOpen.bind(this)
rootElement.setOpeningPosition = this.setOpeningPosition.bind(this)
this._onChange = onChange.bind(null, this)
SznElements.injectStyles(CSS_STYLES, 'szn-select--button')
}
onMount() {
observeSelect(this)
addEventListeners(this)
updateLabel(this)
updateDisabledStatus(this)
}
onUnmount() {
this._observer.disconnect()
removeEventListeners(this)
}
setSelectElement(selectElement) {
if (selectElement === this._select) {
return
}
if (!this._label) {
this._root.appendChild(buildUI(this))
}
if (this._select) {
removeEventListeners(this)
this._observer.disconnect()
}
this._select = selectElement
observeSelect(this)
addEventListeners(this)
updateLabel(this)
updateDisabledStatus(this)
}
setOpen(isOpen) {
if (isOpen) {
this._root.setAttribute('data-szn-select--button--open', '')
} else {
this._root.removeAttribute('data-szn-select--button--open')
this._root.removeAttribute('data-szn-select--button--open-at-top')
}
}
setOpeningPosition(openingPosition) {
if (openingPosition === OPENING_POSITION.UP) {
this._root.setAttribute('data-szn-select--button--open-at-top', '')
} else {
this._root.removeAttribute('data-szn-select--button--open-at-top')
}
}
}
function addEventListeners(instance) {
if (!instance._select) {
return
}
instance._select.addEventListener('change', instance._onChange)
}
function removeEventListeners(instance) {
if (!instance._select) {
return
}
instance._select.removeEventListener('change', instance._onChange)
}
function onChange(instance) {
updateLabel(instance)
}
function observeSelect(instance) {
if (!instance._select) {
return
}
instance._observer.observe(instance._select, {
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeFilter: ['disabled', 'selected'],
})
}
function buildUI(instance) {
return SznElements.buildDom(
`
<szn- data-szn-select--button--label data-szn-ref></szn->
<szn- data-szn-select--button--mark></szn->
`,
label => {
instance._label = label
},
false,
)
}
function updateLabel(instance) {
if (!instance._select) {
return
}
const selectedOption = instance._select.options.item(instance._select.selectedIndex)
const selectedOptionText = selectedOption.text
if (instance._label.innerText !== selectedOptionText) {
instance._label.innerText = selectedOptionText
}
}
function updateDisabledStatus(instance) {
if (!instance._select) {
return
}
if (instance._select.disabled) {
instance._root.setAttribute('disabled', '')
} else {
instance._root.removeAttribute('disabled')
}
}
})(self)