git ssb

3+

dangerousbeans / scuttle-vue



Tree: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd

Files: 8dc25143a3f37f1618f7a92b3c46dec512b43bfd / modules / search.js

3325 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 sbot_fulltext_search: '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
54function fallback(reader) {
55 var fallbackRead
56 return function (read) {
57 return function (abort, cb) {
58 read(abort, function next(end, data) {
59 if (end && reader && (fallbackRead = reader(end))) {
60 reader = null
61 read = fallbackRead
62 read(abort, next)
63 } else {
64 cb(end, data)
65 }
66 })
67 }
68 }
69}
70
71exports.create = function (api) {
72 return function (path) {
73 if(path[0] === '?' || '#') {
74 var queryStr = path
75 if (queryStr[0] === '?') {
76 queryStr = queryStr.substring(1).trim()
77 }
78 console.log(path)
79 console.log(queryStr)
80 var query = queryStr.split(whitespace)
81 var _matches = searchFilter(query)
82
83 var total = 0, matches = 0
84 var usingLinearSearch = false
85
86 var header = h('div.search_header', '')
87 var content = h('div.column.scroller__content')
88 var div = h('div.column.scroller',
89 {style: {'overflow':'auto'}},
90 h('div.scroller__wrapper',
91 header,
92 content
93 )
94 )
95
96 function matchesQuery (data) {
97 total++
98 var m = _matches(data)
99 if(m) matches++
100 //if(usingLinearSearch) {
101 //header.textContent = 'searched:'+total+', found:'+matches
102 //}
103 return m
104 }
105
106 function renderMsg(msg) {
107 var el = api.message_render(msg)
108 highlight(el, createOrRegExp(query))
109 return el
110 }
111
112 pull(
113 api.sbot_log({old: false}),
114 pull.filter(matchesQuery),
115 Scroller(div, content, renderMsg, true, false)
116 )
117
118 pull(
119 u.next(api.sbot_fulltext_search, {query: queryStr, reverse: true, limit: 500, live: false}),
120 fallback(function (err) {
121 if (/^no source/.test(err.message)) {
122 usingLinearSearch = true
123 return pull(
124 u.next(api.sbot_log, {reverse: true, limit: 500, live: false}),
125 pull.filter(matchesQuery)
126 )
127 }
128 }),
129 Scroller(div, content, renderMsg, false, false)
130 )
131
132 return div
133 }
134 }
135
136}
137

Built with git-ssb-web