git ssb

16+

Dominic / patchbay



Tree: e37288c1eddd72e5d911ccbc966d5b58020827c8

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

2858 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
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 === '@') api.about.async.suggest(word, cb)
71 if (char === '#') api.channel.async.suggest(word, cb)
72 if (char === '/') cb(null, getPagesSuggestions(word))
73 }, { cls: 'PatchSuggest' })
74
75 // TODO extract - as in when something supplies something as a menuItem, this should also be auto-populated...
76 function getPagesSuggestions (word) {
77 const pages = [
78 'blogs', 'calendar', 'posts', 'public', 'private', 'inbox', 'profile', 'notifications', 'settings', 'shortcuts',
79 'network',
80 'gatherings', 'chess', 'books', 'imageSearch', 'polls', 'query', 'dark-crystal', 'postRank', 'scry', 'scry/new'
81 ]
82
83 return pages
84 .filter(page => ~page.indexOf(word))
85 .sort((a, b) => a.indexOf(word) < b.indexOf(word) ? -1 : +1)
86 .map(page => {
87 return {
88 title: '/' + page,
89 id: '/' + page,
90 value: '/' + page
91 }
92 })
93 }
94
95 return _search
96 })
97}
98

Built with git-ssb-web