166 lines
6.1 KiB
JavaScript
166 lines
6.1 KiB
JavaScript
import { Directive, ElementRef, Input, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ContentChildren, Output, NgModule } from '@angular/core';
|
|
import { DomHandler } from 'primeng/dom';
|
|
import { CommonModule } from '@angular/common';
|
|
import { RippleModule } from 'primeng/ripple';
|
|
import { PrimeTemplate } from 'primeng/api';
|
|
|
|
class ButtonDirective {
|
|
constructor(el) {
|
|
this.el = el;
|
|
this.iconPos = 'left';
|
|
}
|
|
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() {
|
|
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;
|
|
}
|
|
get label() {
|
|
return this._label;
|
|
}
|
|
set label(val) {
|
|
this._label = val;
|
|
if (this.initialized) {
|
|
DomHandler.findSingle(this.el.nativeElement, '.p-button-label').textContent = this._label || ' ';
|
|
this.setStyleClass();
|
|
}
|
|
}
|
|
get icon() {
|
|
return this._icon;
|
|
}
|
|
set icon(val) {
|
|
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;
|
|
}
|
|
}
|
|
ButtonDirective.decorators = [
|
|
{ type: Directive, args: [{
|
|
selector: '[pButton]'
|
|
},] }
|
|
];
|
|
ButtonDirective.ctorParameters = () => [
|
|
{ type: ElementRef }
|
|
];
|
|
ButtonDirective.propDecorators = {
|
|
iconPos: [{ type: Input }],
|
|
label: [{ type: Input }],
|
|
icon: [{ type: Input }]
|
|
};
|
|
class Button {
|
|
constructor() {
|
|
this.type = "button";
|
|
this.iconPos = 'left';
|
|
this.onClick = new EventEmitter();
|
|
this.onFocus = new EventEmitter();
|
|
this.onBlur = new EventEmitter();
|
|
}
|
|
ngAfterContentInit() {
|
|
this.templates.forEach((item) => {
|
|
switch (item.getType()) {
|
|
case 'content':
|
|
this.contentTemplate = item.template;
|
|
break;
|
|
default:
|
|
this.contentTemplate = item.template;
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
Button.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-button',
|
|
template: `
|
|
<button [attr.type]="type" [class]="styleClass" [ngStyle]="style" [disabled]="disabled"
|
|
[ngClass]="{'p-button p-component':true,
|
|
'p-button-icon-only': (icon && !label),
|
|
'p-button-vertical': (iconPos === 'top' || iconPos === 'bottom') && label}"
|
|
(click)="onClick.emit($event)" (focus)="onFocus.emit($event)" (blur)="onBlur.emit($event)" pRipple>
|
|
<ng-content></ng-content>
|
|
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
|
|
<span [ngClass]="{'p-button-icon': true,
|
|
'p-button-icon-left': iconPos === 'left' && label,
|
|
'p-button-icon-right': iconPos === 'right' && label,
|
|
'p-button-icon-top': iconPos === 'top' && label,
|
|
'p-button-icon-bottom': iconPos === 'bottom' && label}"
|
|
[class]="icon" *ngIf="icon" [attr.aria-hidden]="true"></span>
|
|
<span class="p-button-label" [attr.aria-hidden]="icon && !label">{{label||' '}}</span>
|
|
<span [ngClass]="'p-badge'" *ngIf="badge" [class]="badgeClass">{{badge}}</span>
|
|
</button>
|
|
`,
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
encapsulation: ViewEncapsulation.None
|
|
},] }
|
|
];
|
|
Button.propDecorators = {
|
|
type: [{ type: Input }],
|
|
iconPos: [{ type: Input }],
|
|
icon: [{ type: Input }],
|
|
badge: [{ type: Input }],
|
|
label: [{ type: Input }],
|
|
disabled: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
badgeClass: [{ type: Input }],
|
|
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }],
|
|
onClick: [{ type: Output }],
|
|
onFocus: [{ type: Output }],
|
|
onBlur: [{ type: Output }]
|
|
};
|
|
class ButtonModule {
|
|
}
|
|
ButtonModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule, RippleModule],
|
|
exports: [ButtonDirective, Button],
|
|
declarations: [ButtonDirective, Button]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { Button, ButtonDirective, ButtonModule };
|
|
//# sourceMappingURL=primeng-button.js.map
|