primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 12.3 kB
JavaScript
import { forwardRef, EventEmitter, ElementRef, ChangeDetectorRef, Output, Input, ViewChild, Component, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { InputTextModule } from 'primeng/inputtext';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const SPINNER_VALUE_ACCESSOR = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Spinner),
multi: true
};
let Spinner = class Spinner {
constructor(el, cd) {
this.el = el;
this.cd = cd;
this.onChange = new EventEmitter();
this.onFocus = new EventEmitter();
this.onBlur = new EventEmitter();
this._step = 1;
this.onModelChange = () => { };
this.onModelTouched = () => { };
this.keyPattern = /[0-9\+\-]/;
this.negativeSeparator = '-';
}
get step() {
return this._step;
}
set step(val) {
this._step = val;
if (this._step != null) {
let tokens = this.step.toString().split(/[,]|[.]/);
this.calculatedPrecision = tokens[1] ? tokens[1].length : undefined;
}
}
ngOnInit() {
if (this.formatInput) {
this.localeDecimalSeparator = (1.1).toLocaleString().substring(1, 2);
this.localeThousandSeparator = (1000).toLocaleString().substring(1, 2);
this.thousandRegExp = new RegExp(`[${this.thousandSeparator || this.localeThousandSeparator}]`, 'gim');
if (this.decimalSeparator && this.thousandSeparator && this.decimalSeparator === this.thousandSeparator) {
console.warn("thousandSeparator and decimalSeparator cannot have the same value.");
}
}
}
repeat(event, interval, dir) {
let i = interval || 500;
this.clearTimer();
this.timer = setTimeout(() => {
this.repeat(event, 40, dir);
}, i);
this.spin(event, dir);
}
spin(event, dir) {
let step = this.step * dir;
let currentValue;
let precision = this.getPrecision();
if (this.value)
currentValue = (typeof this.value === 'string') ? this.parseValue(this.value) : this.value;
else
currentValue = 0;
if (precision)
this.value = parseFloat(this.toFixed(currentValue + step, precision));
else
this.value = currentValue + step;
if (this.maxlength !== undefined && this.value.toString().length > this.maxlength) {
this.value = currentValue;
}
if (this.min !== undefined && this.value < this.min) {
this.value = this.min;
}
if (this.max !== undefined && this.value > this.max) {
this.value = this.max;
}
this.formatValue();
this.onModelChange(this.value);
this.onChange.emit(event);
}
getPrecision() {
return this.precision === undefined ? this.calculatedPrecision : this.precision;
}
toFixed(value, precision) {
let power = Math.pow(10, precision || 0);
return String(Math.round(value * power) / power);
}
onUpButtonMousedown(event) {
if (!this.disabled) {
this.inputfieldViewChild.nativeElement.focus();
this.repeat(event, null, 1);
this.updateFilledState();
event.preventDefault();
}
}
onUpButtonMouseup(event) {
if (!this.disabled) {
this.clearTimer();
}
}
onUpButtonMouseleave(event) {
if (!this.disabled) {
this.clearTimer();
}
}
onDownButtonMousedown(event) {
if (!this.disabled) {
this.inputfieldViewChild.nativeElement.focus();
this.repeat(event, null, -1);
this.updateFilledState();
event.preventDefault();
}
}
onDownButtonMouseup(event) {
if (!this.disabled) {
this.clearTimer();
}
}
onDownButtonMouseleave(event) {
if (!this.disabled) {
this.clearTimer();
}
}
onInputKeydown(event) {
if (event.which == 38) {
this.spin(event, 1);
event.preventDefault();
}
else if (event.which == 40) {
this.spin(event, -1);
event.preventDefault();
}
}
onInputChange(event) {
this.onChange.emit(event);
}
onInput(event) {
this.value = this.parseValue(event.target.value);
this.onModelChange(this.value);
this.updateFilledState();
}
onInputBlur(event) {
this.focus = false;
this.formatValue();
this.onModelTouched();
this.onBlur.emit(event);
}
onInputFocus(event) {
this.focus = true;
this.onFocus.emit(event);
}
parseValue(val) {
let value;
let precision = this.getPrecision();
if (val.trim() === '') {
value = null;
}
else {
if (this.formatInput) {
val = val.replace(this.thousandRegExp, '');
}
if (precision) {
val = this.formatInput ? val.replace(this.decimalSeparator || this.localeDecimalSeparator, '.') : val.replace(',', '.');
value = parseFloat(val);
}
else {
value = parseInt(val, 10);
}
if (!isNaN(value)) {
if (this.max !== null && value > this.max) {
value = this.max;
}
if (this.min !== null && value < this.min) {
value = this.min;
}
}
else {
value = null;
}
}
return value;
}
formatValue() {
let value = this.value;
let precision = this.getPrecision();
if (value != null) {
if (this.formatInput) {
value = value.toLocaleString(undefined, { maximumFractionDigits: 20 });
if (this.decimalSeparator && this.thousandSeparator) {
value = value.split(this.localeDecimalSeparator);
if (precision && value[1]) {
value[1] = (this.decimalSeparator || this.localeDecimalSeparator) + value[1];
}
if (this.thousandSeparator && value[0].length > 3) {
value[0] = value[0].replace(new RegExp(`[${this.localeThousandSeparator}]`, 'gim'), this.thousandSeparator);
}
value = value.join('');
}
}
this.formattedValue = value.toString();
}
else {
this.formattedValue = null;
}
if (this.inputfieldViewChild && this.inputfieldViewChild.nativeElement) {
this.inputfieldViewChild.nativeElement.value = this.formattedValue;
}
}
clearTimer() {
if (this.timer) {
clearInterval(this.timer);
}
}
writeValue(value) {
this.value = value;
this.formatValue();
this.updateFilledState();
this.cd.markForCheck();
}
registerOnChange(fn) {
this.onModelChange = fn;
}
registerOnTouched(fn) {
this.onModelTouched = fn;
}
setDisabledState(val) {
this.disabled = val;
}
updateFilledState() {
this.filled = (this.value !== undefined && this.value != null);
}
};
Spinner.ctorParameters = () => [
{ type: ElementRef },
{ type: ChangeDetectorRef }
];
__decorate([
Output()
], Spinner.prototype, "onChange", void 0);
__decorate([
Output()
], Spinner.prototype, "onFocus", void 0);
__decorate([
Output()
], Spinner.prototype, "onBlur", void 0);
__decorate([
Input()
], Spinner.prototype, "min", void 0);
__decorate([
Input()
], Spinner.prototype, "max", void 0);
__decorate([
Input()
], Spinner.prototype, "maxlength", void 0);
__decorate([
Input()
], Spinner.prototype, "size", void 0);
__decorate([
Input()
], Spinner.prototype, "placeholder", void 0);
__decorate([
Input()
], Spinner.prototype, "inputId", void 0);
__decorate([
Input()
], Spinner.prototype, "disabled", void 0);
__decorate([
Input()
], Spinner.prototype, "readonly", void 0);
__decorate([
Input()
], Spinner.prototype, "tabindex", void 0);
__decorate([
Input()
], Spinner.prototype, "required", void 0);
__decorate([
Input()
], Spinner.prototype, "name", void 0);
__decorate([
Input()
], Spinner.prototype, "ariaLabelledBy", void 0);
__decorate([
Input()
], Spinner.prototype, "inputStyle", void 0);
__decorate([
Input()
], Spinner.prototype, "inputStyleClass", void 0);
__decorate([
Input()
], Spinner.prototype, "formatInput", void 0);
__decorate([
Input()
], Spinner.prototype, "decimalSeparator", void 0);
__decorate([
Input()
], Spinner.prototype, "thousandSeparator", void 0);
__decorate([
Input()
], Spinner.prototype, "precision", void 0);
__decorate([
ViewChild('inputfield')
], Spinner.prototype, "inputfieldViewChild", void 0);
__decorate([
Input()
], Spinner.prototype, "step", null);
Spinner = __decorate([
Component({
selector: 'p-spinner',
template: `
<span class="ui-spinner ui-widget ui-corner-all">
<input #inputfield type="text" [attr.id]="inputId" [value]="formattedValue||null" [attr.name]="name" [attr.aria-valumin]="min" [attr.aria-valuemax]="max" [attr.aria-valuenow]="value" [attr.aria-labelledby]="ariaLabelledBy"
[attr.size]="size" [attr.maxlength]="maxlength" [attr.tabindex]="tabindex" [attr.placeholder]="placeholder" [disabled]="disabled" [readonly]="readonly" [attr.required]="required"
(keydown)="onInputKeydown($event)" (blur)="onInputBlur($event)" (input)="onInput($event)" (change)="onInputChange($event)" (focus)="onInputFocus($event)"
[ngStyle]="inputStyle" [class]="inputStyleClass" [ngClass]="'ui-spinner-input ui-inputtext ui-widget ui-state-default ui-corner-all'">
<button type="button" [ngClass]="{'ui-spinner-button ui-spinner-up ui-corner-tr ui-button ui-widget ui-state-default':true,'ui-state-disabled':disabled}" [disabled]="disabled||readonly" tabindex="-1" [attr.readonly]="readonly"
(mouseleave)="onUpButtonMouseleave($event)" (mousedown)="onUpButtonMousedown($event)" (mouseup)="onUpButtonMouseup($event)">
<span class="ui-spinner-button-icon pi pi-caret-up ui-clickable"></span>
</button>
<button type="button" [ngClass]="{'ui-spinner-button ui-spinner-down ui-corner-br ui-button ui-widget ui-state-default':true,'ui-state-disabled':disabled}" [disabled]="disabled||readonly" tabindex="-1" [attr.readonly]="readonly"
(mouseleave)="onDownButtonMouseleave($event)" (mousedown)="onDownButtonMousedown($event)" (mouseup)="onDownButtonMouseup($event)">
<span class="ui-spinner-button-icon pi pi-caret-down ui-clickable"></span>
</button>
</span>
`,
host: {
'[class.ui-inputwrapper-filled]': 'filled',
'[class.ui-inputwrapper-focus]': 'focus'
},
providers: [SPINNER_VALUE_ACCESSOR],
changeDetection: ChangeDetectionStrategy.Default
})
], Spinner);
let SpinnerModule = class SpinnerModule {
};
SpinnerModule = __decorate([
NgModule({
imports: [CommonModule, InputTextModule],
exports: [Spinner],
declarations: [Spinner]
})
], SpinnerModule);
/**
* Generated bundle index. Do not edit.
*/
export { SPINNER_VALUE_ACCESSOR, Spinner, SpinnerModule };
//# sourceMappingURL=primeng-spinner.js.map