import {NgModule,Directive,Component,ElementRef,EventEmitter,AfterViewInit,Output,OnDestroy,Input,ChangeDetectionStrategy, ViewEncapsulation, ContentChildren, AfterContentInit, TemplateRef, QueryList} from '@angular/core'; import {DomHandler} from 'mirra-ui/dom'; import {CommonModule} from '@angular/common'; import {RippleModule} from 'mirra-ui/ripple'; import {PrimeTemplate} from 'mirra-ui/api'; @Directive({ selector: '[pButton]' }) export class ButtonDirective implements AfterViewInit, OnDestroy { @Input() iconPos: 'left' | 'right' | 'top' | 'bottom' = 'left'; public _label: string; public _icon: string; public initialized: boolean; public _initialStyleClass: string; constructor(public el: ElementRef) {} ngAfterViewInit() { this._initialStyleClass = this.el.nativeElement.className; DomHandler.addMultipleClasses(this.el.nativeElement, this.getStyleClass()); if (this.icon) { let iconElement = document.createElement("span"); iconElement.className = 'p-button-icon'; iconElement.setAttribute("aria-hidden", "true"); let iconPosClass = this.label ? 'p-button-icon-' + this.iconPos : null; if (iconPosClass) { DomHandler.addClass(iconElement, iconPosClass); } DomHandler.addMultipleClasses(iconElement, this.icon); this.el.nativeElement.appendChild(iconElement); } let labelElement = document.createElement("span"); if (this.icon && !this.label) { labelElement.setAttribute('aria-hidden', 'true'); } labelElement.className = 'p-button-label'; labelElement.appendChild(document.createTextNode(this.label||'')); this.el.nativeElement.appendChild(labelElement); this.initialized = true; } getStyleClass(): string { let styleClass = 'p-button p-component'; if (this.icon && !this.label) { styleClass = styleClass + ' p-button-icon-only'; } return styleClass; } setStyleClass() { let styleClass = this.getStyleClass(); this.el.nativeElement.className = styleClass + ' ' + this._initialStyleClass; } @Input() get label(): string { return this._label; } set label(val: string) { this._label = val; if (this.initialized) { DomHandler.findSingle(this.el.nativeElement, '.p-button-label').textContent = this._label || ''; this.setStyleClass(); } } @Input() get icon(): string { return this._icon; } set icon(val: string) { this._icon = val; if (this.initialized) { if (this.iconPos) DomHandler.findSingle(this.el.nativeElement, '.p-button-icon').className = 'p-button-icon p-button-icon-' + this.iconPos + ' ' + this._icon; else DomHandler.findSingle(this.el.nativeElement, '.p-button-icon').className = 'p-button-icon ' + this._icon; this.setStyleClass(); } } ngOnDestroy() { while(this.el.nativeElement.hasChildNodes()) { this.el.nativeElement.removeChild(this.el.nativeElement.lastChild); } this.initialized = false; } } @Component({ selector: 'p-button', template: ` `, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None }) export class Button implements AfterContentInit { @Input() type: string = "button"; @Input() iconPos: string = 'left'; @Input() icon: string; @Input() badge: string; @Input() label: string; @Input() disabled: boolean; @Input() style: any; @Input() styleClass: string; @Input() badgeClass: string; contentTemplate: TemplateRef; @ContentChildren(PrimeTemplate) templates: QueryList; @Output() onClick: EventEmitter = new EventEmitter(); @Output() onFocus: EventEmitter = new EventEmitter(); @Output() onBlur: EventEmitter = new EventEmitter(); ngAfterContentInit() { this.templates.forEach((item) => { switch(item.getType()) { case 'content': this.contentTemplate = item.template; break; default: this.contentTemplate = item.template; break; } }); } } @NgModule({ imports: [CommonModule,RippleModule], exports: [ButtonDirective,Button], declarations: [ButtonDirective,Button] }) export class ButtonModule { }