UNPKG

8.15 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 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};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.BreadcrumbsRendererFactory = exports.BreadcrumbsRenderer = void 0;
28const React = require("react");
29const inversify_1 = require("inversify");
30const widgets_1 = require("../widgets");
31const breadcrumbs_service_1 = require("./breadcrumbs-service");
32const breadcrumb_renderer_1 = require("./breadcrumb-renderer");
33const perfect_scrollbar_1 = require("perfect-scrollbar");
34const common_1 = require("../../common");
35const core_preferences_1 = require("../core-preferences");
36const breadcrumbs_constants_1 = require("./breadcrumbs-constants");
37const label_provider_1 = require("../label-provider");
38let BreadcrumbsRenderer = class BreadcrumbsRenderer extends widgets_1.ReactRenderer {
39 constructor() {
40 super(...arguments);
41 this.onDidChangeActiveStateEmitter = new common_1.Emitter();
42 this.breadcrumbs = [];
43 this.refreshCancellationMarker = { canceled: true };
44 this.togglePopup = (breadcrumb, event) => {
45 var _a;
46 event.stopPropagation();
47 event.preventDefault();
48 let openPopup = true;
49 if ((_a = this.popup) === null || _a === void 0 ? void 0 : _a.isOpen) {
50 this.popup.dispose();
51 // There is a popup open. If the popup is the popup that belongs to the currently clicked breadcrumb
52 // just close the popup. If another breadcrumb was clicked, open the new popup immediately.
53 openPopup = this.popup.breadcrumbId !== breadcrumb.id;
54 }
55 else {
56 this.popup = undefined;
57 }
58 if (openPopup) {
59 const { currentTarget } = event;
60 const breadcrumbElement = currentTarget.closest(`.${breadcrumbs_constants_1.Styles.BREADCRUMB_ITEM}`);
61 if (breadcrumbElement) {
62 const { left: x, bottom: y } = breadcrumbElement.getBoundingClientRect();
63 this.breadcrumbsService.openPopup(breadcrumb, { x, y }).then(popup => { this.popup = popup; });
64 }
65 }
66 };
67 }
68 get onDidChangeActiveState() {
69 return this.onDidChangeActiveStateEmitter.event;
70 }
71 get active() {
72 return !!this.breadcrumbs.length;
73 }
74 get breadCrumbsContainer() {
75 var _a;
76 return (_a = this.host.firstElementChild) !== null && _a !== void 0 ? _a : undefined;
77 }
78 init() {
79 this.toDispose.push(this.onDidChangeActiveStateEmitter);
80 this.toDispose.push(this.breadcrumbsService.onDidChangeBreadcrumbs(uri => {
81 var _a;
82 if ((_a = this.uri) === null || _a === void 0 ? void 0 : _a.isEqual(uri)) {
83 this.refresh(this.uri);
84 }
85 }));
86 this.toDispose.push(this.corePreferences.onPreferenceChanged(change => {
87 if (change.preferenceName === 'breadcrumbs.enabled') {
88 this.refresh(this.uri);
89 }
90 }));
91 this.toDispose.push(this.labelProvider.onDidChange(() => this.refresh(this.uri)));
92 }
93 dispose() {
94 super.dispose();
95 this.toDispose.dispose();
96 if (this.popup) {
97 this.popup.dispose();
98 }
99 if (this.scrollbar) {
100 this.scrollbar.destroy();
101 this.scrollbar = undefined;
102 }
103 }
104 async refresh(uri) {
105 this.uri = uri;
106 this.refreshCancellationMarker.canceled = true;
107 const currentCallCanceled = { canceled: false };
108 this.refreshCancellationMarker = currentCallCanceled;
109 let breadcrumbs;
110 if (uri && this.corePreferences['breadcrumbs.enabled']) {
111 breadcrumbs = await this.breadcrumbsService.getBreadcrumbs(uri);
112 }
113 else {
114 breadcrumbs = [];
115 }
116 if (currentCallCanceled.canceled) {
117 return;
118 }
119 const wasActive = this.active;
120 this.breadcrumbs = breadcrumbs;
121 const isActive = this.active;
122 if (wasActive !== isActive) {
123 this.onDidChangeActiveStateEmitter.fire(isActive);
124 }
125 this.update();
126 }
127 update() {
128 this.render();
129 if (!this.scrollbar) {
130 this.createScrollbar();
131 }
132 else {
133 this.scrollbar.update();
134 }
135 this.scrollToEnd();
136 }
137 createScrollbar() {
138 const { breadCrumbsContainer } = this;
139 if (breadCrumbsContainer) {
140 this.scrollbar = new perfect_scrollbar_1.default(breadCrumbsContainer, {
141 handlers: ['drag-thumb', 'keyboard', 'wheel', 'touch'],
142 useBothWheelAxes: true,
143 scrollXMarginOffset: 4,
144 suppressScrollY: true
145 });
146 }
147 }
148 scrollToEnd() {
149 const { breadCrumbsContainer } = this;
150 if (breadCrumbsContainer) {
151 breadCrumbsContainer.scrollLeft = breadCrumbsContainer.scrollWidth;
152 }
153 }
154 doRender() {
155 return React.createElement("ul", { className: breadcrumbs_constants_1.Styles.BREADCRUMBS }, this.renderBreadcrumbs());
156 }
157 renderBreadcrumbs() {
158 return this.breadcrumbs.map(breadcrumb => this.breadcrumbRenderer.render(breadcrumb, this.togglePopup));
159 }
160};
161__decorate([
162 (0, inversify_1.inject)(breadcrumbs_service_1.BreadcrumbsService),
163 __metadata("design:type", breadcrumbs_service_1.BreadcrumbsService)
164], BreadcrumbsRenderer.prototype, "breadcrumbsService", void 0);
165__decorate([
166 (0, inversify_1.inject)(breadcrumb_renderer_1.BreadcrumbRenderer),
167 __metadata("design:type", Object)
168], BreadcrumbsRenderer.prototype, "breadcrumbRenderer", void 0);
169__decorate([
170 (0, inversify_1.inject)(core_preferences_1.CorePreferences),
171 __metadata("design:type", Object)
172], BreadcrumbsRenderer.prototype, "corePreferences", void 0);
173__decorate([
174 (0, inversify_1.inject)(label_provider_1.LabelProvider),
175 __metadata("design:type", label_provider_1.LabelProvider)
176], BreadcrumbsRenderer.prototype, "labelProvider", void 0);
177__decorate([
178 (0, inversify_1.postConstruct)(),
179 __metadata("design:type", Function),
180 __metadata("design:paramtypes", []),
181 __metadata("design:returntype", void 0)
182], BreadcrumbsRenderer.prototype, "init", null);
183BreadcrumbsRenderer = __decorate([
184 (0, inversify_1.injectable)()
185], BreadcrumbsRenderer);
186exports.BreadcrumbsRenderer = BreadcrumbsRenderer;
187exports.BreadcrumbsRendererFactory = Symbol('BreadcrumbsRendererFactory');
188//# sourceMappingURL=breadcrumbs-renderer.js.map
\No newline at end of file