-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathbutton.ts
425 lines (391 loc) · 13.2 KB
/
button.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { Property, NotifyPropertyChanges, INotifyPropertyChanged, Component, isBlazor, isRippleEnabled } from '@syncfusion/ej2-base';
import { addClass, Event, EmitType, detach, removeClass } from '@syncfusion/ej2-base';
import { rippleEffect, EventHandler, Observer, SanitizeHtmlHelper } from '@syncfusion/ej2-base';
import { ButtonModel } from './button-model';
import { getTextNode } from '../common/common';
/**
* Defines the icon position of button.
*/
export enum IconPosition {
/**
* Positions the Icon at the left of the text content in the Button.
*/
Left = 'Left',
/**
* Positions the Icon at the right of the text content in the Button.
*/
Right = 'Right',
/**
* Positions the Icon at the top of the text content in the Button.
*/
Top = 'Top',
/**
* Positions the Icon at the bottom of the text content in the Button.
*/
Bottom = 'Bottom',
}
export const buttonObserver: Observer = new Observer();
const cssClassName: CssClassNameT = {
RTL: 'e-rtl',
BUTTON: 'e-btn',
PRIMARY: 'e-primary',
ICONBTN: 'e-icon-btn'
};
/**
* The Button is a graphical user interface element that triggers an event on its click action. It can contain a text, an image, or both.
* ```html
* <button id="button">Button</button>
* ```
* ```typescript
* <script>
* var btnObj = new Button();
* btnObj.appendTo("#button");
* </script>
* ```
*/
@NotifyPropertyChanges
export class Button extends Component<HTMLButtonElement> implements INotifyPropertyChanged {
private removeRippleEffect: Function;
/**
* Positions the icon before/after the text content in the Button.
* The possible values are:
* * Left: The icon will be positioned to the left of the text content.
* * Right: The icon will be positioned to the right of the text content.
*
* @isenumeration true
* @default IconPosition.Left
* @asptype IconPosition
*/
@Property('Left')
public iconPosition: string | IconPosition;
/**
* Defines class/multiple classes separated by a space for the Button that is used to include an icon.
* Buttons can also include font icon and sprite image.
*
* @default ""
*/
@Property('')
public iconCss: string;
/**
* Specifies a value that indicates whether the Button is `disabled` or not.
*
* @default false.
*/
@Property(false)
public disabled: boolean;
/**
* Allows the appearance of the Button to be enhanced and visually appealing when set to `true`.
*
* @default false
*/
@Property(false)
public isPrimary: boolean;
/**
* Defines class/multiple classes separated by a space in the Button element. The Button types, styles, and
* size can be defined by using
* [`this`](http://ej2.syncfusion.com/documentation/button/howto.html?lang=typescript#create-a-block-button).
* {% codeBlock src='button/cssClass/index.md' %}{% endcodeBlock %}
*
* @default ""
*/
@Property('')
public cssClass: string;
/**
* Defines the text `content` of the Button element.
* {% codeBlock src='button/content/index.md' %}{% endcodeBlock %}
*
* @default ""
*/
@Property('')
public content: string;
/**
* Makes the Button toggle, when set to `true`. When you click it, the state changes from normal to active.
*
* @default false
*/
@Property(false)
public isToggle: boolean;
/**
* Overrides the global culture and localization value for this component. Default global culture is 'en-US'.
*
* @private
*/
@Property()
public locale: string;
/**
* Specifies whether to enable the rendering of untrusted HTML values in the Button component.
* If 'enableHtmlSanitizer' set to true, the component will sanitize any suspected untrusted strings and scripts before rendering them.
*
* @default true
*/
@Property(true)
public enableHtmlSanitizer: boolean;
/**
* Triggers once the component rendering is completed.
*
* @event created
*/
@Event()
public created: EmitType<Event>;
/**
* Constructor for creating the widget
*
* @param {ButtonModel} options - Specifies the button model
* @param {string|HTMLButtonElement} element - Specifies the target element
*/
constructor(options?: ButtonModel, element?: string | HTMLButtonElement) {
super(options, <string | HTMLButtonElement>element);
}
protected preRender(): void {
// pre render code snippets
}
/**
* Initialize the control rendering
*
* @returns {void}
* @private
*/
public render(): void {
this.initialize();
this.removeRippleEffect = rippleEffect(this.element, { selector: '.' + cssClassName.BUTTON });
this.renderComplete();
}
private initialize(): void {
if (this.cssClass) {
addClass([this.element], this.cssClass.replace(/\s+/g, ' ').trim().split(' '));
}
if (this.isPrimary) {
this.element.classList.add(cssClassName.PRIMARY);
}
if (!isBlazor() || (isBlazor() && this.getModuleName() !== 'progress-btn')) {
if (this.content) {
const tempContent: string = (this.enableHtmlSanitizer) ? SanitizeHtmlHelper.sanitize(this.content) : this.content;
this.element.innerHTML = tempContent;
}
this.setIconCss();
}
if (this.enableRtl) {
this.element.classList.add(cssClassName.RTL);
}
if (this.disabled) {
this.controlStatus(this.disabled);
} else {
this.wireEvents();
}
}
private controlStatus(disabled: boolean): void {
this.element.disabled = disabled;
}
private setIconCss(): void {
if (this.iconCss) {
const span: HTMLElement = this.createElement('span', { className: 'e-btn-icon ' + this.iconCss });
if (!this.element.textContent.trim()) {
this.element.classList.add(cssClassName.ICONBTN);
} else {
span.classList.add('e-icon-' + this.iconPosition.toLowerCase());
if (this.iconPosition === 'Top' || this.iconPosition === 'Bottom') {
this.element.classList.add('e-' + this.iconPosition.toLowerCase() + '-icon-btn');
}
}
const node: Node = this.element.childNodes[0];
if (node && (this.iconPosition === 'Left' || this.iconPosition === 'Top')) {
this.element.insertBefore(span, node);
} else {
this.element.appendChild(span);
}
}
}
protected wireEvents(): void {
if (this.isToggle) {
EventHandler.add(this.element, 'click', this.btnClickHandler, this);
}
}
protected unWireEvents(): void {
if (this.isToggle) {
EventHandler.remove(this.element, 'click', this.btnClickHandler);
}
}
private btnClickHandler(): void {
if (this.element.classList.contains('e-active')) {
this.element.classList.remove('e-active');
} else {
this.element.classList.add('e-active');
}
}
/**
* Destroys the widget.
*
* @returns {void}
*/
public destroy(): void {
let classList: string[] = [cssClassName.PRIMARY, cssClassName.RTL, cssClassName.ICONBTN, 'e-success', 'e-info', 'e-danger',
'e-warning', 'e-flat', 'e-outline', 'e-small', 'e-bigger', 'e-active', 'e-round',
'e-top-icon-btn', 'e-bottom-icon-btn'];
if (this.cssClass) {
classList = classList.concat(this.cssClass.split(/\s+/).filter((c: string) => c.length > 0));
}
super.destroy();
removeClass([this.element], classList);
if (!this.element.getAttribute('class')) {
this.element.removeAttribute('class');
}
if (this.disabled) {
this.element.removeAttribute('disabled');
}
if (this.content) {
this.element.innerHTML = this.element.innerHTML.replace(this.content, '');
}
const span: Element = this.element.querySelector('span.e-btn-icon') as Element;
if (span) {
detach(span);
}
this.unWireEvents();
if (isRippleEnabled) {
this.removeRippleEffect();
}
}
/**
* Get component name.
*
* @returns {string} - Module name
* @private
*/
public getModuleName(): string {
return 'btn';
}
/**
* Get the properties to be maintained in the persisted state.
*
* @returns {string} - Persist Data
* @private
*/
public getPersistData(): string {
return this.addOnPersist([]);
}
/**
* Dynamically injects the required modules to the component.
*
* @private
* @returns {void}
*/
public static Inject(): void {
// Inject code snippets
}
/**
* Called internally if any of the property value changed.
*
* @param {ButtonModel} newProp - Specifies new properties
* @param {ButtonModel} oldProp - Specifies old properties
* @returns {void}
* @private
*/
public onPropertyChanged(newProp: ButtonModel, oldProp: ButtonModel): void {
let span: Element = this.element.querySelector('span.e-btn-icon') as Element;
for (const prop of Object.keys(newProp)) {
switch (prop) {
case 'isPrimary':
if (newProp.isPrimary) {
this.element.classList.add(cssClassName.PRIMARY);
} else {
this.element.classList.remove(cssClassName.PRIMARY);
}
break;
case 'disabled':
this.controlStatus(newProp.disabled as boolean);
break;
case 'iconCss': {
span = this.element.querySelector('span.e-btn-icon') as Element;
if (span) {
if (newProp.iconCss) {
span.className = 'e-btn-icon ' + newProp.iconCss;
if (this.element.textContent.trim()) {
if (this.iconPosition === 'Left') {
span.classList.add('e-icon-left');
} else {
span.classList.add('e-icon-right');
}
}
} else {
detach(span);
}
} else {
this.setIconCss();
}
break;
}
case 'iconPosition':
removeClass([this.element], ['e-top-icon-btn', 'e-bottom-icon-btn']);
span = this.element.querySelector('span.e-btn-icon') as Element;
if (span) {
detach(span);
}
this.setIconCss();
break;
case 'cssClass':
if (oldProp.cssClass) {
removeClass([this.element], oldProp.cssClass.split(/\s+/).filter((c: string) => c.length > 0));
}
if (newProp.cssClass) {
addClass([this.element], newProp.cssClass.replace(/\s+/g, ' ').trim().split(' '));
}
break;
case 'enableRtl':
if (newProp.enableRtl) {
this.element.classList.add(cssClassName.RTL);
} else {
this.element.classList.remove(cssClassName.RTL);
}
break;
case 'content': {
const node: Node = getTextNode(this.element);
if (!node) {
this.element.classList.remove(cssClassName.ICONBTN);
}
if (!isBlazor() || (isBlazor() && !this.isServerRendered && this.getModuleName() !== 'progress-btn')) {
if (this.enableHtmlSanitizer) {
newProp.content = SanitizeHtmlHelper.sanitize(newProp.content as string);
}
this.element.innerHTML = newProp.content as string;
this.setIconCss();
}
break;
}
case 'isToggle':
if (newProp.isToggle) {
EventHandler.add(this.element, 'click', this.btnClickHandler, this);
} else {
EventHandler.remove(this.element, 'click', this.btnClickHandler);
removeClass([this.element], ['e-active']);
}
break;
}
}
}
/**
* Click the button element
* its native method
*
* @public
* @returns {void}
*/
public click(): void {
this.element.click();
}
/**
* Sets the focus to Button
* its native method
*
* @public
* @returns {void}
*/
public focusIn(): void {
this.element.focus();
}
}
interface CssClassNameT {
/** Defines the type of the classname. */
RTL: string;
BUTTON: string;
PRIMARY: string;
ICONBTN: string;
}