git ssb

16+

Dominic / patchbay



Tree: 5149067cc270bfcbf301fd7c1db66cd15659a53f

Files: 5149067cc270bfcbf301fd7c1db66cd15659a53f / app / html / search-bar.js

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

Built with git-ssb-web