All files / components/upp-form upp-form.ts

0% Statements 0/58
0% Branches 0/1
0% Functions 0/14
0% Lines 0/48

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                                                                                                                                                                                                                                                                                                                                                                                             
import { Component, AfterViewInit } from '@angular/core';
import { ChangeDetectorRef } from '@angular/core';
import { Input, HostBinding } from '@angular/core';
import { FormGroup } from '@angular/forms';
 
/************************************/
/* COMMON INTERFACE FOR VIEWS       */
/************************************/
 
/**
 * Represents a view for editing a product, providing methods to validate and handle user actions.
 */
export interface OnForm {
    /**
     * Determines whether the form can be accepted.
     * 
     * @returns {[boolean, boolean]} An array containing two boolean values:
     * 
     * - `true` if the form is valid and can be accepted; otherwise, `false`.
     * - `true` if the result is inconclusive and further validations should be performed.
     */    
    CanAccept(): boolean[];
 
    /**
     * Handles the aplication process.
     * 
     * @returns {Promise<boolean>} Resolves to `true` if the response is inconclusive,
     * allowing further validations to continue; otherwise, `false`.
     */
    OnApply(): Promise <boolean>;
 
    /**
     * Handles the acceptance process.
     * 
     * @returns {Promise<boolean>} Resolves to `true` if the response is inconclusive,
     * allowing further validations to continue; otherwise, `false`.
     */
    OnAccept(): Promise<boolean>;
 
    /**
     * Handles the cancellation process.
     * 
     * @returns {Promise<boolean>} Resolves to `true` if the response is inconclusive,
     * allowing further validations to continue; otherwise, `false`.
     */
    OnCancel(): Promise<boolean>;
}
 
/************************************/
/* UPP-FORM IMPLEMENTATION          */
/************************************/
 
@Component({
    selector: 'upp-form',
    templateUrl: './upp-form.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormComponent {
    @Input() formGroup: FormGroup | null = null;
 
    /*
        Angular cannot proyect twice the ng-content, so we cannot have:
 
        <ng-container *ngIf="formGroup !== null">
            <ng-content></ng-content>
        </ng-container>
        <ng-container *ngIf="formGroup === null">
            <ng-content></ng-content>
        </ng-container>
 
        This is why, we neer a dummyForm, so that the form tag always has linked formGroup
    */
    private _dummyForm: FormGroup | null = null;
    get dummyForm(): FormGroup {
        Iif (!this._dummyForm){
            this._dummyForm = new FormGroup({});
        }
        return this._dummyForm;
    }
}
 
@Component({
    selector: 'upp-form-image',
    templateUrl: './parts/upp-image.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormImageComponent {
    // nothing to do
}
 
@Component({
    selector: 'upp-form-section',
    templateUrl: './parts/upp-section.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormSectionComponent {
    @Input() title: string | null = null;
    @Input() subtitle: string | null = null;
    @Input() image: string | null = null;
    @Input() icon: string | null = null;
    @Input() fill: 'clear' | 'solid' = 'solid';
 
    @HostBinding('class') get panelClasses(): string {
        const floatClass = `--${this.fill}`;
        return `${floatClass}`.trim();
    }        
}
 
@Component({
    selector: 'upp-form-action',
    templateUrl: './parts/upp-action.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormActionComponent {
    @Input() color: string | null = null;
    @Input() slot: 'start' | 'end' = 'start';
    @Input() icon: string | null = null;
    @Input() title: string | null = null;
    @Input() subtitle: string | null = null;
}
 
@Component({
    selector: 'upp-form-warning',
    templateUrl: './parts/upp-warning.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormWarningComponent implements AfterViewInit {
    @Input() color: string | null = null;
    @Input() lines: 'solid' | 'none' = 'solid'; 
    @Input() slot: 'start' | 'end' = 'start';
    @Input() icon: string | null = null;
    @Input() title: string | null = null;
    @Input() subtitle: string | null = null;
 
    @HostBinding('class') get panelClasses(): string {
        const borderClass = `--${this.lines}`;
        return `${borderClass}`.trim();
    }  
 
    private _visible = false;
    get Visible(): boolean {
        return this._visible;
    }
 
    set Visible(value: boolean){
        this._visible = value;
        this.change.markForCheck();
    }
 
    constructor(private change: ChangeDetectorRef){
        // nothing to do
    }
 
    ngAfterViewInit(): void {
        setTimeout(() => {
            this.Visible = true;
        }, 100);
    }
}
 
@Component({
    selector: 'upp-form-buttonbar',
    templateUrl: './parts/upp-buttonbar.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormButtonBarComponent {
    // nothing to do
}
 
@Component({
    selector: 'upp-form-barbutton',
    templateUrl: './parts/upp-barbutton.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormBarButtonComponent {
    @Input() active = true;
}
 
@Component({
    selector: 'upp-form-buttons',
    templateUrl: './parts/upp-buttons.html',
    styleUrls: ['./upp-form.scss'],
})
export class UppFormButtonsComponent {
    @Input() float: 'top' | 'bottom' = 'bottom';
 
    @HostBinding('class') get panelClasses(): string {
        const floatClass = `--${this.float}`;
        return `${floatClass}`.trim();
    }    
}