git ssb

16+

Dominic / patchbay



Tree: d7221d981ca25931b54c37c514c78e9803027278

Files: d7221d981ca25931b54c37c514c78e9803027278 / modules_extra / search.js

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

Built with git-ssb-web