ng-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
265 lines (258 loc) • 10.4 kB
JavaScript
import { __decorate, __metadata } from 'tslib';
import { Directionality, BidiModule } from '@angular/cdk/bidi';
import { Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, ChangeDetectorRef, Renderer2, Optional, ContentChild, Input, NgModule } from '@angular/core';
import { NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
import { warnDeprecation } from 'ng-zorro-antd/core/logger';
import { InputBoolean } from 'ng-zorro-antd/core/util';
import { NzIconDirective, NzIconModule } from 'ng-zorro-antd/icon';
import { Subject } from 'rxjs';
import { takeUntil, startWith, filter } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
import { ɵNzTransitionPatchModule } from 'ng-zorro-antd/core/transition-patch';
import { NzWaveModule } from 'ng-zorro-antd/core/wave';
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
const NZ_CONFIG_MODULE_NAME = 'button';
class NzButtonComponent {
constructor(elementRef, cdr, renderer, nzConfigService, directionality) {
this.elementRef = elementRef;
this.cdr = cdr;
this.renderer = renderer;
this.nzConfigService = nzConfigService;
this.directionality = directionality;
this._nzModuleName = NZ_CONFIG_MODULE_NAME;
this.nzBlock = false;
this.nzGhost = false;
this.nzSearch = false;
this.nzLoading = false;
this.nzDanger = false;
this.disabled = false;
this.tabIndex = null;
this.nzType = null;
this.nzShape = null;
this.nzSize = 'default';
this.dir = 'ltr';
this.destroy$ = new Subject();
this.loading$ = new Subject();
// TODO: move to host after View Engine deprecation
this.elementRef.nativeElement.classList.add('ant-btn');
this.nzConfigService
.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.cdr.markForCheck();
});
}
insertSpan(nodes, renderer) {
nodes.forEach(node => {
if (node.nodeName === '#text') {
const span = renderer.createElement('span');
const parent = renderer.parentNode(node);
renderer.insertBefore(parent, span, node);
renderer.appendChild(span, node);
}
});
}
assertIconOnly(element, renderer) {
const listOfNode = Array.from(element.childNodes);
const iconCount = listOfNode.filter(node => node.nodeName === 'I').length;
const noText = listOfNode.every(node => node.nodeName !== '#text');
const noSpan = listOfNode.every(node => node.nodeName !== 'SPAN');
const isIconOnly = noSpan && noText && iconCount >= 1;
if (isIconOnly) {
renderer.addClass(element, 'ant-btn-icon-only');
}
}
ngOnInit() {
var _a;
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
this.cdr.detectChanges();
});
this.dir = this.directionality.value;
}
ngOnChanges(changes) {
const { nzLoading, nzType } = changes;
if (nzLoading) {
this.loading$.next(this.nzLoading);
}
if ((nzType === null || nzType === void 0 ? void 0 : nzType.currentValue) === 'danger') {
warnDeprecation(`'danger' value of 'nzType' in Button is going to be removed in 12.0.0. Please use 'nzDanger' instead.`);
}
}
ngAfterViewInit() {
this.assertIconOnly(this.elementRef.nativeElement, this.renderer);
this.insertSpan(this.elementRef.nativeElement.childNodes, this.renderer);
}
ngAfterContentInit() {
this.loading$
.pipe(startWith(this.nzLoading), filter(() => !!this.nzIconDirectiveElement), takeUntil(this.destroy$))
.subscribe(loading => {
const nativeElement = this.nzIconDirectiveElement.nativeElement;
if (loading) {
this.renderer.setStyle(nativeElement, 'display', 'none');
}
else {
this.renderer.removeStyle(nativeElement, 'display');
}
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
NzButtonComponent.decorators = [
{ type: Component, args: [{
selector: 'button[nz-button], a[nz-button]',
exportAs: 'nzButton',
preserveWhitespaces: false,
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
<i nz-icon nzType="loading" *ngIf="nzLoading"></i>
<ng-content></ng-content>
`,
host: {
'[class.ant-btn-primary]': `nzType === 'primary'`,
'[class.ant-btn-dashed]': `nzType === 'dashed'`,
'[class.ant-btn-link]': `nzType === 'link'`,
'[class.ant-btn-text]': `nzType === 'text'`,
'[class.ant-btn-danger]': `nzType === 'danger'`,
'[class.ant-btn-circle]': `nzShape === 'circle'`,
'[class.ant-btn-round]': `nzShape === 'round'`,
'[class.ant-btn-lg]': `nzSize === 'large'`,
'[class.ant-btn-sm]': `nzSize === 'small'`,
'[class.ant-btn-dangerous]': `nzDanger`,
'[class.ant-btn-loading]': `nzLoading`,
'[class.ant-btn-background-ghost]': `nzGhost`,
'[class.ant-btn-block]': `nzBlock`,
'[class.ant-input-search-button]': `nzSearch`,
'[class.ant-btn-rtl]': `dir === 'rtl'`,
'[attr.tabindex]': 'disabled ? -1 : (tabIndex === null ? null : tabIndex)',
'[attr.disabled]': 'disabled || null'
}
},] }
];
NzButtonComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: ChangeDetectorRef },
{ type: Renderer2 },
{ type: NzConfigService },
{ type: Directionality, decorators: [{ type: Optional }] }
];
NzButtonComponent.propDecorators = {
nzIconDirectiveElement: [{ type: ContentChild, args: [NzIconDirective, { read: ElementRef },] }],
nzBlock: [{ type: Input }],
nzGhost: [{ type: Input }],
nzSearch: [{ type: Input }],
nzLoading: [{ type: Input }],
nzDanger: [{ type: Input }],
disabled: [{ type: Input }],
tabIndex: [{ type: Input }],
nzType: [{ type: Input }],
nzShape: [{ type: Input }],
nzSize: [{ type: Input }]
};
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "nzBlock", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "nzGhost", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "nzSearch", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "nzLoading", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "nzDanger", void 0);
__decorate([
InputBoolean(),
__metadata("design:type", Boolean)
], NzButtonComponent.prototype, "disabled", void 0);
__decorate([
WithConfig(),
__metadata("design:type", String)
], NzButtonComponent.prototype, "nzSize", void 0);
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzButtonGroupComponent {
constructor(elementRef, directionality) {
this.elementRef = elementRef;
this.directionality = directionality;
this.nzSize = 'default';
this.dir = 'ltr';
this.destroy$ = new Subject();
// TODO: move to host after View Engine deprecation
this.elementRef.nativeElement.classList.add('ant-btn-group');
}
ngOnInit() {
var _a;
this.dir = this.directionality.value;
(_a = this.directionality.change) === null || _a === void 0 ? void 0 : _a.pipe(takeUntil(this.destroy$)).subscribe((direction) => {
this.dir = direction;
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}
NzButtonGroupComponent.decorators = [
{ type: Component, args: [{
selector: 'nz-button-group',
exportAs: 'nzButtonGroup',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
'[class.ant-btn-group-lg]': `nzSize === 'large'`,
'[class.ant-btn-group-sm]': `nzSize === 'small'`,
'[class.ant-btn-group-rtl]': `dir === 'rtl'`
},
preserveWhitespaces: false,
template: `
<ng-content></ng-content>
`
},] }
];
NzButtonGroupComponent.ctorParameters = () => [
{ type: ElementRef },
{ type: Directionality, decorators: [{ type: Optional }] }
];
NzButtonGroupComponent.propDecorators = {
nzSize: [{ type: Input }]
};
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
class NzButtonModule {
}
NzButtonModule.decorators = [
{ type: NgModule, args: [{
declarations: [NzButtonComponent, NzButtonGroupComponent],
exports: [NzButtonComponent, NzButtonGroupComponent, ɵNzTransitionPatchModule, NzWaveModule],
imports: [BidiModule, CommonModule, NzWaveModule, NzIconModule, ɵNzTransitionPatchModule]
},] }
];
/**
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/
/**
* Generated bundle index. Do not edit.
*/
export { NzButtonComponent, NzButtonGroupComponent, NzButtonModule };
//# sourceMappingURL=ng-zorro-antd-button.js.map