Files: bfff9ddc0fc0159cb662f4168b1960ff7822dfa0 / modules / app / html / search.js
2130 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 | 'intl.sync.i18n': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest('app.html.search') |
12 | |
13 | var pages = ['/public', '/private', '/mentions', '/all', '/gatherings'] |
14 | |
15 | exports.create = function (api) { |
16 | const i18n = api.intl.sync.i18n |
17 | return nest('app.html.search', function (setView) { |
18 | var getProfileSuggestions = api.profile.async.suggest() |
19 | var getChannelSuggestions = api.channel.async.suggest() |
20 | var searchBox = h('input.search', { |
21 | type: 'search', |
22 | placeholder: i18n('word, @key, #channel'), |
23 | 'ev-suggestselect': (ev) => { |
24 | setView(ev.detail.id) |
25 | searchBox.value = ev.detail.id |
26 | }, |
27 | 'ev-keydown': (ev) => { |
28 | if (ev.key === 'Enter') { |
29 | doSearch() |
30 | ev.preventDefault() |
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 | } else if (inputText[0] === '/') { |
42 | cb(null, getPageSuggestions(inputText)) |
43 | } |
44 | }, {cls: 'SuggestBox'}) |
45 | }) |
46 | |
47 | return searchBox |
48 | |
49 | function doSearch () { |
50 | var value = searchBox.value.trim() |
51 | if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) { |
52 | if (value.startsWith('@') && value.length < 30) { |
53 | // probably not a key |
54 | } else if (value.length > 2) { |
55 | setView(value) |
56 | } |
57 | } else if (value.trim()) { |
58 | if (value.length > 2) { |
59 | setView(`?${value.trim()}`) |
60 | } |
61 | } |
62 | } |
63 | |
64 | function getPageSuggestions (input) { |
65 | return pages.sort().filter(p => p.startsWith(input.toLowerCase())).map(p => { |
66 | return { |
67 | id: p, |
68 | value: p, |
69 | title: p |
70 | } |
71 | }) |
72 | } |
73 | }) |
74 | } |
75 |
Built with git-ssb-web