git ssb

16+

Dominic / patchbay



Tree: d006827557e63f5aabd07e480b910ca50ddb9646

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

2915 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 // NOTE - this input is sometimes set by
46 // function buildSearchBarTermFromLocation (app/html/tabs.js)
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