git ssb

10+

Matt McKegg / patchwork



Commit 81be424f72e758de508ff828259c7c30cea65236

extract searchbox into depject module

Matt McKegg committed on 2/21/2017, 1:21:39 AM
Parent: d3822aadfe8e7501ded08bca709a22dc48512806

Files changed

main-window.jschanged
modules/app/html/search.jsadded
main-window.jsView
@@ -10,9 +10,8 @@
1010 var MutantMap = require('mutant/map')
1111 var Url = require('url')
1212 var insertCss = require('insert-css')
1313 var nest = require('depnest')
14-var addSuggest = require('suggest-box')
1514 var LatestUpdate = require('./lib/latest-update')
1615
1716 require('./lib/context-menu-and-spellcheck.js')
1817
@@ -28,39 +27,15 @@
2827 var api = entry(sockets, nest({
2928 'page.html.render': 'first',
3029 'keys.sync.id': 'first',
3130 'blob.sync.url': 'first',
32- 'profile.async.suggest': 'first',
33- 'channel.async.suggest': 'first'
31 + 'app.html.search': 'first'
3432 }))
3533
3634 var renderPage = api.page.html.render
3735 var id = api.keys.sync.id()
38- var getProfileSuggestions = api.profile.async.suggest()
39- var getChannelSuggestions = api.channel.async.suggest()
4036 var latestUpdate = LatestUpdate()
4137
42- var searchTimer = null
43- var searchBox = h('input.search', {
44- type: 'search',
45- placeholder: 'word, @key, #channel',
46- 'ev-suggestselect': (ev) => {
47- setView(ev.detail.id)
48- searchBox.value = ev.detail.id
49- }
50- })
51-
52- searchBox.oninput = function () {
53- clearTimeout(searchTimer)
54- searchTimer = setTimeout(doSearch, 500)
55- }
56-
57- searchBox.onfocus = function () {
58- if (searchBox.value) {
59- doSearch()
60- }
61- }
62-
6338 var forwardHistory = []
6439 var backHistory = []
6540
6641 var views = MutantDict({
@@ -119,9 +94,9 @@
11994 tab('Public', '/public'),
12095 tab('Private', '/private')
12196 ]),
12297 h('span.appTitle', ['Patchwork']),
123- h('span', [ searchBox ]),
98 + h('span', [ api.app.html.search(setView) ]),
12499 h('span.nav', [
125100 tab('Profile', id),
126101 tab('Mentions', '/mentions')
127102 ])
@@ -135,16 +110,8 @@
135110 ),
136111 mainElement
137112 ])
138113
139- addSuggest(searchBox, (inputText, cb) => {
140- if (inputText[0] === '@') {
141- cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true})
142- } else if (inputText[0] === '#') {
143- cb(null, getChannelSuggestions(inputText.slice(1)))
144- }
145- }, {cls: 'SuggestBox'})
146-
147114 return container
148115
149116 // scoped
150117
@@ -201,9 +168,8 @@
201168 }
202169
203170 function goBack () {
204171 if (backHistory.length) {
205-
206172 canGoForward.set(true)
207173 forwardHistory.push(currentView())
208174
209175 var view = backHistory.pop()
@@ -252,25 +218,8 @@
252218 currentView.set(view)
253219 }
254220 }
255221
256- function doSearch () {
257- var value = searchBox.value.trim()
258- if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) {
259- if (value.startsWith('@') && value.length < 30) {
260- return // probably not a key
261- } else if (value.length > 2) {
262- setView(value)
263- }
264- } else if (value.trim()) {
265- if (value.length > 2) {
266- setView(`?${value.trim()}`)
267- }
268- } else {
269- setView('/public')
270- }
271- }
272-
273222 function selected (view) {
274223 return computed([currentView, view], (currentView, view) => {
275224 return currentView === view
276225 })
modules/app/html/search.jsView
@@ -1,0 +1,64 @@
1 +var h = require('mutant/h')
2 +var nest = require('depnest')
3 +var addSuggest = require('suggest-box')
4 +
5 +exports.needs = nest({
6 + 'profile.async.suggest': 'first',
7 + 'channel.async.suggest': 'first'
8 +})
9 +
10 +exports.gives = nest('app.html.search')
11 +
12 +exports.create = function (api) {
13 + return nest('app.html.search', function (setView) {
14 + var getProfileSuggestions = api.profile.async.suggest()
15 + var getChannelSuggestions = api.channel.async.suggest()
16 + var searchTimer = null
17 + var searchBox = h('input.search', {
18 + type: 'search',
19 + placeholder: 'word, @key, #channel',
20 + 'ev-suggestselect': (ev) => {
21 + setView(ev.detail.id)
22 + searchBox.value = ev.detail.id
23 + },
24 + 'ev-input': (ev) => {
25 + clearTimeout(searchTimer)
26 + searchTimer = setTimeout(doSearch, 500)
27 + },
28 + 'ev-focus': (ev) => {
29 + if (searchBox.value) {
30 + doSearch()
31 + }
32 + }
33 + })
34 +
35 + setImmediate(() => {
36 + addSuggest(searchBox, (inputText, cb) => {
37 + if (inputText[0] === '@') {
38 + cb(null, getProfileSuggestions(inputText.slice(1)), {idOnly: true})
39 + } else if (inputText[0] === '#') {
40 + cb(null, getChannelSuggestions(inputText.slice(1)))
41 + }
42 + }, {cls: 'SuggestBox'})
43 + })
44 +
45 + return searchBox
46 +
47 + function doSearch () {
48 + var value = searchBox.value.trim()
49 + if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) {
50 + if (value.startsWith('@') && value.length < 30) {
51 + return // probably not a key
52 + } else if (value.length > 2) {
53 + setView(value)
54 + }
55 + } else if (value.trim()) {
56 + if (value.length > 2) {
57 + setView(`?${value.trim()}`)
58 + }
59 + } else {
60 + setView('/public')
61 + }
62 + }
63 + })
64 +}

Built with git-ssb-web