clr-angular-static-fix
Version:
1. Install Clarity Icons package through npm:
150 lines (137 loc) • 5.68 kB
text/typescript
/*
* Copyright (c) 2016-2018 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import { animate, AnimationEvent, state, style, transition, trigger } from '@angular/animations';
import {
Component,
EventEmitter,
HostBinding,
HostListener,
Input,
OnChanges,
OnDestroy,
Output,
SimpleChange,
ViewChild,
} from '@angular/core';
import { FocusTrapDirective } from '../utils/focus-trap/focus-trap.directive';
import { ScrollingService } from '../utils/scrolling/scrolling-service';
import { GHOST_PAGE_ANIMATION } from './utils/ghost-page-animations';
export class ClrModal implements OnChanges, OnDestroy {
focusTrap: FocusTrapDirective;
_open: boolean = false;
_openChanged: EventEmitter<boolean> = new EventEmitter<boolean>(false);
closable: boolean = true;
size: string;
staticBackdrop: boolean = false;
skipAnimation: string = 'false';
// presently this is only used by wizards
ghostPageState: string = 'hidden';
bypassScrollService: boolean = false;
stopClose: boolean = false;
altClose: EventEmitter<boolean> = new EventEmitter<boolean>(false);
constructor(private _scrollingService: ScrollingService) {}
get sizeClass(): string {
if (this.size) {
return 'modal-' + this.size;
} else {
return '';
}
}
// Detect when _open is set to true and set no-scrolling to true
ngOnChanges(changes: { [propName: string]: SimpleChange }): void {
if (!this.bypassScrollService && changes && changes.hasOwnProperty('_open')) {
if (changes._open.currentValue) {
this._scrollingService.stopScrolling();
} else {
this._scrollingService.resumeScrolling();
}
}
}
ngOnDestroy(): void {
this._scrollingService.resumeScrolling();
}
open(): void {
if (this._open === true) {
return;
}
this._open = true;
this._openChanged.emit(true);
}
close(): void {
if (this.stopClose) {
this.altClose.emit(false);
return;
}
if (!this.closable || this._open === false) {
return;
}
this._open = false;
// todo: remove this after animation bug is fixed https://github.com/angular/angular/issues/15798
// this was handled by the fadeDone event below, but that AnimationEvent is not firing in Angular 4.0.
this._openChanged.emit(false);
// SPECME
this.focusTrap.setPreviousFocus(); // Handles moving focus back to the element that had it before.
}
fadeDone(e: AnimationEvent) {
if (e.toState === 'void') {
this._openChanged.emit(false);
}
}
}