Files: 6d06fba92397438506f8120c5a1ae2b56c895b64 / app / html / scroller.js
1404 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 = [], append = [] } = {}) { |
10 | const 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 | (el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el) |
49 | el.focus() |
50 | curMsgEl = el |
51 | } |
52 | |
53 | return function scroll (d) { |
54 | selectChild((!curMsgEl || d === 'first') ? container.firstChild |
55 | : d < 0 ? curMsgEl.previousElementSibling || container.firstChild |
56 | : d > 0 ? curMsgEl.nextElementSibling || container.lastChild |
57 | : curMsgEl) |
58 | |
59 | return curMsgEl |
60 | } |
61 | } |
62 |
Built with git-ssb-web