Files: bbf9e423c5c7c91fb95d8d2b7f0bd30e6615b94a / modules / search-box.js
2297 bytesRaw
1 | var h = require('hyperscript') |
2 | var suggest = require('suggest-box') |
3 | var pull = require('pull-stream') |
4 | var plugs = require('../plugs') |
5 | var sbot_query = plugs.first(exports.sbot_query = []) |
6 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
7 | |
8 | var channels = [] |
9 | |
10 | var signified = require('../plugs').first(exports.signified = []) |
11 | |
12 | |
13 | exports.search_box = function (go) { |
14 | |
15 | var suggestBox |
16 | var search = h('input.searchprompt', { |
17 | type: 'search', |
18 | onkeydown: function (ev) { |
19 | switch (ev.keyCode) { |
20 | case 13: // enter |
21 | if (suggestBox && suggestBox.active) { |
22 | suggestBox.complete() |
23 | ev.stopPropagation() |
24 | } |
25 | if (go(search.value.trim(), !ev.ctrlKey)) |
26 | search.blur() |
27 | return |
28 | case 27: // escape |
29 | ev.preventDefault() |
30 | search.blur() |
31 | return |
32 | } |
33 | } |
34 | }) |
35 | |
36 | search.activate = function (sigil, ev) { |
37 | search.focus() |
38 | ev.preventDefault() |
39 | if (search.value[0] === sigil) { |
40 | search.selectionStart = 1 |
41 | search.selectionEnd = search.value.length |
42 | } else { |
43 | search.value = sigil |
44 | } |
45 | } |
46 | |
47 | var suggestions = {} |
48 | |
49 | // delay until the element has a parent |
50 | setTimeout(function () { |
51 | suggestBox = suggest(search, function (word, cb) { |
52 | if(/^#\w/.test(word)) |
53 | cb(null, channels.filter(function (chan) { |
54 | return ('#'+chan.name).substring(0, word.length) === word |
55 | }) |
56 | .map(function (chan) { |
57 | var name = '#'+chan.name |
58 | return { |
59 | title: name, |
60 | value: name, |
61 | subtitle: chan.rank |
62 | } |
63 | })) |
64 | else if(/^@\w/.test(word)) { |
65 | signified(word, function (_, names) { |
66 | cb(null, names.map(function (e) { |
67 | return { |
68 | title: e.name + ':'+e.id.substring(0, 10), |
69 | value: e.id, |
70 | subtitle: e.rank |
71 | } |
72 | })) |
73 | }) |
74 | } |
75 | }, {}) |
76 | }, 10) |
77 | |
78 | |
79 | pull( |
80 | sbot_query({query: [ |
81 | {$filter: {value: {content: {channel: {$gt: ''}}}}}, |
82 | {$reduce: { |
83 | name: ['value', 'content', 'channel'], |
84 | rank: {$count: true} |
85 | }} |
86 | ]}), |
87 | pull.collect(function (err, chans) { |
88 | if (err) return console.error(err) |
89 | channels = chans |
90 | }) |
91 | ) |
92 | |
93 | return search |
94 | } |
95 | |
96 | |
97 | |
98 |
Built with git-ssb-web