UNPKG

2.98 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2019 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { LocationProgress, ProgressLocationService } from './progress-location-service';
18import { DisposableCollection, Disposable } from '../common';
19import { injectable, inject, postConstruct } from 'inversify';
20import { ProgressBarOptions } from './progress-bar-factory';
21
22@injectable()
23export class ProgressBar implements Disposable {
24
25 @inject(ProgressLocationService)
26 protected readonly progressLocationService: ProgressLocationService;
27
28 @inject(ProgressBarOptions)
29 protected readonly options: ProgressBarOptions;
30
31 protected readonly toDispose = new DisposableCollection();
32 dispose(): void {
33 this.toDispose.dispose();
34 }
35
36 protected progressBar: HTMLDivElement;
37 protected progressBarContainer: HTMLDivElement;
38
39 constructor() {
40 this.progressBar = document.createElement('div');
41 this.progressBar.className = 'theia-progress-bar';
42 this.progressBar.style.display = 'none';
43 this.progressBarContainer = document.createElement('div');
44 this.progressBarContainer.className = 'theia-progress-bar-container';
45 this.progressBarContainer.append(this.progressBar);
46 }
47
48 @postConstruct()
49 protected init(): void {
50 const { container, insertMode, locationId } = this.options;
51 if (insertMode === 'prepend') {
52 container.prepend(this.progressBarContainer);
53 } else {
54 container.append(this.progressBarContainer);
55 }
56 this.toDispose.push(Disposable.create(() => this.progressBarContainer.remove()));
57 const onProgress = this.progressLocationService.onProgress(locationId);
58 this.toDispose.push(onProgress(event => this.onProgress(event)));
59 const current = this.progressLocationService.getProgress(locationId);
60 if (current) {
61 this.onProgress(current);
62 }
63 }
64
65 protected onProgress(event: LocationProgress): void {
66 if (this.toDispose.disposed) {
67 return;
68 }
69 this.setVisible(event.show);
70 }
71
72 protected setVisible(visible: boolean): void {
73 this.progressBar.style.display = visible ? 'block' : 'none';
74 }
75
76}