Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | import { Optional, Component, OnInit, AfterViewInit, OnChanges, OnDestroy } from '@angular/core'; import { Input, forwardRef } from '@angular/core'; import { Output, EventEmitter } from '@angular/core'; import { ViewChild } from '@angular/core'; import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core'; import { AbstractControl, FormGroup, FormGroupDirective } from '@angular/forms'; import { ControlContainer, ControlValueAccessor } from "@angular/forms"; import { NG_VALUE_ACCESSOR } from "@angular/forms"; import { Subscription } from 'rxjs'; import { platformService } from '@unpispas/upp-base'; // refs: https://almerosteyn.com/2016/04/linkup-custom-control-to-ngcontrol-ngmodel interface ErrorInfo { [key: string]: string; } /**********************************/ /* UPP-INPUT KEYBOARD COMPONENT */ /**********************************/ /** * A custom Angular component for keyboard-based input. * Supports integration with Angular Forms through `ControlValueAccessor`. * Provides custom behavior for `price` and `number` types, and supports kiosk mode. * * @implements {OnInit} * @implements {AfterViewInit} * @implements {OnChanges} * @implements {OnDestroy} */ @Component({ selector: 'upp-kb-input', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./kb/upp-input.html', styleUrls: [ './kb/upp-input.scss' ], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UppKbInputComponent), multi: true } ] }) export class UppKbInputComponent implements ControlValueAccessor, AfterViewInit, OnChanges, OnDestroy { /** Placeholder text for the input. */ @Input() placeholder = ''; /** Indicates if the input should be readonly. */ @Input() readonly = false; /** Name of the input control. */ @Input() name = 'none'; /** Enables or disables kiosk mode for the input. */ @Input() kiosk = true; /** Type of the input (e.g., `text`, `number`, `price`). */ @Input() type = ''; /** Disables the input when set to true. */ @Input() disabled = false; /** Configures autocomplete behavior for the input. */ @Input() autocomplete = 'on'; /** Emits the current value when it changes. */ @Output() Changed = new EventEmitter<string | null>(); /** Emits an event when the input gains focus. */ @Output() Focused = new EventEmitter<void>(); /** Reference to the native input element. */ @ViewChild('input', {static: false}) _input: any; /** Internal flag indicating if the input is disabled. */ public isDisabled = false; /* eslint-disable @typescript-eslint/no-unused-vars */ private _onChangeCallback = (_v:any) => { // nothing to do (required by angular) }; private _onTouchCallback = () => { // nothing to do (required by angular) }; /* eslint-enable @typescript-eslint/no-unused-vars */ /** Internal value of the input. */ private _innerValue: string | null | undefined = undefined; /** Indicates if the component is in kiosk mode. */ public _kiosk = false; /** Tracks if the input currently has focus. */ public _focus = false; /** * Handles focus events and emits `uppFocus`. * @param value Whether the input has focus. */ onFocus(value: boolean): void{ this._focus = value; this.Focused.emit(); } /** * Native element reference for the input. * @returns {any} The parent node of the input element. */ get nativeElement(): any{ Iif (!this._input){ return null; } return this._input?.nativeElement.parentNode; } /** * Simulates a key press for the input element. * @param key The key to simulate. */ OnKeyPress(key: string): void{ const _event = new KeyboardEvent("keyup", { key: key, shiftKey: false, // you don't need to include values ctrlKey: false, // if you aren't going to use them. metaKey: false // these are here for example's sake. }) this.onKeyUp(_event); } /** * Sets focus on the input element. */ SetFocus(): void{ Iif (!this._input){ return; } setTimeout(() => { this._input.nativeElement.focus(); this._input.nativeElement.click(); }, 50); } /** * Gets the formatted price value. * @returns {string} The formatted price or placeholder text. */ get Price(): string{ Iif (!this._innerValue) { return this._focus ? '' : this.placeholder; } return Number(this._innerValue).toFixed(2); } /** * Constructs an instance of `UppKbInputComponent`. * * @param {ChangeDetectorRef} change - A service to trigger manual change detection, ensuring the component reflects updates to its state. * @param {platformService} platform - A service to provide platform-specific functionality, such as kiosk detection. */ constructor(private change: ChangeDetectorRef, private platform: platformService) { // nothing to do } private _kiosk_subscription: Subscription | null = null; /** * Lifecycle hook. Called after the view is initialized. * Configures kiosk mode and subscribes to platform changes. */ ngAfterViewInit(){ this.platform.ready(). then(() => { this._kiosk = this.kiosk && this.platform._isKiosk; this.change.markForCheck(); Iif (this.kiosk){ // do not subscribe if kiosk is disabled this._kiosk_subscription = this.platform.OnKioskChanged.subscribe( () => { this._kiosk = this.platform._isKiosk; this.change.markForCheck(); }); } }); } public _price = ''; // keep the complete price value /** * Lifecycle hook. Updates the component when inputs change. */ ngOnChanges() { Iif (this.type == 'price'){ const _parts = this.value ? this.value.toString().split('.') : [ '0', '0' ]; Iif (_parts.length == 2){ this._price = _parts[0] + (_parts[1] + "00").slice(0, 2); } } } /** * Lifecycle hook. Cleans up subscriptions on destroy. */ ngOnDestroy(){ Iif (this._kiosk_subscription){ this._kiosk_subscription.unsubscribe(); this._kiosk_subscription = null; } } /** * Determines the type of the input field. * Returns `text` if the input type is `password` and the password visibility is toggled on. * Otherwise, it returns the original input type. * * @returns {string} The type of the input (`text` or `password`). */ get _text(): string{ Iif (this.type == 'password'){ return (this._showpassword) ? 'text' : 'password'; } return 'text'; } private _showpassword = false; /** * Indicates whether the password input is visible (`true`) or hidden (`false`). * * @returns {boolean} The visibility state of the password input field. */ get ShowPassword(){ return this._showpassword; } /** * Toggles the visibility of the password input field. * Changes the input type between `text` and `password`. * Triggers Angular's change detection to update the UI state. * * @returns {void} */ OnShowPassword(){ this._showpassword = !this._showpassword; this.change.markForCheck(); } /** * Handles the `change` event for input fields, specifically designed to capture browser autocomplete changes. * Unlike `onInput` and `onKeyUp`, this method will trigger when the browser's autocomplete * feature populates the input field, ensuring that autocompleted values are properly processed. * * The method processes the input value according to the component's type: * - For regular text inputs, it simply updates the value * - For price inputs, it converts the input to the correct price format * - For number inputs, it handles decimal separators appropriately * * @param {Event} event - The DOM change event from the input element * @returns {void} */ onChange(event: Event): void { const _value = event.target ? (event.target as HTMLInputElement).value : ''; Iif (_value != ''){ this._innerValue = null; } switch(this.type){ case 'price': this._price = _value; this.value = _value.replace(",", "."); break; case 'number': this.value = _value.replace(",", "."); break; default: this.value = _value; break; } } /** * Handles the `input` event for text-like input fields (excluding `price` and `number` types). * Updates the internal value (`this.value`) when the user modifies the input field. * * @param {Event} event - The `input` event triggered by the user. * @returns {void} */ onInput(event: Event): void { Iif (this.readonly){ return; } const _value = event.target ? (event.target as HTMLInputElement).value : ''; Iif (![ 'price', 'number' ].includes(this.type)) { this.value = _value; } } /** * Handles the `keyup` event and updates the input value based on the type. * @param event The keyboard event. */ onKeyUp(event: KeyboardEvent) : void { Iif (this.readonly){ return; } switch(this.type){ case 'price': { if (event.key == 'Backspace'){ this._price = this._price || ''; Iif (this._price.length > 0){ this._price = this._price.slice(0, -1); } } else { Iif (isNaN(Number(event.key))){ return; // ignore key if not a number } this._price = (this._price || '') + event.key; } this.value = (Number(this._price)/100).toString(); Iif (isNaN(Number(this.value))){ this.value = this._price = ''; } break; } case 'number': { if (event.key == 'Backspace'){ this._price = this._price ? this._price.slice(0, -1) : ''; } else { Iif (isNaN(Number(event.key)) && ([',', '.'].indexOf(event.key) == -1)){ return; // ignore key if not a number, ',' or '.' } this._price = (this._price || '') + event.key; } this.value = this._price.replace(",", "."); const _isnumber = this.value.endsWith('.') ? this.value.slice(0, this.value.length - 1) : this.value; Iif (isNaN(Number(_isnumber))){ this.value = this._price = ''; } break; } } } /** * Handles changes when in kiosk mode. * @param event The input event. */ onKiosk(event: Event): void{ Iif (this.readonly){ return; } const _value = event.target ? (event.target as HTMLInputElement).value : ''; switch(this.type){ case 'price': this.value = _value ? (Number(_value)/100).toString() : ''; Iif (isNaN(Number(this.value))){ this.value = ''; } break; case 'number': this.value = _value ? _value.replace(",", ".") : ''; Iif (isNaN(Number(this.value))){ this.value = ''; } break; default: this.value = _value ? _value : ''; break; } } /********************************************/ /* get/set accesors */ /********************************************/ @Input() /** * Gets the current value of the input. * This represents the internal state of the input field. * * @returns {string | null} The current value of the input. */ get value(): string | null { return this._innerValue || null; } /** * Sets a new value for the input. * This updates the internal state, triggers the `onChange` and `onTouch` callbacks, * and emits the `uppChanged` event. * * @param {string | null} v - The new value to set. */ set value(v: string | null) { const _firstcall = (this._innerValue === undefined); Iif (v !== this._innerValue) { this._innerValue = v; Iif (v === null){ this._price = ''; } this._onChangeCallback(v); this._onTouchCallback(); Iif (!_firstcall){ this.Changed.emit(v); } } } /********************************************/ /* ControlValueAccessor */ /********************************************/ /** * Writes a value to the component. Required by `ControlValueAccessor`. * @param value The new value to set. */ writeValue(value: any) { Iif (value !== this._innerValue) { this.value = value; } } /** * Registers a callback function to be called when the value changes. * @param fn The callback function. */ registerOnChange(fn: any) { this._onChangeCallback = fn; } /** * Registers a callback function to be called when the input is touched. * @param fn The callback function. */ registerOnTouched(fn: any) { this._onTouchCallback = fn; } /** * Sets the disabled state of the input. * @param isDisabled Whether the input should be disabled. */ setDisabledState(isDisabled: boolean): void { this.isDisabled = isDisabled; } } /**********************************/ /* UPP-INPUT ERROR COMPONENT */ /**********************************/ /** * A component for displaying validation errors for form controls. * Automatically integrates with Angular reactive forms using `formControlName`. */ @Component({ selector: 'upp-er-input', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./er/upp-input.html', styleUrls: [ './er/upp-input.scss' ] }) export class UppErInputComponent implements OnChanges { /** The formGroup provided to the related upp-input. */ @Input() form: FormGroup | null = null; /** The name of the form control to track errors for. */ @Input() name: string | null = null; /** A mapping of error keys to their respective error messages. */ @Input() errornfo: ErrorInfo | null = null /** Whether the error messages should be centered. */ @Input() centered = false; /** Forces OnChanges to be called on value change. */ @Input() value: string | null = null; /** The list of active error keys. */ public errors: string[] = [] /** The form control associated with this component. */ public control: AbstractControl | null = null; /** * Constructor for dependency injection. */ constructor(){ // nothing to do } /** * Lifecycle hook. Updates the list of errors when inputs change. */ ngOnChanges() { Iif (this.form && this.name){ this.control = this.form.get(this.name); } Iif (this.control){ this.errors = []; Iif (this.errornfo && this.control.errors){ this.errors = Object.keys(this.control.errors).filter(key => Object.prototype.hasOwnProperty.call(this.errornfo, key)) } } } } /**********************************/ /* UPP-INPUT COMPONENT */ /**********************************/ /** * A reusable input component that supports Angular Forms integration, * custom validation, and advanced UI features like kiosk mode. * * @implements {ControlValueAccessor} * @implements {OnInit} * @implements {OnChanges} * @implements {OnDestroy} */ @Component({ selector: 'upp-input', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./upp-input.html', styleUrls: [ './upp-input.scss' ], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UppInputComponent), multi: true } ] }) export class UppInputComponent implements ControlValueAccessor, OnInit, OnChanges, OnDestroy { /** Placeholder text for the input. */ @Input() placeholder = ''; /** The name of the form control this input is bound to. */ @Input() formControlName = ''; /** Indicates if the input should be readonly. */ @Input() readonly = false; /** Indicates if the input should be disabled. */ @Input() disabled = false; /** The type of the input field (e.g., `text`, `number`). */ @Input() type = ''; /** Enables od disabled the kiosk input */ @Input() kiosk = true; /** A title to display above the input. */ @Input() title = ''; /** Configures autocomplete behavior for the input. */ @Input() autocomplete = 'off'; /** A mapping of error keys to their respective error messages. */ @Input() errornfo: ErrorInfo = {} /** Emits the current value when it changes. */ @Output() Changed = new EventEmitter<string | null>(); /** Emits an event when the input gains focus. */ @Output() Focus = new EventEmitter<void>(); @ViewChild('input', {static: false}) _input: any; /** optional FromGroup */ public formGroup: FormGroup | null = null public formControl: AbstractControl | null = null; private _innerValue: string | null | undefined = undefined; /* eslint-disable @typescript-eslint/no-unused-vars */ /** Callback to notify Angular Forms when the value changes. */ private _onChangeCallback = (v: any) => { // nothing to do (required by angular) } /** Callback to notify Angular Forms when the input is touched. */ private _onTouchCallback = () => { // nothing to do (required by angular) } /* eslint-enable @typescript-eslint/no-unused-vars */ private _showtitle = false; /** * Determines whether the input's title should be displayed. * @returns {boolean} True if the title should be displayed; otherwise, false. */ get showTitle(): boolean { return this._showtitle; } private _itemtitle: string | null = null; /** * Returns the processed title value. * @returns {string} The input's title. */ get viewTitle(): string { return this._itemtitle || ''; } /** * Constructor for dependency injection. * @param controlContainer Provides access to the parent form group. * @param change A service to trigger manual change detection. */ constructor(@Optional() private controlContainer: ControlContainer, private change: ChangeDetectorRef) { // nothing to do } private _form_subscription: Subscription | null = null; /** * Lifecycle hook that initializes the component. * Sets up form control integration and initializes the value. */ ngOnInit() { this._innerValue = this.value; Iif (this.controlContainer && this.controlContainer instanceof FormGroupDirective){ this.formGroup = this.controlContainer.form; } this.formControl = (this.formGroup && this.formControlName) ? this.formGroup.get(this.formControlName) : null; Iif (this.formControl){ this._innerValue = this.formControl.value || null; this._form_subscription = this.formControl.valueChanges.subscribe( () => { this.value = this.formControl?.value || null; }); } this.onChange(); } /** * Lifecycle hook that detects changes to the input's properties. */ ngOnChanges() { this.onChange(); } /** * Lifecycle hook that cleans up resources on component destruction. * Unsubscribes from form control value changes. */ ngOnDestroy() { Iif (this._form_subscription){ this._form_subscription.unsubscribe(); this._form_subscription = null; } } /** * Handles key press events and delegates them to the input element. * @param key The key that was pressed. */ OnKeyPress(key: string) : void { Iif (this._input){ this._input.OnKeyPress(key); } } /** * Sets focus on the input element. */ SetFocus(): void { Iif (this._input){ this._input.SetFocus(); } } /** * Handles value changes and updates the title visibility. * Emits a value change event. */ onChange(): void { this._itemtitle = (this.title) ? this.title : this.placeholder; this._showtitle = (this._innerValue !== null) && (this._innerValue !== ''); Iif (this._innerValue != undefined){ this.Changed.emit(this._innerValue); } this.change.markForCheck(); } /** * Emits a focus event when the input gains focus. */ onFocus(): void { this.Focus.emit(); } /********************************************/ /* disable control */ /********************************************/ /** * Determines whether the input is disabled. * @returns {boolean} True if the input is disabled; otherwise, false. */ get _disabled(): boolean { // if directly disabled, return true Iif (this.disabled){ return true; } // check if the the form control is disabled const _formcontrol = (this.formGroup && this.formControlName) ? this.formGroup.get(this.formControlName) : null; Iif (_formcontrol){ return _formcontrol.disabled; } // not disabled return false; } /** * Determines whether the input must display error information * @returns {boolean} True if the input has errors; otherwise, false. */ get errors(): boolean { Iif (this.formControl){ return (this.formControl.dirty || this.formControl.touched) && (this.formControl.errors != null); } return false; } /********************************************/ /* get/set accesors */ /********************************************/ @Input() /** * Gets the current value of the input. * @returns {string | null} The current value of the input. */ get value(): string | null { return this._innerValue || null; } /** * Sets a new value for the input. * Updates the internal value, notifies Angular Forms, and triggers value change events. * @param {string | null} v - The new value to set. */ set value(v: string | null) { Iif (v != this._innerValue) { this._innerValue = v; this.onChange(); this._onChangeCallback(v); this._onTouchCallback(); } } /********************************************/ /* ControlValueAccessor */ /********************************************/ /** * Writes a value to the input. Required by `ControlValueAccessor`. * @param {string} value - The value to write. */ writeValue(value: string) { Iif (value !== this._innerValue) { this.value = value; this.onChange(); } } /** * Registers a callback function to be called when the input value changes. * Required by `ControlValueAccessor`. * @param {Function} fn - The callback function. */ registerOnChange(fn: any) { this._onChangeCallback = fn; } /** * Registers a callback function to be called when the input is touched. * Required by `ControlValueAccessor`. * @param {Function} fn - The callback function. */ registerOnTouched(fn: any) { this._onTouchCallback = fn; } } |