UNPKG

sprotty

Version:

A next-gen framework for graphical views

118 lines 5.58 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.InitializeCanvasBoundsCommand = exports.InitializeCanvasBoundsAction = exports.CanvasBoundsInitializer = void 0; const inversify_1 = require("inversify"); const geometry_1 = require("sprotty-protocol/lib/utils/geometry"); const types_1 = require("../types"); const smodel_1 = require("../model/smodel"); const command_1 = require("../commands/command"); const browser_1 = require("../../utils/browser"); /** * Grabs the bounds from the root element in page coordinates and fires a * InitializeCanvasBoundsAction. This size is needed for other actions such * as FitToScreenAction. */ let CanvasBoundsInitializer = class CanvasBoundsInitializer { decorate(vnode, element) { if (element instanceof smodel_1.SModelRootImpl && !geometry_1.Dimension.isValid(element.canvasBounds)) { this.rootAndVnode = [element, vnode]; } return vnode; } postUpdate() { if (this.rootAndVnode !== undefined) { const domElement = this.rootAndVnode[1].elm; const oldBounds = this.rootAndVnode[0].canvasBounds; if (domElement !== undefined) { const newBounds = this.getBoundsInPage(domElement); if (!((0, geometry_1.almostEquals)(newBounds.x, oldBounds.x) && (0, geometry_1.almostEquals)(newBounds.y, oldBounds.y) && (0, geometry_1.almostEquals)(newBounds.width, oldBounds.width) && (0, geometry_1.almostEquals)(newBounds.height, oldBounds.width))) this.actionDispatcher.dispatch(InitializeCanvasBoundsAction.create(newBounds)); } this.rootAndVnode = undefined; } } getBoundsInPage(element) { const bounds = element.getBoundingClientRect(); const scroll = (0, browser_1.getWindowScroll)(); return { x: bounds.left + scroll.x, y: bounds.top + scroll.y, width: bounds.width, height: bounds.height }; } }; exports.CanvasBoundsInitializer = CanvasBoundsInitializer; __decorate([ (0, inversify_1.inject)(types_1.TYPES.IActionDispatcher), __metadata("design:type", Object) ], CanvasBoundsInitializer.prototype, "actionDispatcher", void 0); exports.CanvasBoundsInitializer = CanvasBoundsInitializer = __decorate([ (0, inversify_1.injectable)() ], CanvasBoundsInitializer); var InitializeCanvasBoundsAction; (function (InitializeCanvasBoundsAction) { InitializeCanvasBoundsAction.KIND = 'initializeCanvasBounds'; function create(newCanvasBounds) { return { kind: InitializeCanvasBoundsAction.KIND, newCanvasBounds }; } InitializeCanvasBoundsAction.create = create; })(InitializeCanvasBoundsAction || (exports.InitializeCanvasBoundsAction = InitializeCanvasBoundsAction = {})); let InitializeCanvasBoundsCommand = class InitializeCanvasBoundsCommand extends command_1.SystemCommand { constructor(action) { super(); this.action = action; } execute(context) { this.newCanvasBounds = this.action.newCanvasBounds; context.root.canvasBounds = this.newCanvasBounds; return context.root; } undo(context) { return context.root; } redo(context) { return context.root; } }; exports.InitializeCanvasBoundsCommand = InitializeCanvasBoundsCommand; InitializeCanvasBoundsCommand.KIND = InitializeCanvasBoundsAction.KIND; exports.InitializeCanvasBoundsCommand = InitializeCanvasBoundsCommand = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(types_1.TYPES.Action)), __metadata("design:paramtypes", [Object]) ], InitializeCanvasBoundsCommand); //# sourceMappingURL=initialize-canvas.js.map