-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
291 lines (245 loc) · 5.32 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
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
/**
* Module dependencies.
*/
var classes = require('classes');
var domify = require('domify');
var Emitter = require('emitter');
var empty = require('empty');
var event = require('event');
var query = require('query');
/**
* Expose `Menu`.
*/
module.exports = Menu;
/**
* Initialize a new `Menu`.
*
* Emits:
*
* - "show" when shown
* - "hide" when hidden
* - "remove" with the item name when an item is removed
* - "select" (item) when an item is selected
* - * menu item events are emitted when clicked
*
* @api public
*/
function Menu() {
if (!(this instanceof Menu)) return new Menu();
this.items = {};
this.el = domify('<ul class="menu" style="display: none;"></ul>');
event.bind(this.el, 'hover', this.deselect.bind(this));
document.body.appendChild(this.el);
this.onkeydown = this.onkeydown.bind(this);
event.bind(document.documentElement, 'click', this.hide.bind(this));
this.on('show', this.bindKeyboardEvents.bind(this));
this.on('hide', this.unbindKeyboardEvents.bind(this));
}
/**
* Mixin `Emitter`.
*/
Emitter(Menu.prototype);
/**
* Deselect selected menu items.
*
* @api private
*/
Menu.prototype.deselect = function(){
var selected = query.all('.selected', this.el);
for (var i = 0; i < selected.length; i++) {
classes(selected[i]).remove('selected');
}
return this;
};
/**
* Bind keyboard events.
*
* @api private
*/
Menu.prototype.bindKeyboardEvents = function(){
event.bind(document.documentElement, 'keydown', this.onkeydown);
return this;
};
/**
* Unbind keyboard events.
*
* @api private
*/
Menu.prototype.unbindKeyboardEvents = function(){
event.unbind(document.documentElement, 'keydown', this.onkeydown);
return this;
};
/**
* Handle keydown events.
*
* @api private
*/
Menu.prototype.onkeydown = function(e){
switch (e.keyCode) {
// esc
case 27:
this.hide();
break;
// up
case 38:
e.preventDefault();
e.stopImmediatePropagation();
this.move('previous');
break;
// down
case 40:
e.preventDefault();
e.stopImmediatePropagation();
this.move('next');
break;
}
};
/**
* Focus on the next menu item in `direction`.
*
* @param {String} direction "previous" or "next"
* @api public
*/
Menu.prototype.move = function(direction){
var prev = query.all('.selected', this.el);
var next = prev.length > 0
? prev[0][direction + 'ElementSibling']
: query('li:first-child', this.el);
if (next) {
for (var i = 0; i < prev.length; i++) {
classes(prev[i]).remove('selected');
}
classes(next).add('selected');
var a = query('a', next);
if (a) a.focus();
}
};
/**
* Add menu item with the given `text` and optional callback `fn`.
*
* When the item is clicked `fn()` will be invoked
* and the `Menu` is immediately closed. When clicked
* an event of the name `text` is emitted regardless of
* the callback function being present.
*
* @param {String} text
* @param {Function} fn
* @return {Menu}
* @api public
*/
Menu.prototype.add = function(text, fn){
var slug;
// slug, text, [fn]
if ('string' == typeof fn) {
slug = text;
text = fn;
fn = arguments[2];
} else {
slug = createSlug(text);
}
var self = this;
var el = domify('<li class="menu-item-' + slug + '">' +
'<a href="#">' + text + '</a>' +
'</li>');
var links = query.all('a', el);
for (var i = 0; i < links.length; i++) {
event.bind(links[i], 'click', onclick);
}
function onclick(e){
e.preventDefault();
e.stopPropagation();
self.hide();
self.emit('select', slug);
self.emit(slug);
fn && fn();
}
this.el.appendChild(el);
this.items[slug] = el;
return this;
};
/**
* Remove menu item with the given `slug`.
*
* @param {String} slug
* @return {Menu}
* @api public
*/
Menu.prototype.remove = function(slug){
var item = this.items[slug] || this.items[createSlug(slug)];
if (!item) throw new Error('no menu item named "' + slug + '"');
this.emit('remove', slug);
this.el.removeChild(item);
delete this.items[slug];
delete this.items[createSlug(slug)];
return this;
};
/**
* Clear all the items from the menu
*
* @return {Menu}
* @api public
*/
Menu.prototype.clear = function(){
empty(this.el);
this.items = {};
this.emit('clear');
return this;
};
/**
* Check if this menu has an item with the given `slug`.
*
* @param {String} slug
* @return {Boolean}
* @api public
*/
Menu.prototype.has = function(slug){
return !! (this.items[slug] || this.items[createSlug(slug)]);
};
/**
* Move context menu to `(x, y)`.
*
* @param {Number} x
* @param {Number} y
* @return {Menu}
* @api public
*/
Menu.prototype.moveTo = function(x, y){
this.el.style.top = y;
this.el.style.left = x;
return this;
};
/**
* Show the menu.
*
* @return {Menu}
* @api public
*/
Menu.prototype.show = function(){
this.el.style.display = 'block';
this.emit('show');
return this;
};
/**
* Hide the menu.
*
* @return {Menu}
* @api public
*/
Menu.prototype.hide = function(){
this.el.style.display = 'none';
this.emit('hide');
return this;
};
/**
* Generate a slug from `str`.
*
* @param {String} str
* @return {String}
* @api private
*/
function createSlug(str) {
return String(str)
.toLowerCase()
.replace(/ +/g, '-')
.replace(/[^a-z0-9-]/g, '');
}