-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-resize-handle-element.coffee
72 lines (60 loc) · 2.77 KB
/
pane-resize-handle-element.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
class PaneResizeHandleElement extends HTMLElement
createdCallback: ->
@resizePane = @resizePane.bind(this)
@resizeStopped = @resizeStopped.bind(this)
@subscribeToDOMEvents()
subscribeToDOMEvents: ->
@addEventListener 'dblclick', @resizeToFitContent.bind(this)
@addEventListener 'mousedown', @resizeStarted.bind(this)
attachedCallback: ->
# For some reason Chromium 58 is firing the attached callback after the
# element has been detached, so we ignore the callback when a parent element
# can't be found.
if @parentElement
@isHorizontal = @parentElement.classList.contains("horizontal")
@classList.add if @isHorizontal then 'horizontal' else 'vertical'
detachedCallback: ->
@resizeStopped()
resizeToFitContent: ->
# clear flex-grow css style of both pane
@previousSibling?.model.setFlexScale(1)
@nextSibling?.model.setFlexScale(1)
resizeStarted: (e) ->
e.stopPropagation()
document.addEventListener 'mousemove', @resizePane
document.addEventListener 'mouseup', @resizeStopped
resizeStopped: ->
document.removeEventListener 'mousemove', @resizePane
document.removeEventListener 'mouseup', @resizeStopped
calcRatio: (ratio1, ratio2, total) ->
allRatio = ratio1 + ratio2
[total * ratio1 / allRatio, total * ratio2 / allRatio]
setFlexGrow: (prevSize, nextSize) ->
@prevModel = @previousSibling.model
@nextModel = @nextSibling.model
totalScale = @prevModel.getFlexScale() + @nextModel.getFlexScale()
flexGrows = @calcRatio(prevSize, nextSize, totalScale)
@prevModel.setFlexScale flexGrows[0]
@nextModel.setFlexScale flexGrows[1]
fixInRange: (val, minValue, maxValue) ->
Math.min(Math.max(val, minValue), maxValue)
resizePane: ({clientX, clientY, which}) ->
return @resizeStopped() unless which is 1
return @resizeStopped() unless @previousSibling? and @nextSibling?
if @isHorizontal
totalWidth = @previousSibling.clientWidth + @nextSibling.clientWidth
#get the left and right width after move the resize view
leftWidth = clientX - @previousSibling.getBoundingClientRect().left
leftWidth = @fixInRange(leftWidth, 0, totalWidth)
rightWidth = totalWidth - leftWidth
# set the flex grow by the ratio of left width and right width
# to change pane width
@setFlexGrow(leftWidth, rightWidth)
else
totalHeight = @previousSibling.clientHeight + @nextSibling.clientHeight
topHeight = clientY - @previousSibling.getBoundingClientRect().top
topHeight = @fixInRange(topHeight, 0, totalHeight)
bottomHeight = totalHeight - topHeight
@setFlexGrow(topHeight, bottomHeight)
module.exports = PaneResizeHandleElement =
document.registerElement 'atom-pane-resize-handle', prototype: PaneResizeHandleElement.prototype