Files: b5cf9912e3e6845bf8f4aeeebea6559069eeb23d / modules_core / search-box.js
1576 bytesRaw
1 | |
2 | var cont = require('cont') |
3 | var h = require('hyperscript') |
4 | var suggest = require('suggest-box') |
5 | |
6 | exports.needs = { |
7 | sbot_query: 'first', sbot_links2: 'first', |
8 | suggest_search: 'map' //REWRITE |
9 | } |
10 | |
11 | exports.gives = 'search_box' |
12 | |
13 | exports.create = function (api) { |
14 | |
15 | return function (go) { |
16 | |
17 | var suggestBox |
18 | var search = h('input.searchprompt', { |
19 | type: 'search', |
20 | placeholder: 'Commands', |
21 | onkeydown: function (ev) { |
22 | switch (ev.keyCode) { |
23 | case 13: // enter |
24 | if (suggestBox && suggestBox.active) { |
25 | suggestBox.complete() |
26 | ev.stopPropagation() |
27 | } |
28 | if (go(search.value.trim(), !ev.ctrlKey)) |
29 | search.blur() |
30 | return |
31 | case 27: // escape |
32 | ev.preventDefault() |
33 | search.blur() |
34 | return |
35 | } |
36 | } |
37 | }) |
38 | |
39 | search.activate = function (sigil, ev) { |
40 | search.focus() |
41 | ev.preventDefault() |
42 | if (search.value[0] === sigil) { |
43 | search.selectionStart = 1 |
44 | search.selectionEnd = search.value.length |
45 | } else { |
46 | search.value = sigil |
47 | } |
48 | } |
49 | |
50 | // delay until the element has a parent |
51 | setTimeout(function () { |
52 | suggestBox = suggest(search, function (word, cb) { |
53 | cont.para(api.suggest_search(word)) |
54 | (function (err, ary) { |
55 | if(err) return cb(err) |
56 | |
57 | cb(null, ary.filter(Boolean).reduce(function (a, b) { |
58 | return a.concat(b) |
59 | }, [])) |
60 | }) |
61 | }, {}) |
62 | }, 10) |
63 | |
64 | return search |
65 | } |
66 | |
67 | } |
68 | |
69 |
Built with git-ssb-web