git ssb

16+

Dominic / patchbay



Tree: dc2e96d4d0a86072d9393a2d15d786a80cb341ff

Files: dc2e96d4d0a86072d9393a2d15d786a80cb341ff / app / html / search-bar.js

2646 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] == '?') { location = { page: 'search', query: location.substring(1) } } else if (!['@', '#', '%', '&', '/'].includes(location[0])) { location = { page: 'search', query: location } }
25
26 api.app.sync.goTo(location)
27 if (!ev.ctrlKey) input.blur()
28 }
29
30 const input = h('input', {
31 type: 'search',
32 placeholder: '?search, @name, #channel',
33 'ev-keyup': ev => {
34 switch (ev.keyCode) {
35 case 13: // enter
36 goToLocation(input.value.trim(), ev)
37 return
38 case 27: // escape
39 ev.preventDefault()
40 input.blur()
41 }
42 }
43 })
44
45 input.addEventListener('suggestselect', ev => {
46 input.value = ev.detail.id // HACK : this over-rides the markdown value
47 goToLocation(input.value.trim(), ev)
48 })
49
50 _search = h('SearchBar', input)
51 _search.input = input
52 _search.activate = (sigil, ev) => {
53 input.focus()
54 ev.preventDefault()
55 if (input.value[0] === sigil) {
56 input.selectionStart = 1
57 input.selectionEnd = input.value.length
58 } else {
59 input.value = sigil
60 }
61 }
62
63 addSuggest(input, (inputText, cb) => {
64 const char = inputText[0]
65 const word = inputText.slice(1)
66
67 if (char === '@') cb(null, getProfileSuggestions(word))
68 if (char === '#') cb(null, getChannelSuggestions(word))
69 if (char === '/') cb(null, getPagesSuggestions(word))
70 }, {cls: 'PatchSuggest'})
71
72 // TODO extract
73 function getPagesSuggestions (word) {
74 const pages = [
75 'public', 'private', 'inbox', 'profile', 'notifications',
76 'gatherings', 'chess', 'books'
77 ]
78
79 return pages
80 .filter(page => ~page.indexOf(word))
81 .sort((a, b) => a.indexOf(word) < b.indexOf(word) ? -1 : +1)
82 .map(page => {
83 return {
84 title: '/' + page,
85 id: '/' + page,
86 value: '/' + page
87 }
88 })
89 }
90
91 return _search
92 })
93}
94

Built with git-ssb-web