git ssb

16+

Dominic / patchbay



Commit 2368dc2750e7187e31f959a729f0c3d539a8e2bf

Add search

Charles Lehner committed on 6/28/2016, 12:36:10 AM
Parent: 2c3326a3f3c4bf8abcccb2b2b36cf5da28804862

Files changed

modules/index.jschanged
modules/tabs.jschanged
modules/search.jsadded
sbot-api.jschanged
style.csschanged
modules/index.jsView
@@ -18,8 +18,9 @@
1818 require('./names.js'),
1919 require('./post.js'),
2020 require('./private.js'),
2121 require('./public.js'),
22+ require('./search.js'),
2223 require('./suggest-mentions.js'),
2324 require('./suggest.js'),
2425 require('./tabs.js'),
2526 require('./thread.js'),
modules/tabs.jsView
@@ -33,14 +33,40 @@
3333 exports.app = function () {
3434 var tabs = Tabs()
3535 tabs.classList.add('screen')
3636
37+ var search = h('input.searchprompt', {
38+ type: 'search',
39+ style: {'float': 'right'},
40+ onkeydown: function (ev) {
41+ switch (ev.keyCode) {
42+ case 13: // enter
43+ var path = '?' + search.value
44+ if(tabs.has(path)) return tabs.select(path)
45+ var el = screen_view(path)
46+ if(el) {
47+ el.scroll = keyscroll(el.querySelector('.scroller__content'))
48+ tabs.add('?' + search.value, el, !ev.ctrlKey)
49+ localStorage.openTabs = JSON.stringify(tabs.tabs)
50+ search.blur()
51+ }
52+ return
53+ case 27: // escape
54+ ev.preventDefault()
55+ search.blur()
56+ return
57+ }
58+ }
59+ })
60+ tabs.insertBefore(search, tabs.querySelector('.hypertabs__content'))
61+
3762 var saved
3863 try { saved = JSON.parse(localStorage.openTabs) }
3964 catch (_) { saved = ['/public', '/private'] }
4065
4166 saved.forEach(function (path) {
4267 var el = screen_view(path)
68+ if (!el) return
4369 el.scroll = keyscroll(el.querySelector('.scroller__content'))
4470 if(el) tabs.add(path, el, true)
4571 })
4672
@@ -71,25 +97,36 @@
7197 window.addEventListener('keydown', function (ev) {
7298 if (ev.target.nodeName === 'INPUT' || ev.target.nodeName === 'TEXTAREA')
7399 return
74100 switch(ev.keyCode) {
101+
75102 // scroll through tabs
76103 case 72: // h
77104 return tabs.selectRelative(-1)
78105 case 76: // l
79106 return tabs.selectRelative(1)
107+
80108 // scroll through messages
81109 case 74: // j
82110 return tabs.selectedTab.scroll(1)
83111 case 75: // k
84112 return tabs.selectedTab.scroll(-1)
113+
85114 // close a tab
86115 case 88: // x
87116 if (tabs.selected !== '/public' && tabs.selected !== '/private') {
88117 tabs.remove(tabs.selected)
89118 localStorage.openTabs = JSON.stringify(tabs.tabs)
90119 }
91120 return
121+
122+ // activate the search field
123+ case 191: // /
124+ ev.preventDefault()
125+ search.focus()
126+ search.selectionStart = 0
127+ search.selectionEnd = search.value.length
128+ return
92129 }
93130 })
94131
95132 return tabs
modules/search.jsView
@@ -1,0 +1,35 @@
1+var h = require('hyperscript')
2+var u = require('../util')
3+var pull = require('pull-stream')
4+var Scroller = require('pull-scroll')
5+
6+var plugs = require('../plugs')
7+var message_render = plugs.first(exports.message_render = [])
8+var sbot_search = plugs.first(exports.sbot_search = [])
9+
10+exports.screen_view = function (path) {
11+ if(path[0] === '?') {
12+ var query = path.substr(1)
13+
14+ var content = h('div.column.scroller__content')
15+ var div = h('div.column.scroller',
16+ {style: {'overflow':'auto'}},
17+ h('div.scroller__wrapper',
18+ content
19+ )
20+ )
21+
22+ pull(
23+ sbot_search({query: query, old: false}),
24+ Scroller(div, content, message_render, true, false)
25+ )
26+
27+ pull(
28+ u.next(sbot_search, {query: query,
29+ reverse: true, limit: 100, live: false}),
30+ Scroller(div, content, message_render, false, false)
31+ )
32+
33+ return div
34+ }
35+}
sbot-api.jsView
@@ -51,8 +51,25 @@
5151 }),
5252 sbot_user_feed: rec.source(function (opts) {
5353 return sbot.createUserStream(opts)
5454 }),
55+ sbot_search: rec.source(function (opts) {
56+ var search = new RegExp(opts.query, 'i')
57+ var limit = opts.limit || Infinity
58+ delete opts.limit
59+ return pull(
60+ sbot.createLogStream(opts),
61+ pull.filter(function (msg) {
62+ var c = msg && msg.value && msg.value.content
63+ return c && (
64+ msg.key == opts.query ||
65+ c.text && search.test(c.text) ||
66+ c.name && search.test(c.name) ||
67+ c.title && search.test(c.title))
68+ }),
69+ pull.take(limit)
70+ )
71+ }),
5572 sbot_get: rec.async(function (key, cb) {
5673 sbot.get(key, cb)
5774 }),
5875 sbot_publish: rec.async(function (msg, cb) {
style.cssView
@@ -155,4 +155,14 @@
155155
156156 .lightbox {
157157 overflow: auto;
158158 }
159+
160+/* searchprompt */
161+
162+.searchprompt {
163+ position: absolute;
164+}
165+
166+.searchprompt:not(:focus) {
167+ opacity: 0;
168+}

Built with git-ssb-web