UNPKG

dockview

Version:

Zero dependency layout manager supporting tabs, grids and splitviews with ReactJS support

328 lines 13.5 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SplitviewComponent = void 0; var lifecycle_1 = require("../lifecycle"); var splitview_1 = require("./core/splitview"); var events_1 = require("../events"); var component_api_1 = require("../api/component.api"); var componentFactory_1 = require("../panel/componentFactory"); /** * A high-level implementation of splitview that works using 'panels' */ var SplitviewComponent = /** @class */ (function (_super) { __extends(SplitviewComponent, _super); function SplitviewComponent(element, options) { var _this = _super.call(this) || this; _this.element = element; _this._disposable = new lifecycle_1.MutableDisposable(); _this.panels = new Map(); _this._onDidLayoutChange = new events_1.Emitter(); _this.onDidLayoutChange = _this._onDidLayoutChange.event; _this._options = options; if (!options.components) { options.components = {}; } if (!options.frameworkComponents) { options.frameworkComponents = {}; } _this.splitview = new splitview_1.Splitview(_this.element, options); _this.addDisposables(_this._disposable); return _this; } Object.defineProperty(SplitviewComponent.prototype, "options", { get: function () { return this._options; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "orientation", { get: function () { return this.splitview.orientation; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "splitview", { get: function () { return this._splitview; }, set: function (value) { var _this = this; this._splitview = value; this._disposable.value = new lifecycle_1.CompositeDisposable(this._splitview.onDidSashEnd(function () { _this._onDidLayoutChange.fire(undefined); })); }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "minimumSize", { get: function () { return this.splitview.minimumSize; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "maximumSize", { get: function () { return this.splitview.maximumSize; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "height", { get: function () { return this.splitview.orientation === splitview_1.Orientation.HORIZONTAL ? this.splitview.orthogonalSize : this.splitview.size; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "width", { get: function () { return this.splitview.orientation === splitview_1.Orientation.HORIZONTAL ? this.splitview.size : this.splitview.orthogonalSize; }, enumerable: false, configurable: true }); Object.defineProperty(SplitviewComponent.prototype, "length", { get: function () { return this.panels.size; }, enumerable: false, configurable: true }); SplitviewComponent.prototype.updateOptions = function (options) { var hasOrientationChanged = typeof options.orientation === 'string' && this.options.orientation !== options.orientation; this._options = __assign(__assign({}, this.options), options); if (hasOrientationChanged) { this.splitview.orientation = options.orientation; } this.splitview.layout(this.splitview.size, this.splitview.orthogonalSize); }; SplitviewComponent.prototype.focus = function () { var _a; (_a = this._activePanel) === null || _a === void 0 ? void 0 : _a.focus(); }; SplitviewComponent.prototype.movePanel = function (from, to) { this.splitview.moveView(from, to); }; SplitviewComponent.prototype.setVisible = function (panel, visible) { var index = this.getPanels().indexOf(panel); this.splitview.setViewVisible(index, visible); }; SplitviewComponent.prototype.setActive = function (view, skipFocus) { this._activePanel = view; this.getPanels() .filter(function (v) { return v !== view; }) .forEach(function (v) { v.api._onDidActiveChange.fire({ isActive: false }); if (!skipFocus) { v.focus(); } }); view.api._onDidActiveChange.fire({ isActive: true }); if (!skipFocus) { view.focus(); } }; SplitviewComponent.prototype.getPanels = function () { return this.splitview.getViews(); }; SplitviewComponent.prototype.removePanel = function (panel, sizing) { var disposable = this.panels.get(panel.id); disposable === null || disposable === void 0 ? void 0 : disposable.dispose(); this.panels.delete(panel.id); var index = this.getPanels().findIndex(function (_) { return _ === panel; }); this.splitview.removeView(index, sizing); var panels = this.getPanels(); if (panels.length > 0) { this.setActive(panels[panels.length - 1]); } }; SplitviewComponent.prototype.getPanel = function (id) { return this.getPanels().find(function (view) { return view.id === id; }); }; SplitviewComponent.prototype.addPanel = function (options) { if (this.panels.has(options.id)) { throw new Error("panel ".concat(options.id, " already exists")); } var view = (0, componentFactory_1.createComponent)(options.id, options.component, this.options.components || {}, this.options.frameworkComponents || {}, this.options.frameworkWrapper ? { createComponent: this.options.frameworkWrapper.createComponent, } : undefined); view.orientation = this.splitview.orientation; view.init({ params: options.params || {}, minimumSize: options.minimumSize, maximumSize: options.maximumSize, snap: options.snap, priority: options.priority, containerApi: new component_api_1.SplitviewApi(this), }); var size = typeof options.size === 'number' ? options.size : splitview_1.Sizing.Distribute; var index = typeof options.index === 'number' ? options.index : undefined; this.splitview.addView(view, size, index); this.doAddView(view); this.setActive(view); }; /** * Resize the layout to fit the parent container */ SplitviewComponent.prototype.resizeToFit = function () { if (!this.element.parentElement) { return; } var _a = this.element.parentElement.getBoundingClientRect(), width = _a.width, height = _a.height; this.layout(width, height); }; SplitviewComponent.prototype.layout = function (width, height) { var _a = __read(this.splitview.orientation === splitview_1.Orientation.HORIZONTAL ? [width, height] : [height, width], 2), size = _a[0], orthogonalSize = _a[1]; this.splitview.layout(size, orthogonalSize); }; SplitviewComponent.prototype.doAddView = function (view) { var _this = this; var disposable = view.api.onDidFocusChange(function (event) { if (!event.isFocused) { return; } _this.setActive(view, true); }); this.panels.set(view.id, disposable); }; SplitviewComponent.prototype.toJSON = function () { var _this = this; var _a; var views = this.splitview .getViews() .map(function (view, i) { var size = _this.splitview.getViewSize(i); return { size: size, data: view.toJSON(), snap: !!view.snap, priority: view.priority, }; }); return { views: views, activeView: (_a = this._activePanel) === null || _a === void 0 ? void 0 : _a.id, size: this.splitview.size, orientation: this.splitview.orientation, }; }; SplitviewComponent.prototype.fromJSON = function (serializedSplitview, deferComponentLayout) { var _this = this; if (deferComponentLayout === void 0) { deferComponentLayout = false; } var views = serializedSplitview.views, orientation = serializedSplitview.orientation, size = serializedSplitview.size, activeView = serializedSplitview.activeView; this.splitview.dispose(); var queue = []; this.splitview = new splitview_1.Splitview(this.element, { orientation: orientation, proportionalLayout: this.options.proportionalLayout, descriptor: { size: size, views: views.map(function (view) { var data = view.data; if (_this.panels.has(data.id)) { throw new Error("panel ".concat(data.id, " already exists")); } var panel = (0, componentFactory_1.createComponent)(data.id, data.component, _this.options.components || {}, _this.options.frameworkComponents || {}, _this.options.frameworkWrapper ? { createComponent: _this.options.frameworkWrapper .createComponent, } : undefined); queue.push(function () { panel.init({ params: data.params || {}, minimumSize: data.minimumSize, maximumSize: data.maximumSize, snap: view.snap, priority: view.priority, containerApi: new component_api_1.SplitviewApi(_this), }); }); panel.orientation = orientation; _this.doAddView(panel); return { size: view.size, view: panel }; }), }, }); this.layout(this.width, this.height); if (deferComponentLayout) { setTimeout(function () { queue.forEach(function (f) { return f(); }); }, 0); } else { queue.forEach(function (f) { return f(); }); } if (typeof activeView === 'string') { var panel = this.getPanel(activeView); if (panel) { this.setActive(panel); } } }; SplitviewComponent.prototype.dispose = function () { Array.from(this.panels.values()).forEach(function (value) { value.dispose(); }); this.panels.clear(); _super.prototype.dispose.call(this); }; return SplitviewComponent; }(lifecycle_1.CompositeDisposable)); exports.SplitviewComponent = SplitviewComponent; //# sourceMappingURL=splitviewComponent.js.map