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 | 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-TEXTAREA 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-textarea', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./kb/upp-textarea.html', styleUrls: [ './kb/upp-textarea.scss' ], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UppKbTextAreaComponent), multi: true } ] }) export class UppKbTextAreaComponent implements ControlValueAccessor, AfterViewInit, OnDestroy { /** Placeholder text for the input. */ @Input() placeholder = ''; /** Indicates if the input should be readonly. */ @Input() readonly = false; /** Number of rows for the text area */ @Input() rows = 1; /** Name of the input control. */ @Input() name = 'none'; /** Enables or disables kiosk mode for the input. */ @Input() kiosk = true; /** Disables the input when set to true. */ @Input() disabled = false; /** Configures autocomplete behavior for the input. */ @Input() autocomplete = 'do-not-autofill'; /** 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); } /** * 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(); }); } }); } /** * Lifecycle hook. Cleans up subscriptions on destroy. */ ngOnDestroy(){ Iif (this._kiosk_subscription){ this._kiosk_subscription.unsubscribe(); this._kiosk_subscription = null; } } /** * Handles the `input` event for text-like input fields * 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 { this.value = event.target ? (event.target as HTMLInputElement).value : ''; } /** * Handles the `keyup` event and updates the input value based on the type. * @param event The keyboard event. */ onKeyUp(event: KeyboardEvent) : void { void event; // nothing to do } /** * Handles changes when in kiosk mode. * @param event The input event. */ onKiosk(event: Event): void{ this.value = event.target ? (event.target as HTMLInputElement).value : ''; } /********************************************/ /* 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; 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-TEXTAREA ERROR COMPONENT */ /*************************************/ /** * A component for displaying validation errors for form controls. * Automatically integrates with Angular reactive forms using `formControlName`. */ @Component({ selector: 'upp-er-textarea', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./er/upp-textarea.html', styleUrls: [ './er/upp-textarea.scss' ] }) export class UppErTextAreaComponent 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-TEXTAREA 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-textarea', changeDetection: ChangeDetectionStrategy.OnPush, templateUrl:'./upp-textarea.html', styleUrls: [ './upp-textarea.scss' ], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => UppTextAreaComponent), multi: true } ] }) export class UppTextAreaComponent 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() kiosk = true; /** Number of rows for the text area */ @Input() rows = 1; /** 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() { Iif (this._innerValue != undefined){ 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; } /********************************************/ /* get/set accesors */ /********************************************/ @Input() /** * Gets the current value of the input. * @returns {string} 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} 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; } } |