Files: 1a1dfa987b784a002e626f97e18aeaf2164d1299 / modules / app / html / search.js
2166 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 | var i18n = require(appRoot + '/lib/i18n').i18n |
7 | |
8 | |
9 | exports.needs = nest({ |
10 | 'profile.async.suggest': 'first', |
11 | 'channel.async.suggest': 'first' |
12 | }) |
13 | |
14 | exports.gives = nest('app.html.search') |
15 | |
16 | var pages = ['/public', '/private', '/mentions', '/all', '/gatherings'] |
17 | |
18 | exports.create = function (api) { |
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