UNPKG

sprotty

Version:

A next-gen framework for graphical views

144 lines 6.08 kB
"use strict"; /******************************************************************************** * Copyright (c) 2017-2018 TypeFox and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.configureLayout = exports.StatefulLayouter = exports.Layouter = exports.LayoutRegistry = void 0; const inversify_1 = require("inversify"); const geometry_1 = require("sprotty-protocol/lib/utils/geometry"); const types_1 = require("../../base/types"); const registry_1 = require("../../utils/registry"); const model_1 = require("./model"); const inversify_2 = require("../../utils/inversify"); let LayoutRegistry = class LayoutRegistry extends registry_1.InstanceRegistry { constructor(layouts = []) { super(); layouts.forEach(layout => { if (this.hasKey(layout.layoutKind)) { this.logger.warn('Layout kind is already defined: ', layout.layoutKind); } else { this.register(layout.layoutKind, layout.factory()); } }); } }; exports.LayoutRegistry = LayoutRegistry; __decorate([ (0, inversify_1.inject)(types_1.TYPES.ILogger), __metadata("design:type", Object) ], LayoutRegistry.prototype, "logger", void 0); exports.LayoutRegistry = LayoutRegistry = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.multiInject)(types_1.TYPES.LayoutRegistration)), __param(0, (0, inversify_1.optional)()), __metadata("design:paramtypes", [Array]) ], LayoutRegistry); let Layouter = class Layouter { layout(element2boundsData) { new StatefulLayouter(element2boundsData, this.layoutRegistry, this.logger).layout(); } }; exports.Layouter = Layouter; __decorate([ (0, inversify_1.inject)(types_1.TYPES.LayoutRegistry), __metadata("design:type", LayoutRegistry) ], Layouter.prototype, "layoutRegistry", void 0); __decorate([ (0, inversify_1.inject)(types_1.TYPES.ILogger), __metadata("design:type", Object) ], Layouter.prototype, "logger", void 0); exports.Layouter = Layouter = __decorate([ (0, inversify_1.injectable)() ], Layouter); class StatefulLayouter { constructor(element2boundsData, layoutRegistry, log) { this.element2boundsData = element2boundsData; this.layoutRegistry = layoutRegistry; this.log = log; this.toBeLayouted = []; element2boundsData.forEach((data, element) => { if ((0, model_1.isLayoutContainer)(element)) this.toBeLayouted.push(element); }); } getBoundsData(element) { let boundsData = this.element2boundsData.get(element); let bounds = element.bounds; if ((0, model_1.isLayoutContainer)(element) && this.toBeLayouted.indexOf(element) >= 0) { bounds = this.doLayout(element); } if (!boundsData) { boundsData = { bounds: bounds, boundsChanged: false, alignmentChanged: false }; this.element2boundsData.set(element, boundsData); } return boundsData; } layout() { while (this.toBeLayouted.length > 0) { const element = this.toBeLayouted[0]; this.doLayout(element); } } doLayout(element) { const index = this.toBeLayouted.indexOf(element); if (index >= 0) this.toBeLayouted.splice(index, 1); const layout = this.layoutRegistry.get(element.layout); if (layout) layout.layout(element, this); const boundsData = this.element2boundsData.get(element); if (boundsData !== undefined && boundsData.bounds !== undefined) { return boundsData.bounds; } else { this.log.error(element, 'Layout failed'); return geometry_1.Bounds.EMPTY; } } } exports.StatefulLayouter = StatefulLayouter; function configureLayout(context, kind, constr) { if (typeof constr === 'function') { if (!(0, inversify_2.isInjectable)(constr)) { throw new Error(`Layouts be @injectable: ${constr.name}`); } if (!context.isBound(constr)) { context.bind(constr).toSelf(); } } context.bind(types_1.TYPES.LayoutRegistration).toDynamicValue(ctx => ({ layoutKind: kind, factory: () => ctx.container.get(constr) })); } exports.configureLayout = configureLayout; //# sourceMappingURL=layout.js.map