Files: 8e75010c68d2c079e58d57c19ae1919e2b878d31 / app / html / scroller.js
1431 bytesRaw
1 | const nest = require('depnest') |
2 | const { h } = require('mutant') |
3 | |
4 | exports.gives = nest('app.html.scroller') |
5 | |
6 | exports.create = function (api) { |
7 | return nest('app.html.scroller', Scroller) |
8 | |
9 | function Scroller ({ prepend = [], content = null, append = [] } = {}) { |
10 | content = content || h('section.content') |
11 | |
12 | const container = h('Scroller', { style: { overflow: 'auto' } }, [ |
13 | h('div.wrapper', [ |
14 | h('header', prepend), |
15 | content, |
16 | h('footer', append) |
17 | ]) |
18 | ]) |
19 | |
20 | container.scroll = keyscroll(content) |
21 | |
22 | return { |
23 | content, |
24 | container |
25 | } |
26 | } |
27 | } |
28 | |
29 | function keyscroll (container) { |
30 | var curMsgEl |
31 | |
32 | if (!container) return () => {} |
33 | |
34 | container.addEventListener('click', onActivateChild, false) |
35 | container.addEventListener('focus', onActivateChild, true) |
36 | |
37 | function onActivateChild (ev) { |
38 | for (var el = ev.target; el; el = el.parentNode) { |
39 | if (el.parentNode === container) { |
40 | curMsgEl = el |
41 | return |
42 | } |
43 | } |
44 | } |
45 | |
46 | function selectChild (el) { |
47 | if (!el) { return } |
48 | |
49 | ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el) |
50 | el.focus() |
51 | curMsgEl = el |
52 | } |
53 | |
54 | return function scroll (d) { |
55 | selectChild((!curMsgEl || d === 'first') ? container.firstChild |
56 | : d < 0 ? curMsgEl.previousElementSibling || container.firstChild |
57 | : d > 0 ? curMsgEl.nextElementSibling || container.lastChild |
58 | : curMsgEl) |
59 | |
60 | return curMsgEl |
61 | } |
62 | } |
63 |
Built with git-ssb-web