git ssb

16+

Dominic / patchbay



Tree: 436b95d0f731a04c71b5f4a1ffcdba5961a2fd92

Files: 436b95d0f731a04c71b5f4a1ffcdba5961a2fd92 / app / html / search-bar.js

2653 bytesRaw
1const nest = require('depnest')
2const { h } = require('mutant')
3const addSuggest = require('suggest-box')
4
5exports.gives = nest('app.html.searchBar')
6
7exports.needs = nest({
8 'app.sync.goTo': 'first',
9 'about.async.suggest': 'first',
10 'channel.async.suggest': 'first',
11 // 'app.async.suggest': 'reduce' // TODO add ability to add to this
12})
13
14exports.create = function (api) {
15 var _search
16
17 return nest('app.html.searchBar', function searchBar () {
18 if (_search) return _search
19
20 const getProfileSuggestions = api.about.async.suggest()
21 const getChannelSuggestions = api.channel.async.suggest()
22
23 function goToLocation(location, ev) {
24 if (location[0] == '?')
25 location = { page: 'search', query: location.substring(1) }
26 else if (!['@', '#', '%', '&', '/'].includes(location[0]))
27 location = { page: 'search', query: location }
28
29 api.app.sync.goTo(location)
30 if (!ev.ctrlKey) input.blur()
31 }
32
33 const input = h('input', {
34 type: 'search',
35 placeholder: '?search, @name, #channel',
36 'ev-keyup': ev => {
37 switch (ev.keyCode) {
38 case 13: // enter
39 goToLocation(input.value.trim(), ev)
40 return
41 case 27: // escape
42 ev.preventDefault()
43 input.blur()
44 }
45 }
46 })
47
48 input.addEventListener('suggestselect', ev => {
49 input.value = ev.detail.id // HACK : this over-rides the markdown value
50 goToLocation(input.value.trim(), ev)
51 })
52
53 _search = h('SearchBar', input)
54 _search.input = input
55 _search.activate = (sigil, ev) => {
56 input.focus()
57 ev.preventDefault()
58 if (input.value[0] === sigil) {
59 input.selectionStart = 1
60 input.selectionEnd = input.value.length
61 } else {
62 input.value = sigil
63 }
64 }
65
66 addSuggest(input, (inputText, cb) => {
67 const char = inputText[0]
68 const word = inputText.slice(1)
69
70 if (char === '@') cb(null, getProfileSuggestions(word))
71 if (char === '#') cb(null, getChannelSuggestions(word))
72 if (char === '/') cb(null, getPagesSuggestions(word))
73 }, {cls: 'PatchSuggest'})
74
75 // TODO extract
76 function getPagesSuggestions (word) {
77 const pages = [
78 'public', 'private', 'inbox', 'profile', 'notifications',
79 'gatherings', 'chess'
80 ]
81
82 return pages
83 .filter(page => ~page.indexOf(word))
84 .sort((a, b) => a.indexOf(word) < b.indexOf(word) ? -1 : +1 )
85 .map(page => {
86 return {
87 title: '/'+page,
88 id: '/'+page,
89 value: '/'+page,
90 }
91 })
92 }
93
94 return _search
95 })
96}
97

Built with git-ssb-web