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