git ssb

30+

cel / git-ssb-web



Commit bdbcf3382e534954bdeab3f996415d9bc9077a57

Add search bar

Charles Lehner committed on 4/13/2016, 10:59:24 AM
Parent: 800afbccfdb2255a0afbc2b0e0008eddd8387374

Files changed

index.jschanged
static/styles.csschanged
index.jsView
@@ -635,8 +635,10 @@
635635 }
636636
637637 if (dir == '')
638638 return serveIndex(req)
639+ else if (dir == 'search')
640+ return serveSearch(req)
639641 else if (ref.isBlobId(dir))
640642 return serveBlob(req, dir)
641643 else if (ref.isMsgId(dir))
642644 return serveMessage(req, dir, dirs.slice(1))
@@ -735,10 +737,11 @@
735737 })
736738 }
737739 }
738740
739- function serveTemplate(title, code, read) {
740- if (read === undefined) return serveTemplate.bind(this, title, code)
741+ function serveTemplate(title, code, req, read) {
742+ if (read === undefined) return serveTemplate.bind(this, title, code, req)
743+ var q = req && req._u.query.q && escapeHTML(req._u.query.q) || ''
741744 return cat([
742745 pull.values([
743746 [code || 200, {
744747 'Content-Type': 'text/html'
@@ -748,13 +751,16 @@
748751 '<link rel=stylesheet href="/static/styles.css"/>',
749752 '<link rel=stylesheet href="/highlight/github.css"/>',
750753 '</head>\n',
751754 '<body>',
752- '<header>',
755+ '<header><form action="/search" method="get">' +
753756 '<h1><a href="/">git ssb' +
754757 (ssbAppname != 'ssb' ? ' <sub>' + ssbAppname + '</sub>' : '') +
755- '</a></h1>',
756- '</header>',
758+ '</a> ' +
759+ '<input class="search-bar" name="q" size="60"' +
760+ ' placeholder="๐Ÿ”" value="' + q + '" />' +
761+ '</h1>',
762+ '</form></header>',
757763 '<article>']),
758764 renderTry(read),
759765 pull.once('<hr/></article></body></html>')
760766 ])
@@ -794,9 +800,9 @@
794800 }
795801
796802 /* Feed */
797803
798- function renderFeed(req, feedId) {
804+ function renderFeed(req, feedId, filter) {
799805 var query = req._u.query
800806 var opts = {
801807 reverse: !query.forwards,
802808 lt: query.lt && +query.lt || Date.now(),
@@ -807,31 +813,40 @@
807813 feedId ? ssb.createUserStream(opts) : ssb.createFeedStream(opts),
808814 pull.filter(function (msg) {
809815 return msg.value.content.type in msgTypes
810816 }),
817+ filter,
811818 pull.take(20),
812819 addAuthorName(about),
813820 query.forwards && pullReverse(),
814821 paginate(
815822 function (first, cb) {
816823 if (!query.lt && !query.gt) return cb(null, '')
817824 var gt = feedId ? first.value.sequence : first.value.timestamp + 1
818- var q = qs.stringify({
819- gt: gt,
820- forwards: 1
821- })
822- cb(null, '<a href="?' + q + '">Newer</a>')
825+ query.gt = gt
826+ query.forwards = 1
827+ delete query.lt
828+ cb(null, '<a href="?' + qs.stringify(query) + '">Newer</a>')
823829 },
824830 paramap(renderFeedItem, 8),
825831 function (last, cb) {
826- cb(null, '<a href="?' + qs.stringify({
827- lt: feedId ? last.value.sequence : last.value.timestamp - 1
828- }) + '">Older</a>')
832+ query.lt = feedId ? last.value.sequence : last.value.timestamp - 1
833+ delete query.gt
834+ delete query.forwards
835+ cb(null, '<a href="?' + qs.stringify(query) + '">Older</a>')
829836 },
830837 function (cb) {
831- cb(null, query.forwards ?
832- '<a href="?lt=' + (opts.gt + 1) + '">Older</a>' :
833- '<a href="?gt=' + (opts.lt - 1) + '&amp;forwards=1">Newer</a>')
838+ if (query.forwards) {
839+ delete query.gt
840+ delete query.forwards
841+ query.lt = opts.gt + 1
842+ } else {
843+ delete query.lt
844+ query.gt = opts.lt - 1
845+ query.forwards = 1
846+ }
847+ cb(null, '<a href="?' + qs.stringify(query) + '">' +
848+ (query.forwards ? 'Older' : 'Newer') + '</a>')
834849 }
835850 )
836851 )
837852 }
@@ -1126,9 +1141,9 @@
11261141 }
11271142 }
11281143
11291144 function serveRepoNotFound(id, err) {
1130- return serveTemplate('Repo not found', 404, pull.values([
1145+ return serveTemplate('Repo not found', 404)(pull.values([
11311146 '<h2>Repo not found</h2>',
11321147 '<p>Repo ' + id + ' was not found</p>',
11331148 '<pre>' + escapeHTML(err.stack) + '</pre>',
11341149 ]))
@@ -1240,8 +1255,29 @@
12401255 renderRepoReadme(repo, rev, path)
12411256 ]))
12421257 }
12431258
1259+ /* Search */
1260+
1261+ function serveSearch(req) {
1262+ var q = String(req._u.query.q || '')
1263+ if (!q) return serveIndex(req)
1264+ var qId = q.replace(/^ssb:\/*/, '')
1265+ if (ref.type(qId))
1266+ return serveRedirect(encodeURI(qId))
1267+
1268+ var search = new RegExp(q, 'i')
1269+ return serveTemplate('git ssb search', 200, req)(
1270+ renderFeed(req, null, pull.filter(function (msg) {
1271+ var c = msg.value.content
1272+ return (
1273+ search.test(msg.key) ||
1274+ c.text && search.test(c.text) ||
1275+ c.title && search.test(c.title))
1276+ }))
1277+ )
1278+ }
1279+
12441280 /* Repo activity */
12451281
12461282 function serveRepoActivity(repo, branch) {
12471283 return renderRepoPage(repo, 'activity', branch, cat([
@@ -1724,9 +1760,9 @@
17241760 })
17251761 }
17261762
17271763 function serveBlobNotFound(repoId, err) {
1728- return serveTemplate('Blob not found', 404, pull.values([
1764+ return serveTemplate('Blob not found', 404)(pull.values([
17291765 '<h2>Blob not found</h2>',
17301766 '<p>Blob in repo ' + link([repoId]) + ' was not found</p>',
17311767 '<pre>' + escapeHTML(err.stack) + '</pre>'
17321768 ]))
static/styles.cssView
@@ -47,8 +47,17 @@
4747 font-style: oblique;
4848 font-size: small;
4949 }
5050
51+header .search-bar {
52+ float: right;
53+ margin-top: .5em;
54+ margin-left: 1ex;
55+ max-width: 65%;
56+ padding: .25em;
57+ font-size: 10pt;
58+}
59+
5160 /* content */
5261
5362 section {
5463 background-color: white;

Built with git-ssb-web