Files: d3ad4af2e605d2690d1e5fcfb6982c41857d1d7f / modules / app / html / search.js
1830 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 searchTimer = null |
17 | var searchBox = h('input.search', { |
18 | type: 'search', |
19 | placeholder: 'word, @key, #channel', |
20 | 'ev-suggestselect': (ev) => { |
21 | setView(ev.detail.id) |
22 | searchBox.value = ev.detail.id |
23 | }, |
24 | 'ev-input': (ev) => { |
25 | clearTimeout(searchTimer) |
26 | searchTimer = setTimeout(doSearch, 500) |
27 | }, |
28 | 'ev-focus': (ev) => { |
29 | if (searchBox.value) { |
30 | doSearch() |
31 | } |
32 | } |
33 | }) |
34 | |
35 | setImmediate(() => { |
36 | addSuggest(searchBox, (inputText, cb) => { |
37 | if (inputText[0] === '@') { |
38 | cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true}) |
39 | } else if (inputText[0] === '#') { |
40 | cb(null, getChannelSuggestions(inputText.slice(1))) |
41 | } |
42 | }, {cls: 'SuggestBox'}) |
43 | }) |
44 | |
45 | return searchBox |
46 | |
47 | function doSearch () { |
48 | var value = searchBox.value.trim() |
49 | if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) { |
50 | if (value.startsWith('@') && value.length < 30) { |
51 | return // probably not a key |
52 | } else if (value.length > 2) { |
53 | setView(value) |
54 | } |
55 | } else if (value.trim()) { |
56 | if (value.length > 2) { |
57 | setView(`?${value.trim()}`) |
58 | } |
59 | } else { |
60 | setView('/public') |
61 | } |
62 | } |
63 | }) |
64 | } |
65 |
Built with git-ssb-web