Files: 586029e96f32a0cff461ec17d522d4877ec63fc0 / modules_basic / search-box.js
2735 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 | //TODO: this list should be generated from plugs |
13 | var builtinTabs = [ |
14 | '/public', '/private', '/notifications', |
15 | '/network', '/query', '/theme', '/versions' |
16 | ].map(function (name) { |
17 | return { |
18 | title: name, |
19 | value: name, |
20 | } |
21 | }) |
22 | |
23 | exports.search_box = function (go) { |
24 | |
25 | var suggestBox |
26 | var search = h('input.searchprompt', { |
27 | type: 'search', |
28 | placeholder: '?word, @key, #channel', |
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 | if(/^#\w/.test(word)) |
64 | cb(null, channels.filter(function (chan) { |
65 | return ('#'+chan.name).substring(0, word.length) === word |
66 | }) |
67 | .map(function (chan) { |
68 | var name = '#'+chan.name |
69 | return { |
70 | title: name, |
71 | value: name, |
72 | subtitle: chan.rank |
73 | } |
74 | })) |
75 | else if(/^@\w/.test(word)) { |
76 | signified(word, function (_, names) { |
77 | cb(null, names.map(function (e) { |
78 | return { |
79 | title: e.name + ':'+e.id.substring(0, 10), |
80 | value: e.id, |
81 | subtitle: e.rank |
82 | } |
83 | })) |
84 | }) |
85 | } else if(/^\//.test(word)) { |
86 | cb(null, builtinTabs.filter(function (name) { |
87 | return name.value.substr(0, word.length) === word |
88 | })) |
89 | } |
90 | }, {}) |
91 | }, 10) |
92 | |
93 | |
94 | pull( |
95 | sbot_query({query: [ |
96 | {$filter: {value: {content: {channel: {$gt: ''}}}}}, |
97 | {$reduce: { |
98 | name: ['value', 'content', 'channel'], |
99 | rank: {$count: true} |
100 | }} |
101 | ]}), |
102 | pull.collect(function (err, chans) { |
103 | if (err) return console.error(err) |
104 | channels = chans |
105 | }) |
106 | ) |
107 | |
108 | return search |
109 | } |
110 | |
111 | |
112 | |
113 |
Built with git-ssb-web