forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpane-axis-element.js
126 lines (110 loc) · 3.68 KB
/
pane-axis-element.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
const { CompositeDisposable } = require('event-kit');
require('./pane-resize-handle-element');
class PaneAxisElement extends HTMLElement {
connectedCallback() {
if (this.subscriptions == null) {
this.subscriptions = this.subscribeToModel();
}
this.model
.getChildren()
.map((child, index) => this.childAdded({ child, index }));
}
disconnectedCallback() {
this.subscriptions.dispose();
this.subscriptions = null;
this.model.getChildren().map(child => this.childRemoved({ child }));
}
initialize(model, viewRegistry) {
this.model = model;
this.viewRegistry = viewRegistry;
if (this.subscriptions == null) {
this.subscriptions = this.subscribeToModel();
}
const iterable = this.model.getChildren();
for (let index = 0; index < iterable.length; index++) {
const child = iterable[index];
this.childAdded({ child, index });
}
switch (this.model.getOrientation()) {
case 'horizontal':
this.classList.add('horizontal', 'pane-row');
break;
case 'vertical':
this.classList.add('vertical', 'pane-column');
break;
}
return this;
}
subscribeToModel() {
const subscriptions = new CompositeDisposable();
subscriptions.add(this.model.onDidAddChild(this.childAdded.bind(this)));
subscriptions.add(
this.model.onDidRemoveChild(this.childRemoved.bind(this))
);
subscriptions.add(
this.model.onDidReplaceChild(this.childReplaced.bind(this))
);
subscriptions.add(
this.model.observeFlexScale(this.flexScaleChanged.bind(this))
);
return subscriptions;
}
isPaneResizeHandleElement(element) {
return (
(element != null ? element.nodeName.toLowerCase() : undefined) ===
'atom-pane-resize-handle'
);
}
childAdded({ child, index }) {
let resizeHandle;
const view = this.viewRegistry.getView(child);
this.insertBefore(view, this.children[index * 2]);
const prevElement = view.previousSibling;
// if previous element is not pane resize element, then insert new resize element
if (prevElement != null && !this.isPaneResizeHandleElement(prevElement)) {
resizeHandle = document.createElement('atom-pane-resize-handle');
this.insertBefore(resizeHandle, view);
}
const nextElement = view.nextSibling;
// if next element isnot resize element, then insert new resize element
if (nextElement != null && !this.isPaneResizeHandleElement(nextElement)) {
resizeHandle = document.createElement('atom-pane-resize-handle');
return this.insertBefore(resizeHandle, nextElement);
}
}
childRemoved({ child }) {
const view = this.viewRegistry.getView(child);
const siblingView = view.previousSibling;
// make sure next sibling view is pane resize view
if (siblingView != null && this.isPaneResizeHandleElement(siblingView)) {
siblingView.remove();
}
return view.remove();
}
childReplaced({ index, oldChild, newChild }) {
let focusedElement;
if (this.hasFocus()) {
focusedElement = document.activeElement;
}
this.childRemoved({ child: oldChild, index });
this.childAdded({ child: newChild, index });
if (document.activeElement === document.body) {
return focusedElement != null ? focusedElement.focus() : undefined;
}
}
flexScaleChanged(flexScale) {
this.style.flexGrow = flexScale;
}
hasFocus() {
return (
this === document.activeElement || this.contains(document.activeElement)
);
}
}
window.customElements.define('atom-pane-axis', PaneAxisElement);
function createPaneAxisElement() {
return document.createElement('atom-pane-axis');
}
module.exports = {
createPaneAxisElement
};