aboutsummaryrefslogtreecommitdiffstats
path: root/deprecated-workflow-designer/sdc-workflow-designer-ui/src/app/paletx/core/pxbutton/button.directive.ts
blob: 842b9fb4607243e44a1200c66202f26092c8b3bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {AfterViewInit, Directive, ElementRef, Input, OnDestroy} from '@angular/core';

import {DomHandler} from '../domhandler';

import {PlxButtonState} from './button-state';

@Directive({selector: '[pxButton]', providers: [DomHandler]})
export class PlxButtonDirective implements AfterViewInit, OnDestroy {
  @Input() iconPos: string = 'left';

  @Input() cornerStyleClass: string = 'ui-corner-all';

  _label: string;

  _loadinglabel: string;

  _icon: string;

  _state: number;

  initialized: boolean;

  constructor(
		public el: ElementRef, public domHandler: DomHandler) {}

  ngAfterViewInit() {
	this.domHandler.addMultipleClasses(
		this.el.nativeElement, this.getStyleClass());
	if (this.icon) {
		let iconElement = document.createElement('span');
		let iconPosClass = (this.iconPos === 'right') ? 'ui-button-icon-right' :
														'ui-button-icon-left';
		iconElement.className =
			iconPosClass + ' ui-c iconfont plx-icon-' + this.icon;
		this.el.nativeElement.appendChild(iconElement);
	}

	let iconAnimationElement = document.createElement('span');
	iconAnimationElement.className =
		'ui-button-icon-left ui-c iconfont plx-icon-circle-o-notch plx-spin';
	iconAnimationElement.style.display = 'none';
	this.el.nativeElement.appendChild(iconAnimationElement);

	let labelElement = document.createElement('span');
	labelElement.className = 'ui-button-text ui-c';
	labelElement.appendChild(document.createTextNode(this.label || ''));
	this.el.nativeElement.appendChild(labelElement);

	if (this.state) {
		let spanElement =
			this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text');
		if (this.state === PlxButtonState.DOING) {
		if (spanElement) {
			spanElement.innerText = this.loadinglabel || 'loading';
		}
		this.el.nativeElement.disabled = true;
		this.setIconELement(true);
		} else {
		spanElement.innerText = this.label || '';
		this.el.nativeElement.disabled = false;
		this.setIconELement(false);
		}
	}

	this.initialized = true;
  }

  getStyleClass(): string {
	let styleClass =
		'ui-button ui-widget ui-state-default ' + this.cornerStyleClass;
	if (this.icon) {
		if (this.label  !== null && this.label  !== undefined) {
		if (this.iconPos === 'left') {
			styleClass = styleClass + ' ui-button-text-icon-left';
		} else {
			styleClass = styleClass + ' ui-button-text-icon-right';
		}
		} else {
		styleClass = styleClass + ' ui-button-icon-only';
		}
	} else {
		styleClass = styleClass + ' ui-button-text-only';
	}

	return styleClass;
  }

  setIconELement(isShowAnimation: boolean) {
	let iconLeftElement = this.domHandler.findSingle(
		this.el.nativeElement, '.ui-button-icon-left.iconfont');
	if (iconLeftElement) {
		iconLeftElement.style.display = isShowAnimation ? 'none' : 'inline-block';
	}
	let iconRightElement = this.domHandler.findSingle(
		this.el.nativeElement, '.ui-button-icon-left.iconfont');
	if (iconRightElement) {
		iconRightElement.style.display =
			isShowAnimation ? 'none' : 'inline-block';
	}
	let iconAnimationElement = this.domHandler.findSingle(
		this.el.nativeElement, '.iconfont.plx-icon-circle-o-notch.plx-spin');
	if (iconAnimationElement) {
		iconAnimationElement.style.display =
			isShowAnimation ? 'inline-block' : 'none';
	}
  }

  @Input()
  get label(): string {
	return this._label;
  }

  set label(val: string) {
	this._label = val;

	if (this.initialized) {
		this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text')
			.textContent = this._label;
	}
  }

  @Input()
  get loadinglabel(): string {
	return this._loadinglabel;
  }

  set loadinglabel(val: string) {
	this._loadinglabel = val;
  }

  @Input()
  get icon(): string {
	return this._icon;
  }

  set icon(val: string) {
	this._icon = val;

	if (this.initialized) {
		let iconPosClass = (this.iconPos === 'right') ? 'ui-button-icon-right' :
														'ui-button-icon-left';
		this.domHandler.findSingle(this.el.nativeElement, '.iconfont').className =
			iconPosClass + ' ui-c iconfont plx-icon-' + this.icon;
	}
  }

  @Input()
  get state(): number {
	return this._state;
  }

  set state(val: number) {
	this._state = val;
	if (this.initialized) {
		let spanElement =
			this.domHandler.findSingle(this.el.nativeElement, '.ui-button-text');
		if (this.state === PlxButtonState.DOING) {
		if (spanElement) {
			spanElement.innerText = this.loadinglabel || 'loading';
		}
		this.el.nativeElement.disabled = true;
		this.setIconELement(true);
		} else {
		spanElement.innerText = this.label || '';
		this.el.nativeElement.disabled = false;
		this.setIconELement(false);
		}
	}
  }

  ngOnDestroy() {
	while (this.el.nativeElement.hasChildNodes()) {
		this.el.nativeElement.removeChild(this.el.nativeElement.lastChild);
	}

	this.initialized = false;
  }
}