forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanel-container.js
118 lines (100 loc) · 2.54 KB
/
panel-container.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
'use strict';
const { Emitter, CompositeDisposable } = require('event-kit');
const { createPanelContainerElement } = require('./panel-container-element');
module.exports = class PanelContainer {
constructor({ location, dock, viewRegistry } = {}) {
this.location = location;
this.emitter = new Emitter();
this.subscriptions = new CompositeDisposable();
this.panels = [];
this.dock = dock;
this.viewRegistry = viewRegistry;
}
destroy() {
for (let panel of this.getPanels()) {
panel.destroy();
}
this.subscriptions.dispose();
this.emitter.emit('did-destroy', this);
this.emitter.dispose();
}
getElement() {
if (!this.element) {
this.element = createPanelContainerElement().initialize(
this,
this.viewRegistry
);
}
return this.element;
}
/*
Section: Event Subscription
*/
onDidAddPanel(callback) {
return this.emitter.on('did-add-panel', callback);
}
onDidRemovePanel(callback) {
return this.emitter.on('did-remove-panel', callback);
}
onDidDestroy(callback) {
return this.emitter.once('did-destroy', callback);
}
/*
Section: Panels
*/
getLocation() {
return this.location;
}
isModal() {
return this.location === 'modal';
}
getPanels() {
return this.panels.slice();
}
addPanel(panel) {
this.subscriptions.add(panel.onDidDestroy(this.panelDestroyed.bind(this)));
const index = this.getPanelIndex(panel);
if (index === this.panels.length) {
this.panels.push(panel);
} else {
this.panels.splice(index, 0, panel);
}
this.emitter.emit('did-add-panel', { panel, index });
return panel;
}
panelForItem(item) {
for (let panel of this.panels) {
if (panel.getItem() === item) {
return panel;
}
}
return null;
}
panelDestroyed(panel) {
const index = this.panels.indexOf(panel);
if (index > -1) {
this.panels.splice(index, 1);
this.emitter.emit('did-remove-panel', { panel, index });
}
}
getPanelIndex(panel) {
const priority = panel.getPriority();
if (['bottom', 'right'].includes(this.location)) {
for (let i = this.panels.length - 1; i >= 0; i--) {
const p = this.panels[i];
if (priority < p.getPriority()) {
return i + 1;
}
}
return 0;
} else {
for (let i = 0; i < this.panels.length; i++) {
const p = this.panels[i];
if (priority < p.getPriority()) {
return i;
}
}
return this.panels.length;
}
}
};