forked from makeup-js/makeup-expander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
264 lines (233 loc) · 10.3 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
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
'use strict';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var nextID = require('makeup-next-id');
var ExitEmitter = require('makeup-exit-emitter');
var focusables = require('makeup-focusables');
var defaultOptions = {
autoCollapse: false,
collapseOnFocusOut: false,
collapseOnMouseOut: false,
collapseOnClickOut: false,
contentSelector: '.expander__content',
expandOnClick: false,
expandOnFocus: false,
expandOnHover: false,
focusManagement: null,
expandedClass: null,
hostSelector: '.expander__host',
simulateSpacebarClick: false
};
// when options.expandOnClick is true, we set a flag if spacebar or enter are pressed
// the idea being that this flag is set BEFORE the click event
function _onKeyDown(e) {
var keyCode = e.keyCode;
if (keyCode === 13 || keyCode === 32) {
this.keyDownFlag = true;
// if host element does not naturally trigger a click event on spacebar, we can force one to trigger here.
// careful! if host already triggers click events naturally, we end up with a "double-click".
if (keyCode === 32 && this.options.simulateSpacebarClick === true) {
this.hostEl.click();
}
}
}
function processDocumentClick(event, el) {
if (el.contains(event.target) === false) {
el.dispatchEvent(new CustomEvent('clickOut', {
bubbles: false
}));
}
}
function _onDocumentClick(e) {
processDocumentClick(e, this.el);
}
function _onDocumentTouchStart() {
this.documentClick = true;
}
function _onDocumentTouchMove() {
this.documentClick = false;
}
function _onDocumentTouchEnd(e) {
if (this.documentClick) {
this.documentClick = false;
processDocumentClick(e, this.el);
}
}
module.exports = function () {
function _class(el, selectedOptions) {
_classCallCheck(this, _class);
this.options = _extends({}, defaultOptions, selectedOptions);
this.el = el;
this.hostEl = el.querySelector(this.options.hostSelector); // the keyboard focusable host el
this.expandeeEl = el.querySelector(this.options.contentSelector);
this.documentClick = false;
// ensure the widget and expandee have an id
nextID(this.el, 'expander');
nextID(this.expandeeEl, this.el.id + '-content');
ExitEmitter.addFocusExit(this.el);
this._hostKeyDownListener = _onKeyDown.bind(this);
this._documentClickListener = _onDocumentClick.bind(this);
this._documentTouchStartListener = _onDocumentTouchStart.bind(this);
this._documentTouchMoveListener = _onDocumentTouchMove.bind(this);
this._documentTouchEndListener = _onDocumentTouchEnd.bind(this);
this._hostClickListener = this.toggle.bind(this);
this._hostFocusListener = this.expand.bind(this);
this._hostHoverListener = this.expand.bind(this);
this._focusExitListener = this.collapse.bind(this);
this._mouseLeaveListener = this.collapse.bind(this);
this._clickOutListener = this.collapse.bind(this);
if (this.hostEl.getAttribute('aria-expanded') === null) {
this.hostEl.setAttribute('aria-expanded', 'false');
}
this.hostEl.setAttribute('aria-controls', this.expandeeEl.id);
this.expandOnClick = this.options.expandOnClick;
this.expandOnFocus = this.options.expandOnFocus;
this.expandOnHover = this.options.expandOnHover;
if (this.options.autoCollapse === false) {
this.collapseOnClickOut = this.options.collapseOnClickOut;
this.collapseOnFocusOut = this.options.collapseOnFocusOut;
this.collapseOnMouseOut = this.options.collapseOnMouseOut;
}
}
_createClass(_class, [{
key: 'isExpanded',
value: function isExpanded() {
return this.hostEl.getAttribute('aria-expanded') === 'true';
}
}, {
key: 'collapse',
value: function collapse() {
if (this.isExpanded() === true) {
this.hostEl.setAttribute('aria-expanded', 'false');
if (this.options.expandedClass) {
this.el.classList.remove(this.options.expandedClass);
}
this.el.dispatchEvent(new CustomEvent('expander-collapse', { bubbles: true, detail: this.expandeeEl }));
}
}
}, {
key: 'expand',
value: function expand(isKeyboard) {
if (this.isExpanded() === false) {
this.hostEl.setAttribute('aria-expanded', 'true');
if (this.options.expandedClass) {
this.el.classList.add(this.options.expandedClass);
}
if (isKeyboard === true) {
var focusManagement = this.options.focusManagement;
if (focusManagement === 'content') {
this.expandeeEl.setAttribute('tabindex', '-1');
this.expandeeEl.focus();
} else if (focusManagement === 'focusable') {
focusables(this.expandeeEl)[0].focus();
} else if (focusManagement === 'interactive') {
focusables(this.expandeeEl, true)[0].focus();
} else if (focusManagement !== null) {
var el = this.expandeeEl.querySelector('#' + focusManagement);
if (el) {
el.focus();
}
}
}
this.el.dispatchEvent(new CustomEvent('expander-expand', { bubbles: true, detail: this.expandeeEl }));
}
}
}, {
key: 'toggle',
value: function toggle() {
if (this.isExpanded() === true) {
this.collapse();
} else {
this.expand(this.keyDownFlag);
}
this.keyDownFlag = false;
}
}, {
key: 'cancelAsync',
value: function cancelAsync() {
this.expandOnClick = false;
this.expandOnFocus = false;
this.expandOnHover = false;
this.collapseOnClickOut = false;
this.collapseOnFocusOut = false;
this.collapseOnMouseOut = false;
}
}, {
key: 'expandOnClick',
set: function set(bool) {
if (bool === true) {
this.hostEl.addEventListener('keydown', this._hostKeyDownListener);
this.hostEl.addEventListener('click', this._hostClickListener);
if (this.options.autoCollapse === true) {
this.collapseOnClickOut = true;
this.collapseOnFocusOut = true;
}
} else {
this.hostEl.removeEventListener('click', this._hostClickListener);
this.hostEl.removeEventListener('keydown', this._hostKeyDownListener);
}
}
}, {
key: 'expandOnFocus',
set: function set(bool) {
if (bool === true) {
this.hostEl.addEventListener('focus', this._hostFocusListener);
if (this.options.autoCollapse === true) {
this.collapseOnClickOut = true;
this.collapseOnFocusOut = true;
}
} else {
this.hostEl.removeEventListener('focus', this._hostFocusListener);
}
}
}, {
key: 'expandOnHover',
set: function set(bool) {
if (bool === true) {
this.hostEl.addEventListener('mouseenter', this._hostHoverListener);
if (this.options.autoCollapse === true) {
this.collapseOnMouseOut = true;
}
} else {
this.hostEl.removeEventListener('mouseenter', this._hostHoverListener);
}
}
}, {
key: 'collapseOnClickOut',
set: function set(bool) {
if (bool === true) {
document.addEventListener('click', this._documentClickListener);
document.addEventListener('touchstart', this._documentTouchStartListener);
document.addEventListener('touchmove', this._documentTouchMoveListener);
document.addEventListener('touchend', this._documentTouchEndListener);
this.el.addEventListener('clickOut', this._clickOutListener);
} else {
this.el.removeEventListener('clickOut', this._clickOutListener);
document.removeEventListener('click', this._documentClickListener);
document.removeEventListener('touchstart', this._documentTouchStartListener);
document.removeEventListener('touchmove', this._documentTouchMoveListener);
document.removeEventListener('touchend', this._documentTouchEndListener);
}
}
}, {
key: 'collapseOnFocusOut',
set: function set(bool) {
if (bool === true) {
this.el.addEventListener('focusExit', this._focusExitListener);
} else {
this.el.removeEventListener('focusExit', this._focusExitListener);
}
}
}, {
key: 'collapseOnMouseOut',
set: function set(bool) {
if (bool === true) {
this.el.addEventListener('mouseleave', this._mouseLeaveListener);
} else {
this.el.removeEventListener('mouseleave', this._mouseLeaveListener);
}
}
}]);
return _class;
}();