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