all files / app/services/ unavailable-service.ts

30.77% Statements 8/26
0% Branches 0/10
25% Functions 1/4
25% Lines 6/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48            1× 1×         1×               1×                                           1×       1×  
/* Copyright CloudLinux Zug GmbH 2010-2017 All Rights Reserved
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 */
 
import {ApplicationRef, ComponentFactoryResolver, Inject, Injectable, ViewContainerRef} from '@angular/core';
import {UnavailableComponent} from '../unavailable.component';
import {ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot} from "@angular/router";
import {Observable} from "rxjs/Observable";
 
@Injectable()
export class UnavailableService implements CanActivate {
    component: UnavailableComponent;
 
    constructor(
        @Inject(ComponentFactoryResolver) private componentFactoryResolver: ComponentFactoryResolver,
        @Inject(ApplicationRef) private appRef: ApplicationRef
    ) {}
 
    public showPage(error) {
        const appInstance = this.appRef.components[0].instance;
        appInstance.routerOutlet.deactivate();
        if (appInstance.tabHeader.nativeElement.parentNode !== null) {
            appInstance.tabHeader.nativeElement.remove();
        }
        if (appInstance.tabHeaderFixed && appInstance.tabHeaderFixed.nativeElement.parentNode !== null) {
            appInstance.tabHeaderFixed.nativeElement.remove();
        }
        if (!this.component) {
            const factory = this.componentFactoryResolver.resolveComponentFactory(UnavailableComponent);
            const ref = appInstance.viewRef.createComponent(factory);
            ref.changeDetectorRef.detectChanges();
            this.component = ref.instance;
        }
        this.component.errorText = error.error_id;
        this.component.errorContext = error.context;
        if (error.icon) {
            this.component.icon = error.icon;
        }
    }
 
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        return !this.component;
    }
 
}