git ssb

16+

Dominic / patchbay



Tree: 38af2fdb5719a156793f1114d29328b6f0386cd7

Files: 38af2fdb5719a156793f1114d29328b6f0386cd7 / modules / search-box.js

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

Built with git-ssb-web