/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ import { Directive, Input, ElementRef, OnInit, Inject } from '@angular/core'; import { IUserAuthStateService, IUSERAUTHSTATESERVICE_TOKEN } from '../interfaces/IUserAuthStateService.interface'; import * as _ from 'underscore'; import {} from 'signalr'; declare var $:JQueryStatic; @Directive({ selector: '[userLevelCheck]' }) export class UserLevelCheckDirective implements OnInit { @Input('userLevelCheck') requiredUserLevel: string; private _overlayId: string; constructor(private _el: ElementRef, @Inject(IUSERAUTHSTATESERVICE_TOKEN) private _userAuthStateService: IUserAuthStateService) {} ngOnInit() { this.doLevelCheck(); } doLevelCheck() { this._userAuthStateService.checkRequestedLevel(this.requiredUserLevel, (username: string, level: string, success: boolean) => { if (!success) { this.disableHostElement(); } else { this.enableHostElement(); } }); } private disableHostElement(): void { if (this._el.nativeElement.disabled) { return; } let overlayContainer = null; let overlaySizeTemplate = null; if (this._el.nativeElement instanceof HTMLUnknownElement) { overlayContainer = $(this._el.nativeElement.firstElementChild); overlaySizeTemplate = overlayContainer; } else { overlayContainer = $(this._el.nativeElement.parentElement); overlaySizeTemplate = $(this._el.nativeElement); } this._overlayId = _.uniqueId(); let overlay = $("
"); overlay.css({ position: "absolute", top: overlaySizeTemplate.position().top, left: overlaySizeTemplate.position().left, width: overlaySizeTemplate.outerWidth(), height: overlaySizeTemplate.outerHeight(), zIndex: overlaySizeTemplate.css("z-index") + 1, backgroundColor: "#fff", opacity: 0 }); overlay.on('click', (e, args) => { this.doLevelCheck(); }) overlayContainer.append(overlay); this._el.nativeElement.disabled = true; } private enableHostElement(): void { if (!this._el.nativeElement.disabled) { return; } $("div[id=" + this._overlayId + "]").remove(); this._el.nativeElement.disabled = false; } }