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