git ssb

16+

Dominic / patchbay



Tree: 103c9cb09aa9eb239d03b8d79511054d4391d90b

Files: 103c9cb09aa9eb239d03b8d79511054d4391d90b / modules_extra / search.js

2280 bytesRaw
1var h = require('hyperscript')
2var u = require('../util')
3var pull = require('pull-stream')
4var Scroller = require('pull-scroll')
5var TextNodeSearcher = require('text-node-searcher')
6
7exports.needs = {
8 build_scroller: 'first',
9 message_render: 'first',
10 sbot_log: 'first'
11}
12
13exports.gives = 'screen_view'
14
15var whitespace = /\s+/
16
17function andSearch(terms, inputs) {
18 for(var i = 0; i < terms.length; i++) {
19 var match = false
20 for(var j = 0; j < inputs.length; j++) {
21 if(terms[i].test(inputs[j])) match = true
22 }
23 //if a term was not matched by anything, filter this one
24 if(!match) return false
25 }
26 return true
27}
28
29function searchFilter(terms) {
30 return function (msg) {
31 var c = msg && msg.value && msg.value.content
32 return c && (
33 msg.key == terms[0] ||
34 andSearch(terms.map(function (term) {
35 return new RegExp('\\b'+term+'\\b', 'i')
36 }), [c.text, c.name, c.title])
37 )
38 }
39}
40
41function createOrRegExp(ary) {
42 return new RegExp(ary.map(function (e) {
43 return '\\b'+e+'\\b'
44 }).join('|'), 'i')
45}
46
47function highlight(el, query) {
48 var searcher = new TextNodeSearcher({container: el})
49 searcher.query = query
50 searcher.highlight()
51 return el
52}
53
54exports.create = function (api) {
55
56 return function (path) {
57 if(path[0] === '?') {
58 var query = path.substr(1).trim().split(whitespace)
59 var _matches = searchFilter(query)
60
61 var total = 0, matches = 0
62
63 var header = h('div.search_header', '')
64 var { container, content } = api.build_scroller({ prepend: header})
65
66 function matchesQuery (data) {
67 total++
68 var m = _matches(data)
69 if(m) matches++
70 header.textContent = 'searched:'+total+', found:'+matches
71 return m
72 }
73
74 function renderMsg(msg) {
75 var el = api.message_render(msg)
76 highlight(el, createOrRegExp(query))
77 return el
78 }
79
80 pull(
81 api.sbot_log({old: false}),
82 pull.filter(matchesQuery),
83 Scroller(container, content, renderMsg, true, false)
84 )
85
86 pull(
87 u.next(api.sbot_log, {reverse: true, limit: 500, live: false}),
88 pull.filter(matchesQuery),
89 Scroller(container, content, renderMsg, false, false)
90 )
91
92 return container
93 }
94 }
95
96}
97

Built with git-ssb-web