UNPKG

15.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('primeng/inputtext'), require('@angular/forms')) :
3 typeof define === 'function' && define.amd ? define('primeng/spinner', ['exports', '@angular/core', '@angular/common', 'primeng/inputtext', '@angular/forms'], factory) :
4 (global = global || self, factory((global.primeng = global.primeng || {}, global.primeng.spinner = {}), global.ng.core, global.ng.common, global.primeng.inputtext, global.ng.forms));
5}(this, (function (exports, core, common, inputtext, forms) { 'use strict';
6
7 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
8 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
9 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
10 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;
11 return c > 3 && r && Object.defineProperty(target, key, r), r;
12 };
13 var SPINNER_VALUE_ACCESSOR = {
14 provide: forms.NG_VALUE_ACCESSOR,
15 useExisting: core.forwardRef(function () { return Spinner; }),
16 multi: true
17 };
18 var Spinner = /** @class */ (function () {
19 function Spinner(el, cd) {
20 this.el = el;
21 this.cd = cd;
22 this.onChange = new core.EventEmitter();
23 this.onFocus = new core.EventEmitter();
24 this.onBlur = new core.EventEmitter();
25 this._step = 1;
26 this.onModelChange = function () { };
27 this.onModelTouched = function () { };
28 this.keyPattern = /[0-9\+\-]/;
29 this.negativeSeparator = '-';
30 }
31 Object.defineProperty(Spinner.prototype, "step", {
32 get: function () {
33 return this._step;
34 },
35 set: function (val) {
36 this._step = val;
37 if (this._step != null) {
38 var tokens = this.step.toString().split(/[,]|[.]/);
39 this.calculatedPrecision = tokens[1] ? tokens[1].length : undefined;
40 }
41 },
42 enumerable: true,
43 configurable: true
44 });
45 Spinner.prototype.ngOnInit = function () {
46 if (this.formatInput) {
47 this.localeDecimalSeparator = (1.1).toLocaleString().substring(1, 2);
48 this.localeThousandSeparator = (1000).toLocaleString().substring(1, 2);
49 this.thousandRegExp = new RegExp("[" + (this.thousandSeparator || this.localeThousandSeparator) + "]", 'gim');
50 if (this.decimalSeparator && this.thousandSeparator && this.decimalSeparator === this.thousandSeparator) {
51 console.warn("thousandSeparator and decimalSeparator cannot have the same value.");
52 }
53 }
54 };
55 Spinner.prototype.repeat = function (event, interval, dir) {
56 var _this = this;
57 var i = interval || 500;
58 this.clearTimer();
59 this.timer = setTimeout(function () {
60 _this.repeat(event, 40, dir);
61 }, i);
62 this.spin(event, dir);
63 };
64 Spinner.prototype.spin = function (event, dir) {
65 var step = this.step * dir;
66 var currentValue;
67 var precision = this.getPrecision();
68 if (this.value)
69 currentValue = (typeof this.value === 'string') ? this.parseValue(this.value) : this.value;
70 else
71 currentValue = 0;
72 if (precision)
73 this.value = parseFloat(this.toFixed(currentValue + step, precision));
74 else
75 this.value = currentValue + step;
76 if (this.maxlength !== undefined && this.value.toString().length > this.maxlength) {
77 this.value = currentValue;
78 }
79 if (this.min !== undefined && this.value < this.min) {
80 this.value = this.min;
81 }
82 if (this.max !== undefined && this.value > this.max) {
83 this.value = this.max;
84 }
85 this.formatValue();
86 this.onModelChange(this.value);
87 this.onChange.emit(event);
88 };
89 Spinner.prototype.getPrecision = function () {
90 return this.precision === undefined ? this.calculatedPrecision : this.precision;
91 };
92 Spinner.prototype.toFixed = function (value, precision) {
93 var power = Math.pow(10, precision || 0);
94 return String(Math.round(value * power) / power);
95 };
96 Spinner.prototype.onUpButtonMousedown = function (event) {
97 if (!this.disabled) {
98 this.inputfieldViewChild.nativeElement.focus();
99 this.repeat(event, null, 1);
100 this.updateFilledState();
101 event.preventDefault();
102 }
103 };
104 Spinner.prototype.onUpButtonMouseup = function (event) {
105 if (!this.disabled) {
106 this.clearTimer();
107 }
108 };
109 Spinner.prototype.onUpButtonMouseleave = function (event) {
110 if (!this.disabled) {
111 this.clearTimer();
112 }
113 };
114 Spinner.prototype.onDownButtonMousedown = function (event) {
115 if (!this.disabled) {
116 this.inputfieldViewChild.nativeElement.focus();
117 this.repeat(event, null, -1);
118 this.updateFilledState();
119 event.preventDefault();
120 }
121 };
122 Spinner.prototype.onDownButtonMouseup = function (event) {
123 if (!this.disabled) {
124 this.clearTimer();
125 }
126 };
127 Spinner.prototype.onDownButtonMouseleave = function (event) {
128 if (!this.disabled) {
129 this.clearTimer();
130 }
131 };
132 Spinner.prototype.onInputKeydown = function (event) {
133 if (event.which == 38) {
134 this.spin(event, 1);
135 event.preventDefault();
136 }
137 else if (event.which == 40) {
138 this.spin(event, -1);
139 event.preventDefault();
140 }
141 };
142 Spinner.prototype.onInputChange = function (event) {
143 this.onChange.emit(event);
144 };
145 Spinner.prototype.onInput = function (event) {
146 this.value = this.parseValue(event.target.value);
147 this.onModelChange(this.value);
148 this.updateFilledState();
149 };
150 Spinner.prototype.onInputBlur = function (event) {
151 this.focus = false;
152 this.formatValue();
153 this.onModelTouched();
154 this.onBlur.emit(event);
155 };
156 Spinner.prototype.onInputFocus = function (event) {
157 this.focus = true;
158 this.onFocus.emit(event);
159 };
160 Spinner.prototype.parseValue = function (val) {
161 var value;
162 var precision = this.getPrecision();
163 if (val.trim() === '') {
164 value = null;
165 }
166 else {
167 if (this.formatInput) {
168 val = val.replace(this.thousandRegExp, '');
169 }
170 if (precision) {
171 val = this.formatInput ? val.replace(this.decimalSeparator || this.localeDecimalSeparator, '.') : val.replace(',', '.');
172 value = parseFloat(val);
173 }
174 else {
175 value = parseInt(val, 10);
176 }
177 if (!isNaN(value)) {
178 if (this.max !== null && value > this.max) {
179 value = this.max;
180 }
181 if (this.min !== null && value < this.min) {
182 value = this.min;
183 }
184 }
185 else {
186 value = null;
187 }
188 }
189 return value;
190 };
191 Spinner.prototype.formatValue = function () {
192 var value = this.value;
193 var precision = this.getPrecision();
194 if (value != null) {
195 if (this.formatInput) {
196 value = value.toLocaleString(undefined, { maximumFractionDigits: 20 });
197 if (this.decimalSeparator && this.thousandSeparator) {
198 value = value.split(this.localeDecimalSeparator);
199 if (precision && value[1]) {
200 value[1] = (this.decimalSeparator || this.localeDecimalSeparator) + value[1];
201 }
202 if (this.thousandSeparator && value[0].length > 3) {
203 value[0] = value[0].replace(new RegExp("[" + this.localeThousandSeparator + "]", 'gim'), this.thousandSeparator);
204 }
205 value = value.join('');
206 }
207 }
208 this.formattedValue = value.toString();
209 }
210 else {
211 this.formattedValue = null;
212 }
213 if (this.inputfieldViewChild && this.inputfieldViewChild.nativeElement) {
214 this.inputfieldViewChild.nativeElement.value = this.formattedValue;
215 }
216 };
217 Spinner.prototype.clearTimer = function () {
218 if (this.timer) {
219 clearInterval(this.timer);
220 }
221 };
222 Spinner.prototype.writeValue = function (value) {
223 this.value = value;
224 this.formatValue();
225 this.updateFilledState();
226 this.cd.markForCheck();
227 };
228 Spinner.prototype.registerOnChange = function (fn) {
229 this.onModelChange = fn;
230 };
231 Spinner.prototype.registerOnTouched = function (fn) {
232 this.onModelTouched = fn;
233 };
234 Spinner.prototype.setDisabledState = function (val) {
235 this.disabled = val;
236 };
237 Spinner.prototype.updateFilledState = function () {
238 this.filled = (this.value !== undefined && this.value != null);
239 };
240 Spinner.ctorParameters = function () { return [
241 { type: core.ElementRef },
242 { type: core.ChangeDetectorRef }
243 ]; };
244 __decorate([
245 core.Output()
246 ], Spinner.prototype, "onChange", void 0);
247 __decorate([
248 core.Output()
249 ], Spinner.prototype, "onFocus", void 0);
250 __decorate([
251 core.Output()
252 ], Spinner.prototype, "onBlur", void 0);
253 __decorate([
254 core.Input()
255 ], Spinner.prototype, "min", void 0);
256 __decorate([
257 core.Input()
258 ], Spinner.prototype, "max", void 0);
259 __decorate([
260 core.Input()
261 ], Spinner.prototype, "maxlength", void 0);
262 __decorate([
263 core.Input()
264 ], Spinner.prototype, "size", void 0);
265 __decorate([
266 core.Input()
267 ], Spinner.prototype, "placeholder", void 0);
268 __decorate([
269 core.Input()
270 ], Spinner.prototype, "inputId", void 0);
271 __decorate([
272 core.Input()
273 ], Spinner.prototype, "disabled", void 0);
274 __decorate([
275 core.Input()
276 ], Spinner.prototype, "readonly", void 0);
277 __decorate([
278 core.Input()
279 ], Spinner.prototype, "tabindex", void 0);
280 __decorate([
281 core.Input()
282 ], Spinner.prototype, "required", void 0);
283 __decorate([
284 core.Input()
285 ], Spinner.prototype, "name", void 0);
286 __decorate([
287 core.Input()
288 ], Spinner.prototype, "ariaLabelledBy", void 0);
289 __decorate([
290 core.Input()
291 ], Spinner.prototype, "inputStyle", void 0);
292 __decorate([
293 core.Input()
294 ], Spinner.prototype, "inputStyleClass", void 0);
295 __decorate([
296 core.Input()
297 ], Spinner.prototype, "formatInput", void 0);
298 __decorate([
299 core.Input()
300 ], Spinner.prototype, "decimalSeparator", void 0);
301 __decorate([
302 core.Input()
303 ], Spinner.prototype, "thousandSeparator", void 0);
304 __decorate([
305 core.Input()
306 ], Spinner.prototype, "precision", void 0);
307 __decorate([
308 core.ViewChild('inputfield')
309 ], Spinner.prototype, "inputfieldViewChild", void 0);
310 __decorate([
311 core.Input()
312 ], Spinner.prototype, "step", null);
313 Spinner = __decorate([
314 core.Component({
315 selector: 'p-spinner',
316 template: "\n <span class=\"ui-spinner ui-widget ui-corner-all\">\n <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\"\n [attr.size]=\"size\" [attr.maxlength]=\"maxlength\" [attr.tabindex]=\"tabindex\" [attr.placeholder]=\"placeholder\" [disabled]=\"disabled\" [readonly]=\"readonly\" [attr.required]=\"required\"\n (keydown)=\"onInputKeydown($event)\" (blur)=\"onInputBlur($event)\" (input)=\"onInput($event)\" (change)=\"onInputChange($event)\" (focus)=\"onInputFocus($event)\"\n [ngStyle]=\"inputStyle\" [class]=\"inputStyleClass\" [ngClass]=\"'ui-spinner-input ui-inputtext ui-widget ui-state-default ui-corner-all'\">\n <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\"\n (mouseleave)=\"onUpButtonMouseleave($event)\" (mousedown)=\"onUpButtonMousedown($event)\" (mouseup)=\"onUpButtonMouseup($event)\">\n <span class=\"ui-spinner-button-icon pi pi-caret-up ui-clickable\"></span>\n </button>\n <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\"\n (mouseleave)=\"onDownButtonMouseleave($event)\" (mousedown)=\"onDownButtonMousedown($event)\" (mouseup)=\"onDownButtonMouseup($event)\">\n <span class=\"ui-spinner-button-icon pi pi-caret-down ui-clickable\"></span>\n </button>\n </span>\n ",
317 host: {
318 '[class.ui-inputwrapper-filled]': 'filled',
319 '[class.ui-inputwrapper-focus]': 'focus'
320 },
321 providers: [SPINNER_VALUE_ACCESSOR],
322 changeDetection: core.ChangeDetectionStrategy.Default
323 })
324 ], Spinner);
325 return Spinner;
326 }());
327 var SpinnerModule = /** @class */ (function () {
328 function SpinnerModule() {
329 }
330 SpinnerModule = __decorate([
331 core.NgModule({
332 imports: [common.CommonModule, inputtext.InputTextModule],
333 exports: [Spinner],
334 declarations: [Spinner]
335 })
336 ], SpinnerModule);
337 return SpinnerModule;
338 }());
339
340 exports.SPINNER_VALUE_ACCESSOR = SPINNER_VALUE_ACCESSOR;
341 exports.Spinner = Spinner;
342 exports.SpinnerModule = SpinnerModule;
343
344 Object.defineProperty(exports, '__esModule', { value: true });
345
346})));
347//# sourceMappingURL=primeng-spinner.umd.js.map