Files: df3a0af70b34b408b510eafb21126103772656a3 / modules / app / html / search.js
2068 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 | var pages = ['/public', '/private', '/mentions', '/all', '/gatherings'] |
13 | |
14 | exports.create = function (api) { |
15 | return nest('app.html.search', function (setView) { |
16 | var getProfileSuggestions = api.profile.async.suggest() |
17 | var getChannelSuggestions = api.channel.async.suggest() |
18 | var searchBox = h('input.search', { |
19 | type: 'search', |
20 | placeholder: 'word, @key, #channel', |
21 | 'ev-suggestselect': (ev) => { |
22 | setView(ev.detail.id) |
23 | searchBox.value = ev.detail.id |
24 | }, |
25 | 'ev-keydown': (ev) => { |
26 | if (ev.key === 'Enter') { |
27 | doSearch() |
28 | ev.preventDefault() |
29 | } |
30 | } |
31 | }) |
32 | |
33 | setImmediate(() => { |
34 | addSuggest(searchBox, (inputText, cb) => { |
35 | if (inputText[0] === '@') { |
36 | cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true}) |
37 | } else if (inputText[0] === '#') { |
38 | cb(null, getChannelSuggestions(inputText.slice(1))) |
39 | } else if (inputText[0] === '/') { |
40 | cb(null, getPageSuggestions(inputText)) |
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 | } |
60 | } |
61 | |
62 | function getPageSuggestions (input) { |
63 | return pages.sort().filter(p => p.startsWith(input.toLowerCase())).map(p => { |
64 | return { |
65 | id: p, |
66 | value: p, |
67 | title: p |
68 | } |
69 | }) |
70 | } |
71 | }) |
72 | } |
73 |
Built with git-ssb-web