-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathfoundation.dropdown.js
More file actions
474 lines (423 loc) · 13.5 KB
/
Copy pathfoundation.dropdown.js
File metadata and controls
474 lines (423 loc) · 13.5 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import $ from 'jquery';
import { Keyboard } from './foundation.util.keyboard';
import { GetYoDigits, ignoreMousedisappear } from './foundation.core.utils';
import { Positionable } from './foundation.positionable';
import { Triggers } from './foundation.util.triggers';
import { Touch } from './foundation.util.touch'
/**
* Dropdown module.
* @module foundation.dropdown
* @requires foundation.util.keyboard
* @requires foundation.util.box
* @requires foundation.util.touch
* @requires foundation.util.triggers
*/
class Dropdown extends Positionable {
/**
* Creates a new instance of a dropdown.
* @class
* @name Dropdown
* @param {jQuery} element - jQuery object to make into a dropdown.
* Object should be of the dropdown panel, rather than its anchor.
* @param {Object} options - Overrides to the default plugin settings.
*/
_setup(element, options) {
this.$element = element;
this.options = $.extend({}, Dropdown.defaults, this.$element.data(), options);
this.className = 'Dropdown'; // ie9 back compat
// Touch and Triggers init are idempotent, just need to make sure they are initialized
Touch.init($);
Triggers.init($);
this._init();
Keyboard.register('Dropdown', {
'ENTER': 'toggle',
'SPACE': 'toggle',
'ESCAPE': 'close'
});
}
/**
* Initializes the plugin by setting/checking options and attributes, adding helper variables, and saving the anchor.
* @function
* @private
*/
_init() {
var $id = this.$element.attr('id');
this.$anchors = $(`[data-toggle="${$id}"]`).length ? $(`[data-toggle="${$id}"]`) : $(`[data-open="${$id}"]`);
this.$anchors.attr({
'aria-controls': $id,
'data-is-focus': false,
'data-yeti-box': $id,
'aria-haspopup': true,
'aria-expanded': false
});
this._setCurrentAnchor(this.$anchors.first());
if(this.options.parentClass){
this.$parent = this.$element.parents('.' + this.options.parentClass);
}else{
this.$parent = null;
}
// Set [aria-labelledby] on the Dropdown if it is not set
if (typeof this.$element.attr('aria-labelledby') === 'undefined') {
// Get the anchor ID or create one
if (typeof this.$currentAnchor.attr('id') === 'undefined') {
this.$currentAnchor.attr('id', GetYoDigits(6, 'dd-anchor'));
}
this.$element.attr('aria-labelledby', this.$currentAnchor.attr('id'));
}
this.$element.attr({
'aria-hidden': 'true',
'data-yeti-box': $id,
'data-resize': $id,
});
if (this.options.position !== 'auto') {
this.position = this.options.position;
} else {
this.position = this._getDefaultPosition();
}
if (this.options.alignment !== 'auto') {
this.alignment = this.options.alignment;
} else {
this.alignment = this._getDefaultAlignment();
}
super._init();
this._events();
}
_getDefaultPosition() {
// handle legacy classnames
var position = this.$element[0].className.match(/(top|left|right|bottom)/g);
if(position) {
return position[0];
} else {
return 'bottom'
}
}
_getDefaultAlignment() {
// handle legacy float approach
var horizontalPosition = /float-(\S+)/.exec(this.$currentAnchor.attr('class'));
if(horizontalPosition) {
return horizontalPosition[1];
}
return super._getDefaultAlignment();
}
/**
* Sets the position and orientation of the dropdown pane, checks for collisions if allow-overlap is not true.
* Recursively calls itself if a collision is detected, with a new position class.
* @function
* @private
*/
_setPosition() {
this.$element.removeClass(`has-position-${this.position} has-alignment-${this.alignment}`);
// Override default _setPosition logic to handle explicit position + fallback alignment
const position = this.position;
const alignment = this.alignment;
// Use Positionable utility to get the best possible position
const offsets = Foundation.Box.GetOffsets(this.$element, this.$currentAnchor, this.options, this.$parent);
// Check for collision using the explicit position
const collision = Foundation.Box.ImNotTouchingYou(this.$element, this.$parent);
// If there is a collision and position is explicitly set
if (!collision && this.options.position !== 'auto') {
// Try changing only alignment
const altAlignments = ['left', 'right', 'center'].filter(a => a !== alignment);
for (let newAlignment of altAlignments) {
this.alignment = newAlignment;
// Reposition with new alignment
super._setPosition(this.$currentAnchor, this.$element, this.$parent);
if (Foundation.Box.ImNotTouchingYou(this.$element, this.$parent)) {
break;
}
}
} else {
// Let Foundation decide when position is auto or there's no collision
super._setPosition(this.$currentAnchor, this.$element, this.$parent);
}
this.$element.addClass(`has-position-${this.position} has-alignment-${this.alignment}`);
}
/**
* Make it a current anchor.
* Current anchor as the reference for the position of Dropdown panes.
* @param {HTML} el - DOM element of the anchor.
* @function
* @private
*/
_setCurrentAnchor(el) {
this.$currentAnchor = $(el);
}
/**
* Adds event listeners to the element utilizing the triggers utility library.
* @function
* @private
*/
_events() {
var _this = this,
hasTouch = 'ontouchstart' in window || (typeof window.ontouchstart !== 'undefined');
this.$element.on({
'open.zf.trigger': this.open.bind(this),
'close.zf.trigger': this.close.bind(this),
'toggle.zf.trigger': this.toggle.bind(this),
'resizeme.zf.trigger': this._setPosition.bind(this)
});
this.$anchors.off('click.zf.trigger')
.on('click.zf.trigger', function(e) {
_this._setCurrentAnchor(this);
if (
// if forceFollow false, always prevent default action
(_this.options.forceFollow === false) ||
// if forceFollow true and hover option true, only prevent default action on 1st click
// on 2nd click (dropown opened) the default action (e.g. follow a href) gets executed
(hasTouch && _this.options.hover && _this.$element.hasClass('is-open') === false)
) {
e.preventDefault();
}
});
if(this.options.hover){
this.$anchors.off('mouseenter.zf.dropdown mouseleave.zf.dropdown')
.on('mouseenter.zf.dropdown', function(){
_this._setCurrentAnchor(this);
var bodyData = $('body').data();
if(typeof(bodyData.whatinput) === 'undefined' || bodyData.whatinput === 'mouse') {
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.open();
_this.$anchors.data('hover', true);
}, _this.options.hoverDelay);
}
}).on('mouseleave.zf.dropdown', ignoreMousedisappear(function(){
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.close();
_this.$anchors.data('hover', false);
}, _this.options.hoverDelay);
}));
if(this.options.hoverPane){
this.$element.off('mouseenter.zf.dropdown mouseleave.zf.dropdown')
.on('mouseenter.zf.dropdown', function(){
clearTimeout(_this.timeout);
}).on('mouseleave.zf.dropdown', ignoreMousedisappear(function(){
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function(){
_this.close();
_this.$anchors.data('hover', false);
}, _this.options.hoverDelay);
}));
}
}
this.$anchors.add(this.$element).on('keydown.zf.dropdown', function(e) {
var $target = $(this);
Keyboard.handleKey(e, 'Dropdown', {
open: function() {
if ($target.is(_this.$anchors) && !$target.is('input, textarea')) {
_this.open();
_this.$element.attr('tabindex', -1).focus();
e.preventDefault();
}
},
close: function() {
_this.close();
_this.$anchors.focus();
}
});
});
}
/**
* Adds an event handler to the body to close any dropdowns on a click.
* @function
* @private
*/
_addBodyHandler() {
var $body = $(document.body).not(this.$element),
_this = this;
$body.off('click.zf.dropdown tap.zf.dropdown')
.on('click.zf.dropdown tap.zf.dropdown', function (e) {
if(_this.$anchors.is(e.target) || _this.$anchors.find(e.target).length) {
return;
}
if(_this.$element.is(e.target) || _this.$element.find(e.target).length) {
return;
}
_this.close();
$body.off('click.zf.dropdown tap.zf.dropdown');
});
}
/**
* Opens the dropdown pane, and fires a bubbling event to close other dropdowns.
* @function
* @fires Dropdown#closeme
* @fires Dropdown#show
*/
open() {
// var _this = this;
/**
* Fires to close other open dropdowns, typically when dropdown is opening
* @event Dropdown#closeme
*/
this.$element.trigger('closeme.zf.dropdown', this.$element.attr('id'));
this.$anchors.addClass('hover')
.attr({'aria-expanded': true});
// this.$element/*.show()*/;
this.$element.addClass('is-opening');
this._setPosition();
this.$element.removeClass('is-opening').addClass('is-open')
.attr({'aria-hidden': false});
if(this.options.autoFocus){
var $focusable = Keyboard.findFocusable(this.$element);
if($focusable.length){
$focusable.eq(0).focus();
}
}
if(this.options.closeOnClick){ this._addBodyHandler(); }
if (this.options.trapFocus) {
Keyboard.trapFocus(this.$element);
}
/**
* Fires once the dropdown is visible.
* @event Dropdown#show
*/
this.$element.trigger('show.zf.dropdown', [this.$element]);
}
/**
* Closes the open dropdown pane.
* @function
* @fires Dropdown#hide
*/
close() {
if(!this.$element.hasClass('is-open')){
return false;
}
this.$element.removeClass('is-open')
.attr({'aria-hidden': true});
this.$anchors.removeClass('hover')
.attr('aria-expanded', false);
/**
* Fires once the dropdown is no longer visible.
* @event Dropdown#hide
*/
this.$element.trigger('hide.zf.dropdown', [this.$element]);
if (this.options.trapFocus) {
Keyboard.releaseFocus(this.$element);
}
}
/**
* Toggles the dropdown pane's visibility.
* @function
*/
toggle() {
if(this.$element.hasClass('is-open')){
if(this.$anchors.data('hover')) return;
this.close();
}else{
this.open();
}
}
/**
* Destroys the dropdown.
* @function
*/
_destroy() {
this.$element.off('.zf.trigger').hide();
this.$anchors.off('.zf.dropdown');
$(document.body).off('click.zf.dropdown tap.zf.dropdown');
}
}
Dropdown.defaults = {
/**
* Class that designates bounding container of Dropdown (default: window)
* @option
* @type {?string}
* @default null
*/
parentClass: null,
/**
* Amount of time to delay opening a submenu on hover event.
* @option
* @type {number}
* @default 250
*/
hoverDelay: 250,
/**
* Allow submenus to open on hover events
* @option
* @type {boolean}
* @default false
*/
hover: false,
/**
* Don't close dropdown when hovering over dropdown pane
* @option
* @type {boolean}
* @default false
*/
hoverPane: false,
/**
* Number of pixels between the dropdown pane and the triggering element on open.
* @option
* @type {number}
* @default 0
*/
vOffset: 0,
/**
* Number of pixels between the dropdown pane and the triggering element on open.
* @option
* @type {number}
* @default 0
*/
hOffset: 0,
/**
* Position of dropdown. Can be left, right, bottom, top, or auto.
* @option
* @type {string}
* @default 'auto'
*/
position: 'auto',
/**
* Alignment of dropdown relative to anchor. Can be left, right, bottom, top, center, or auto.
* @option
* @type {string}
* @default 'auto'
*/
alignment: 'auto',
/**
* Allow overlap of container/window. If false, dropdown will first try to position as defined by data-position and data-alignment, but reposition if it would cause an overflow.
* @option
* @type {boolean}
* @default false
*/
allowOverlap: false,
/**
* Allow overlap of only the bottom of the container. This is the most common
* behavior for dropdowns, allowing the dropdown to extend the bottom of the
* screen but not otherwise influence or break out of the container.
* @option
* @type {boolean}
* @default true
*/
allowBottomOverlap: true,
/**
* Allow the plugin to trap focus to the dropdown pane if opened with keyboard commands.
* @option
* @type {boolean}
* @default false
*/
trapFocus: false,
/**
* Allow the plugin to set focus to the first focusable element within the pane, regardless of method of opening.
* @option
* @type {boolean}
* @default false
*/
autoFocus: false,
/**
* Allows a click on the body to close the dropdown.
* @option
* @type {boolean}
* @default false
*/
closeOnClick: false,
/**
* If true the default action of the toggle (e.g. follow a link with href) gets executed on click. If hover option is also true the default action gets prevented on first click for mobile / touch devices and executed on second click.
* @option
* @type {boolean}
* @default true
*/
forceFollow: true
};
export {Dropdown};