git ssb

16+

Dominic / patchbay



Tree: f456ab9ca0eda1c5a4bf1abe84875e807e7ba7b6

Files: f456ab9ca0eda1c5a4bf1abe84875e807e7ba7b6 / app / html / scroller.js

1883 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 = '' } = 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
32function 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 content.parentElement.scrollTop = el.offsetTop - content.parentElement.offsetTop - 10
64 // if (!el.scrollIntoViewIfNeeded && !el.scrollIntoView) return
65 // ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el)
66 if (el.focus) el.focus()
67 curMsgEl = el
68 }
69}
70

Built with git-ssb-web