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 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x 8x 8x 2x 2x 2x 6x 2x 4x 2x 2x 1x 3x 5x 5x 8x 3x 1x 2x 2x 1x 1x 2x 6x 6x 6x 6x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x | import { Injectable, Component } from '@angular/core';
import { OnInit, AfterViewInit, OnChanges, OnDestroy } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { ViewChild } from '@angular/core';
import { Input, forwardRef } from '@angular/core';
import { Output, EventEmitter } from '@angular/core';
import { ControlValueAccessor } from "@angular/forms";
import { NG_VALUE_ACCESSOR } from "@angular/forms";
import { ModalController } from '@ionic/angular';
import { firstValueFrom, Subject, Subscription } from 'rxjs';
import { GenericUtils } from '@unpispas/upp-defs';
import { platformService } from './platform';
/**
* Defines the configuration options for alert modals.
*
* @remarks
* The `AlertOptions` type is used to specify various configuration options for alert modals within the application.
* These options can include properties such as input elements, titles, messages, and settings for enabling or disabling
* backdrop dismissal. The type is flexible and can be extended to include any additional options needed for customizing
* alert modals.
*
* @public
*/
export type AlertOptions = any | null;
/************************************/
/* MODAL-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-modal-input',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl:'./input/modal-input.html',
styleUrls: [
'./input/modal-input.scss'
],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => modalInputComponent),
multi: true
}
]
})
export class modalInputComponent implements ControlValueAccessor, OnInit, 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 = 'do-not-autofill';
/** Emits the current value when it changes. */
@Output() Changed = new EventEmitter<string>();
/** Emits an event when the input gains focus. */
@Output() Focus = 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 = '';
/** 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.Focus.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. Initializes the component and configures initial values.
*/
ngOnInit(){
this.value = this.value || '';
Iif ((this.type == 'price') || (this.type == 'number')){
this.value = this.value.toString().replace(",", ".");
}
}
/**
* 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 `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 {
const _value = event.target ? (event.target as HTMLInputElement).value : '';
Iif (![ 'price', 'number' ].includes(this.type)) {
this.value = _value ? _value : '';
}
}
/**
* Handles the `keyup` event and updates the input value based on the type.
* @param event The keyboard event.
*/
onKeyUp(event: KeyboardEvent) : void {
switch(this.type){
case 'price': {
if (event.key == 'Backspace'){
this._price = 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{
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} The current value of the input.
*/
get value(): string {
return this._innerValue;
}
/**
* 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} v - The new value to set.
*/
set value(v: string) {
Iif (v !== this._innerValue) {
this._innerValue = v;
Iif (!v){
this._price = '';
}
this._onChangeCallback(v);
this._onTouchCallback();
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;
}
}
/************************************/
/* MODAL-ALERT COMPONENT */
/************************************/
/**
* A component that displays a customizable alert modal with various input types.
*
* @remarks
* This component is used to create alert modals within the application. It supports various input types such as text,
* checkboxes, and radio buttons, allowing for a flexible and interactive user interface. The component also manages
* user input data, processes dismissal actions, and communicates with the modal controller to present and dismiss the modal.
*
* @public
*/
@Component({
selector: 'upp-modal-alert',
templateUrl: './alert/modal-alert.html',
styleUrls: [
'./alert/modal-alert.scss'
]
})
export class modalAlertComponent implements OnInit {
@Input() data = {};
/**
* Initializes a new instance of the `modalAlertComponent` class.
*
* @param modalCtrl - The modal controller instance used to control the modal.
* @param params - The navigation parameters passed into the modal.
*/
constructor(private modalCtrl: ModalController) {
// nothing to do
}
/**
* Normalizes a string to be used as a variable.
* @param str - The string to be normalized.
* @returns The normalized string or null if no string is provided.
*/
normalize(str: string) : string | null {
return GenericUtils.Normalize(str)
}
/** Holds the parameters passed to the modal component */
public _params: any = {};
/** Stores the input data for the modal component */
public _data: any = {};
/** Indicates whether the modal can be dismissed by clicking on the backdrop */
public _close = true;
/**
* Lifecycle hook that is called after the component's view has been fully initialized.
*
* @remarks
* Retrieves and processes the modal data on initialization.
*/
ngOnInit(){
this._params = this.data;
if (this._params && ('inputs' in this._params)){
for(const _input of this._params['inputs']){
if (_input.type == 'radiobutton'){
continue; // get value from radiogroup
}
this._data[_input.name] = _input.value || null;
}
}
if (this._params && ('backdropDismiss' in this._params)){
this._close = this._params['backdropDismiss'];
}
}
/**
* Closes the modal dialog.
*/
OnClose(){
this.modalCtrl.dismiss(null);
}
/**
* Determines if the input is of text type.
*
* @param _input - The input object to check.
* @returns `true` if the input is a text field, otherwise `false`.
*/
IsText(_input: { type: string }): boolean {
return !this.IsCheck(_input) && !this.IsRadio(_input);
}
/**
* Determines if the input is of checkbox type.
*
* @param _input - The input object to check.
* @returns `true` if the input is a checkbox, otherwise `false`.
*/
IsCheck(_input: { type: string }): boolean {
return _input.type == 'checkbox';
}
/**
* Determines if the input is a radio button or radiogroup.
*
* @param _input - The input object to check.
* @returns `true` if the input is a radio button or radiogroup, otherwise `false`.
*/
IsRadio(_input: { type: string }): boolean {
return _input.type == 'radiogroup' || _input.type == 'radiobutton';
}
/** Stores the names of radio inputs that have been processed to ensure each radio input is handled only once. */
public _radio: any = [];
/**
* Manages the selection state of radio inputs.
*
* @param name - The name of the radio input.
* @returns `true` if the radio input was not previously selected, otherwise `false`.
*/
CheckRadio(name: string): boolean {
if (this._radio.indexOf(name) != -1){
return false;
}
this._radio.push(name);
return true;
}
/**
* Handles the change event for text inputs and updates the `_data` property.
*
* @param value - The new value of the text input.
* @param input - The input object that triggered the change.
*/
OnTextChange(value: any, input: { name: string }): void {
this._data[input.name] = value;
}
/**
* Handles the change event for checkbox inputs.
*
* @param value - The new value of the checkbox input.
* @param input - The input object that triggered the change.
*/
OnCheckChange(value: any, input: any): void {
this._data[input.name] = value.detail.checked;
}
}
/**
* Controller class to manage the presentation and dismissal of the alert modal component.
*/
export class AlertModalController {
private _onDismiss = new Subject<any> ();
private _options: AlertOptions = null;
/**
* Initializes a new instance of the `AlertModalController` class.
*
* @param modalCtrl - The modal controller instance.
* @param options - Options to configure the modal behavior.
*/
constructor(private modalCtrl: ModalController, options: any){
this._options = options;
}
/**
* Presents the alert modal.
*
* @returns A promise that resolves when the modal is presented.
*/
async present(): Promise<void> {
Iif (!this._options){
return;
}
const _modal = await this.modalCtrl.create({
component: modalAlertComponent,
backdropDismiss: ('backdropDismiss' in this._options) ? this._options['backdropDismiss'] : true,
cssClass: 'modal-alert',
componentProps: {
data: this._options
}
});
_modal.onDidDismiss().then(data => {
this._onDismiss.next(data);
this._onDismiss.complete();
});
await _modal.present();
}
/**
* Dismisses the alert modal with the provided data.
*
* @param data - Data to pass back upon dismissal.
* @returns A promise that resolves when the modal is dismissed.
*/
async dismiss(data: any): Promise<void> {
(document.activeElement as HTMLElement)?.blur();
await this.modalCtrl.dismiss(data);
this._onDismiss.next(data);
this._onDismiss.complete();
}
/**
* Returns a promise that resolves with the data when the modal is dismissed.
*
* @returns A promise containing the data from the dismissal event.
*/
onDidDismiss(): Promise<any> {
return firstValueFrom(this._onDismiss.asObservable());
}
}
/************************************/
/* MODAL-ALERT SERVICE */
/************************************/
/**
* A service for creating and managing alert modals within the application.
*
* @remarks
* The `alertService` provides a simplified interface for creating `AlertModalController` instances,
* which are used to present and control alert modals. This service encapsulates the modal controller
* and provides an easy way to configure and present alert modals with custom options, such as enabling
* or disabling backdrop dismissal and specifying input elements within the modal.
*
* @public
*/
@Injectable({
providedIn: 'root'
})
export class alertService {
/**
* Initializes a new instance of the `alertService` class.
*
* @param modalCtrl - The modal controller instance used to create new modals.
*/
constructor(private modalCtrl: ModalController) {
// nothing to do
}
/**
* Creates a new alert modal controller with the specified options.
*
* @param options - Configuration options for the alert modal.
* @returns An instance of `AlertModalController` to manage the modal.
*/
alert(options: AlertOptions): AlertModalController {
return new AlertModalController(this.modalCtrl, options);
}
}
|