git ssb

16+

Dominic / patchbay



Tree: 8f59da724703887a5b3c411137cd3854f78c74d1

Files: 8f59da724703887a5b3c411137cd3854f78c74d1 / app / html / search-bar.js

2969 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 const prefixes = ['@', '#', '%', '&', '/', 'ssb:']
22
23 if (location[0] === '?') {
24 location = { page: 'search', query: location.substring(1) }
25 } else if (prefixes.every(p => !location.startsWith(p))) {
26 location = { page: 'search', query: location }
27 }
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 // NOTE - this input is sometimes set by
48 // function buildSearchBarTermFromLocation (app/html/tabs.js)
49
50 input.addEventListener('suggestselect', ev => {
51 input.value = ev.detail.id // HACK : this over-rides the markdown value
52 goToLocation(input.value.trim(), ev)
53 })
54
55 _search = h('SearchBar', input)
56 _search.input = input
57 _search.activate = (sigil, ev) => {
58 input.focus()
59 ev.preventDefault()
60 if (input.value[0] === sigil) {
61 input.selectionStart = 1
62 input.selectionEnd = input.value.length
63 } else {
64 input.value = sigil
65 }
66 }
67
68 addSuggest(input, (inputText, cb) => {
69 const char = inputText[0]
70 const word = inputText.slice(1)
71
72 if (char === '@') api.about.async.suggest(word, cb)
73 if (char === '#') api.channel.async.suggest(word, cb)
74 if (char === '/') cb(null, getPagesSuggestions(word))
75 }, { cls: 'PatchSuggest' })
76
77 // TODO extract - as in when something supplies something as a menuItem, this should also be auto-populated...
78 function getPagesSuggestions (word) {
79 const pages = [
80 'blogs', 'calendar', 'posts', 'public', 'private', 'inbox', 'profile', 'notifications', 'settings', 'shortcuts',
81 'network',
82 'gatherings', 'chess', 'books', 'imageSearch', 'polls', 'query', 'dark-crystal', 'postRank', 'scry', 'scry/new'
83 ]
84
85 return pages
86 .filter(page => ~page.indexOf(word))
87 .sort((a, b) => a.indexOf(word) < b.indexOf(word) ? -1 : +1)
88 .map(page => {
89 return {
90 title: '/' + page,
91 id: '/' + page,
92 value: '/' + page
93 }
94 })
95 }
96
97 return _search
98 })
99}
100

Built with git-ssb-web