@eclipse-glsp/client
Version:
A sprotty-based client for GLSP
128 lines • 5.64 kB
JavaScript
"use strict";
/********************************************************************************
* Copyright (c) 2024 EclipseSource 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GLSPAbstractUIExtension = exports.CSS_HIDDEN_EXTENSION_CLASS = exports.CSS_UI_EXTENSION_CLASS = void 0;
const sprotty_1 = require("@eclipse-glsp/sprotty");
const inversify_1 = require("inversify");
exports.CSS_UI_EXTENSION_CLASS = 'ui-extension';
exports.CSS_HIDDEN_EXTENSION_CLASS = 'hidden';
/**
* Reusable abstract base implementation for UI extensions.
* Intended for UI extensions that directly interact with the DOM API to create and manage UI elements.
*/
let GLSPAbstractUIExtension = class GLSPAbstractUIExtension extends sprotty_1.AbstractUIExtension {
constructor() {
super(...arguments);
this.toDisposeOnHide = new sprotty_1.DisposableCollection();
}
get diagramContainerId() {
return this.options.baseDiv;
}
get parentContainerSelector() {
return '#' + this.diagramContainerId;
}
get containerSelector() {
return '#' + this.id();
}
get initialized() {
return !!this.containerElement;
}
initialize() {
if (this.initialized) {
return true;
}
try {
this.containerElement = this.getOrCreateContainer();
this.initializeContainer(this.containerElement);
this.initializeContents(this.containerElement);
}
catch (error) {
const msg = error instanceof Error ? error.message : `Could not retrieve container element for UI extension ${this.id}`;
this.logger.error(this, msg);
return false;
}
return true;
}
getOrCreateContainer() {
if (this.containerElement) {
return this.containerElement;
}
// check if the container already exists, independent from any potential parent container
// this allows us to use existing elements defined anywhere in the document
const existingContainer = this.getContainer();
if (existingContainer) {
return existingContainer;
}
// to create a container the parent container
const parent = this.getParentContainer();
if (!(parent === null || parent === void 0 ? void 0 : parent.isConnected)) {
throw new Error(`Could not obtain attached parent for initializing UI extension ${this.id}`);
}
const container = this.createContainer(parent);
this.insertContainerIntoParent(container, parent);
return container;
}
getContainer() {
return document.querySelector(this.containerSelector);
}
createContainer(parent) {
const container = document.createElement('div');
container.id = parent.id + '_' + this.id();
return container;
}
initializeContainer(container) {
container.classList.add(exports.CSS_UI_EXTENSION_CLASS, this.containerClass());
}
getParentContainer() {
return document.querySelector(this.parentContainerSelector);
}
insertContainerIntoParent(container, parent) {
parent.insertBefore(container, parent.firstChild);
}
setContainerVisible(visible) {
var _a, _b;
// the parent class simply sets the style directly, however classes provide more fine-grained control
if (visible) {
(_a = this.containerElement) === null || _a === void 0 ? void 0 : _a.classList.remove(exports.CSS_HIDDEN_EXTENSION_CLASS);
}
else {
(_b = this.containerElement) === null || _b === void 0 ? void 0 : _b.classList.add(exports.CSS_HIDDEN_EXTENSION_CLASS);
}
}
isContainerVisible() {
var _a;
return !((_a = this.containerElement) === null || _a === void 0 ? void 0 : _a.classList.contains(exports.CSS_HIDDEN_EXTENSION_CLASS));
}
toggleContainerVisible() {
this.setContainerVisible(!this.isContainerVisible());
}
hide() {
super.hide();
this.toDisposeOnHide.dispose();
}
};
exports.GLSPAbstractUIExtension = GLSPAbstractUIExtension;
exports.GLSPAbstractUIExtension = GLSPAbstractUIExtension = __decorate([
(0, inversify_1.injectable)()
], GLSPAbstractUIExtension);
//# sourceMappingURL=ui-extension.js.map