forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathpanel-container-element.js
123 lines (104 loc) · 3.09 KB
/
panel-container-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
'use strict';
const { createFocusTrap } = require('focus-trap');
const { CompositeDisposable } = require('event-kit');
class PanelContainerElement extends HTMLElement {
constructor() {
super();
this.subscriptions = new CompositeDisposable();
}
connectedCallback() {
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,
delayInitialFocus: false
};
if (panel.autoFocus !== true) {
focusOptions.initialFocus = panel.autoFocus;
}
const modalFocusTrap = createFocusTrap(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();
}
}
}
}
window.customElements.define('atom-panel-container', PanelContainerElement);
function createPanelContainerElement() {
return document.createElement('atom-panel-container');
}
module.exports = {
createPanelContainerElement
};