Files: 54d70439428e8fd11338aa37918eeab2dbd9f31a / modules / app / html / search.js
1678 bytesRaw
1 | var h = require('mutant/h') |
2 | var nest = require('depnest') |
3 | var addSuggest = require('suggest-box') |
4 | |
5 | exports.needs = nest({ |
6 | 'profile.async.suggest': 'first', |
7 | 'channel.async.suggest': 'first' |
8 | }) |
9 | |
10 | exports.gives = nest('app.html.search') |
11 | |
12 | exports.create = function (api) { |
13 | return nest('app.html.search', function (setView) { |
14 | var getProfileSuggestions = api.profile.async.suggest() |
15 | var getChannelSuggestions = api.channel.async.suggest() |
16 | var searchBox = h('input.search', { |
17 | type: 'search', |
18 | placeholder: 'word, @key, #channel', |
19 | 'ev-suggestselect': (ev) => { |
20 | setView(ev.detail.id) |
21 | searchBox.value = ev.detail.id |
22 | }, |
23 | 'ev-keydown': (ev) => { |
24 | if (ev.code === 'Enter') { |
25 | doSearch() |
26 | ev.preventDefault() |
27 | } |
28 | } |
29 | }) |
30 | |
31 | setImmediate(() => { |
32 | addSuggest(searchBox, (inputText, cb) => { |
33 | if (inputText[0] === '@') { |
34 | cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true}) |
35 | } else if (inputText[0] === '#') { |
36 | cb(null, getChannelSuggestions(inputText.slice(1))) |
37 | } |
38 | }, {cls: 'SuggestBox'}) |
39 | }) |
40 | |
41 | return searchBox |
42 | |
43 | function doSearch () { |
44 | var value = searchBox.value.trim() |
45 | if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) { |
46 | if (value.startsWith('@') && value.length < 30) { |
47 | return // probably not a key |
48 | } else if (value.length > 2) { |
49 | setView(value) |
50 | } |
51 | } else if (value.trim()) { |
52 | if (value.length > 2) { |
53 | setView(`?${value.trim()}`) |
54 | } |
55 | } |
56 | } |
57 | }) |
58 | } |
59 |
Built with git-ssb-web