coreui-angular-ex
Version:
CoreUI Components Library for Angular
179 lines (157 loc) • 4.78 kB
text/typescript
import {
AfterContentInit,
Component,
ContentChildren,
EventEmitter,
HostBinding,
HostListener,
Input,
Output,
QueryList
} from '@angular/core';
import { NgIf, NgTemplateOutlet } from '@angular/common';
import { animate, AnimationEvent, state, style, transition, trigger } from '@angular/animations';
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
import { Colors } from '../coreui.types';
import { TemplateIdDirective } from '../shared';
import { ButtonCloseDirective } from '../button';
type AnimateType = ('hide' | 'show');
export class AlertComponent implements AfterContentInit {
static ngAcceptInputType_dismissible: BooleanInput;
static ngAcceptInputType_fade: BooleanInput;
static ngAcceptInputType_visible: BooleanInput;
hide!: boolean;
/**
* Sets the color context of the component to one of CoreUI’s themed colors.
*
* @type Colors
* @default 'primary'
*/
color: Colors = 'primary';
/**
* Default role for alert. [docs]
* @type string
* @default 'alert'
*/
role = 'alert';
/**
* Set the alert variant to a solid.
* @type string
*/
variant?: 'solid' | string;
/**
* Event triggered on the alert dismiss.
*/
visibleChange: EventEmitter<boolean> = new EventEmitter();
templates: any = {};
contentTemplates!: QueryList<TemplateIdDirective>;
private _dismissible = false;
/**
* Optionally adds a close button to alert and allow it to self dismiss.
* @type boolean
*/
get dismissible(): boolean {
return this._dismissible;
}
set dismissible(value: boolean) {
this._dismissible = coerceBooleanProperty(value);
}
private _fade = false;
/**
* Adds animation for dismissible alert.
* @type boolean
*/
get fade(): boolean {
return this._fade;
}
set fade(value: boolean) {
this._fade = coerceBooleanProperty(value);
}
private _visible = true;
get visible() {
return this._visible;
}
/**
* Toggle the visibility of alert component.
* @type boolean
*/
set visible(value: boolean) {
if (this._visible !== value) {
this._visible = coerceBooleanProperty(value);
this.visibleChange.emit(value);
}
};
get animationDisabled(): boolean {
return !this.fade;
}
get animateType(): AnimateType {
return this.visible ? 'show' : 'hide';
}
get hostClasses(): any {
return {
alert: true,
'alert-dismissible': this.dismissible,
fade: this.fade,
show: !this.hide,
[`alert-${this.color}`]: !!this.color && this.variant !== 'solid',
[`bg-${this.color}`]: !!this.color && this.variant === 'solid',
'text-white': !!this.color && this.variant === 'solid'
};
}
onAnimationStart($event: AnimationEvent): void {
this.onAnimationEvent($event);
}
onAnimationDone($event: AnimationEvent): void {
this.onAnimationEvent($event);
}
ngAfterContentInit(): void {
this.contentTemplates.forEach((child: TemplateIdDirective) => {
this.templates[child.id] = child.templateRef;
});
}
onAnimationEvent(event: AnimationEvent): void {
this.hide = event.phaseName === 'start' && event.toState === 'show';
if (event.phaseName === 'done') {
this.hide = (event.toState === 'hide' || event.toState === 'void');
if (event.toState === 'show') {
this.hide = false;
}
}
}
}