Files: 35ee32eefff5c36f236876e4918c46cbeb22ef6d / app / html / scroller.js
2077 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-step') |
6 | |
7 | exports.gives = nest('app.html.scroller') |
8 | |
9 | exports.needs = nest({ |
10 | 'message.html.render': 'first' |
11 | }) |
12 | |
13 | exports.create = function (api) { |
14 | return nest('app.html.scroller', createScroller) |
15 | |
16 | function createScroller (opts = {}) { |
17 | const { |
18 | stream, |
19 | filter = () => pull.filter((msg) => true), |
20 | } = opts |
21 | |
22 | const streamToTop = pull( |
23 | next(stream, {old: false, limit: 100, property: ['value', 'timestamp']}), |
24 | filter() // is a pull-stream through |
25 | ) |
26 | |
27 | const streamToBottom = pull( |
28 | next(stream, {reverse: true, limit: 100, live: false, property: ['value', 'timestamp']}), |
29 | filter() |
30 | ) |
31 | |
32 | return Scroller(Object.assign({}, opts, { streamToTop, streamToBottom })) |
33 | // valid Scroller opts : see github.com/mixmix/mutant-scroll |
34 | // classList = [], |
35 | // prepend = [], |
36 | // append = [], |
37 | // streamToTop, |
38 | // streamToBottom, |
39 | // render, |
40 | // updateTop = updateTopDefault, |
41 | // updateBottom = updateBottomDefault, |
42 | // store = MutantArray(), |
43 | // cb = (err) => { if (err) throw err } |
44 | } |
45 | } |
46 | |
47 | function keyscroll (content) { |
48 | var curMsgEl |
49 | |
50 | if (!content) return () => {} |
51 | |
52 | content.addEventListener('click', onActivateChild, false) |
53 | content.addEventListener('focus', onActivateChild, true) |
54 | |
55 | function onActivateChild (ev) { |
56 | for (var el = ev.target; el; el = el.parentNode) { |
57 | if (el.parentNode === content) { |
58 | curMsgEl = el |
59 | return |
60 | } |
61 | } |
62 | } |
63 | |
64 | function selectChild (el) { |
65 | if (!el) { return } |
66 | |
67 | ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el) |
68 | el.focus() |
69 | curMsgEl = el |
70 | } |
71 | |
72 | return function scroll (d) { |
73 | selectChild((!curMsgEl || d === 'first') ? content.firstChild |
74 | : d < 0 ? curMsgEl.previousElementSibling || content.firstChild |
75 | : d > 0 ? curMsgEl.nextElementSibling || content.lastChild |
76 | : curMsgEl) |
77 | |
78 | return curMsgEl |
79 | } |
80 | } |
81 |
Built with git-ssb-web