git ssb

16+

Dominic / patchbay



Tree: 0b83856cdd663c07cc28d7f3f0f3a883b41881da

Files: 0b83856cdd663c07cc28d7f3f0f3a883b41881da / app / html / search-bar.js

1793 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 'about.async.suggest': 'first',
9 'channel.async.suggest': 'first'
10})
11
12exports.create = function (api) {
13 return nest('app.html.searchBar', searchBar)
14
15 function searchBar (go) {
16 const getProfileSuggestions = api.about.async.suggest()
17 const getChannelSuggestions = api.channel.async.suggest()
18
19 const input = h('input', {
20 type: 'search',
21 placeholder: '?search, @name, #channel',
22 'ev-keyup': ev => {
23 switch (ev.keyCode) {
24 case 13: // enter
25 if (go(input.value.trim(), !ev.ctrlKey)) {
26 input.blur()
27 }
28 return
29 case 27: // escape
30 ev.preventDefault()
31 input.blur()
32 return
33 }
34 }
35 })
36 input.go = go // crude navigation api
37 input.addEventListener('suggestselect', ev => {
38 input.value = ev.detail.id // HACK : this over-rides the markdown value
39
40 // if (go(input.value.trim(), !ev.ctrlKey))
41 // input.blur()
42 })
43 const search = h('SearchBar', input)
44
45 search.input = input
46 search.activate = (sigil, ev) => {
47 input.focus()
48 ev.preventDefault()
49 if (input.value[0] === sigil) {
50 input.selectionStart = 1
51 input.selectionEnd = input.value.length
52 } else {
53 input.value = sigil
54 }
55 }
56
57 addSuggest(input, (inputText, cb) => {
58 if (inputText[0] === '@') {
59 cb(null, getProfileSuggestions(inputText.slice(1)))
60 } else if (inputText[0] === '#') {
61 cb(null, getChannelSuggestions(inputText.slice(1)))
62 }
63 }, {cls: 'SuggestBox'})
64
65 return search
66 }
67}
68
69

Built with git-ssb-web