Files: ddabf06774e3c5a4d1e76831bc096791b8795042 / app / page / blogs.js
2287 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | // const pull = require('pull-stream') |
4 | const Scroller = require('mutant-scroll') |
5 | const next = require('pull-next-query') |
6 | |
7 | exports.gives = nest({ |
8 | 'app.html.menuItem': true, |
9 | 'app.page.blogs': true |
10 | }) |
11 | |
12 | exports.needs = nest({ |
13 | 'app.sync.goTo': 'first', |
14 | 'message.html.render': 'first', |
15 | 'sbot.pull.stream': 'first' |
16 | }) |
17 | |
18 | exports.create = function (api) { |
19 | return nest({ |
20 | 'app.html.menuItem': menuItem, |
21 | 'app.page.blogs': blogsPage |
22 | }) |
23 | |
24 | function menuItem () { |
25 | return h('a', { |
26 | 'ev-click': () => api.app.sync.goTo({ page: 'blogs' }) |
27 | }, '/blogs') |
28 | } |
29 | |
30 | function blogsPage (location) { |
31 | const createStream = (opts) => { |
32 | const query = [{ |
33 | $filter: { |
34 | timestamp: { $gt: 0 }, |
35 | value: { |
36 | content: { type: 'blog' } |
37 | } |
38 | } |
39 | }] |
40 | return api.sbot.pull.stream(server => { |
41 | return next(server.query.read, Object.assign({}, { limit: 100, query }, opts), ['timestamp']) |
42 | }) |
43 | } |
44 | var page = Scroller({ |
45 | classList: ['Blogs'], |
46 | streamToTop: createStream({ live: true, old: false }), |
47 | streamToBottom: createStream({ reverse: true }), |
48 | render: api.message.html.render |
49 | }) |
50 | |
51 | page.title = '/blogs' |
52 | page.scroll = keyscroll(page.querySelector('section.content')) |
53 | return page |
54 | } |
55 | } |
56 | |
57 | // copied from app.html.scroller |
58 | function keyscroll (content) { |
59 | var curMsgEl |
60 | |
61 | if (!content) return () => {} |
62 | |
63 | content.addEventListener('click', onActivateChild, false) |
64 | content.addEventListener('focus', onActivateChild, true) |
65 | |
66 | function onActivateChild (ev) { |
67 | for (var el = ev.target; el; el = el.parentNode) { |
68 | if (el.parentNode === content) { |
69 | curMsgEl = el |
70 | return |
71 | } |
72 | } |
73 | } |
74 | |
75 | return function scroll (d) { |
76 | selectChild((!curMsgEl || d === 'first') ? content.firstChild |
77 | : d < 0 ? curMsgEl.previousElementSibling || content.firstChild |
78 | : d > 0 ? curMsgEl.nextElementSibling || content.lastChild |
79 | : curMsgEl) |
80 | |
81 | return curMsgEl |
82 | } |
83 | |
84 | function selectChild (el) { |
85 | if (!el) { return } |
86 | |
87 | if (!el.scrollIntoViewIfNeeded && !el.scrollIntoView) return |
88 | ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el) |
89 | el.focus() |
90 | curMsgEl = el |
91 | } |
92 | } |
93 |
Built with git-ssb-web