@annalib/anna-core 4.2.20 → 4.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4157,6 +4157,290 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.7", ngImpor
4157
4157
  }]
4158
4158
  }] });
4159
4159
 
4160
+ class DigitOnlyDirective {
4161
+ constructor(el) {
4162
+ this.el = el;
4163
+ this.hasDecimalPoint = false;
4164
+ this.hasNegativeSign = false;
4165
+ this.navigationKeys = [
4166
+ 'Backspace',
4167
+ 'Delete',
4168
+ 'Tab',
4169
+ 'Escape',
4170
+ 'Enter',
4171
+ 'Home',
4172
+ 'End',
4173
+ 'ArrowLeft',
4174
+ 'ArrowRight',
4175
+ 'Clear',
4176
+ 'Copy',
4177
+ 'Paste',
4178
+ ];
4179
+ this.decimal = false;
4180
+ this.decimalSeparator = '.';
4181
+ this.allowNegatives = false;
4182
+ this.allowPaste = true;
4183
+ this.negativeSign = '-';
4184
+ this.min = -Infinity;
4185
+ this.max = Infinity;
4186
+ this.regex = null;
4187
+ this.inputElement = el.nativeElement;
4188
+ }
4189
+ ngOnChanges(changes) {
4190
+ if (changes['pattern']) {
4191
+ this.regex = this.pattern ? RegExp(this.pattern) : null;
4192
+ }
4193
+ if (changes['min']) {
4194
+ const maybeMin = Number(this.min);
4195
+ this.min = isNaN(maybeMin) ? -Infinity : maybeMin;
4196
+ }
4197
+ if (changes['max']) {
4198
+ const maybeMax = Number(this.max);
4199
+ this.max = isNaN(maybeMax) ? Infinity : maybeMax;
4200
+ }
4201
+ }
4202
+ onBeforeInput(e) {
4203
+ if (isNaN(Number(e.data))) {
4204
+ if (e.data === this.decimalSeparator ||
4205
+ (e.data === this.negativeSign && this.allowNegatives)) {
4206
+ return; // go on
4207
+ }
4208
+ e.preventDefault();
4209
+ e.stopPropagation();
4210
+ }
4211
+ }
4212
+ onKeyDown(e) {
4213
+ console.log("called");
4214
+ if (this.navigationKeys.indexOf(e.key) > -1 || // Allow: navigation keys: backspace, delete, arrows etc.
4215
+ ((e.key === 'a' || e.code === 'KeyA') && e.ctrlKey === true) || // Allow: Ctrl+A
4216
+ ((e.key === 'c' || e.code === 'KeyC') && e.ctrlKey === true) || // Allow: Ctrl+C
4217
+ ((e.key === 'v' || e.code === 'KeyV') && e.ctrlKey === true) || // Allow: Ctrl+V
4218
+ ((e.key === 'x' || e.code === 'KeyX') && e.ctrlKey === true) || // Allow: Ctrl+X
4219
+ ((e.key === 'a' || e.code === 'KeyA') && e.metaKey === true) || // Allow: Cmd+A (Mac)
4220
+ ((e.key === 'c' || e.code === 'KeyC') && e.metaKey === true) || // Allow: Cmd+C (Mac)
4221
+ ((e.key === 'v' || e.code === 'KeyV') && e.metaKey === true) || // Allow: Cmd+V (Mac)
4222
+ ((e.key === 'x' || e.code === 'KeyX') && e.metaKey === true) // Allow: Cmd+X (Mac)
4223
+ ) {
4224
+ // let it happen, don't do anything
4225
+ return;
4226
+ }
4227
+ let newValue = '';
4228
+ if (this.decimal && e.key === this.decimalSeparator) {
4229
+ newValue = this.forecastValue(e.key);
4230
+ if (newValue.split(this.decimalSeparator).length > 2) {
4231
+ // has two or more decimal points
4232
+ e.preventDefault();
4233
+ return;
4234
+ }
4235
+ else {
4236
+ this.hasDecimalPoint = newValue.indexOf(this.decimalSeparator) > -1;
4237
+ return; // Allow: only one decimal point
4238
+ }
4239
+ }
4240
+ if (e.key === this.negativeSign && this.allowNegatives) {
4241
+ newValue = this.forecastValue(e.key);
4242
+ if (newValue.charAt(0) !== this.negativeSign ||
4243
+ newValue.split(this.negativeSign).length > 2) {
4244
+ e.preventDefault();
4245
+ return;
4246
+ }
4247
+ else {
4248
+ this.hasNegativeSign = newValue.split(this.negativeSign).length > -1;
4249
+ return;
4250
+ }
4251
+ }
4252
+ // Ensure that it is a number and stop the keypress
4253
+ if (e.key === ' ' || isNaN(Number(e.key))) {
4254
+ e.preventDefault();
4255
+ return;
4256
+ }
4257
+ newValue = newValue || this.forecastValue(e.key);
4258
+ // check the input pattern RegExp
4259
+ if (this.regex) {
4260
+ if (!this.regex.test(newValue)) {
4261
+ e.preventDefault();
4262
+ return;
4263
+ }
4264
+ }
4265
+ const newNumber = Number(newValue);
4266
+ if (newNumber > this.max || newNumber < this.min) {
4267
+ e.preventDefault();
4268
+ }
4269
+ }
4270
+ onPaste(event) {
4271
+ if (this.allowPaste === true) {
4272
+ let pastedInput = '';
4273
+ if (window['clipboardData']) {
4274
+ // Browser is IE
4275
+ pastedInput = window['clipboardData'].getData('text');
4276
+ }
4277
+ else if (event.clipboardData && event.clipboardData.getData) {
4278
+ // Other browsers
4279
+ pastedInput = event.clipboardData.getData('text/plain');
4280
+ }
4281
+ this.pasteData(pastedInput);
4282
+ event.preventDefault();
4283
+ }
4284
+ else {
4285
+ // this prevents the paste
4286
+ event.preventDefault();
4287
+ event.stopPropagation();
4288
+ }
4289
+ }
4290
+ onDrop(event) {
4291
+ var _a, _b;
4292
+ const textData = (_b = (_a = event.dataTransfer) === null || _a === void 0 ? void 0 : _a.getData('text')) !== null && _b !== void 0 ? _b : '';
4293
+ this.inputElement.focus();
4294
+ this.pasteData(textData);
4295
+ event.preventDefault();
4296
+ }
4297
+ pasteData(pastedContent) {
4298
+ const sanitizedContent = this.sanitizeInput(pastedContent);
4299
+ if (sanitizedContent.includes(this.negativeSign) &&
4300
+ this.hasNegativeSign &&
4301
+ !this.getSelection().includes(this.negativeSign)) {
4302
+ return;
4303
+ }
4304
+ const pasted = document.execCommand('insertText', false, sanitizedContent);
4305
+ if (!pasted) {
4306
+ if (this.inputElement.setRangeText) {
4307
+ const { selectionStart: start, selectionEnd: end } = this.inputElement;
4308
+ this.inputElement.setRangeText(sanitizedContent, start !== null && start !== void 0 ? start : 0, end !== null && end !== void 0 ? end : 0, 'end');
4309
+ // Angular's Reactive Form relies on "input" event, but on Firefox, the setRangeText method doesn't trigger it
4310
+ // so we have to trigger it ourself.
4311
+ if (typeof window['InstallTrigger'] !==
4312
+ 'undefined') {
4313
+ this.inputElement.dispatchEvent(new Event('input', { cancelable: true }));
4314
+ }
4315
+ }
4316
+ else {
4317
+ // Browser does not support setRangeText, e.g. IE
4318
+ this.insertAtCursor(this.inputElement, sanitizedContent);
4319
+ }
4320
+ }
4321
+ if (this.decimal) {
4322
+ this.hasDecimalPoint =
4323
+ this.inputElement.value.indexOf(this.decimalSeparator) > -1;
4324
+ }
4325
+ this.hasNegativeSign =
4326
+ this.inputElement.value.indexOf(this.negativeSign) > -1;
4327
+ }
4328
+ // The following 2 methods were added from the below article for browsers that do not support setRangeText
4329
+ // https://stackoverflow.com/questions/11076975/how-to-insert-text-into-the-textarea-at-the-current-cursor-position
4330
+ insertAtCursor(myField, myValue) {
4331
+ var _a, _b;
4332
+ const startPos = (_a = myField.selectionStart) !== null && _a !== void 0 ? _a : 0;
4333
+ const endPos = (_b = myField.selectionEnd) !== null && _b !== void 0 ? _b : 0;
4334
+ myField.value =
4335
+ myField.value.substring(0, startPos) +
4336
+ myValue +
4337
+ myField.value.substring(endPos, myField.value.length);
4338
+ const pos = startPos + myValue.length;
4339
+ myField.focus();
4340
+ myField.setSelectionRange(pos, pos);
4341
+ this.triggerEvent(myField, 'input');
4342
+ }
4343
+ triggerEvent(el, type) {
4344
+ if ('createEvent' in document) {
4345
+ // modern browsers, IE9+
4346
+ const e = document.createEvent('HTMLEvents');
4347
+ e.initEvent(type, false, true);
4348
+ el.dispatchEvent(e);
4349
+ }
4350
+ }
4351
+ // end stack overflow code
4352
+ sanitizeInput(input) {
4353
+ let result = '';
4354
+ let regex;
4355
+ if (this.decimal && this.isValidDecimal(input)) {
4356
+ regex = new RegExp(`${this.getNegativeSignRegExp()}[^0-9${this.decimalSeparator}]`, 'g');
4357
+ }
4358
+ else {
4359
+ regex = new RegExp(`${this.getNegativeSignRegExp()}[^0-9]`, 'g');
4360
+ }
4361
+ result = input.replace(regex, '');
4362
+ const maxLength = this.inputElement.maxLength;
4363
+ if (maxLength > 0) {
4364
+ // the input element has maxLength limit
4365
+ const allowedLength = maxLength -
4366
+ this.inputElement.value.length +
4367
+ (result.includes(`${this.negativeSign}`) ? 1 : 0);
4368
+ result = allowedLength > 0 ? result.substring(0, allowedLength) : '';
4369
+ }
4370
+ return result;
4371
+ }
4372
+ getNegativeSignRegExp() {
4373
+ return this.allowNegatives &&
4374
+ (!this.hasNegativeSign || this.getSelection().includes(this.negativeSign))
4375
+ ? `(?!^${this.negativeSign})`
4376
+ : '';
4377
+ }
4378
+ isValidDecimal(string) {
4379
+ if (!this.hasDecimalPoint) {
4380
+ return string.split(this.decimalSeparator).length <= 2;
4381
+ }
4382
+ else {
4383
+ // the input element already has a decimal separator
4384
+ const selectedText = this.getSelection();
4385
+ if (selectedText && selectedText.indexOf(this.decimalSeparator) > -1) {
4386
+ return string.split(this.decimalSeparator).length <= 2;
4387
+ }
4388
+ else {
4389
+ return string.indexOf(this.decimalSeparator) < 0;
4390
+ }
4391
+ }
4392
+ }
4393
+ getSelection() {
4394
+ var _a, _b;
4395
+ return this.inputElement.value.substring((_a = this.inputElement.selectionStart) !== null && _a !== void 0 ? _a : 0, (_b = this.inputElement.selectionEnd) !== null && _b !== void 0 ? _b : 0);
4396
+ }
4397
+ forecastValue(key) {
4398
+ var _a, _b;
4399
+ const selectionStart = (_a = this.inputElement.selectionStart) !== null && _a !== void 0 ? _a : 0;
4400
+ const selectionEnd = (_b = this.inputElement.selectionEnd) !== null && _b !== void 0 ? _b : 0;
4401
+ const oldValue = this.inputElement.value;
4402
+ return (oldValue.substring(0, selectionStart) +
4403
+ key +
4404
+ oldValue.substring(selectionEnd));
4405
+ }
4406
+ }
4407
+ DigitOnlyDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.7", ngImport: i0, type: DigitOnlyDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive });
4408
+ DigitOnlyDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "13.2.7", type: DigitOnlyDirective, selector: "[digitOnly]", inputs: { decimal: "decimal", decimalSeparator: "decimalSeparator", allowNegatives: "allowNegatives", allowPaste: "allowPaste", negativeSign: "negativeSign", min: "min", max: "max", pattern: "pattern" }, host: { listeners: { "beforeinput": "onBeforeInput($event)", "keydown": "onKeyDown($event)", "paste": "onPaste($event)", "drop": "onDrop($event)" } }, usesOnChanges: true, ngImport: i0 });
4409
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.7", ngImport: i0, type: DigitOnlyDirective, decorators: [{
4410
+ type: Directive,
4411
+ args: [{
4412
+ selector: '[digitOnly]',
4413
+ }]
4414
+ }], ctorParameters: function () { return [{ type: i0.ElementRef }]; }, propDecorators: { decimal: [{
4415
+ type: Input
4416
+ }], decimalSeparator: [{
4417
+ type: Input
4418
+ }], allowNegatives: [{
4419
+ type: Input
4420
+ }], allowPaste: [{
4421
+ type: Input
4422
+ }], negativeSign: [{
4423
+ type: Input
4424
+ }], min: [{
4425
+ type: Input
4426
+ }], max: [{
4427
+ type: Input
4428
+ }], pattern: [{
4429
+ type: Input
4430
+ }], onBeforeInput: [{
4431
+ type: HostListener,
4432
+ args: ['beforeinput', ['$event']]
4433
+ }], onKeyDown: [{
4434
+ type: HostListener,
4435
+ args: ['keydown', ['$event']]
4436
+ }], onPaste: [{
4437
+ type: HostListener,
4438
+ args: ['paste', ['$event']]
4439
+ }], onDrop: [{
4440
+ type: HostListener,
4441
+ args: ['drop', ['$event']]
4442
+ }] } });
4443
+
4160
4444
  // Angular import statements
