UNPKG

sprotty

Version:

A next-gen framework for graphical views

118 lines 5.13 kB
"use strict"; 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.AbstractUIExtension = exports.isUIExtension = void 0; /******************************************************************************** * Copyright (c) 2019 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 ********************************************************************************/ const inversify_1 = require("inversify"); const sprotty_protocol_1 = require("sprotty-protocol"); const types_1 = require("../types"); function isUIExtension(object) { return (0, sprotty_protocol_1.hasOwnProperty)(object, 'id', 'function') && (0, sprotty_protocol_1.hasOwnProperty)(object, 'show', 'function') && (0, sprotty_protocol_1.hasOwnProperty)(object, 'hide', 'function'); } exports.isUIExtension = isUIExtension; /** * Abstract base class for UI extensions. */ let AbstractUIExtension = class AbstractUIExtension { show(root, ...contextElementIds) { this.activeElement = document.activeElement; if (!this.containerElement) { if (!this.initialize()) return; } this.onBeforeShow(this.containerElement, root, ...contextElementIds); this.setContainerVisible(true); } hide() { this.setContainerVisible(false); this.restoreFocus(); this.activeElement = null; } restoreFocus() { const focusedElement = this.activeElement; if (focusedElement) { focusedElement.focus(); } } initialize() { const baseDiv = document.getElementById(this.options.baseDiv); if (!baseDiv) { this.logger.warn(this, `Could not obtain sprotty base container for initializing UI extension ${this.id}`, this); return false; } this.containerElement = this.getOrCreateContainer(baseDiv.id); this.initializeContents(this.containerElement); if (baseDiv) { baseDiv.insertBefore(this.containerElement, baseDiv.firstChild); } return true; } getOrCreateContainer(baseDivId) { let container = document.getElementById(this.id()); if (container === null) { container = document.createElement('div'); container.id = baseDivId + "_" + this.id(); container.classList.add(this.containerClass()); } return container; } setContainerVisible(visible) { if (this.containerElement) { if (visible) { this.containerElement.style.visibility = 'visible'; this.containerElement.style.opacity = '1'; } else { this.containerElement.style.visibility = 'hidden'; this.containerElement.style.opacity = '0'; } } } /** * Updates the `containerElement` under the given `context` before it becomes visible. * * Subclasses may override this method to, for instance, modifying the position of the * `containerElement`, add or remove elements, etc. depending on the specified `root` * or `contextElementIds`. */ onBeforeShow(containerElement, root, ...contextElementIds) { // default: do nothing } }; exports.AbstractUIExtension = AbstractUIExtension; __decorate([ (0, inversify_1.inject)(types_1.TYPES.ViewerOptions), __metadata("design:type", Object) ], AbstractUIExtension.prototype, "options", void 0); __decorate([ (0, inversify_1.inject)(types_1.TYPES.ILogger), __metadata("design:type", Object) ], AbstractUIExtension.prototype, "logger", void 0); exports.AbstractUIExtension = AbstractUIExtension = __decorate([ (0, inversify_1.injectable)() ], AbstractUIExtension); //# sourceMappingURL=ui-extension.js.map