forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollbar-component.coffee
79 lines (63 loc) · 2.49 KB
/
scrollbar-component.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module.exports =
class ScrollbarComponent
constructor: ({@orientation, @onScroll}) ->
@domNode = document.createElement('div')
@domNode.classList.add "#{@orientation}-scrollbar"
@domNode.style['-webkit-transform'] = 'translateZ(0)' # See atom/atom#3559
@domNode.style.left = 0 if @orientation is 'horizontal'
@contentNode = document.createElement('div')
@contentNode.classList.add "scrollbar-content"
@domNode.appendChild(@contentNode)
@domNode.addEventListener 'scroll', @onScrollCallback
destroy: ->
@domNode.removeEventListener 'scroll', @onScrollCallback
@onScroll = null
getDomNode: ->
@domNode
updateSync: (state) ->
@oldState ?= {}
switch @orientation
when 'vertical'
@newState = state.verticalScrollbar
@updateVertical()
when 'horizontal'
@newState = state.horizontalScrollbar
@updateHorizontal()
if @newState.visible isnt @oldState.visible
if @newState.visible
@domNode.style.display = ''
else
@domNode.style.display = 'none'
@oldState.visible = @newState.visible
updateVertical: ->
if @newState.width isnt @oldState.width
@domNode.style.width = @newState.width + 'px'
@oldState.width = @newState.width
if @newState.bottom isnt @oldState.bottom
@domNode.style.bottom = @newState.bottom + 'px'
@oldState.bottom = @newState.bottom
if @newState.scrollHeight isnt @oldState.scrollHeight
@contentNode.style.height = @newState.scrollHeight + 'px'
@oldState.scrollHeight = @newState.scrollHeight
if @newState.scrollTop isnt @oldState.scrollTop
@domNode.scrollTop = @newState.scrollTop
@oldState.scrollTop = @newState.scrollTop
updateHorizontal: ->
if @newState.height isnt @oldState.height
@domNode.style.height = @newState.height + 'px'
@oldState.height = @newState.height
if @newState.right isnt @oldState.right
@domNode.style.right = @newState.right + 'px'
@oldState.right = @newState.right
if @newState.scrollWidth isnt @oldState.scrollWidth
@contentNode.style.width = @newState.scrollWidth + 'px'
@oldState.scrollWidth = @newState.scrollWidth
if @newState.scrollLeft isnt @oldState.scrollLeft
@domNode.scrollLeft = @newState.scrollLeft
@oldState.scrollLeft = @newState.scrollLeft
onScrollCallback: =>
switch @orientation
when 'vertical'
@onScroll(@domNode.scrollTop)
when 'horizontal'
@onScroll(@domNode.scrollLeft)