UNPKG

sprotty

Version:

A next-gen framework for graphical views

155 lines 7.62 kB
"use strict"; /******************************************************************************** * Copyright (c) 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.SwitchEditModeCommand = exports.SwitchEditModeAction = void 0; const inversify_1 = require("inversify"); const command_1 = require("../../base/commands/command"); const smodel_1 = require("../../base/model/smodel"); const types_1 = require("../../base/types"); const model_1 = require("../routing/model"); const routing_1 = require("../routing/routing"); const model_2 = require("./model"); var SwitchEditModeAction; (function (SwitchEditModeAction) { SwitchEditModeAction.KIND = "switchEditMode"; function create(options) { var _a, _b; return { kind: SwitchEditModeAction.KIND, elementsToActivate: (_a = options.elementsToActivate) !== null && _a !== void 0 ? _a : [], elementsToDeactivate: (_b = options.elementsToDeactivate) !== null && _b !== void 0 ? _b : [] }; } SwitchEditModeAction.create = create; })(SwitchEditModeAction || (exports.SwitchEditModeAction = SwitchEditModeAction = {})); let SwitchEditModeCommand = class SwitchEditModeCommand extends command_1.Command { constructor(action) { super(); this.action = action; this.elementsToActivate = []; this.elementsToDeactivate = []; this.handlesToRemove = []; } execute(context) { const index = context.root.index; this.action.elementsToActivate.forEach(id => { const element = index.getById(id); if (element !== undefined) this.elementsToActivate.push(element); }); this.action.elementsToDeactivate.forEach(id => { const element = index.getById(id); if (element !== undefined) this.elementsToDeactivate.push(element); if (element instanceof model_1.SRoutingHandleImpl && element.parent instanceof model_1.SRoutableElementImpl) { const parent = element.parent; if (this.shouldRemoveHandle(element, parent)) { this.handlesToRemove.push({ handle: element, parent }); this.elementsToDeactivate.push(parent); this.elementsToActivate.push(parent); } } }); return this.doExecute(context); } doExecute(context) { this.handlesToRemove.forEach(entry => { entry.point = entry.parent.routingPoints.splice(entry.handle.pointIndex, 1)[0]; }); this.elementsToDeactivate.forEach(element => { if (element instanceof model_1.SRoutableElementImpl) element.removeAll(child => child instanceof model_1.SRoutingHandleImpl); else if (element instanceof model_1.SRoutingHandleImpl) { element.editMode = false; if (element.danglingAnchor) { if (element.parent instanceof model_1.SRoutableElementImpl && element.danglingAnchor.original) { if (element.parent.source === element.danglingAnchor) element.parent.sourceId = element.danglingAnchor.original.id; else if (element.parent.target === element.danglingAnchor) element.parent.targetId = element.danglingAnchor.original.id; element.danglingAnchor.parent.remove(element.danglingAnchor); element.danglingAnchor = undefined; } } } }); this.elementsToActivate.forEach(element => { if ((0, model_2.canEditRouting)(element) && element instanceof smodel_1.SParentElementImpl) { const router = this.edgeRouterRegistry.get(element.routerKind); router.createRoutingHandles(element); } else if (element instanceof model_1.SRoutingHandleImpl) element.editMode = true; }); return context.root; } shouldRemoveHandle(handle, parent) { if (handle.kind === 'junction') { const route = this.edgeRouterRegistry.route(parent); return route.find(rp => rp.pointIndex === handle.pointIndex) === undefined; } return false; } undo(context) { this.handlesToRemove.forEach(entry => { if (entry.point !== undefined) entry.parent.routingPoints.splice(entry.handle.pointIndex, 0, entry.point); }); this.elementsToActivate.forEach(element => { if (element instanceof model_1.SRoutableElementImpl) element.removeAll(child => child instanceof model_1.SRoutingHandleImpl); else if (element instanceof model_1.SRoutingHandleImpl) element.editMode = false; }); this.elementsToDeactivate.forEach(element => { if ((0, model_2.canEditRouting)(element)) { const router = this.edgeRouterRegistry.get(element.routerKind); router.createRoutingHandles(element); } else if (element instanceof model_1.SRoutingHandleImpl) element.editMode = true; }); return context.root; } redo(context) { return this.doExecute(context); } }; exports.SwitchEditModeCommand = SwitchEditModeCommand; SwitchEditModeCommand.KIND = SwitchEditModeAction.KIND; __decorate([ (0, inversify_1.inject)(routing_1.EdgeRouterRegistry), __metadata("design:type", routing_1.EdgeRouterRegistry) ], SwitchEditModeCommand.prototype, "edgeRouterRegistry", void 0); exports.SwitchEditModeCommand = SwitchEditModeCommand = __decorate([ (0, inversify_1.injectable)(), __param(0, (0, inversify_1.inject)(types_1.TYPES.Action)), __metadata("design:paramtypes", [Object]) ], SwitchEditModeCommand); //# sourceMappingURL=edit-routing.js.map