forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.js
More file actions
369 lines (287 loc) · 8.54 KB
/
select.js
File metadata and controls
369 lines (287 loc) · 8.54 KB
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
/**
* MUI CSS/JS select module
* @module forms/select
*/
'use strict';
var jqLite = require('../lib/jqLite.js'),
util = require('../lib/util.js'),
wrapperClass = 'mui-select',
cssSelector = '.mui-select > select',
menuClass = 'mui-select-menu',
optionHeight = 42, // from CSS
menuPadding = 8; // from CSS
/**
* Initialize select element.
* @param {Element} selectEl - The select element.
*/
function initialize(selectEl) {
// check flag
if (selectEl._muiSelect === true) return;
else selectEl._muiSelect = true;
// initialize element
new Select(selectEl);
}
/**
* Creates a new Select object
* @class
*/
function Select(selectEl) {
// instance variables
this.selectEl = selectEl;
this.wrapperEl = selectEl.parentNode;
this.useDefault = false;
// attach event handlers
jqLite.on(selectEl, 'touchstart', util.callback(this, 'touchstartHandler'));
jqLite.on(selectEl, 'mousedown', util.callback(this, 'mousedownHandler'));
jqLite.on(selectEl, 'focus', util.callback(this, 'focusHandler'));
jqLite.on(selectEl, 'click', util.callback(this, 'clickHandler'));
// make wrapper focusable and fix firefox bug
this.wrapperEl.tabIndex = -1;
var callbackFn = util.callback(this, 'wrapperFocusHandler');
jqLite.on(this.wrapperEl, 'focus', callbackFn);
}
/**
* Use default on touch devices.
*/
Select.prototype.touchstartHandler = function() {
// set flag
this.useDefault = true;
}
/**
* Disable default dropdown on mousedown.
* @param {Event} ev - The DOM event
*/
Select.prototype.mousedownHandler = function(ev) {
if (ev.button !== 0 || this.useDefault === true) return;
ev.preventDefault();
}
/**
* Handle focus event on select element.
* @param {Event} ev - The DOM event
*/
Select.prototype.focusHandler = function(ev) {
// check flag
if (this.useDefault === true) return;
var selectEl = this.selectEl,
wrapperEl = this.wrapperEl,
origIndex = selectEl.tabIndex,
keydownFn = util.callback(this, 'keydownHandler');
// attach keydown handler
jqLite.on(document, 'keydown', keydownFn);
// disable tabfocus once
selectEl.tabIndex = -1;
jqLite.one(wrapperEl, 'blur', function() {
selectEl.tabIndex = origIndex;
jqLite.off(document, 'keydown', keydownFn);
});
// defer focus to parent
wrapperEl.focus();
}
/**
* Handle keydown events on document
**/
Select.prototype.keydownHandler = function(ev) {
// spacebar, down, up
if (ev.keyCode === 32 || ev.keyCode === 38 || ev.keyCode === 40) {
// prevent window scroll
ev.preventDefault();
if (this.selectEl.disabled !== true) this.renderMenu();
}
}
/**
* Handle focus event on wrapper element.
*/
Select.prototype.wrapperFocusHandler = function() {
// firefox bugfix
if (this.selectEl.disabled) return this.wrapperEl.blur();
}
/**
* Handle click events on select element.
* @param {Event} ev - The DOM event
*/
Select.prototype.clickHandler = function(ev) {
// only left clicks
if (ev.button !== 0) return;
this.renderMenu();
}
/**
* Render options dropdown.
*/
Select.prototype.renderMenu = function() {
// check and reset flag
if (this.useDefault === true) return this.useDefault = false;
new Menu(this.selectEl);
}
/**
* Creates a new Menu
* @class
*/
function Menu(selectEl) {
// instance variables
this.origIndex = null;
this.currentIndex = null;
this.selectEl = selectEl;
this.menuEl = this._createMenuEl(selectEl);
this.clickCallbackFn = util.callback(this, 'clickHandler');
this.keydownCallbackFn = util.callback(this, 'keydownHandler');
this.destroyCallbackFn = util.callback(this, 'destroy');
// add to DOM
selectEl.parentNode.appendChild(this.menuEl);
// blur active element
setTimeout(function() {
// ie10 bugfix
if (document.activeElement.nodeName.toLowerCase() !== "body") {
document.activeElement.blur();
}
}, 0);
// attach event handlers
jqLite.on(this.menuEl, 'click', this.clickCallbackFn);
jqLite.on(document, 'keydown', this.keydownCallbackFn);
jqLite.on(window, 'resize', this.destroyCallbackFn);
// attach event handler after current event loop exits
var fn = this.destroyCallbackFn;
setTimeout(function() {jqLite.on(document, 'click', fn);}, 0);
}
/**
* Create menu element
* @param {Element} selectEl - The select element
*/
Menu.prototype._createMenuEl = function(selectEl) {
var optionEl, itemEl, i, minTop, maxTop, top;
var menuEl = document.createElement('div'),
optionList = selectEl.children,
m = optionList.length,
selectedPos = 0,
idealTop = 13;
// create element
menuEl.className = menuClass;
// add options
for (i=0; i < m; i++) {
optionEl = optionList[i];
itemEl = document.createElement('div');
itemEl.textContent = optionEl.textContent;
itemEl._muiPos = i;
if (optionEl.selected) selectedPos = i;
menuEl.appendChild(itemEl);
}
// add selected attribute
menuEl.children[selectedPos].setAttribute('selected', true);
// save indices
this.origIndex = selectedPos;
this.currentIndex = selectedPos;
var viewHeight = document.documentElement.clientHeight;
// set height (use viewport as maximum height)
var height = m * optionHeight + 2 * menuPadding;
height = Math.min(height, viewHeight);
jqLite.css(menuEl, 'height', height + 'px');
// ideal position
idealTop += selectedPos * optionHeight;
idealTop = -1 * idealTop;
// minimum position
minTop = -1 * selectEl.getBoundingClientRect().top;
// maximium position
maxTop = (viewHeight - height) + minTop;
// prevent overflow-y
top = Math.max(idealTop, minTop);
top = Math.min(top, maxTop);
jqLite.css(menuEl, 'top', top + 'px');
return menuEl;
}
/**
* Handle keydown events on document element.
* @param {Event} ev - The DOM event
*/
Menu.prototype.keydownHandler = function(ev) {
var keyCode = ev.keyCode;
// tab
if (keyCode === 9) return this.destroy();
// escape | up | down | enter
if (keyCode === 27 || keyCode === 40 || keyCode === 38 || keyCode === 13) {
ev.preventDefault();
}
if (keyCode === 27) {
this.destroy();
} else if (keyCode === 40) {
this.increment();
} else if (keyCode === 38) {
this.decrement();
} else if (keyCode === 13) {
this.selectCurrent();
this.destroy();
}
}
/**
* Handle click events on menu element.
* @param {Event} ev - The DOM event
*/
Menu.prototype.clickHandler = function(ev) {
// don't allow events to bubble
ev.stopPropagation();
var pos = ev.target._muiPos;
// ignore clicks on non-items
if (pos === undefined) return;
// select option
this.currentIndex = pos;
this.selectCurrent();
// destroy menu
this.destroy();
}
/**
* Increment selected item
*/
Menu.prototype.increment = function() {
if (this.currentIndex === this.menuEl.children.length - 1) return;
this.menuEl.children[this.currentIndex].removeAttribute('selected');
this.currentIndex += 1;
this.menuEl.children[this.currentIndex].setAttribute('selected', true);
}
/**
* Decrement selected item
*/
Menu.prototype.decrement = function() {
if (this.currentIndex === 0) return;
this.menuEl.children[this.currentIndex].removeAttribute('selected');
this.currentIndex -= 1;
this.menuEl.children[this.currentIndex].setAttribute('selected', true);
}
/**
* Select current item
*/
Menu.prototype.selectCurrent = function() {
if (this.currentIndex !== this.origIndex) {
this.selectEl.children[this.origIndex].selected = false;
this.selectEl.children[this.currentIndex].selected = true;
// trigger change event
util.dispatchEvent(this.selectEl, 'change');
}
}
/**
* Destroy menu and detach event handlers
*/
Menu.prototype.destroy = function() {
// remove element and focus element
this.menuEl.parentNode.removeChild(this.menuEl);
this.selectEl.focus();
// remove event handlers
jqLite.off(this.menuEl, 'click', this.clickCallbackFn);
jqLite.off(document, 'keydown', this.keydownCallbackFn);
jqLite.off(document, 'click', this.destroyCallbackFn);
jqLite.off(window, 'resize', this.destroyCallbackFn);
}
/** Define module API */
module.exports = {
/** Initialize module listeners */
initListeners: function() {
var doc = document;
// markup elements available when method is called
var elList = doc.querySelectorAll(cssSelector);
for (var i=elList.length - 1; i >= 0; i--) initialize(elList[i]);
// listen for new elements
util.onNodeInserted(function(el) {
if (el.tagName === 'SELECT' &&
jqLite.hasClass(el.parentNode, wrapperClass)) {
initialize(el);
}
});
}
};