Files: 2ed843ec43ce4c8195cf646b049ce93812ab2a5e / app / html / scroller.js
1773 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 (opts = {}) { |
10 | const { prepend, content = null, append, classList = [], className = '', title = '' } = opts |
11 | |
12 | const contentSection = h('section.content', { title: '' }, content) |
13 | |
14 | const container = h('Scroller', |
15 | { classList, className, title, style: { 'overflow-y': 'scroll', 'overflow-x': 'auto' } }, |
16 | [ |
17 | prepend ? h('section.top', prepend) : null, |
18 | contentSection, |
19 | append ? h('section.bottom', append) : null |
20 | ] |
21 | ) |
22 | |
23 | container.scroll = keyscroll(contentSection) |
24 | |
25 | return { |
26 | content: contentSection, |
27 | container |
28 | } |
29 | } |
30 | } |
31 | |
32 | function keyscroll (content) { |
33 | var curMsgEl |
34 | |
35 | if (!content) return () => {} |
36 | |
37 | content.addEventListener('click', onActivateChild, false) |
38 | content.addEventListener('focus', onActivateChild, true) |
39 | |
40 | function onActivateChild (ev) { |
41 | for (var el = ev.target; el; el = el.parentNode) { |
42 | if (el.parentNode === content) { |
43 | curMsgEl = el |
44 | return |
45 | } |
46 | } |
47 | } |
48 | |
49 | return function scroll (d) { |
50 | const child = (!curMsgEl || d === 'first') ? content.firstChild |
51 | : (!curMsgEl || d === 'last') ? content.lastChild |
52 | : d < 0 ? curMsgEl.previousElementSibling || content.firstChild |
53 | : d > 0 ? curMsgEl.nextElementSibling || content.lastChild |
54 | : curMsgEl |
55 | selectChild(child) |
56 | |
57 | return curMsgEl |
58 | } |
59 | |
60 | function selectChild (el) { |
61 | if (!el) { return } |
62 | |
63 | if (!el.scrollIntoViewIfNeeded && !el.scrollIntoView) return |
64 | ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el) |
65 | el.focus() |
66 | curMsgEl = el |
67 | } |
68 | } |
69 |
Built with git-ssb-web