UNPKG

7.44 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16/*-----------------------------------------------------------------------------
17| Copyright (c) 2014-2017, PhosphorJS Contributors
18|
19| Distributed under the terms of the BSD 3-Clause License.
20|
21| The full license is in the file LICENSE, distributed with this software.
22|----------------------------------------------------------------------------*/
23var algorithm_1 = require("@phosphor/algorithm");
24var messaging_1 = require("@phosphor/messaging");
25var layout_1 = require("./layout");
26var widget_1 = require("./widget");
27/**
28 * A concrete layout implementation which holds a single widget.
29 *
30 * #### Notes
31 * This class is useful for creating simple container widgets which
32 * hold a single child. The child should be positioned with CSS.
33 */
34var SingletonLayout = /** @class */ (function (_super) {
35 __extends(SingletonLayout, _super);
36 function SingletonLayout() {
37 var _this = _super !== null && _super.apply(this, arguments) || this;
38 _this._widget = null;
39 return _this;
40 }
41 /**
42 * Dispose of the resources held by the layout.
43 */
44 SingletonLayout.prototype.dispose = function () {
45 if (this._widget) {
46 var widget = this._widget;
47 this._widget = null;
48 widget.dispose();
49 }
50 _super.prototype.dispose.call(this);
51 };
52 Object.defineProperty(SingletonLayout.prototype, "widget", {
53 /**
54 * Get the child widget for the layout.
55 */
56 get: function () {
57 return this._widget;
58 },
59 /**
60 * Set the child widget for the layout.
61 *
62 * #### Notes
63 * Setting the child widget will cause the old child widget to be
64 * automatically disposed. If that is not desired, set the parent
65 * of the old child to `null` before assigning a new child.
66 */
67 set: function (widget) {
68 // Remove the widget from its current parent. This is a no-op
69 // if the widget's parent is already the layout parent widget.
70 if (widget) {
71 widget.parent = this.parent;
72 }
73 // Bail early if the widget does not change.
74 if (this._widget === widget) {
75 return;
76 }
77 // Dispose of the old child widget.
78 if (this._widget) {
79 this._widget.dispose();
80 }
81 // Update the internal widget.
82 this._widget = widget;
83 // Attach the new child widget if needed.
84 if (this.parent && widget) {
85 this.attachWidget(widget);
86 }
87 },
88 enumerable: true,
89 configurable: true
90 });
91 /**
92 * Create an iterator over the widgets in the layout.
93 *
94 * @returns A new iterator over the widgets in the layout.
95 */
96 SingletonLayout.prototype.iter = function () {
97 return this._widget ? algorithm_1.once(this._widget) : algorithm_1.empty();
98 };
99 /**
100 * Remove a widget from the layout.
101 *
102 * @param widget - The widget to remove from the layout.
103 *
104 * #### Notes
105 * A widget is automatically removed from the layout when its `parent`
106 * is set to `null`. This method should only be invoked directly when
107 * removing a widget from a layout which has yet to be installed on a
108 * parent widget.
109 *
110 * This method does *not* modify the widget's `parent`.
111 */
112 SingletonLayout.prototype.removeWidget = function (widget) {
113 // Bail early if the widget does not exist in the layout.
114 if (this._widget !== widget) {
115 return;
116 }
117 // Clear the internal widget.
118 this._widget = null;
119 // If the layout is parented, detach the widget from the DOM.
120 if (this.parent) {
121 this.detachWidget(widget);
122 }
123 };
124 /**
125 * Perform layout initialization which requires the parent widget.
126 */
127 SingletonLayout.prototype.init = function () {
128 var _this = this;
129 _super.prototype.init.call(this);
130 algorithm_1.each(this, function (widget) { _this.attachWidget(widget); });
131 };
132 /**
133 * Attach a widget to the parent's DOM node.
134 *
135 * @param index - The current index of the widget in the layout.
136 *
137 * @param widget - The widget to attach to the parent.
138 *
139 * #### Notes
140 * This method is called automatically by the single layout at the
141 * appropriate time. It should not be called directly by user code.
142 *
143 * The default implementation adds the widgets's node to the parent's
144 * node at the proper location, and sends the appropriate attach
145 * messages to the widget if the parent is attached to the DOM.
146 *
147 * Subclasses may reimplement this method to control how the widget's
148 * node is added to the parent's node.
149 */
150 SingletonLayout.prototype.attachWidget = function (widget) {
151 // Send a `'before-attach'` message if the parent is attached.
152 if (this.parent.isAttached) {
153 messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.BeforeAttach);
154 }
155 // Add the widget's node to the parent.
156 this.parent.node.appendChild(widget.node);
157 // Send an `'after-attach'` message if the parent is attached.
158 if (this.parent.isAttached) {
159 messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.AfterAttach);
160 }
161 };
162 /**
163 * Detach a widget from the parent's DOM node.
164 *
165 * @param widget - The widget to detach from the parent.
166 *
167 * #### Notes
168 * This method is called automatically by the single layout at the
169 * appropriate time. It should not be called directly by user code.
170 *
171 * The default implementation removes the widget's node from the
172 * parent's node, and sends the appropriate detach messages to the
173 * widget if the parent is attached to the DOM.
174 *
175 * Subclasses may reimplement this method to control how the widget's
176 * node is removed from the parent's node.
177 */
178 SingletonLayout.prototype.detachWidget = function (widget) {
179 // Send a `'before-detach'` message if the parent is attached.
180 if (this.parent.isAttached) {
181 messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.BeforeDetach);
182 }
183 // Remove the widget's node from the parent.
184 this.parent.node.removeChild(widget.node);
185 // Send an `'after-detach'` message if the parent is attached.
186 if (this.parent.isAttached) {
187 messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.AfterDetach);
188 }
189 };
190 return SingletonLayout;
191}(layout_1.Layout));
192exports.SingletonLayout = SingletonLayout;