UNPKG

sprotty

Version:

A next-gen framework for graphical views

125 lines 4.78 kB
"use strict"; /******************************************************************************** * Copyright (c) 2017-2024 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 ********************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.transformToRootBounds = exports.containsSome = exports.translateBounds = exports.translatePoint = exports.findParentByFeature = exports.findParent = exports.registerModelElement = void 0; const types_1 = require("../types"); const smodel_1 = require("./smodel"); /** * Register a model element constructor for an element type. */ function registerModelElement(context, type, constr, features, isOverride) { context.bind(types_1.TYPES.SModelElementRegistration).toConstantValue({ type, constr, features, isOverride }); } exports.registerModelElement = registerModelElement; /** * Find a parent element that satisfies the given predicate. */ function findParent(element, predicate) { let current = element; while (current !== undefined) { if (predicate(current)) return current; else if (current instanceof smodel_1.SChildElementImpl) current = current.parent; else current = undefined; } return current; } exports.findParent = findParent; /** * Find a parent element that implements the feature identified with the given predicate. */ function findParentByFeature(element, predicate) { let current = element; while (current !== undefined) { if (predicate(current)) return current; else if (current instanceof smodel_1.SChildElementImpl) current = current.parent; else current = undefined; } return current; } exports.findParentByFeature = findParentByFeature; /** * Translate a point from the coordinate system of the source element to the coordinate system * of the target element. */ function translatePoint(point, source, target) { if (source !== target) { // Translate from the source to the root element while (source instanceof smodel_1.SChildElementImpl) { point = source.localToParent(point); source = source.parent; if (source === target) return point; } // Translate from the root to the target element const targetTrace = []; while (target instanceof smodel_1.SChildElementImpl) { targetTrace.push(target); target = target.parent; } if (source !== target) throw new Error("Incompatible source and target: " + source.id + ", " + target.id); for (let i = targetTrace.length - 1; i >= 0; i--) { point = targetTrace[i].parentToLocal(point); } } return point; } exports.translatePoint = translatePoint; /** * Translate some bounds from the coordinate system of the source element to the coordinate system * of the target element. */ function translateBounds(bounds, source, target) { const upperLeft = translatePoint(bounds, source, target); const lowerRight = translatePoint({ x: bounds.x + bounds.width, y: bounds.y + bounds.height }, source, target); return { x: upperLeft.x, y: upperLeft.y, width: lowerRight.x - upperLeft.x, height: lowerRight.y - upperLeft.y }; } exports.translateBounds = translateBounds; /** * Tests if the given model contains an id of then given element or one of its descendants. */ function containsSome(root, element) { const test = (el) => root.index.getById(el.id) !== undefined; const find = (elements) => elements.some(el => test(el) || find(el.children)); return find([element]); } exports.containsSome = containsSome; /** * Transforms the local bounds all the way up to the root. */ function transformToRootBounds(parent, bounds) { while (parent instanceof smodel_1.SChildElementImpl) { bounds = parent.localToParent(bounds); parent = parent.parent; } return bounds; } exports.transformToRootBounds = transformToRootBounds; //# sourceMappingURL=smodel-utils.js.map