UNPKG

primeng

Version:

[![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![npm downloads](https://img.shields.io/npm/dm/primeng.svg)](https://www.npmjs.com/package/primeng) [![Actions CI](https://github.com/primefaces/primeng/workflows/No

373 lines (369 loc) 18.2 kB
import * as i1 from '@angular/common'; import { DOCUMENT, CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { Directive, Inject, Input, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ContentChildren, Output, NgModule } from '@angular/core'; import { PrimeTemplate, SharedModule } from 'primeng/api'; import { DomHandler } from 'primeng/dom'; import * as i2 from 'primeng/ripple'; import { RippleModule } from 'primeng/ripple'; import { ObjectUtils } from 'primeng/utils'; import { SpinnerIcon } from 'primeng/icons/spinner'; const INTERNAL_BUTTON_CLASSES = { button: 'p-button', component: 'p-component', iconOnly: 'p-button-icon-only', disabled: 'p-disabled', loading: 'p-button-loading', labelOnly: 'p-button-loading-label-only' }; class ButtonDirective { constructor(el, document) { this.el = el; this.document = document; this.iconPos = 'left'; this._loading = false; this._internalClasses = Object.values(INTERNAL_BUTTON_CLASSES); this.spinnerIcon = `<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" class="p-icon-spin"> <g clip-path="url(#clip0_417_21408)"> <path d="M6.99701 14C5.85441 13.999 4.72939 13.7186 3.72012 13.1832C2.71084 12.6478 1.84795 11.8737 1.20673 10.9284C0.565504 9.98305 0.165424 8.89526 0.041387 7.75989C-0.0826496 6.62453 0.073125 5.47607 0.495122 4.4147C0.917119 3.35333 1.59252 2.4113 2.46241 1.67077C3.33229 0.930247 4.37024 0.413729 5.4857 0.166275C6.60117 -0.0811796 7.76026 -0.0520535 8.86188 0.251112C9.9635 0.554278 10.9742 1.12227 11.8057 1.90555C11.915 2.01493 11.9764 2.16319 11.9764 2.31778C11.9764 2.47236 11.915 2.62062 11.8057 2.73C11.7521 2.78503 11.688 2.82877 11.6171 2.85864C11.5463 2.8885 11.4702 2.90389 11.3933 2.90389C11.3165 2.90389 11.2404 2.8885 11.1695 2.85864C11.0987 2.82877 11.0346 2.78503 10.9809 2.73C9.9998 1.81273 8.73246 1.26138 7.39226 1.16876C6.05206 1.07615 4.72086 1.44794 3.62279 2.22152C2.52471 2.99511 1.72683 4.12325 1.36345 5.41602C1.00008 6.70879 1.09342 8.08723 1.62775 9.31926C2.16209 10.5513 3.10478 11.5617 4.29713 12.1803C5.48947 12.7989 6.85865 12.988 8.17414 12.7157C9.48963 12.4435 10.6711 11.7264 11.5196 10.6854C12.3681 9.64432 12.8319 8.34282 12.8328 7C12.8328 6.84529 12.8943 6.69692 13.0038 6.58752C13.1132 6.47812 13.2616 6.41667 13.4164 6.41667C13.5712 6.41667 13.7196 6.47812 13.8291 6.58752C13.9385 6.69692 14 6.84529 14 7C14 8.85651 13.2622 10.637 11.9489 11.9497C10.6356 13.2625 8.85432 14 6.99701 14Z" fill="currentColor" /> </g> <defs> <clipPath id="clip0_417_21408"> <rect width="14" height="14" fill="white" /> </clipPath> </defs> </svg>`; } get label() { return this._label; } set label(val) { this._label = val; if (this.initialized) { this.updateLabel(); this.updateIcon(); this.setStyleClass(); } } get icon() { return this._icon; } set icon(val) { this._icon = val; if (this.initialized) { this.updateIcon(); this.setStyleClass(); } } get loading() { return this._loading; } set loading(val) { this._loading = val; if (this.initialized) { this.updateIcon(); this.setStyleClass(); } } get htmlElement() { return this.el.nativeElement; } ngAfterViewInit() { DomHandler.addMultipleClasses(this.htmlElement, this.getStyleClass().join(' ')); this.createIcon(); this.createLabel(); this.initialized = true; } getStyleClass() { const styleClass = [INTERNAL_BUTTON_CLASSES.button, INTERNAL_BUTTON_CLASSES.component]; if (this.icon && !this.label && ObjectUtils.isEmpty(this.htmlElement.textContent)) { styleClass.push(INTERNAL_BUTTON_CLASSES.iconOnly); } if (this.loading) { styleClass.push(INTERNAL_BUTTON_CLASSES.disabled, INTERNAL_BUTTON_CLASSES.loading); if (!this.icon && this.label) { styleClass.push(INTERNAL_BUTTON_CLASSES.labelOnly); } } return styleClass; } setStyleClass() { const styleClass = this.getStyleClass(); this.htmlElement.classList.remove(...this._internalClasses); this.htmlElement.classList.add(...styleClass); } createLabel() { if (this.label) { let labelElement = this.document.createElement('span'); if (this.icon && !this.label) { labelElement.setAttribute('aria-hidden', 'true'); } labelElement.className = 'p-button-label'; labelElement.appendChild(this.document.createTextNode(this.label)); this.htmlElement.appendChild(labelElement); } } createIcon() { if (this.icon || this.loading) { let iconElement = this.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); } let iconClass = this.getIconClass(); if (iconClass) { DomHandler.addMultipleClasses(iconElement, iconClass); } if (!this.loadingIcon && this.loading) { iconElement.innerHTML = this.spinnerIcon; } this.htmlElement.insertBefore(iconElement, this.htmlElement.firstChild); } } updateLabel() { let labelElement = DomHandler.findSingle(this.htmlElement, '.p-button-label'); if (!this.label) { labelElement && this.htmlElement.removeChild(labelElement); return; } labelElement ? (labelElement.textContent = this.label) : this.createLabel(); } updateIcon() { let iconElement = DomHandler.findSingle(this.htmlElement, '.p-button-icon'); if (!this.icon && !this.loading) { iconElement && this.htmlElement.removeChild(iconElement); return; } if (iconElement) { if (this.iconPos) iconElement.className = 'p-button-icon p-button-icon-' + this.iconPos + ' ' + this.getIconClass(); else iconElement.className = 'p-button-icon ' + this.getIconClass(); } else { this.createIcon(); } } getIconClass() { return this.loading ? 'p-button-loading-icon ' + (this.loadingIcon ? this.loadingIcon : 'p-icon') : this._icon; } ngOnDestroy() { this.initialized = false; } } ButtonDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: ButtonDirective, deps: [{ token: i0.ElementRef }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Directive }); ButtonDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.4", type: ButtonDirective, selector: "[pButton]", inputs: { iconPos: "iconPos", loadingIcon: "loadingIcon", label: "label", icon: "icon", loading: "loading" }, host: { classAttribute: "p-element" }, ngImport: i0 }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: ButtonDirective, decorators: [{ type: Directive, args: [{ selector: '[pButton]', host: { class: 'p-element' } }] }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: Document, decorators: [{ type: Inject, args: [DOCUMENT] }] }]; }, propDecorators: { iconPos: [{ type: Input }], loadingIcon: [{ type: Input }], label: [{ type: Input }], icon: [{ type: Input }], loading: [{ type: Input }] } }); class Button { constructor() { this.type = 'button'; this.iconPos = 'left'; this.loading = false; this.onClick = new EventEmitter(); this.onFocus = new EventEmitter(); this.onBlur = new EventEmitter(); } iconClass() { return { 'p-button-icon': true, 'p-button-icon-left': this.iconPos === 'left' && this.label, 'p-button-icon-right': this.iconPos === 'right' && this.label, 'p-button-icon-top': this.iconPos === 'top' && this.label, 'p-button-icon-bottom': this.iconPos === 'bottom' && this.label }; } buttonClass() { return { 'p-button p-component': true, 'p-button-icon-only': this.icon && !this.label, 'p-button-vertical': (this.iconPos === 'top' || this.iconPos === 'bottom') && this.label, 'p-disabled': this.disabled || this.loading, 'p-button-loading': this.loading, 'p-button-loading-label-only': this.loading && !this.icon && this.label }; } ngAfterContentInit() { this.templates.forEach((item) => { switch (item.getType()) { case 'content': this.contentTemplate = item.template; break; case 'icon': this.iconTemplate = item.template; break; case 'loadingicon': this.loadingIconTemplate = item.template; break; default: this.contentTemplate = item.template; break; } }); } badgeStyleClass() { return { 'p-badge p-component': true, 'p-badge-no-gutter': this.badge && String(this.badge).length === 1 }; } } Button.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: Button, deps: [], target: i0.ɵɵFactoryTarget.Component }); Button.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.2.4", type: Button, selector: "p-button", inputs: { type: "type", iconPos: "iconPos", icon: "icon", badge: "badge", label: "label", disabled: "disabled", loading: "loading", loadingIcon: "loadingIcon", style: "style", styleClass: "styleClass", badgeClass: "badgeClass", ariaLabel: "ariaLabel" }, outputs: { onClick: "onClick", onFocus: "onFocus", onBlur: "onBlur" }, host: { classAttribute: "p-element" }, queries: [{ propertyName: "templates", predicate: PrimeTemplate }], ngImport: i0, template: ` <button [attr.type]="type" [attr.aria-label]="ariaLabel" [class]="styleClass" [ngStyle]="style" [disabled]="disabled || loading" [ngClass]="buttonClass()" (click)="onClick.emit($event)" (focus)="onFocus.emit($event)" (blur)="onBlur.emit($event)" pRipple > <ng-content></ng-content> <ng-container *ngTemplateOutlet="contentTemplate"></ng-container> <ng-container *ngIf="loading"> <ng-container *ngIf="!loadingIconTemplate"> <span *ngIf="loadingIcon" [class]="'p-button-loading-icon' + icon" [ngClass]="iconClass()"></span> <SpinnerIcon *ngIf="!loadingIcon" [styleClass]="iconClass() + ' p-button-loading-icon'" [spin]="true" /> </ng-container> <span *ngIf="loadingIconTemplate" class="p-button-loading-icon"> <ng-template *ngTemplateOutlet="loadingIconTemplate"></ng-template> </span> </ng-container> <ng-container *ngIf="!loading"> <span *ngIf="icon && !iconTemplate" [class]="icon" [ngClass]="iconClass()"></span> <span *ngIf="!icon && iconTemplate" [ngClass]="iconClass()"> <ng-template [ngIf]="!icon" *ngTemplateOutlet="iconTemplate;"></ng-template> </span> </ng-container> <span class="p-button-label" [attr.aria-hidden]="icon && !label" *ngIf="!contentTemplate && label">{{ label }}</span> <span [ngClass]="badgeStyleClass()" [class]="badgeClass" *ngIf="!contentTemplate && badge">{{ badge }}</span> </button> `, isInline: true, dependencies: [{ kind: "directive", type: i0.forwardRef(function () { return i1.NgClass; }), selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgIf; }), selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgTemplateOutlet; }), selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i0.forwardRef(function () { return i1.NgStyle; }), selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: i0.forwardRef(function () { return i2.Ripple; }), selector: "[pRipple]" }, { kind: "component", type: i0.forwardRef(function () { return SpinnerIcon; }), selector: "SpinnerIcon" }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: Button, decorators: [{ type: Component, args: [{ selector: 'p-button', template: ` <button [attr.type]="type" [attr.aria-label]="ariaLabel" [class]="styleClass" [ngStyle]="style" [disabled]="disabled || loading" [ngClass]="buttonClass()" (click)="onClick.emit($event)" (focus)="onFocus.emit($event)" (blur)="onBlur.emit($event)" pRipple > <ng-content></ng-content> <ng-container *ngTemplateOutlet="contentTemplate"></ng-container> <ng-container *ngIf="loading"> <ng-container *ngIf="!loadingIconTemplate"> <span *ngIf="loadingIcon" [class]="'p-button-loading-icon' + icon" [ngClass]="iconClass()"></span> <SpinnerIcon *ngIf="!loadingIcon" [styleClass]="iconClass() + ' p-button-loading-icon'" [spin]="true" /> </ng-container> <span *ngIf="loadingIconTemplate" class="p-button-loading-icon"> <ng-template *ngTemplateOutlet="loadingIconTemplate"></ng-template> </span> </ng-container> <ng-container *ngIf="!loading"> <span *ngIf="icon && !iconTemplate" [class]="icon" [ngClass]="iconClass()"></span> <span *ngIf="!icon && iconTemplate" [ngClass]="iconClass()"> <ng-template [ngIf]="!icon" *ngTemplateOutlet="iconTemplate;"></ng-template> </span> </ng-container> <span class="p-button-label" [attr.aria-hidden]="icon && !label" *ngIf="!contentTemplate && label">{{ label }}</span> <span [ngClass]="badgeStyleClass()" [class]="badgeClass" *ngIf="!contentTemplate && badge">{{ badge }}</span> </button> `, changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, host: { class: 'p-element' } }] }], propDecorators: { type: [{ type: Input }], iconPos: [{ type: Input }], icon: [{ type: Input }], badge: [{ type: Input }], label: [{ type: Input }], disabled: [{ type: Input }], loading: [{ type: Input }], loadingIcon: [{ type: Input }], style: [{ type: Input }], styleClass: [{ type: Input }], badgeClass: [{ type: Input }], ariaLabel: [{ type: Input }], templates: [{ type: ContentChildren, args: [PrimeTemplate] }], onClick: [{ type: Output }], onFocus: [{ type: Output }], onBlur: [{ type: Output }] } }); class ButtonModule { } ButtonModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: ButtonModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); ButtonModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.4", ngImport: i0, type: ButtonModule, declarations: [ButtonDirective, Button], imports: [CommonModule, RippleModule, SharedModule, SpinnerIcon], exports: [ButtonDirective, Button, SharedModule] }); ButtonModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: ButtonModule, imports: [CommonModule, RippleModule, SharedModule, SpinnerIcon, SharedModule] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.4", ngImport: i0, type: ButtonModule, decorators: [{ type: NgModule, args: [{ imports: [CommonModule, RippleModule, SharedModule, SpinnerIcon], exports: [ButtonDirective, Button, SharedModule], declarations: [ButtonDirective, Button] }] }] }); /** * Generated bundle index. Do not edit. */ export { Button, ButtonDirective, ButtonModule }; //# sourceMappingURL=primeng-button.mjs.map //# sourceMappingURL=primeng-button.mjs.map