forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropdown.js
More file actions
111 lines (85 loc) · 2.73 KB
/
dropdown.js
File metadata and controls
111 lines (85 loc) · 2.73 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
/**
* MUI CSS/JS dropdown module
* @module dropdowns
*/
'use strict';
var jqLite = require('./lib/jqLite'),
util = require('./lib/util'),
attrKey = 'data-mui-toggle',
attrSelector = '[data-mui-toggle="dropdown"]',
openClass = 'mui--is-open',
menuClass = 'mui-dropdown__menu';
/**
* Initialize toggle element.
* @param {Element} toggleEl - The toggle element.
*/
function initialize(toggleEl) {
// check flag
if (toggleEl._muiDropdown === true) return;
else toggleEl._muiDropdown = true;
// use type "button" to prevent form submission by default
if (!toggleEl.hasAttribute('type')) toggleEl.type = 'button';
// attach click handler
jqLite.on(toggleEl, 'click', clickHandler);
}
/**
* Handle click events on dropdown toggle element.
* @param {Event} ev - The DOM event
*/
function clickHandler(ev) {
// only left clicks
if (ev.button !== 0) return;
var toggleEl = this;
// exit if toggle button is disabled
if (toggleEl.getAttribute('disabled') !== null) return;
// toggle dropdown
toggleDropdown(toggleEl);
}
/**
* Toggle the dropdown.
* @param {Element} toggleEl - The dropdown toggle element.
*/
function toggleDropdown(toggleEl) {
var wrapperEl = toggleEl.parentNode,
menuEl = toggleEl.nextElementSibling,
doc = wrapperEl.ownerDocument;
// exit if no menu element
if (!menuEl || !jqLite.hasClass(menuEl, menuClass)) {
return util.raiseError('Dropdown menu element not found');
}
// method to close dropdown
function closeDropdownFn() {
jqLite.removeClass(menuEl, openClass);
// remove event handlers
jqLite.off(doc, 'click', closeDropdownFn);
}
// method to open dropdown
function openDropdownFn() {
// position menu element below toggle button
var wrapperRect = wrapperEl.getBoundingClientRect(),
toggleRect = toggleEl.getBoundingClientRect();
var top = toggleRect.top - wrapperRect.top + toggleRect.height;
jqLite.css(menuEl, 'top', top + 'px');
// add open class to wrapper
jqLite.addClass(menuEl, openClass);
// close dropdown when user clicks outside of menu
setTimeout(function() {jqLite.on(doc, 'click', closeDropdownFn);}, 0);
}
// toggle dropdown
if (jqLite.hasClass(menuEl, openClass)) closeDropdownFn();
else openDropdownFn();
}
/** Define module API */
module.exports = {
/** Initialize module listeners */
initListeners: function() {
var doc = document;
// markup elements available when method is called
var elList = doc.querySelectorAll(attrSelector);
for (var i=elList.length - 1; i >= 0; i--) initialize(elList[i]);
// listen for new elements
util.onNodeInserted(function(el) {
if (el.getAttribute(attrKey) === 'dropdown') initialize(el);
});
}
};