angular/devtools/projects/ng-devtools/src/lib/devtools.component.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {animate, style, transition, trigger} from '@angular/animations';
import {Platform} from '@angular/cdk/platform';
import {DOCUMENT} from '@angular/common';
import {Component, Inject, OnDestroy, OnInit} from '@angular/core';
import {Events, MessageBus} from 'protocol';
import {interval} from 'rxjs';
import {ThemeService} from './theme-service';
2020-01-27 18:40:18 +00:00
@Component({
selector: 'ng-devtools',
templateUrl: './devtools.component.html',
styleUrls: ['./devtools.component.scss'],
animations: [
trigger(
'enterAnimation',
[
transition(':enter', [style({opacity: 0}), animate('200ms', style({opacity: 1}))]),
transition(':leave', [style({opacity: 1}), animate('200ms', style({opacity: 0}))]),
]),
],
2020-01-27 18:40:18 +00:00
})
export class DevToolsComponent implements OnInit, OnDestroy {
angularExists: boolean|null = null;
angularVersion: string|boolean|undefined = undefined;
angularIsInDevMode = true;
ivy!: boolean;
private readonly _firefoxStyleName = 'firefox_styles.css';
private readonly _chromeStyleName = 'chrome_styles.css';
constructor(
private _messageBus: MessageBus<Events>, private _themeService: ThemeService,
private _platform: Platform, @Inject(DOCUMENT) private _document: Document) {}
2020-01-27 18:40:18 +00:00
private _interval$ = interval(500).subscribe((attempt) => {
if (attempt === 10) {
this.angularExists = false;
}
this._messageBus.emit('queryNgAvailability');
});
2020-01-27 18:40:18 +00:00
ngOnInit(): void {
this._themeService.initializeThemeWatcher();
this._messageBus.once('ngAvailability', ({version, devMode, ivy}) => {
this.angularExists = !!version;
this.angularVersion = version;
this.angularIsInDevMode = devMode;
2020-04-20 17:13:18 +00:00
this.ivy = ivy;
2020-01-27 18:40:18 +00:00
this._interval$.unsubscribe();
});
const browserStyleName =
this._platform.FIREFOX ? this._firefoxStyleName : this._chromeStyleName;
this._loadStyle(browserStyleName);
2020-01-27 18:40:18 +00:00
}
get majorAngularVersion(): number {
if (!this.angularVersion) {
return -1;
}
2020-04-20 17:13:18 +00:00
return parseInt(this.angularVersion.toString().split('.')[0], 10);
}
get supportedVersion(): boolean {
2020-04-20 17:13:18 +00:00
return (this.majorAngularVersion >= 9 || this.majorAngularVersion === 0) && this.ivy;
}
/** Add a style file in header based on fileName */
private _loadStyle(styleName: string) {
const head = this._document.getElementsByTagName('head')[0];
const style = this._document.createElement('link');
style.rel = 'stylesheet';
style.href = `./styles/${styleName}`;
head.appendChild(style);
}
ngOnDestroy(): void {
2020-01-27 18:40:18 +00:00
this._interval$.unsubscribe();
}
}