1 import {AfterViewInit, Directive, ElementRef, Input, OnDestroy} from '@angular/core';
3 import {DomHandler} from '../domhandler';
5 import {PlxButtonState} from './button-state';
7 @Directive({selector: '[pxButton]', providers: [DomHandler]})
8 export class PlxButtonDirective implements AfterViewInit, OnDestroy {
9 @Input() iconPos: string = 'left';
11 @Input() cornerStyleClass: string = 'ui-corner-all';
15 _loadinglabel: string;
24 public el: ElementRef, public domHandler: DomHandler) {}
27 this.domHandler.addMultipleClasses(
28 this.el.nativeElement, this.getStyleClass());
30 let iconElement = document.createElement('span');
31 let iconPosClass = (this.iconPos === 'right') ? 'ui-button-icon-right' :
32 'ui-button-icon-left';
33 iconElement.className =
34 iconPosClass + ' ui-c iconfont plx-icon-' + this.icon;
35 this.el.nativeElement.appendChild(iconElement);
38 let iconAnimationElement = document.createElement('span');
39 iconAnimationElement.className =
40 'ui-button-icon-left ui-c iconfont plx-icon-circle-o-notch plx-spin';
41 iconAnimationElement.style.display = 'none';
42 this.el.nativeElement.appendChild(iconAnimationElement);
44 let labelElement = document.createElement('span');
45 labelElement.className = 'ui-button-text ui-c';
46 labelElement.appendChild(document.createTextNode(this.label || ''));
47 this.el.nativeElement.appendChild(labelElement);
51 this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text');
52 if (this.state === PlxButtonState.DOING) {
54 spanElement.innerText = this.loadinglabel || 'loading';
56 this.el.nativeElement.disabled = true;
57 this.setIconELement(true);
59 spanElement.innerText = this.label || '';
60 this.el.nativeElement.disabled = false;
61 this.setIconELement(false);
65 this.initialized = true;
68 getStyleClass(): string {
70 'ui-button ui-widget ui-state-default ' + this.cornerStyleClass;
72 if (this.label !== null && this.label !== undefined) {
73 if (this.iconPos === 'left') {
74 styleClass = styleClass + ' ui-button-text-icon-left';
76 styleClass = styleClass + ' ui-button-text-icon-right';
79 styleClass = styleClass + ' ui-button-icon-only';
82 styleClass = styleClass + ' ui-button-text-only';
88 setIconELement(isShowAnimation: boolean) {
89 let iconLeftElement = this.domHandler.findSingle(
90 this.el.nativeElement, '.ui-button-icon-left.iconfont');
91 if (iconLeftElement) {
92 iconLeftElement.style.display = isShowAnimation ? 'none' : 'inline-block';
94 let iconRightElement = this.domHandler.findSingle(
95 this.el.nativeElement, '.ui-button-icon-left.iconfont');
96 if (iconRightElement) {
97 iconRightElement.style.display =
98 isShowAnimation ? 'none' : 'inline-block';
100 let iconAnimationElement = this.domHandler.findSingle(
101 this.el.nativeElement, '.iconfont.plx-icon-circle-o-notch.plx-spin');
102 if (iconAnimationElement) {
103 iconAnimationElement.style.display =
104 isShowAnimation ? 'inline-block' : 'none';
109 get label(): string {
113 set label(val: string) {
116 if (this.initialized) {
117 this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text')
118 .textContent = this._label;
123 get loadinglabel(): string {
124 return this._loadinglabel;
127 set loadinglabel(val: string) {
128 this._loadinglabel = val;
136 set icon(val: string) {
139 if (this.initialized) {
140 let iconPosClass = (this.iconPos === 'right') ? 'ui-button-icon-right' :
141 'ui-button-icon-left';
142 this.domHandler.findSingle(this.el.nativeElement, '.iconfont').className =
143 iconPosClass + ' ui-c iconfont plx-icon-' + this.icon;
148 get state(): number {
152 set state(val: number) {
154 if (this.initialized) {
156 this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text');
157 if (this.state === PlxButtonState.DOING) {
159 spanElement.innerText = this.loadinglabel || 'loading';
161 this.el.nativeElement.disabled = true;
162 this.setIconELement(true);
164 spanElement.innerText = this.label || '';
165 this.el.nativeElement.disabled = false;
166 this.setIconELement(false);
172 while (this.el.nativeElement.hasChildNodes()) {
173 this.el.nativeElement.removeChild(this.el.nativeElement.lastChild);
176 this.initialized = false;