1 | "use strict";
|
2 | var __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 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var algorithm_1 = require("@phosphor/algorithm");
|
24 | var domutils_1 = require("@phosphor/domutils");
|
25 | var messaging_1 = require("@phosphor/messaging");
|
26 | var properties_1 = require("@phosphor/properties");
|
27 | var boxengine_1 = require("./boxengine");
|
28 | var layout_1 = require("./layout");
|
29 | var panellayout_1 = require("./panellayout");
|
30 | var widget_1 = require("./widget");
|
31 |
|
32 |
|
33 |
|
34 | var BoxLayout = (function (_super) {
|
35 | __extends(BoxLayout, _super);
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | function BoxLayout(options) {
|
42 | if (options === void 0) { options = {}; }
|
43 | var _this = _super.call(this) || this;
|
44 | _this._fixed = 0;
|
45 | _this._spacing = 4;
|
46 | _this._dirty = false;
|
47 | _this._sizers = [];
|
48 | _this._items = [];
|
49 | _this._box = null;
|
50 | _this._alignment = 'start';
|
51 | _this._direction = 'top-to-bottom';
|
52 | if (options.direction !== undefined) {
|
53 | _this._direction = options.direction;
|
54 | }
|
55 | if (options.alignment !== undefined) {
|
56 | _this._alignment = options.alignment;
|
57 | }
|
58 | if (options.spacing !== undefined) {
|
59 | _this._spacing = Private.clampSpacing(options.spacing);
|
60 | }
|
61 | return _this;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | BoxLayout.prototype.dispose = function () {
|
67 |
|
68 | algorithm_1.each(this._items, function (item) { item.dispose(); });
|
69 |
|
70 | this._box = null;
|
71 | this._items.length = 0;
|
72 | this._sizers.length = 0;
|
73 |
|
74 | _super.prototype.dispose.call(this);
|
75 | };
|
76 | Object.defineProperty(BoxLayout.prototype, "direction", {
|
77 | |
78 |
|
79 |
|
80 | get: function () {
|
81 | return this._direction;
|
82 | },
|
83 | |
84 |
|
85 |
|
86 | set: function (value) {
|
87 | if (this._direction === value) {
|
88 | return;
|
89 | }
|
90 | this._direction = value;
|
91 | if (!this.parent) {
|
92 | return;
|
93 | }
|
94 | this.parent.dataset['direction'] = value;
|
95 | this.parent.fit();
|
96 | },
|
97 | enumerable: true,
|
98 | configurable: true
|
99 | });
|
100 | Object.defineProperty(BoxLayout.prototype, "alignment", {
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | get: function () {
|
111 | return this._alignment;
|
112 | },
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | set: function (value) {
|
123 | if (this._alignment === value) {
|
124 | return;
|
125 | }
|
126 | this._alignment = value;
|
127 | if (!this.parent) {
|
128 | return;
|
129 | }
|
130 | this.parent.dataset['alignment'] = value;
|
131 | this.parent.update();
|
132 | },
|
133 | enumerable: true,
|
134 | configurable: true
|
135 | });
|
136 | Object.defineProperty(BoxLayout.prototype, "spacing", {
|
137 | |
138 |
|
139 |
|
140 | get: function () {
|
141 | return this._spacing;
|
142 | },
|
143 | |
144 |
|
145 |
|
146 | set: function (value) {
|
147 | value = Private.clampSpacing(value);
|
148 | if (this._spacing === value) {
|
149 | return;
|
150 | }
|
151 | this._spacing = value;
|
152 | if (!this.parent) {
|
153 | return;
|
154 | }
|
155 | this.parent.fit();
|
156 | },
|
157 | enumerable: true,
|
158 | configurable: true
|
159 | });
|
160 | |
161 |
|
162 |
|
163 | BoxLayout.prototype.init = function () {
|
164 | this.parent.dataset['direction'] = this.direction;
|
165 | this.parent.dataset['alignment'] = this.alignment;
|
166 | _super.prototype.init.call(this);
|
167 | };
|
168 | |
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | BoxLayout.prototype.attachWidget = function (index, widget) {
|
179 |
|
180 | algorithm_1.ArrayExt.insert(this._items, index, new layout_1.LayoutItem(widget));
|
181 |
|
182 | algorithm_1.ArrayExt.insert(this._sizers, index, new boxengine_1.BoxSizer());
|
183 |
|
184 | if (this.parent.isAttached) {
|
185 | messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.BeforeAttach);
|
186 | }
|
187 |
|
188 | this.parent.node.appendChild(widget.node);
|
189 |
|
190 | if (this.parent.isAttached) {
|
191 | messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.AfterAttach);
|
192 | }
|
193 |
|
194 | this.parent.fit();
|
195 | };
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | BoxLayout.prototype.moveWidget = function (fromIndex, toIndex, widget) {
|
209 |
|
210 | algorithm_1.ArrayExt.move(this._items, fromIndex, toIndex);
|
211 |
|
212 | algorithm_1.ArrayExt.move(this._sizers, fromIndex, toIndex);
|
213 |
|
214 | this.parent.update();
|
215 | };
|
216 | |
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | BoxLayout.prototype.detachWidget = function (index, widget) {
|
227 |
|
228 | var item = algorithm_1.ArrayExt.removeAt(this._items, index);
|
229 |
|
230 | algorithm_1.ArrayExt.removeAt(this._sizers, index);
|
231 |
|
232 | if (this.parent.isAttached) {
|
233 | messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.BeforeDetach);
|
234 | }
|
235 |
|
236 | this.parent.node.removeChild(widget.node);
|
237 |
|
238 | if (this.parent.isAttached) {
|
239 | messaging_1.MessageLoop.sendMessage(widget, widget_1.Widget.Msg.AfterDetach);
|
240 | }
|
241 |
|
242 | item.dispose();
|
243 |
|
244 | this.parent.fit();
|
245 | };
|
246 | |
247 |
|
248 |
|
249 | BoxLayout.prototype.onBeforeShow = function (msg) {
|
250 | _super.prototype.onBeforeShow.call(this, msg);
|
251 | this.parent.update();
|
252 | };
|
253 | |
254 |
|
255 |
|
256 | BoxLayout.prototype.onBeforeAttach = function (msg) {
|
257 | _super.prototype.onBeforeAttach.call(this, msg);
|
258 | this.parent.fit();
|
259 | };
|
260 | |
261 |
|
262 |
|
263 | BoxLayout.prototype.onChildShown = function (msg) {
|
264 | this.parent.fit();
|
265 | };
|
266 | |
267 |
|
268 |
|
269 | BoxLayout.prototype.onChildHidden = function (msg) {
|
270 | this.parent.fit();
|
271 | };
|
272 | |
273 |
|
274 |
|
275 | BoxLayout.prototype.onResize = function (msg) {
|
276 | if (this.parent.isVisible) {
|
277 | this._update(msg.width, msg.height);
|
278 | }
|
279 | };
|
280 | |
281 |
|
282 |
|
283 | BoxLayout.prototype.onUpdateRequest = function (msg) {
|
284 | if (this.parent.isVisible) {
|
285 | this._update(-1, -1);
|
286 | }
|
287 | };
|
288 | |
289 |
|
290 |
|
291 | BoxLayout.prototype.onFitRequest = function (msg) {
|
292 | if (this.parent.isAttached) {
|
293 | this._fit();
|
294 | }
|
295 | };
|
296 | |
297 |
|
298 |
|
299 | BoxLayout.prototype._fit = function () {
|
300 |
|
301 | var nVisible = 0;
|
302 | for (var i = 0, n = this._items.length; i < n; ++i) {
|
303 | nVisible += +!this._items[i].isHidden;
|
304 | }
|
305 |
|
306 | this._fixed = this._spacing * Math.max(0, nVisible - 1);
|
307 |
|
308 | var horz = Private.isHorizontal(this._direction);
|
309 | var minW = horz ? this._fixed : 0;
|
310 | var minH = horz ? 0 : this._fixed;
|
311 |
|
312 | for (var i = 0, n = this._items.length; i < n; ++i) {
|
313 |
|
314 | var item = this._items[i];
|
315 | var sizer = this._sizers[i];
|
316 |
|
317 | if (item.isHidden) {
|
318 | sizer.minSize = 0;
|
319 | sizer.maxSize = 0;
|
320 | continue;
|
321 | }
|
322 |
|
323 | item.fit();
|
324 |
|
325 | sizer.sizeHint = BoxLayout.getSizeBasis(item.widget);
|
326 | sizer.stretch = BoxLayout.getStretch(item.widget);
|
327 |
|
328 | if (horz) {
|
329 | sizer.minSize = item.minWidth;
|
330 | sizer.maxSize = item.maxWidth;
|
331 | minW += item.minWidth;
|
332 | minH = Math.max(minH, item.minHeight);
|
333 | }
|
334 | else {
|
335 | sizer.minSize = item.minHeight;
|
336 | sizer.maxSize = item.maxHeight;
|
337 | minH += item.minHeight;
|
338 | minW = Math.max(minW, item.minWidth);
|
339 | }
|
340 | }
|
341 |
|
342 | var box = this._box = domutils_1.ElementExt.boxSizing(this.parent.node);
|
343 | minW += box.horizontalSum;
|
344 | minH += box.verticalSum;
|
345 |
|
346 | var style = this.parent.node.style;
|
347 | style.minWidth = minW + "px";
|
348 | style.minHeight = minH + "px";
|
349 |
|
350 | this._dirty = true;
|
351 |
|
352 |
|
353 | if (this.parent.parent) {
|
354 | messaging_1.MessageLoop.sendMessage(this.parent.parent, widget_1.Widget.Msg.FitRequest);
|
355 | }
|
356 |
|
357 |
|
358 | if (this._dirty) {
|
359 | messaging_1.MessageLoop.sendMessage(this.parent, widget_1.Widget.Msg.UpdateRequest);
|
360 | }
|
361 | };
|
362 | |
363 |
|
364 |
|
365 |
|
366 |
|
367 | BoxLayout.prototype._update = function (offsetWidth, offsetHeight) {
|
368 |
|
369 | this._dirty = false;
|
370 |
|
371 | var nVisible = 0;
|
372 | for (var i = 0, n = this._items.length; i < n; ++i) {
|
373 | nVisible += +!this._items[i].isHidden;
|
374 | }
|
375 |
|
376 | if (nVisible === 0) {
|
377 | return;
|
378 | }
|
379 |
|
380 | if (offsetWidth < 0) {
|
381 | offsetWidth = this.parent.node.offsetWidth;
|
382 | }
|
383 | if (offsetHeight < 0) {
|
384 | offsetHeight = this.parent.node.offsetHeight;
|
385 | }
|
386 |
|
387 | if (!this._box) {
|
388 | this._box = domutils_1.ElementExt.boxSizing(this.parent.node);
|
389 | }
|
390 |
|
391 | var top = this._box.paddingTop;
|
392 | var left = this._box.paddingLeft;
|
393 | var width = offsetWidth - this._box.horizontalSum;
|
394 | var height = offsetHeight - this._box.verticalSum;
|
395 |
|
396 | var delta;
|
397 | switch (this._direction) {
|
398 | case 'left-to-right':
|
399 | delta = boxengine_1.BoxEngine.calc(this._sizers, Math.max(0, width - this._fixed));
|
400 | break;
|
401 | case 'top-to-bottom':
|
402 | delta = boxengine_1.BoxEngine.calc(this._sizers, Math.max(0, height - this._fixed));
|
403 | break;
|
404 | case 'right-to-left':
|
405 | delta = boxengine_1.BoxEngine.calc(this._sizers, Math.max(0, width - this._fixed));
|
406 | left += width;
|
407 | break;
|
408 | case 'bottom-to-top':
|
409 | delta = boxengine_1.BoxEngine.calc(this._sizers, Math.max(0, height - this._fixed));
|
410 | top += height;
|
411 | break;
|
412 | default:
|
413 | throw 'unreachable';
|
414 | }
|
415 |
|
416 | var extra = 0;
|
417 | var offset = 0;
|
418 |
|
419 | if (delta > 0) {
|
420 | switch (this._alignment) {
|
421 | case 'start':
|
422 | break;
|
423 | case 'center':
|
424 | extra = 0;
|
425 | offset = delta / 2;
|
426 | break;
|
427 | case 'end':
|
428 | extra = 0;
|
429 | offset = delta;
|
430 | break;
|
431 | case 'justify':
|
432 | extra = delta / nVisible;
|
433 | offset = 0;
|
434 | break;
|
435 | default:
|
436 | throw 'unreachable';
|
437 | }
|
438 | }
|
439 |
|
440 | for (var i = 0, n = this._items.length; i < n; ++i) {
|
441 |
|
442 | var item = this._items[i];
|
443 |
|
444 | if (item.isHidden) {
|
445 | continue;
|
446 | }
|
447 |
|
448 | var size = this._sizers[i].size;
|
449 |
|
450 | switch (this._direction) {
|
451 | case 'left-to-right':
|
452 | item.update(left + offset, top, size + extra, height);
|
453 | left += size + extra + this._spacing;
|
454 | break;
|
455 | case 'top-to-bottom':
|
456 | item.update(left, top + offset, width, size + extra);
|
457 | top += size + extra + this._spacing;
|
458 | break;
|
459 | case 'right-to-left':
|
460 | item.update(left - offset - size - extra, top, size + extra, height);
|
461 | left -= size + extra + this._spacing;
|
462 | break;
|
463 | case 'bottom-to-top':
|
464 | item.update(left, top - offset - size - extra, width, size + extra);
|
465 | top -= size + extra + this._spacing;
|
466 | break;
|
467 | default:
|
468 | throw 'unreachable';
|
469 | }
|
470 | }
|
471 | };
|
472 | return BoxLayout;
|
473 | }(panellayout_1.PanelLayout));
|
474 | exports.BoxLayout = BoxLayout;
|
475 |
|
476 |
|
477 |
|
478 | (function (BoxLayout) {
|
479 | |
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 | function getStretch(widget) {
|
487 | return Private.stretchProperty.get(widget);
|
488 | }
|
489 | BoxLayout.getStretch = getStretch;
|
490 | |
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 | function setStretch(widget, value) {
|
498 | Private.stretchProperty.set(widget, value);
|
499 | }
|
500 | BoxLayout.setStretch = setStretch;
|
501 | |
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 | function getSizeBasis(widget) {
|
509 | return Private.sizeBasisProperty.get(widget);
|
510 | }
|
511 | BoxLayout.getSizeBasis = getSizeBasis;
|
512 | |
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 | function setSizeBasis(widget, value) {
|
520 | Private.sizeBasisProperty.set(widget, value);
|
521 | }
|
522 | BoxLayout.setSizeBasis = setSizeBasis;
|
523 | })(BoxLayout = exports.BoxLayout || (exports.BoxLayout = {}));
|
524 | exports.BoxLayout = BoxLayout;
|
525 |
|
526 |
|
527 |
|
528 | var Private;
|
529 | (function (Private) {
|
530 | |
531 |
|
532 |
|
533 | Private.stretchProperty = new properties_1.AttachedProperty({
|
534 | name: 'stretch',
|
535 | create: function () { return 0; },
|
536 | coerce: function (owner, value) { return Math.max(0, Math.floor(value)); },
|
537 | changed: onChildSizingChanged
|
538 | });
|
539 | |
540 |
|
541 |
|
542 | Private.sizeBasisProperty = new properties_1.AttachedProperty({
|
543 | name: 'sizeBasis',
|
544 | create: function () { return 0; },
|
545 | coerce: function (owner, value) { return Math.max(0, Math.floor(value)); },
|
546 | changed: onChildSizingChanged
|
547 | });
|
548 | |
549 |
|
550 |
|
551 | function isHorizontal(dir) {
|
552 | return dir === 'left-to-right' || dir === 'right-to-left';
|
553 | }
|
554 | Private.isHorizontal = isHorizontal;
|
555 | |
556 |
|
557 |
|
558 | function clampSpacing(value) {
|
559 | return Math.max(0, Math.floor(value));
|
560 | }
|
561 | Private.clampSpacing = clampSpacing;
|
562 | |
563 |
|
564 |
|
565 | function onChildSizingChanged(child) {
|
566 | if (child.parent && child.parent.layout instanceof BoxLayout) {
|
567 | child.parent.fit();
|
568 | }
|
569 | }
|
570 | })(Private || (Private = {}));
|