4161
4445
  class AnnaCoreModule {
4162
4446
  }
@@ -4180,7 +4464,8 @@ AnnaCoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version:
4180
4464
  AnnaVirtualTableDirective,
4181
4465
  AnnaNonEditableGenericTableComponent,
4182
4466
  AnnaTableVirtualScrollViewportComponent,
4183
- AnnaFixedRowSizeTableVirtualScrollStrategyDirective], imports: [CommonModule,
4467
+ AnnaFixedRowSizeTableVirtualScrollStrategyDirective,
4468
+ DigitOnlyDirective], imports: [CommonModule,
4184
4469
  NgbModule,
4185
4470
  FormsModule,
4186
4471
  MatRadioModule,
@@ -4207,7 +4492,8 @@ AnnaCoreModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version:
4207
4492
  AnnaVirtualTableDirective,
4208
4493
  AnnaNonEditableGenericTableComponent,
4209
4494
  AnnaTableVirtualScrollViewportComponent,
4210
- AnnaFixedRowSizeTableVirtualScrollStrategyDirective] });
4495
+ AnnaFixedRowSizeTableVirtualScrollStrategyDirective,
4496
+ DigitOnlyDirective] });
4211
4497
  AnnaCoreModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.7", ngImport: i0, type: AnnaCoreModule, imports: [[
