UNPKG

12.7 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 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 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};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26var TabBarToolbar_1;
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.TabBarToolbar = exports.TabBarToolbarFactory = void 0;
29const inversify_1 = require("inversify");
30const React = require("react");
31const common_1 = require("../../../common");
32const context_menu_renderer_1 = require("../../context-menu-renderer");
33const label_parser_1 = require("../../label-parser");
34const widgets_1 = require("../../widgets");
35const tab_bar_toolbar_registry_1 = require("./tab-bar-toolbar-registry");
36const tab_bar_toolbar_types_1 = require("./tab-bar-toolbar-types");
37/**
38 * Factory for instantiating tab-bar toolbars.
39 */
40exports.TabBarToolbarFactory = Symbol('TabBarToolbarFactory');
41/**
42 * Tab-bar toolbar widget representing the active [tab-bar toolbar items](TabBarToolbarItem).
43 */
44let TabBarToolbar = TabBarToolbar_1 = class TabBarToolbar extends widgets_1.ReactWidget {
45 constructor() {
46 super();
47 this.inline = new Map();
48 this.more = new Map();
49 this.onRender = () => this.show();
50 this.toDisposeOnSetCurrent = new common_1.DisposableCollection();
51 this.showMoreContextMenu = (event) => {
52 event.stopPropagation();
53 event.preventDefault();
54 const anchor = this.toAnchor(event);
55 this.renderMoreContextMenu(anchor);
56 };
57 this.executeCommand = (e) => {
58 e.preventDefault();
59 e.stopPropagation();
60 const item = this.inline.get(e.currentTarget.id);
61 if ((item === null || item === void 0 ? void 0 : item.command) && item.menuPath) {
62 this.menuCommandExecutor.executeCommand(item.menuPath, item.command, this.current);
63 }
64 else if (item === null || item === void 0 ? void 0 : item.command) {
65 this.commands.executeCommand(item.command, this.current);
66 }
67 else if (item === null || item === void 0 ? void 0 : item.menuPath) {
68 this.renderMoreContextMenu(this.toAnchor(e), item.menuPath);
69 }
70 this.update();
71 };
72 this.onMouseDownEvent = (e) => {
73 if (e.button === 0) {
74 e.currentTarget.classList.add('active');
75 }
76 };
77 this.onMouseUpEvent = (e) => {
78 e.currentTarget.classList.remove('active');
79 };
80 this.addClass(TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR);
81 this.hide();
82 this.onRender = this.onRender.bind(this);
83 }
84 updateItems(items, current) {
85 this.inline.clear();
86 this.more.clear();
87 for (const item of items.sort(tab_bar_toolbar_types_1.TabBarToolbarItem.PRIORITY_COMPARATOR).reverse()) {
88 if ('render' in item || item.group === undefined || item.group === 'navigation') {
89 this.inline.set(item.id, item);
90 }
91 else {
92 this.more.set(item.id, item);
93 }
94 }
95 this.setCurrent(current);
96 if (!items.length) {
97 this.hide();
98 }
99 this.update();
100 }
101 updateTarget(current) {
102 const operativeWidget = tab_bar_toolbar_types_1.TabBarDelegator.is(current) ? current.getTabBarDelegate() : current;
103 const items = operativeWidget ? this.toolbarRegistry.visibleItems(operativeWidget) : [];
104 this.updateItems(items, operativeWidget);
105 }
106 setCurrent(current) {
107 this.toDisposeOnSetCurrent.dispose();
108 this.toDispose.push(this.toDisposeOnSetCurrent);
109 this.current = current;
110 if (current) {
111 const resetCurrent = () => {
112 this.setCurrent(undefined);
113 this.update();
114 };
115 current.disposed.connect(resetCurrent);
116 this.toDisposeOnSetCurrent.push(common_1.Disposable.create(() => current.disposed.disconnect(resetCurrent)));
117 }
118 }
119 render() {
120 return React.createElement(React.Fragment, null,
121 this.renderMore(),
122 [...this.inline.values()].map(item => tab_bar_toolbar_types_1.TabBarToolbarItem.is(item) ? this.renderItem(item) : item.render(this.current)));
123 }
124 renderItem(item) {
125 var _a;
126 let innerText = '';
127 const classNames = [];
128 if (item.text) {
129 for (const labelPart of this.labelParser.parse(item.text)) {
130 if (label_parser_1.LabelIcon.is(labelPart)) {
131 const className = `fa fa-${labelPart.name}${labelPart.animation ? ' fa-' + labelPart.animation : ''}`;
132 classNames.push(...className.split(' '));
133 }
134 else {
135 innerText = labelPart;
136 }
137 }
138 }
139 const command = item.command ? this.commands.getCommand(item.command) : undefined;
140 let iconClass = (typeof item.icon === 'function' && item.icon()) || item.icon || (command && command.iconClass);
141 if (iconClass) {
142 iconClass += ` ${widgets_1.ACTION_ITEM}`;
143 classNames.push(iconClass);
144 }
145 const tooltip = item.tooltip || (command && command.label);
146 const toolbarItemClassNames = this.getToolbarItemClassNames((_a = command === null || command === void 0 ? void 0 : command.id) !== null && _a !== void 0 ? _a : item.command);
147 if (item.menuPath && !item.command) {
148 toolbarItemClassNames.push('enabled');
149 }
150 return React.createElement("div", { key: item.id, ref: this.onRender, className: toolbarItemClassNames.join(' '), onMouseDown: this.onMouseDownEvent, onMouseUp: this.onMouseUpEvent, onMouseOut: this.onMouseUpEvent },
151 React.createElement("div", { id: item.id, className: classNames.join(' '), onClick: this.executeCommand, title: tooltip }, innerText));
152 }
153 getToolbarItemClassNames(commandId) {
154 const classNames = [TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR_ITEM];
155 if (commandId) {
156 if (this.commandIsEnabled(commandId)) {
157 classNames.push('enabled');
158 }
159 if (this.commandIsToggled(commandId)) {
160 classNames.push('toggled');
161 }
162 }
163 return classNames;
164 }
165 renderMore() {
166 return !!this.more.size && React.createElement("div", { key: '__more__', className: TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR_ITEM + ' enabled' },
167 React.createElement("div", { id: '__more__', className: (0, widgets_1.codicon)('ellipsis', true), onClick: this.showMoreContextMenu, title: common_1.nls.localizeByDefault('More Actions...') }));
168 }
169 toAnchor(event) {
170 var _a;
171 const itemBox = (_a = event.currentTarget.closest('.' + TabBarToolbar_1.Styles.TAB_BAR_TOOLBAR_ITEM)) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
172 return itemBox ? { y: itemBox.bottom, x: itemBox.left } : event.nativeEvent;
173 }
174 renderMoreContextMenu(anchor, subpath) {
175 var _a, _b;
176 const toDisposeOnHide = new common_1.DisposableCollection();
177 this.addClass('menu-open');
178 toDisposeOnHide.push(common_1.Disposable.create(() => this.removeClass('menu-open')));
179 if (subpath) {
180 toDisposeOnHide.push(this.menus.linkSubmenu(tab_bar_toolbar_types_1.TAB_BAR_TOOLBAR_CONTEXT_MENU, subpath));
181 }
182 else {
183 for (const item of this.more.values()) {
184 if (item.menuPath && !item.command) {
185 toDisposeOnHide.push(this.menus.linkSubmenu(tab_bar_toolbar_types_1.TAB_BAR_TOOLBAR_CONTEXT_MENU, item.menuPath, undefined, item.group));
186 }
187 else if (item.command) {
188 // Register a submenu for the item, if the group is in format `<submenu group>/<submenu name>/.../<item group>`
189 if ((_a = item.group) === null || _a === void 0 ? void 0 : _a.includes('/')) {
190 const split = item.group.split('/');
191 const paths = [];
192 for (let i = 0; i < split.length - 1; i += 2) {
193 paths.push(split[i], split[i + 1]);
194 toDisposeOnHide.push(this.menus.registerSubmenu([...tab_bar_toolbar_types_1.TAB_BAR_TOOLBAR_CONTEXT_MENU, ...paths], split[i + 1], { order: item.order }));
195 }
196 }
197 toDisposeOnHide.push(this.menus.registerMenuAction([...tab_bar_toolbar_types_1.TAB_BAR_TOOLBAR_CONTEXT_MENU, ...item.group.split('/')], {
198 label: item.tooltip,
199 commandId: item.command,
200 when: item.when,
201 order: item.order,
202 }));
203 }
204 }
205 }
206 return this.contextMenuRenderer.render({
207 menuPath: tab_bar_toolbar_types_1.TAB_BAR_TOOLBAR_CONTEXT_MENU,
208 args: [this.current],
209 anchor,
210 context: (_b = this.current) === null || _b === void 0 ? void 0 : _b.node,
211 onHide: () => toDisposeOnHide.dispose()
212 });
213 }
214 shouldHandleMouseEvent(event) {
215 return event.target instanceof Element && this.node.contains(event.target);
216 }
217 commandIsEnabled(command) {
218 return this.commands.isEnabled(command, this.current);
219 }
220 commandIsToggled(command) {
221 return this.commands.isToggled(command, this.current);
222 }
223};
224__decorate([
225 (0, inversify_1.inject)(common_1.CommandRegistry),
226 __metadata("design:type", common_1.CommandRegistry)
227], TabBarToolbar.prototype, "commands", void 0);
228__decorate([
229 (0, inversify_1.inject)(label_parser_1.LabelParser),
230 __metadata("design:type", label_parser_1.LabelParser)
231], TabBarToolbar.prototype, "labelParser", void 0);
232__decorate([
233 (0, inversify_1.inject)(common_1.MenuModelRegistry),
234 __metadata("design:type", common_1.MenuModelRegistry)
235], TabBarToolbar.prototype, "menus", void 0);
236__decorate([
237 (0, inversify_1.inject)(common_1.MenuCommandExecutor),
238 __metadata("design:type", Object)
239], TabBarToolbar.prototype, "menuCommandExecutor", void 0);
240__decorate([
241 (0, inversify_1.inject)(context_menu_renderer_1.ContextMenuRenderer),
242 __metadata("design:type", context_menu_renderer_1.ContextMenuRenderer)
243], TabBarToolbar.prototype, "contextMenuRenderer", void 0);
244__decorate([
245 (0, inversify_1.inject)(tab_bar_toolbar_registry_1.TabBarToolbarRegistry),
246 __metadata("design:type", tab_bar_toolbar_registry_1.TabBarToolbarRegistry)
247], TabBarToolbar.prototype, "toolbarRegistry", void 0);
248TabBarToolbar = TabBarToolbar_1 = __decorate([
249 (0, inversify_1.injectable)(),
250 __metadata("design:paramtypes", [])
251], TabBarToolbar);
252exports.TabBarToolbar = TabBarToolbar;
253(function (TabBarToolbar) {
254 let Styles;
255 (function (Styles) {
256 Styles.TAB_BAR_TOOLBAR = 'p-TabBar-toolbar';
257 Styles.TAB_BAR_TOOLBAR_ITEM = 'item';
258 })(Styles = TabBarToolbar.Styles || (TabBarToolbar.Styles = {}));
259})(TabBarToolbar = exports.TabBarToolbar || (exports.TabBarToolbar = {}));
260exports.TabBarToolbar = TabBarToolbar;
261//# sourceMappingURL=tab-bar-toolbar.js.map
\No newline at end of file