git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: ab49d98e85de76db59f84cc035e271040023fad3

Files: ab49d98e85de76db59f84cc035e271040023fad3 / modules / app / html / search.js

2131 bytesRaw
1var h = require('mutant/h')
2var nest = require('depnest')
3var addSuggest = require('suggest-box')
4
5exports.needs = nest({
6 'profile.async.suggest': 'first',
7 'channel.async.suggest': 'first',
8 'intl.sync.i18n': 'first'
9})
10
11exports.gives = nest('app.html.search')
12
13var pages = ['/public', '/private', '/mentions', '/all', '/gatherings']
14
15exports.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