4212
4498
  CommonModule,
4213
4499
  NgbModule,
@@ -4251,6 +4537,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.7", ngImpor
4251
4537
  AnnaNonEditableGenericTableComponent,
4252
4538
  AnnaTableVirtualScrollViewportComponent,
4253
4539
  AnnaFixedRowSizeTableVirtualScrollStrategyDirective,
4540
+ DigitOnlyDirective
4254
4541
  ],
4255
4542
  imports: [
4256
4543
  CommonModule,
@@ -4291,7 +4578,8 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.7", ngImpor
4291
4578
  AnnaVirtualTableDirective,
4292
4579
  AnnaNonEditableGenericTableComponent,
4293
4580
  AnnaTableVirtualScrollViewportComponent,
4294
- AnnaFixedRowSizeTableVirtualScrollStrategyDirective
4581
+ AnnaFixedRowSizeTableVirtualScrollStrategyDirective,
4582
+ DigitOnlyDirective
4295
4583
  ]
4296
4584
  }]
4297
4585
  }] });
@@ -4340,5 +4628,5 @@ var Days;
4340
4628
  * Generated bundle index. Do not edit.
4341
4629
  */
4342
4630
 
4343
- export { AllSelectedStatus, AnnaBuyerApprovalIconTemplateComponent, AnnaCalendarFilterComponent, AnnaConvertZeroOrNullOrUndefinedPipe, AnnaCoreModule, AnnaDateTimeFormatService, AnnaFilterSearchedTextPipe, AnnaFilterService, AnnaFixedRowSizeTableVirtualScrollStrategy, AnnaFixedRowSizeTableVirtualScrollStrategyDirective, AnnaGenericTableService, AnnaGlobalConfigService, AnnaIconColumnComponent, AnnaLiveIconTemplateComponent, AnnaNoDataComponent, AnnaNonEditableGenericTableComponent, AnnaNotifyIconTemplateComponent, AnnaPayForPerformanceIconTemplateComponent, AnnaRejectedIconTemplateComponent, AnnaReplaceCharPipe, AnnaSortComponent, AnnaSortService, AnnaTableVirtualScrollViewportComponent, AnnaTypeofDataPipe, AnnaVirtualTableDirective, AnnaWeekCalendarComponent, Constants, Days, ErrorCodes, TooltipModel, fixedSizeVirtualScrollStrategyFactory, radioButtonModel, showEllipsisTextOnHoverDirective, tooltipModelForColumnLevelFiltering };
4631
+ export { AllSelectedStatus, AnnaBuyerApprovalIconTemplateComponent, AnnaCalendarFilterComponent, AnnaConvertZeroOrNullOrUndefinedPipe, AnnaCoreModule, AnnaDateTimeFormatService, AnnaFilterSearchedTextPipe, AnnaFilterService, AnnaFixedRowSizeTableVirtualScrollStrategy, AnnaFixedRowSizeTableVirtualScrollStrategyDirective, AnnaGenericTableService, AnnaGlobalConfigService, AnnaIconColumnComponent, AnnaLiveIconTemplateComponent, AnnaNoDataComponent, AnnaNonEditableGenericTableComponent, AnnaNotifyIconTemplateComponent, AnnaPayForPerformanceIconTemplateComponent, AnnaRejectedIconTemplateComponent, AnnaReplaceCharPipe, AnnaSortComponent, AnnaSortService, AnnaTableVirtualScrollViewportComponent, AnnaTypeofDataPipe, AnnaVirtualTableDirective, AnnaWeekCalendarComponent, Constants, Days, DigitOnlyDirective, ErrorCodes, TooltipModel, fixedSizeVirtualScrollStrategyFactory, radioButtonModel, showEllipsisTextOnHoverDirective, tooltipModelForColumnLevelFiltering };
4344
4632
  //# sourceMappingURL=annalib-anna-core.mjs.map