git ssb

16+

Dominic / patchbay



Tree: 9746cd345f014701f5911b233fe5aec3e3c52edb

Files: 9746cd345f014701f5911b233fe5aec3e3c52edb / modules_extra / search.js

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

Built with git-ssb-web