forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpane-axis.js
208 lines (172 loc) · 5.06 KB
/
pane-axis.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const { Emitter, CompositeDisposable } = require('event-kit');
const { flatten } = require('underscore-plus');
const Model = require('./model');
const { createPaneAxisElement } = require('./pane-axis-element');
class PaneAxis extends Model {
static deserialize(state, { deserializers, views }) {
state.children = state.children.map(childState =>
deserializers.deserialize(childState)
);
return new PaneAxis(state, views);
}
constructor({ orientation, children, flexScale }, viewRegistry) {
super();
this.parent = null;
this.container = null;
this.orientation = orientation;
this.viewRegistry = viewRegistry;
this.emitter = new Emitter();
this.subscriptionsByChild = new WeakMap();
this.subscriptions = new CompositeDisposable();
this.flexScale = flexScale != null ? flexScale : 1;
this.children = [];
if (children) {
for (let child of children) {
this.addChild(child);
}
}
}
serialize() {
return {
deserializer: 'PaneAxis',
children: this.children.map(child => child.serialize()),
orientation: this.orientation,
flexScale: this.flexScale
};
}
getElement() {
if (!this.element) {
this.element = createPaneAxisElement().initialize(
this,
this.viewRegistry
);
}
return this.element;
}
getFlexScale() {
return this.flexScale;
}
setFlexScale(flexScale) {
this.flexScale = flexScale;
this.emitter.emit('did-change-flex-scale', this.flexScale);
return this.flexScale;
}
getParent() {
return this.parent;
}
setParent(parent) {
this.parent = parent;
return this.parent;
}
getContainer() {
return this.container;
}
setContainer(container) {
if (container && container !== this.container) {
this.container = container;
this.children.forEach(child => child.setContainer(container));
}
}
getOrientation() {
return this.orientation;
}
getChildren() {
return this.children.slice();
}
getPanes() {
return flatten(this.children.map(child => child.getPanes()));
}
getItems() {
return flatten(this.children.map(child => child.getItems()));
}
onDidAddChild(fn) {
return this.emitter.on('did-add-child', fn);
}
onDidRemoveChild(fn) {
return this.emitter.on('did-remove-child', fn);
}
onDidReplaceChild(fn) {
return this.emitter.on('did-replace-child', fn);
}
onDidDestroy(fn) {
return this.emitter.once('did-destroy', fn);
}
onDidChangeFlexScale(fn) {
return this.emitter.on('did-change-flex-scale', fn);
}
observeFlexScale(fn) {
fn(this.flexScale);
return this.onDidChangeFlexScale(fn);
}
addChild(child, index = this.children.length) {
this.children.splice(index, 0, child);
child.setParent(this);
child.setContainer(this.container);
this.subscribeToChild(child);
return this.emitter.emit('did-add-child', { child, index });
}
adjustFlexScale() {
// get current total flex scale of children
let total = 0;
for (var child of this.children) {
total += child.getFlexScale();
}
const needTotal = this.children.length;
// set every child's flex scale by the ratio
for (child of this.children) {
child.setFlexScale((needTotal * child.getFlexScale()) / total);
}
}
removeChild(child, replacing = false) {
const index = this.children.indexOf(child);
if (index === -1) {
throw new Error('Removing non-existent child');
}
this.unsubscribeFromChild(child);
this.children.splice(index, 1);
this.adjustFlexScale();
this.emitter.emit('did-remove-child', { child, index });
if (!replacing && this.children.length < 2) {
this.reparentLastChild();
}
}
replaceChild(oldChild, newChild) {
this.unsubscribeFromChild(oldChild);
this.subscribeToChild(newChild);
newChild.setParent(this);
newChild.setContainer(this.container);
const index = this.children.indexOf(oldChild);
this.children.splice(index, 1, newChild);
this.emitter.emit('did-replace-child', { oldChild, newChild, index });
}
insertChildBefore(currentChild, newChild) {
const index = this.children.indexOf(currentChild);
return this.addChild(newChild, index);
}
insertChildAfter(currentChild, newChild) {
const index = this.children.indexOf(currentChild);
return this.addChild(newChild, index + 1);
}
reparentLastChild() {
const lastChild = this.children[0];
lastChild.setFlexScale(this.flexScale);
this.parent.replaceChild(this, lastChild);
this.destroy();
}
subscribeToChild(child) {
const subscription = child.onDidDestroy(() => this.removeChild(child));
this.subscriptionsByChild.set(child, subscription);
this.subscriptions.add(subscription);
}
unsubscribeFromChild(child) {
const subscription = this.subscriptionsByChild.get(child);
this.subscriptions.remove(subscription);
subscription.dispose();
}
destroyed() {
this.subscriptions.dispose();
this.emitter.emit('did-destroy');
this.emitter.dispose();
}
}
module.exports = PaneAxis;