forked from makeup/makeup-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (142 loc) · 4.63 KB
/
index.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
'use strict';
const defaultOptions = {
bem: {
control: 'switch__control'
},
customElementMode: false
};
module.exports = class {
constructor(el, selectedOptions) {
this._options = Object.assign({}, defaultOptions, selectedOptions);
this.el = el;
this._onClickListener = _onClick.bind(this);
this._onKeyDownListener = _onKeyDown.bind(this);
this._onMutationListener = _onMutation.bind(this);
if (this.disabled) {
this._focusableElement.setAttribute('tabindex', '-1');
}
this.el.classList.add('switch--js');
if (!this._options.customElementMode) {
this._mutationObserver = new MutationObserver(this._onMutationListener);
this._observeMutations();
this._observeEvents();
}
}
_observeMutations() {
if (!this._options.customElementMode) {
this._mutationObserver.observe(this._focusableElement, {
attributes: true,
childList: false,
subtree: false
});
}
}
_unobserveMutations() {
if (!this._options.customElementMode) {
this._mutationObserver.disconnect();
}
}
_observeEvents() {
this._focusableElement.addEventListener('click', this._onClickListener);
this._focusableElement.addEventListener('keydown', this._onKeyDownListener);
}
_unobserveEvents() {
this._focusableElement.removeEventListener('click', this._onClickListener);
this._focusableElement.removeEventListener('keydown', this._onKeyDownListener);
}
get _focusableElement() {
return this.el.querySelector(`.${this._options.bem.control}`);
}
set checked(isChecked) {
this._unobserveMutations();
this._focusableElement.setAttribute('aria-checked', isChecked.toString());
this.el.dispatchEvent(new CustomEvent('makeup-switch-toggle', {
composed: true,
detail: {
on: this.checked
}
}));
this._observeMutations();
}
get checked() {
return this._focusableElement.getAttribute('aria-checked') === 'true';
}
set disabled(isDisabled) {
this._unobserveMutations();
this._focusableElement.setAttribute('aria-disabled', isDisabled.toString());
this._focusableElement.setAttribute('tabindex', isDisabled ? '-1' : '0');
this._observeMutations();
}
get disabled() {
return this._focusableElement.getAttribute('aria-disabled') === 'true';
}
set labelledby(theId) {
this._unobserveMutations();
this._focusableElement.setAttribute('aria-labelledby', theId);
// customElementMode a11y workaround
// aria-labelledby cannot resolve element id references that live outside of the Shadow DOM
// as a workaround we can use aria-label
if (this._options.customElementMode) {
const labellingEl = document.getElementById(this.labelledby);
if (labellingEl && labellingEl.innerText !== '') {
this.label = labellingEl.innerText;
}
}
this._observeMutations();
}
get labelledby() {
return this._focusableElement.getAttribute('aria-labelledby');
}
get label() {
return this._focusableElement.getAttribute('aria-label');
}
set label(theLabel) {
this._unobserveMutations();
this._focusableElement.setAttribute('aria-label', theLabel);
this._observeMutations();
}
toggle() {
this.checked = !(this.checked);
}
destroy() {
this._unobserveMutations();
this._unobserveEvents();
this._onClickListener = null;
this._onKeyDownListener = null;
this._onMutationListener = null;
}
};
function _onKeyDown(e) {
if (!this.disabled) {
switch (e.keyCode) {
case 32:
e.preventDefault();
this.toggle();
break;
case 37:
this.checked = false;
break;
case 39:
this.checked = true;
break;
default:
break;
}
}
}
function _onClick() {
if (!this.disabled) {
this.toggle();
}
}
function _onMutation(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === 'attributes') {
this.el.dispatchEvent(new CustomEvent('makeup-switch-mutation', {
detail: {
attributeName: mutation.attributeName
}
}));
}
}
}