Files: 84f42d561913f861b364caecc35050feadc23744 / modules / app / html / search.js
2178 bytesRaw
1 | var h = require('mutant/h') |
2 | var nest = require('depnest') |
3 | var addSuggest = require('suggest-box') |
4 | |
5 | var appRoot = require('app-root-path'); |
6 | |
7 | exports.needs = nest({ |
8 | 'profile.async.suggest': 'first', |
9 | 'channel.async.suggest': 'first', |
10 | 'intl.sync.i18n': 'first' |
11 | }) |
12 | |
13 | exports.gives = nest('app.html.search') |
14 | |
15 | var pages = ['/public', '/private', '/mentions', '/all', '/gatherings'] |
16 | |
17 | exports.create = function (api) { |
18 | const i18n = api.intl.sync.i18n |
19 | return nest('app.html.search', function (setView) { |
20 | var getProfileSuggestions = api.profile.async.suggest() |
21 | var getChannelSuggestions = api.channel.async.suggest() |
22 | var searchBox = h('input.search', { |
23 | type: 'search', |
24 | placeholder: i18n('word, @key, #channel'), |
25 | 'ev-suggestselect': (ev) => { |
26 | setView(ev.detail.id) |
27 | searchBox.value = ev.detail.id |
28 | }, |
29 | 'ev-keydown': (ev) => { |
30 | if (ev.key === 'Enter') { |
31 | doSearch() |
32 | ev.preventDefault() |
33 | } |
34 | } |
35 | }) |
36 | |
37 | setImmediate(() => { |
38 | addSuggest(searchBox, (inputText, cb) => { |
39 | if (inputText[0] === '@') { |
40 | cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true}) |
41 | } else if (inputText[0] === '#') { |
42 | cb(null, getChannelSuggestions(inputText.slice(1))) |
43 | } else if (inputText[0] === '/') { |
44 | cb(null, getPageSuggestions(inputText)) |
45 | } |
46 | }, {cls: 'SuggestBox'}) |
47 | }) |
48 | |
49 | return searchBox |
50 | |
51 | function doSearch () { |
52 | var value = searchBox.value.trim() |
53 | if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) { |
54 | if (value.startsWith('@') && value.length < 30) { |
55 | return // probably not a key |
56 | } else if (value.length > 2) { |
57 | setView(value) |
58 | } |
59 | } else if (value.trim()) { |
60 | if (value.length > 2) { |
61 | setView(`?${value.trim()}`) |
62 | } |
63 | } |
64 | } |
65 | |
66 | function getPageSuggestions (input) { |
67 | return pages.sort().filter(p => p.startsWith(input.toLowerCase())).map(p => { |
68 | return { |
69 | id: p, |
70 | value: p, |
71 | title: p |
72 | } |
73 | }) |
74 | } |
75 | }) |
76 | } |
77 |
Built with git-ssb-web