UNPKG

7.92 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { MessageLoop } from '@lumino/messaging';
4import { AccordionLayout, AccordionPanel, Widget } from '@lumino/widgets';
5import { caretDownIcon } from '../icon';
6/**
7 * Accordion panel layout that adds a toolbar in widget title if present.
8 */
9class AccordionToolbarLayout extends AccordionLayout {
10 constructor() {
11 super(...arguments);
12 this._toolbars = new WeakMap();
13 }
14 /**
15 * Insert a widget into the layout at the specified index.
16 *
17 * @param index - The index at which to insert the widget.
18 *
19 * @param widget - The widget to insert into the layout.
20 *
21 * #### Notes
22 * The index will be clamped to the bounds of the widgets.
23 *
24 * If the widget is already added to the layout, it will be moved.
25 *
26 * #### Undefined Behavior
27 * An `index` which is non-integral.
28 */
29 insertWidget(index, widget) {
30 if (widget.toolbar) {
31 this._toolbars.set(widget, widget.toolbar);
32 widget.toolbar.addClass('jp-AccordionPanel-toolbar');
33 }
34 super.insertWidget(index, widget);
35 }
36 /**
37 * Remove the widget at a given index from the layout.
38 *
39 * @param index - The index of the widget to remove.
40 *
41 * #### Notes
42 * A widget is automatically removed from the layout when its `parent`
43 * is set to `null`. This method should only be invoked directly when
44 * removing a widget from a layout which has yet to be installed on a
45 * parent widget.
46 *
47 * This method does *not* modify the widget's `parent`.
48 *
49 * #### Undefined Behavior
50 * An `index` which is non-integral.
51 */
52 removeWidgetAt(index) {
53 const widget = this.widgets[index];
54 super.removeWidgetAt(index);
55 // Remove the toolbar after the widget has `removeWidgetAt` will call `detachWidget`
56 if (widget && this._toolbars.has(widget)) {
57 this._toolbars.delete(widget);
58 }
59 }
60 /**
61 * Attach a widget to the parent's DOM node.
62 *
63 * @param index - The current index of the widget in the layout.
64 *
65 * @param widget - The widget to attach to the parent.
66 */
67 attachWidget(index, widget) {
68 super.attachWidget(index, widget);
69 const toolbar = this._toolbars.get(widget);
70 if (toolbar) {
71 // Send a `'before-attach'` message if the parent is attached.
72 if (this.parent.isAttached) {
73 MessageLoop.sendMessage(toolbar, Widget.Msg.BeforeAttach);
74 }
75 // Insert the toolbar in the title node.
76 this.titles[index].appendChild(toolbar.node);
77 // Send an `'after-attach'` message if the parent is attached.
78 if (this.parent.isAttached) {
79 MessageLoop.sendMessage(toolbar, Widget.Msg.AfterAttach);
80 }
81 }
82 }
83 /**
84 * Detach a widget from the parent's DOM node.
85 *
86 * @param index - The previous index of the widget in the layout.
87 *
88 * @param widget - The widget to detach from the parent.
89 */
90 detachWidget(index, widget) {
91 const toolbar = this._toolbars.get(widget);
92 if (toolbar) {
93 // Send a `'before-detach'` message if the parent is attached.
94 if (this.parent.isAttached) {
95 MessageLoop.sendMessage(toolbar, Widget.Msg.BeforeDetach);
96 }
97 // Remove the toolbar in the title node.
98 this.titles[index].removeChild(toolbar.node);
99 // Send an `'after-detach'` message if the parent is attached.
100 if (this.parent.isAttached) {
101 MessageLoop.sendMessage(toolbar, Widget.Msg.AfterDetach);
102 }
103 }
104 super.detachWidget(index, widget);
105 }
106 /**
107 * A message handler invoked on a `'before-attach'` message.
108 *
109 * #### Notes
110 * The default implementation of this method forwards the message
111 * to all widgets. It assumes all widget nodes are attached to the
112 * parent widget node.
113 *
114 * This may be reimplemented by subclasses as needed.
115 */
116 onBeforeAttach(msg) {
117 this.notifyToolbars(msg);
118 super.onBeforeAttach(msg);
119 }
120 /**
121 * A message handler invoked on an `'after-attach'` message.
122 *
123 * #### Notes
124 * The default implementation of this method forwards the message
125 * to all widgets. It assumes all widget nodes are attached to the
126 * parent widget node.
127 *
128 * This may be reimplemented by subclasses as needed.
129 */
130 onAfterAttach(msg) {
131 super.onAfterAttach(msg);
132 this.notifyToolbars(msg);
133 }
134 /**
135 * A message handler invoked on a `'before-detach'` message.
136 *
137 * #### Notes
138 * The default implementation of this method forwards the message
139 * to all widgets. It assumes all widget nodes are attached to the
140 * parent widget node.
141 *
142 * This may be reimplemented by subclasses as needed.
143 */
144 onBeforeDetach(msg) {
145 this.notifyToolbars(msg);
146 super.onBeforeDetach(msg);
147 }
148 /**
149 * A message handler invoked on an `'after-detach'` message.
150 *
151 * #### Notes
152 * The default implementation of this method forwards the message
153 * to all widgets. It assumes all widget nodes are attached to the
154 * parent widget node.
155 *
156 * This may be reimplemented by subclasses as needed.
157 */
158 onAfterDetach(msg) {
159 super.onAfterDetach(msg);
160 this.notifyToolbars(msg);
161 }
162 notifyToolbars(msg) {
163 this.widgets.forEach(widget => {
164 const toolbar = this._toolbars.get(widget);
165 if (toolbar) {
166 toolbar.processMessage(msg);
167 }
168 });
169 }
170}
171export var AccordionToolbar;
172(function (AccordionToolbar) {
173 /**
174 * Custom renderer for the SidePanel
175 */
176 class Renderer extends AccordionPanel.Renderer {
177 /**
178 * Render the collapse indicator for a section title.
179 *
180 * @param data - The data to use for rendering the section title.
181 *
182 * @returns A element representing the collapse indicator.
183 */
184 createCollapseIcon(data) {
185 const iconDiv = document.createElement('div');
186 caretDownIcon.element({
187 container: iconDiv
188 });
189 return iconDiv;
190 }
191 /**
192 * Render the element for a section title.
193 *
194 * @param data - The data to use for rendering the section title.
195 *
196 * @returns A element representing the section title.
197 */
198 createSectionTitle(data) {
199 const handle = super.createSectionTitle(data);
200 handle.classList.add('jp-AccordionPanel-title');
201 return handle;
202 }
203 }
204 AccordionToolbar.Renderer = Renderer;
205 AccordionToolbar.defaultRenderer = new Renderer();
206 /**
207 * Create an accordion layout for accordion panel with toolbar in the title.
208 *
209 * @param options Panel options
210 * @returns Panel layout
211 *
212 * #### Note
213 *
214 * Default titleSpace is 32 px (default var(--jp-private-toolbar-height) - but not styled)
215 */
216 function createLayout(options) {
217 var _a;
218 return (options.layout ||
219 new AccordionToolbarLayout({
220 renderer: options.renderer || AccordionToolbar.defaultRenderer,
221 orientation: options.orientation,
222 alignment: options.alignment,
223 spacing: options.spacing,
224 titleSpace: (_a = options.titleSpace) !== null && _a !== void 0 ? _a : 32
225 }));
226 }
227 AccordionToolbar.createLayout = createLayout;
228})(AccordionToolbar || (AccordionToolbar = {}));
229//# sourceMappingURL=accordiontoolbar.js.map
\No newline at end of file