forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-axis-element.coffee
72 lines (58 loc) · 2.81 KB
/
pane-axis-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
{CompositeDisposable} = require 'event-kit'
PaneResizeHandleElement = require './pane-resize-handle-element'
class PaneAxisElement extends HTMLElement
attachedCallback: ->
@subscriptions ?= @subscribeToModel()
@childAdded({child, index}) for child, index in @model.getChildren()
detachedCallback: ->
@subscriptions.dispose()
@subscriptions = null
@childRemoved({child}) for child in @model.getChildren()
initialize: (@model, {@views}) ->
throw new Error("Must pass a views parameter when initializing TextEditorElements") unless @views?
@subscriptions ?= @subscribeToModel()
@childAdded({child, index}) for child, index in @model.getChildren()
switch @model.getOrientation()
when 'horizontal'
@classList.add('horizontal', 'pane-row')
when 'vertical'
@classList.add('vertical', 'pane-column')
this
subscribeToModel: ->
subscriptions = new CompositeDisposable
subscriptions.add @model.onDidAddChild(@childAdded.bind(this))
subscriptions.add @model.onDidRemoveChild(@childRemoved.bind(this))
subscriptions.add @model.onDidReplaceChild(@childReplaced.bind(this))
subscriptions.add @model.observeFlexScale(@flexScaleChanged.bind(this))
subscriptions
isPaneResizeHandleElement: (element) ->
element?.nodeName.toLowerCase() is 'atom-pane-resize-handle'
childAdded: ({child, index}) ->
view = @views.getView(child)
@insertBefore(view, @children[index * 2])
prevElement = view.previousSibling
# if previous element is not pane resize element, then insert new resize element
if prevElement? and not @isPaneResizeHandleElement(prevElement)
resizeHandle = document.createElement('atom-pane-resize-handle')
@insertBefore(resizeHandle, view)
nextElement = view.nextSibling
# if next element isnot resize element, then insert new resize element
if nextElement? and not @isPaneResizeHandleElement(nextElement)
resizeHandle = document.createElement('atom-pane-resize-handle')
@insertBefore(resizeHandle, nextElement)
childRemoved: ({child}) ->
view = @views.getView(child)
siblingView = view.previousSibling
# make sure next sibling view is pane resize view
if siblingView? and @isPaneResizeHandleElement(siblingView)
siblingView.remove()
view.remove()
childReplaced: ({index, oldChild, newChild}) ->
focusedElement = document.activeElement if @hasFocus()
@childRemoved({child: oldChild, index})
@childAdded({child: newChild, index})
focusedElement?.focus() if document.activeElement is document.body
flexScaleChanged: (flexScale) -> @style.flexGrow = flexScale
hasFocus: ->
this is document.activeElement or @contains(document.activeElement)
module.exports = PaneAxisElement = document.registerElement 'atom-pane-axis', prototype: PaneAxisElement.prototype