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