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