ng2-bootstrap-base-modified
Version:
Native Angular Bootstrap Components Typeahead modified
106 lines (91 loc) • 2.98 kB
text/typescript
// todo: add animations when https://github.com/angular/angular/issues/9947 solved
import {
Directive, ElementRef, EventEmitter, HostBinding, Input, OnInit, Output,
Renderer, style
} from '@angular/core';
export class CollapseDirective {
/** This event fires as soon as content collapses */
public collapsed: EventEmitter<any> = new EventEmitter();
/** This event fires as soon as content becomes visible */
public expanded: EventEmitter<any> = new EventEmitter();
public display: string;
// shown
public isExpanded: boolean = true;
// hidden
public isCollapsed: boolean = false;
// stale state
public isCollapse: boolean = true;
// animation state
public isCollapsing: boolean = false;
/** A flag indicating visibility of content (shown or hidden) */
public set collapse(value: boolean) {
this.isExpanded = value;
this.toggle();
}
public get collapse(): boolean {
return this.isExpanded;
}
protected _el: ElementRef;
protected _renderer: Renderer;
public constructor(_el: ElementRef, _renderer: Renderer) {
this._el = _el;
this._renderer = _renderer;
}
/** allows to manually toggle content visibility */
public toggle(): void {
if (this.isExpanded) {
this.hide();
} else {
this.show();
}
}
/** allows to manually hide content */
public hide(): void {
this.isCollapse = false;
this.isCollapsing = true;
this.isExpanded = false;
this.isCollapsed = true;
this.isCollapse = true;
this.isCollapsing = false;
this.display = 'none';
this.collapsed.emit(this);
}
/** allows to manually show collapsed content */
public show(): void {
this.isCollapse = false;
this.isCollapsing = true;
this.isExpanded = true;
this.isCollapsed = false;
this.display = 'block';
// this.height = 'auto';
this.isCollapse = true;
this.isCollapsing = false;
this._renderer.setElementStyle(this._el.nativeElement, 'overflow', 'visible');
this._renderer.setElementStyle(this._el.nativeElement, 'height', 'auto');
this.expanded.emit(this);
}
}