This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
104 lines (82 loc) · 1.75 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
var bind = require('event').bind
, keyname = require('keyname')
, menu = require('menu')
, MenuItem = require('./item')
, prevent = require('prevent')
, template = require('./index.html')
, value = require('value')
, view = require('view');
/**
* Expose the `Nav` constructor.
*/
var Nav = module.exports = view(template, function (self) {
self.search = self.el.querySelector('.nav-search');
self.form = self.el.querySelector('.nav-form');
bind(self.form, 'submit', prevent);
bind(self.search, 'focus', function () {
self.menu.next();
});
});
/**
* Create a `Menu` constructor.
*/
var Menu = menu(MenuItem);
/**
* Add a document to the menu.
*
* @param {Object} doc
* @return {Nav}
*/
Nav.prototype.add = function (doc) {
this.menu.add(doc);
return this;
};
/**
* Remove a document from the menu.
*
* @param {String} id
* @return {Nav}
*/
Nav.prototype.remove = function (id) {
this.menu.remove(id);
return this;
};
/**
* Focus the nav's search input.
*
* @return {Nav}
*/
Nav.prototype.focus = function () {
this.search.focus();
return this;
};
/**
* Reactive menu replacement.
*
* @return {Element}
*/
Nav.prototype.replaceMenu = function () {
var self = this;
this.menu = new Menu()
.on('select', function (doc) {
self.emit('select', doc);
});
return this.menu.el;
};
/**
* On search, filter the menu.
*/
Nav.prototype.onSearch = function (e) {
switch (keyname(e.keyCode)) {
case 'enter':
return this.menu.select();
case 'up':
return this.menu.previous();
case 'down':
return this.menu.next();
}
var string = value(e.target);
this.menu.filter(function (el, model, view) {
return model.title().toLowerCase().indexOf(string) !== -1;
});
};