forked from atom/atom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpane-axis.coffee
More file actions
147 lines (111 loc) · 3.98 KB
/
pane-axis.coffee
File metadata and controls
147 lines (111 loc) · 3.98 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{Emitter, CompositeDisposable} = require 'event-kit'
{flatten} = require 'underscore-plus'
Model = require './model'
PaneAxisElement = require './pane-axis-element'
module.exports =
class PaneAxis extends Model
parent: null
container: null
orientation: null
@deserialize: (state, {deserializers, views}) ->
state.children = state.children.map (childState) ->
deserializers.deserialize(childState)
new this(state, views)
constructor: ({@orientation, children, flexScale}, @viewRegistry) ->
@emitter = new Emitter
@subscriptionsByChild = new WeakMap
@subscriptions = new CompositeDisposable
@children = []
if children?
@addChild(child) for child in children
@flexScale = flexScale ? 1
serialize: ->
deserializer: 'PaneAxis'
children: @children.map (child) -> child.serialize()
orientation: @orientation
flexScale: @flexScale
getElement: ->
@element ?= new PaneAxisElement().initialize(this, @viewRegistry)
getFlexScale: -> @flexScale
setFlexScale: (@flexScale) ->
@emitter.emit 'did-change-flex-scale', @flexScale
@flexScale
getParent: -> @parent
setParent: (@parent) -> @parent
getContainer: -> @container
setContainer: (container) ->
if container and container isnt @container
@container = container
child.setContainer(container) for child in @children
getOrientation: -> @orientation
getChildren: -> @children.slice()
getPanes: ->
flatten(@children.map (child) -> child.getPanes())
getItems: ->
flatten(@children.map (child) -> child.getItems())
onDidAddChild: (fn) ->
@emitter.on 'did-add-child', fn
onDidRemoveChild: (fn) ->
@emitter.on 'did-remove-child', fn
onDidReplaceChild: (fn) ->
@emitter.on 'did-replace-child', fn
onDidDestroy: (fn) ->
@emitter.once 'did-destroy', fn
onDidChangeFlexScale: (fn) ->
@emitter.on 'did-change-flex-scale', fn
observeFlexScale: (fn) ->
fn(@flexScale)
@onDidChangeFlexScale(fn)
addChild: (child, index=@children.length) ->
@children.splice(index, 0, child)
child.setParent(this)
child.setContainer(@container)
@subscribeToChild(child)
@emitter.emit 'did-add-child', {child, index}
adjustFlexScale: ->
# get current total flex scale of children
total = 0
total += child.getFlexScale() for child in @children
needTotal = @children.length
# set every child's flex scale by the ratio
for child in @children
child.setFlexScale(needTotal * child.getFlexScale() / total)
removeChild: (child, replacing=false) ->
index = @children.indexOf(child)
throw new Error("Removing non-existent child") if index is -1
@unsubscribeFromChild(child)
@children.splice(index, 1)
@adjustFlexScale()
@emitter.emit 'did-remove-child', {child, index}
@reparentLastChild() if not replacing and @children.length < 2
replaceChild: (oldChild, newChild) ->
@unsubscribeFromChild(oldChild)
@subscribeToChild(newChild)
newChild.setParent(this)
newChild.setContainer(@container)
index = @children.indexOf(oldChild)
@children.splice(index, 1, newChild)
@emitter.emit 'did-replace-child', {oldChild, newChild, index}
insertChildBefore: (currentChild, newChild) ->
index = @children.indexOf(currentChild)
@addChild(newChild, index)
insertChildAfter: (currentChild, newChild) ->
index = @children.indexOf(currentChild)
@addChild(newChild, index + 1)
reparentLastChild: ->
lastChild = @children[0]
lastChild.setFlexScale(@flexScale)
@parent.replaceChild(this, lastChild)
@destroy()
subscribeToChild: (child) ->
subscription = child.onDidDestroy => @removeChild(child)
@subscriptionsByChild.set(child, subscription)
@subscriptions.add(subscription)
unsubscribeFromChild: (child) ->
subscription = @subscriptionsByChild.get(child)
@subscriptions.remove(subscription)
subscription.dispose()
destroyed: ->
@subscriptions.dispose()
@emitter.emit 'did-destroy'
@emitter.dispose()