@kieler/klighd-core
Version:
Core KLighD diagram visualization with Sprotty
175 lines • 8.4 kB
JavaScript
"use strict";
/*
* KIELER - Kiel Integrated Environment for Layout Eclipse RichClient
*
* http://rtsys.informatik.uni-kiel.de/kieler
*
* Copyright 2021 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.OptionsRegistry = void 0;
const inversify_1 = require("inversify");
const registry_1 = require("../base/registry");
const services_1 = require("../services");
const actions_1 = require("./actions");
const options_blacklist_1 = require("./options-blacklist");
/**
* {@link Registry} that stores and manages KLighD options provided by the server.
*
* Acts as an action handler that handles UpdateOptionsActions and modifications
* to the Options. Changes are synchronized with the server.
*/
let OptionsRegistry = class OptionsRegistry extends registry_1.Registry {
constructor() {
super(...arguments);
this._modelUri = '';
this._synthesisOptions = [];
this._layoutOptions = [];
this._displayedActions = [];
}
get modelUri() {
return this._modelUri;
}
get valuedSynthesisOptions() {
return this._synthesisOptions;
}
get layoutOptions() {
return this._layoutOptions;
}
get displayedActions() {
return this._displayedActions;
}
/** Returns `true` when the registry contains options and is therefore not empty. */
hasOptions() {
return (this._displayedActions.length !== 0 ||
this._synthesisOptions.length !== 0 ||
this._layoutOptions.length !== 0);
}
initialize(registry) {
registry.register(actions_1.UpdateOptionsAction.KIND, this);
registry.register(actions_1.PerformOptionsActionAction.KIND, this);
registry.register(actions_1.SetSynthesisOptionsAction.KIND, this);
registry.register(actions_1.SetLayoutOptionsAction.KIND, this);
registry.register(actions_1.ResetSynthesisOptionsAction.KIND, this);
registry.register(actions_1.ResetLayoutOptionsAction.KIND, this);
}
handle(action) {
if (actions_1.UpdateOptionsAction.isThisAction(action)) {
this.handleUpdateOptions(action);
}
else if (actions_1.PerformOptionsActionAction.isThisAction(action)) {
this.handlePerformOptionsAction(action);
}
else if (actions_1.SetSynthesisOptionsAction.isThisAction(action)) {
this.handleSetSynthesisOptions(action);
}
else if (actions_1.SetLayoutOptionsAction.isThisAction(action)) {
this.handleSetLayoutOptions(action);
}
else if (actions_1.ResetSynthesisOptionsAction.isThisAction(action)) {
this.handleResetSynthesisOptions(action);
}
else if (actions_1.ResetLayoutOptionsAction.isThisAction(action)) {
this.handleResetLayoutOptions(action);
}
}
handleUpdateOptions(action) {
this._modelUri = action.modelUri;
this._displayedActions = action.actions;
// Transform valued synthesis options to synthesis options by setting their current value and remove blacklisted options
this._synthesisOptions = action.valuedSynthesisOptions
.filter((opt) => !options_blacklist_1.optionsBlacklist.includes(opt.synthesisOption.id))
.map((valuedOption) => {
var _a;
return (Object.assign(Object.assign({}, valuedOption.synthesisOption), { currentValue: (_a = valuedOption.currentValue) !== null && _a !== void 0 ? _a : valuedOption.synthesisOption.initialValue }));
});
// Transform layout options to ensure that they have a current value.
// Fallback to an already stored currentValue, since the server does not provide a current value for layout options.
this._layoutOptions = action.layoutOptions.map((option, i) => {
var _a, _b, _c;
return (Object.assign(Object.assign({}, option), { currentValue: (_c = (_a = option.currentValue) !== null && _a !== void 0 ? _a : (_b = this._layoutOptions[i]) === null || _b === void 0 ? void 0 : _b.currentValue) !== null && _c !== void 0 ? _c : option.defaultValue.k }));
});
this.notifyListeners();
}
handlePerformOptionsAction(action) {
this.connection.sendNotification("keith/diagramOptions/performAction" /* NotificationType.PerformAction */, {
actionId: action.actionId,
uri: this.modelUri,
});
}
handleSetSynthesisOptions(action) {
// Optimistic update. Replaces all changed options with the new options
this._synthesisOptions = this._synthesisOptions.map((option) => { var _a; return (_a = action.options.find((newOpt) => newOpt.id === option.id)) !== null && _a !== void 0 ? _a : option; });
this.notifyListeners();
if (action.sendToServer) {
this.connection.sendNotification("keith/diagramOptions/setSynthesisOptions" /* NotificationType.SetSynthesisOption */, {
synthesisOptions: action.options,
uri: this.modelUri,
});
}
}
handleSetLayoutOptions(action) {
// Optimistic update. Replaces all changed options with the new options
this._layoutOptions = this._layoutOptions.map((option) => {
const newValue = action.options.find((newOpt) => newOpt.optionId === option.optionId);
return newValue ? Object.assign(Object.assign({}, option), { currentValue: newValue.value }) : option;
});
this.notifyListeners();
if (action.sendToServer) {
this.connection.sendNotification("keith/diagramOptions/setLayoutOptions" /* NotificationType.SetLayoutOption */, {
layoutOptions: action.options,
uri: this.modelUri,
});
}
}
handleResetSynthesisOptions(action) {
this._synthesisOptions = this._synthesisOptions.map((option) => (Object.assign(Object.assign({}, option), { currentValue: option.initialValue })));
this.notifyListeners();
if (action.sendToServer) {
this.connection.sendNotification("keith/diagramOptions/setSynthesisOptions" /* NotificationType.SetSynthesisOption */, {
synthesisOptions: this._synthesisOptions,
uri: this.modelUri,
});
}
}
handleResetLayoutOptions(action) {
this._layoutOptions = this._layoutOptions.map((option) => (Object.assign(Object.assign({}, option), { currentValue: option.defaultValue.k })));
this.notifyListeners();
if (action.sendToServer) {
this.connection.sendNotification("keith/diagramOptions/setLayoutOptions" /* NotificationType.SetLayoutOption */, {
layoutOptions: this._layoutOptions.map((o) => ({
optionId: o.optionId,
value: o.currentValue,
})),
uri: this.modelUri,
});
}
}
};
exports.OptionsRegistry = OptionsRegistry;
__decorate([
(0, inversify_1.inject)(services_1.ServiceTypes.Connection),
__metadata("design:type", Object)
], OptionsRegistry.prototype, "connection", void 0);
exports.OptionsRegistry = OptionsRegistry = __decorate([
(0, inversify_1.injectable)()
], OptionsRegistry);
//# sourceMappingURL=options-registry.js.map