UNPKG

@kieler/klighd-core

Version:

Core KLighD diagram visualization with Sprotty

218 lines 9.18 kB
"use strict"; /* * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient * * http://rtsys.informatik.uni-kiel.de/kieler * * Copyright 2021-2024 by * + Kiel University * + Department of Computer Science * + Real-Time and Embedded Systems Group * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * SPDX-License-Identifier: EPL-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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PreferencesRegistry = exports.ClientLayoutOption = exports.IncrementalDiagramGeneratorOption = exports.ShouldSelectTextOption = exports.ShouldSelectDiagramOption = void 0; const inversify_1 = require("inversify"); const sprotty_1 = require("sprotty"); const registry_1 = require("./base/registry"); const actions_1 = require("./options/actions"); const option_models_1 = require("./options/option-models"); const services_1 = require("./services"); /** * Indicates whether or not a text selection should select the corresponding diagram part. */ class ShouldSelectDiagramOption { constructor() { this.id = ShouldSelectDiagramOption.ID; this.name = ShouldSelectDiagramOption.NAME; this.type = option_models_1.TransformationOptionType.CHECK; this.initialValue = false; this.currentValue = false; this.notifyServer = true; } } exports.ShouldSelectDiagramOption = ShouldSelectDiagramOption; ShouldSelectDiagramOption.ID = 'diagram.shouldSelectDiagram'; ShouldSelectDiagramOption.NAME = 'Text Selects Diagram'; /** * Indicates whether or nat a selection in the diagram should also highlight the corresponding text. */ class ShouldSelectTextOption { constructor() { this.id = ShouldSelectTextOption.ID; this.name = ShouldSelectTextOption.NAME; this.type = option_models_1.TransformationOptionType.CHECK; this.initialValue = true; this.currentValue = true; this.notifyServer = true; } } exports.ShouldSelectTextOption = ShouldSelectTextOption; ShouldSelectTextOption.ID = 'diagram.shouldSelectText'; ShouldSelectTextOption.NAME = 'Diagram Selects Text'; /** * Instructs the server if the diagram should be sent incrementally in pieces. */ class IncrementalDiagramGeneratorOption { constructor() { this.id = IncrementalDiagramGeneratorOption.ID; this.name = IncrementalDiagramGeneratorOption.NAME; this.type = option_models_1.TransformationOptionType.CHECK; this.initialValue = false; this.currentValue = false; this.notifyServer = true; this.debug = true; } } exports.IncrementalDiagramGeneratorOption = IncrementalDiagramGeneratorOption; IncrementalDiagramGeneratorOption.ID = 'diagram.incrementalDiagramGenerator'; IncrementalDiagramGeneratorOption.NAME = 'Incremental Diagram Generator'; /** * Switch between client-only and server-only layout. */ class ClientLayoutOption { constructor() { this.description = 'Switch between client-only and server-only layout.'; this.id = ClientLayoutOption.ID; this.name = ClientLayoutOption.NAME; this.type = option_models_1.TransformationOptionType.CHECK; this.initialValue = false; this.currentValue = false; this.notifyServer = true; this.debug = true; } } exports.ClientLayoutOption = ClientLayoutOption; ClientLayoutOption.ID = 'diagram.clientLayout'; ClientLayoutOption.NAME = 'Client Layout'; /** * {@link Registry} that stores user preferences which change the behavior of the diagram view. * * This registry should store options or preferences that are not provided by the Synthesis as LayoutOptions but that also * should be send to the server. * In contrast to RenderOptions they are cannot be solely handled by the client. */ let PreferencesRegistry = class PreferencesRegistry extends registry_1.Registry { constructor() { super(); this._preferences = new Map(); // Add available preferences this.register(ShouldSelectDiagramOption); this.register(ShouldSelectTextOption); this.register(IncrementalDiagramGeneratorOption); this.register(ClientLayoutOption); } init() { this.storage.onClear(this.handleClear.bind(this)); this.storage .getItem('preference') .then((data) => { if (data) this.loadPersistedData(data); }) .then(() => { // Wait until values are loaded before notifying. this.notifyListeners(); // Notify the server about initial preferences. this.notifyServer(); }); } /** * Restores options that where previously persisted in storage. * Since preferences are not provided by the server, they have to be retrieved from storage. */ loadPersistedData(data) { for (const entry of Object.entries(data)) { const option = this._preferences.get(entry[0]); if (option) { // eslint-disable-next-line prefer-destructuring option.currentValue = entry[1]; } } } register(Option) { this._preferences.set(Option.ID, new Option()); } handle(action) { if (actions_1.SetPreferencesAction.isThisAction(action)) { // Update storage values this.storage.setItem('preference', (prev) => { const obj = prev !== null && prev !== void 0 ? prev : {}; for (const option of action.options) { obj[option.id] = option.value; // Update local value from storage const localPreference = this._preferences.get(option.id); if (localPreference) { localPreference.currentValue = option.value; } } return obj; }); this.notifyListeners(); this.notifyServer(); } else if (actions_1.ResetPreferencesAction.isThisAction(action)) { this._preferences.forEach((option) => { option.currentValue = option.initialValue; }); this.notifyListeners(); } } /** Notifies the server about changed preferences that are supported by the client. */ notifyServer() { this.connection.onReady().then(async () => { const obj = { 'diagram.shouldSelectDiagram': this.getValue(ShouldSelectDiagramOption), 'diagram.shouldSelectText': this.getValue(ShouldSelectTextOption), 'diagram.incrementalDiagramGenerator': this.getValue(IncrementalDiagramGeneratorOption), 'diagram.clientLayout': this.getValue(ClientLayoutOption), }; this.connection.sendNotification("keith/preferences/setPreferences" /* NotificationType.SetPreferences */, obj); }); } getValue(option) { var _a; return (_a = this._preferences.get(option.ID)) === null || _a === void 0 ? void 0 : _a.currentValue; } /** Reset all stored options when the storage gets cleared from outside. */ handleClear() { this.dispatcher.dispatch(actions_1.ResetPreferencesAction.create()); } }; exports.PreferencesRegistry = PreferencesRegistry; __decorate([ (0, inversify_1.inject)(services_1.ServiceTypes.Connection), __metadata("design:type", Object) ], PreferencesRegistry.prototype, "connection", void 0); __decorate([ (0, inversify_1.inject)(services_1.ServiceTypes.PersistenceStorage), __metadata("design:type", Object) ], PreferencesRegistry.prototype, "storage", void 0); __decorate([ (0, inversify_1.inject)(sprotty_1.TYPES.IActionDispatcher), __metadata("design:type", Object) ], PreferencesRegistry.prototype, "dispatcher", void 0); __decorate([ (0, inversify_1.postConstruct)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], PreferencesRegistry.prototype, "init", null); exports.PreferencesRegistry = PreferencesRegistry = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", []) ], PreferencesRegistry); //# sourceMappingURL=preferences-registry.js.map