UNPKG

@eclipse-glsp/graph

Version:

The typescript implementation of the GLSP graphical model (GModel)

153 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GModelRootBuilder = exports.GModelRoot = exports.GModelElementBuilder = exports.GModelElement = void 0; /******************************************************************************** * Copyright (c) 2022-2024 STMicroelectronics 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 ********************************************************************************/ const protocol_1 = require("@eclipse-glsp/protocol"); const uuid = require("uuid"); /** * Base type for all elements of the graphical model. * Each model element must have a unique ID and a type that is used to look up its view. */ class GModelElement { constructor() { /** * A set of css classes that should be applied to the DOM element that corresponds to this element. */ this.cssClasses = []; /** * A `GModelElement can have an arbitrary amount of children. This parent-child relation ship is also reflected in * the corresponding DOM element i.e. DOM elements that reflect children of this element are also children * of the DOM element that reflects this element. */ this.children = []; } /** * Retrieve the {@link GModelRoot} element by traversing up the parent hierachy. */ get root() { let current = this; while (!(current instanceof GModelRoot)) { current = current.parent; } return current; } } exports.GModelElement = GModelElement; /** * A fluent builder API that simplifies the construction of complex {@link GModelElement}s. * The builder API is derived from the Java GLSP server implementation where it is used to hide the complexity * of creating EMF objects. However, the API is also useful in a Typescript/Node context to declare the creation of a new * {@link GModelElement} in a more concise way. */ class GModelElementBuilder { constructor(elementConstructor) { this.elementConstructor = elementConstructor; this.proxy = new elementConstructor(); this.proxy.cssClasses = []; this.proxy.children = []; this.proxy.id = uuid.v4(); } reset() { this.proxy = new this.elementConstructor(); return this; } build() { const element = new this.elementConstructor(); Object.assign(element, this.proxy); element.children.forEach(child => (child.parent = element)); if (element.id === undefined) { throw new Error('The `id` property of a GModelElement must not be undefined!'); } if (element.type === undefined) { throw new Error('The `type` property of a GModelElement must not be undefined!'); } return element; } id(id) { this.proxy.id = id; return this; } type(type) { this.proxy.type = type; return this; } addCssClass(cssClass) { this.proxy.cssClasses.push(cssClass); return this; } addCssClasses(...cssClasses) { (0, protocol_1.flatPush)(this.proxy.cssClasses, cssClasses); return this; } add(child) { this.proxy.children.push(child); return this; } addChildren(...children) { (0, protocol_1.flatPush)(this.proxy.children, children); return this; } addArg(key, value) { if (!this.proxy.args) { this.proxy.args = {}; } this.proxy.args[key] = value; return this; } addArgs(args) { const toAssign = {}; if (args instanceof Map) { [...args.keys()].forEach(key => (toAssign[key] = args.get(key))); } else { Object.keys(args).forEach(key => (toAssign[key] = args[key])); } if (this.proxy.args) { Object.assign(this.proxy.args, toAssign); } else { this.proxy.args = toAssign; } return this; } } exports.GModelElementBuilder = GModelElementBuilder; class GModelRoot extends GModelElement { static builder() { return new GModelRootBuilder(GModelRoot); } } exports.GModelRoot = GModelRoot; class GModelRootBuilder extends GModelElementBuilder { revision(revision) { this.proxy.revision = revision; return this; } canvasBounds(positionOrBounds, size) { let bounds; if (size) { bounds = { ...positionOrBounds, ...size }; } else { bounds = positionOrBounds; } this.proxy.canvasBounds = bounds; return this; } } exports.GModelRootBuilder = GModelRootBuilder; //# sourceMappingURL=gmodel-element.js.map