-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (146 loc) · 3.7 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
var emitter = require('component-emitter');
/**
* A drop down menu
* @constructor
* @param {Object} options
* @param {HTMLElement} options.el
* @param {HTMLElement} [options.trigger]
*/
function Menu(options) {
var self = this;
this.canBeToggled = options.canBeToggled || function(menu) {return true};
/** @private */
this.el = options.el;
/** @private */
this.trigger = options.trigger;
if (this.trigger) {
this.onClickTrigger = this.onClickTrigger.bind(this);
this.onClickOutside = this.onClickOutside.bind(this);
//toggle the menu when the trigger is clicked
this.trigger.addEventListener('click', this.onClickTrigger);
//toggle the classes on the trigger when the menu is opened or closed
this
.on('opened', function() {
self.trigger.classList.add('is-open');
})
.on('closed', function() {
self.trigger.classList.remove('is-open');
})
;
}
/** @private */
this.children = [];
var items = Array.prototype.slice.call(this.el.querySelectorAll(Menu.ITEM_SELECTOR))
.filter(function(itemElement) {
return itemElement.parentNode === self.el; //ignore menu items deeper than one level
})
.forEach(function(itemElement) {
var triggerElement = itemElement.querySelector(Menu.TRIGGER_SELECTOR);
var menuElement = itemElement.querySelector(Menu.MENU_SELECTOR);
if (menuElement && menuElement.parentNode === itemElement) {
//ignore triggers deeper than one level
if (triggerElement && triggerElement.parentNode !== itemElement) {
triggerElement = null;
}
//create a child menu
var menu = new Menu({
el: menuElement,
trigger: triggerElement,
canBeToggled: self.canBeToggled
});
self.children.push(menu);
}
})
;
}
emitter(Menu.prototype);
Menu.TRIGGER_SELECTOR = '.js-trigger';
Menu.MENU_SELECTOR = '.js-menu';
Menu.ITEM_SELECTOR = '.js-item';
/**
* Get whether the menu is open
* @returns {boolean}
*/
Menu.prototype.isOpen = function() {
return this.el.classList.contains('is-open');
};
/**
* Open the menu
* @returns {Menu}
*/
Menu.prototype.open = function() {
this.el.classList.add('is-open');
this.emit('opened');
//let the menu be opened
var self = this;
setTimeout(function() {
document.documentElement.addEventListener('click', self.onClickOutside);
}, 0);
return this;
};
/**
* Close the menu
* @returns {Menu}
*/
Menu.prototype.close = function() {
document.documentElement.removeEventListener('click', this.onClickOutside);
this.el.classList.remove('is-open');
this.emit('closed');
return this;
};
/**
* Toggle the menu
* @returns {Menu}
*/
Menu.prototype.toggle = function() {
if (this.isOpen()) {
this.close();
} else {
this.open();
}
return this;
};
/**
* Get the child menus
* @returns {Array.<Menu>}
*/
Menu.prototype.getChildren = function() {
return this.children;
};
/**
* Toggle the menu when the user clicks the trigger
* @param event
*/
Menu.prototype.onClickTrigger = function(event) {
if (this.canBeToggled(self)) {
event.preventDefault();
this.toggle();
}
};
/**
* Close the menu when the user clicks outside of the menu
* @param event
*/
Menu.prototype.onClickOutside = function(event) {
if (!this.el.contains(event.target) && this.isOpen()) {
this.close();
}
};
/**
* When a menu is opened, close all of its siblings
* @param {Array.<Menu>} siblings
*/
Menu.closeSiblingsWhenAMenuIsOpened = function (siblings) {
for (var i=0; i<siblings.length; ++i) {
var menu = siblings[i];
menu.on('opened', function() {
for (var i=0; i<siblings.length; ++i) {
var sibling = siblings[i];
if (sibling !== this) {
sibling.close();
}
}
});
}
};
module.exports = Menu;