git ssb

16+

Dominic / patchbay



Tree: 6aed4c72a3b36330eb67723b515b426ec7f5f06d

Files: 6aed4c72a3b36330eb67723b515b426ec7f5f06d / app / html / scroller.js

1726 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 h('section.top', prepend),
18 contentSection,
19 h('section.bottom', append)
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 selectChild((!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
56 return curMsgEl
57 }
58
59 function selectChild (el) {
60 if (!el) { return }
61
62 if (!el.scrollIntoViewIfNeeded && !el.scrollIntoView) return
63 ;(el.scrollIntoViewIfNeeded || el.scrollIntoView).call(el)
64 el.focus()
65 curMsgEl = el
66 }
67}
68

Built with git-ssb-web