git ssb

16+

Dominic / patchbay



Tree: 1c2926f12ff8e365e1d469f23180b50d50b9c749

Files: 1c2926f12ff8e365e1d469f23180b50d50b9c749 / app / html / scroller.js

1985 bytesRaw
1const nest = require('depnest')
2const { h } = require('mutant')
3
4exports.gives = nest('app.html.scroller')
5
6exports.create = function (api) {
7 return nest('app.html.scroller', Scroller)
8
9 function Scroller (opts = {}) {
10 const { prepend, content = null, append, classList = [], className = '', title = '', scrollIntoView } = 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, scrollIntoView)
24
25 return {
26 content: contentSection,
27 container
28 }
29 }
30}
31
32function keyscroll (content, scrollIntoView = false) {
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 (scrollIntoView) {
64 if (!el.scrollIntoViewIfNeeded && !el.scrollIntoView) return
65 ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el)
66 } else {
67 content.parentElement.scrollTop = el.offsetTop - content.parentElement.offsetTop - 10
68 }
69
70 if (el.focus) el.focus()
71 curMsgEl = el
72 }
73}
74

Built with git-ssb-web