forked from atom/atom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanel-container-element.js
More file actions
115 lines (98 loc) · 2.92 KB
/
panel-container-element.js
File metadata and controls
115 lines (98 loc) · 2.92 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
'use strict';
const focusTrap = require('focus-trap');
const { CompositeDisposable } = require('event-kit');
class PanelContainerElement extends HTMLElement {
createdCallback() {
this.subscriptions = new CompositeDisposable();
}
attachedCallback() {
if (this.model.dock) {
this.model.dock.elementAttached();
}
}
initialize(model, viewRegistry) {
this.model = model;
this.viewRegistry = viewRegistry;
this.subscriptions.add(
this.model.onDidAddPanel(this.panelAdded.bind(this))
);
this.subscriptions.add(this.model.onDidDestroy(this.destroyed.bind(this)));
this.classList.add(this.model.getLocation());
// Add the dock.
if (this.model.dock != null) {
this.appendChild(this.model.dock.getElement());
}
return this;
}
getModel() {
return this.model;
}
panelAdded({ panel, index }) {
const panelElement = panel.getElement();
panelElement.classList.add(this.model.getLocation());
if (this.model.isModal()) {
panelElement.classList.add('overlay', 'from-top');
} else {
panelElement.classList.add(
'tool-panel',
`panel-${this.model.getLocation()}`
);
}
if (index >= this.childNodes.length) {
this.appendChild(panelElement);
} else {
const referenceItem = this.childNodes[index];
this.insertBefore(panelElement, referenceItem);
}
if (this.model.isModal()) {
this.hideAllPanelsExcept(panel);
this.subscriptions.add(
panel.onDidChangeVisible(visible => {
if (visible) {
this.hideAllPanelsExcept(panel);
}
})
);
if (panel.autoFocus) {
const focusOptions = {
// focus-trap will attempt to give focus to the first tabbable element
// on activation. If there aren't any tabbable elements,
// give focus to the panel element itself
fallbackFocus: panelElement,
// closing is handled by core Atom commands and this already deactivates
// on visibility changes
escapeDeactivates: false
};
if (panel.autoFocus !== true) {
focusOptions.initialFocus = panel.autoFocus;
}
const modalFocusTrap = focusTrap(panelElement, focusOptions);
this.subscriptions.add(
panel.onDidChangeVisible(visible => {
if (visible) {
modalFocusTrap.activate();
} else {
modalFocusTrap.deactivate();
}
})
);
}
}
}
destroyed() {
this.subscriptions.dispose();
if (this.parentNode != null) {
this.parentNode.removeChild(this);
}
}
hideAllPanelsExcept(excludedPanel) {
for (let panel of this.model.getPanels()) {
if (panel !== excludedPanel) {
panel.hide();
}
}
}
}
module.exports = document.registerElement('atom-panel-container', {
prototype: PanelContainerElement.prototype
});