UNPKG

3.95 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 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;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24exports.ContextMenuRenderer = exports.ContextMenuAccess = exports.coordinateFromAnchor = exports.toAnchor = exports.Coordinate = void 0;
25/* eslint-disable @typescript-eslint/no-explicit-any */
26const inversify_1 = require("inversify");
27const disposable_1 = require("../common/disposable");
28exports.Coordinate = Symbol('Coordinate');
29function toAnchor(anchor) {
30 return anchor instanceof HTMLElement ? { x: anchor.offsetLeft, y: anchor.offsetTop } : anchor;
31}
32exports.toAnchor = toAnchor;
33function coordinateFromAnchor(anchor) {
34 const { x, y } = anchor instanceof MouseEvent ? { x: anchor.clientX, y: anchor.clientY } : anchor;
35 return { x, y };
36}
37exports.coordinateFromAnchor = coordinateFromAnchor;
38class ContextMenuAccess {
39 constructor(toClose) {
40 this.toDispose = new disposable_1.DisposableCollection();
41 this.onDispose = this.toDispose.onDispose;
42 this.toDispose.push(toClose);
43 }
44 get disposed() {
45 return this.toDispose.disposed;
46 }
47 dispose() {
48 this.toDispose.dispose();
49 }
50}
51exports.ContextMenuAccess = ContextMenuAccess;
52let ContextMenuRenderer = class ContextMenuRenderer {
53 constructor() {
54 this.toDisposeOnSetCurrent = new disposable_1.DisposableCollection();
55 }
56 /**
57 * Currently opened context menu.
58 * Rendering a new context menu will close the current.
59 */
60 get current() {
61 return this._current;
62 }
63 setCurrent(current) {
64 if (this._current === current) {
65 return;
66 }
67 this.toDisposeOnSetCurrent.dispose();
68 this._current = current;
69 if (current) {
70 this.toDisposeOnSetCurrent.push(current.onDispose(() => {
71 this._current = undefined;
72 }));
73 this.toDisposeOnSetCurrent.push(current);
74 }
75 }
76 render(options) {
77 const resolvedOptions = this.resolve(options);
78 const access = this.doRender(resolvedOptions);
79 this.setCurrent(access);
80 return access;
81 }
82 resolve(options) {
83 const args = options.args ? options.args.slice() : [];
84 if (options.includeAnchorArg !== false) {
85 args.push(options.anchor);
86 }
87 return {
88 ...options,
89 args
90 };
91 }
92};
93ContextMenuRenderer = __decorate([
94 (0, inversify_1.injectable)()
95], ContextMenuRenderer);
96exports.ContextMenuRenderer = ContextMenuRenderer;
97//# sourceMappingURL=context-menu-renderer.js.map
\No newline at end of file