ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
44 lines (37 loc) • 1.14 kB
text/typescript
import { Directive, ElementRef, Input, OnDestroy, Renderer2 } from '@angular/core';
import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import { ACLService } from './acl.service';
import { ACLCanType } from './acl.type';
export class ACLDirective implements OnDestroy {
private _value: ACLCanType;
private change$: Subscription;
set acl(value: ACLCanType) {
this.set(value);
}
set ability(value: ACLCanType) {
this.set(this.srv.parseAbility(value));
}
private set(value: ACLCanType): void {
this._value = value;
const CLS = 'acl__hide';
const el = this.el.nativeElement;
if (this.srv.can(this._value)) {
this.renderer.removeClass(el, CLS);
} else {
this.renderer.addClass(el, CLS);
}
}
constructor(private el: ElementRef, private renderer: Renderer2, protected srv: ACLService) {
this.change$ = this.srv.change.pipe(filter(r => r != null)).subscribe(() => this.set(this._value));
}
ngOnDestroy(): void {
this.change$.unsubscribe();
}
}