All files / components/view upp-view.ts

89.6% Statements 112/125
87.23% Branches 41/47
84.21% Functions 16/19
88.88% Lines 104/117

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 2686x 6x 6x 6x           6x   6x 6x 6x 6x   6x       6x                     20x       6x 20x 20x   20x 20x   20x 20x     20x 20x     3x 1x 1x     3x 1x 1x     3x 3x           3x 3x               21x   21x 2x 2x     21x 2x 2x                     1x       6x 1x     5x 5x 1x 1x       4x       3x       3x     3x 1x 1x 1x 1x           3x 2x 1x         3x 2x     3x       2x 2x 1x 1x       1x               1x       3x 1x     2x 2x       3x 1x     2x 2x                       1x 1x         1x       1x 1x 1x 1x 1x     1x 2x     1x       1x       1x 1x 1x 1x 1x     1x 2x     1x       1x 1x   1x 1x 1x   1x 2x     1x   1x       1x 1x                          
import { Component, OnChanges, OnDestroy } from '@angular/core';
import { Input, Output, EventEmitter } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { ViewChildren, QueryList } from '@angular/core';
import { CdkDragDrop } from '@angular/cdk/drag-drop';
 
import { Subscription } from 'rxjs';
 
import { OnForm } from '@unpispas/upp-wdgt';
import { ViewRenderer } from '@unpispas/upp-wdgt';
 
import { alertService } from '@unpispas/upp-base';
import { languageService, languageSupport } from '@unpispas/upp-base';
import { dataService, ProductOptView } from '@unpispas/upp-data';
import { viewService } from '@unpispas/upp-base';
 
import { ProductOpt } from '@unpispas/upp-data';
import { ProductView } from '@unpispas/upp-data';
import { CategoryView } from '@unpispas/upp-data';
 
import { UppOptionEditComponent } from '../option/edit/upp-edit';
 
@Component({
    selector: 'upp-category-view',
    changeDetection: ChangeDetectionStrategy.OnPush,
    templateUrl: './upp-view.html',
    styleUrls: [
      './upp-view.scss'
    ],
    providers: [{
        provide: ViewRenderer,
        useFactory: (change: ChangeDetectorRef) => new ViewRenderer(change),
        deps: [ChangeDetectorRef]
    }]       
})
export class UppCategoryViewComponent extends languageSupport implements OnForm, OnChanges, OnDestroy {
    @Input() product: ProductView | null = null;
    @Input() category: CategoryView | null = null;
 
    @Output() Updated = new EventEmitter<CategoryView>();
    @Output() Edit = new EventEmitter<CategoryView>();
 
    constructor(private lang: languageService, private change: ViewRenderer, private alertCtrl: alertService, private data: dataService, public view: viewService){
        super(lang, change.cdref);
    }
 
    private _catgry_subscription: Subscription | null = null;
    private _prodct_subscription: Subscription | null = null;
 
    ngOnChanges(){
        if (this._prodct_subscription){
            this._prodct_subscription.unsubscribe();
            this._prodct_subscription = null;
        }
 
        if (this._catgry_subscription){
            this._catgry_subscription.unsubscribe();
            this._catgry_subscription = null;
        }
 
        if (this.product){
            this._prodct_subscription = this.product.OnViewRefresh.subscribe(
            () => {
                this.change.markForCheck();
            });
        }
 
        if (this.category){
            this._catgry_subscription = this.category.OnViewRefresh.subscribe(
            () => {
                this.change.markForCheck();
            });
        }
    }
 
    ngOnDestroy(): void {
        super.OnDestroy();
 
        if (this._prodct_subscription){
            this._prodct_subscription.unsubscribe();
            this._prodct_subscription = null;
        }
 
        if (this._catgry_subscription){
            this._catgry_subscription.unsubscribe();
            this._catgry_subscription = null;
        }
    }
 
    /********************************/
    /* ONFORM IMPLEMENTATION        */
    /********************************/
    
    @ViewChildren(UppOptionEditComponent) _formitems!: QueryList<OnForm>;
 
    CanAccept(): boolean[] {
        return [ true, true ];
    }
 
    async OnApply(): Promise <boolean> {
        if (!this.category){
            return false;
        }
 
        const _forms = this._formitems.toArray();
        for (const _form of _forms){
            if (_form && !(await _form.OnApply())){
                return false;   // operation completed
            }
        }
 
        return true;
    }
 
    async OnAccept(): Promise <boolean> {
        Iif (!this.category){
            return false;
        }
 
        await this.OnApply();
 
        // remove the invalid dependencies
        for (const _depend of this.category.depends){
            const productoptview = this.data.alives.get(_depend.option) as ProductOptView;
            if (productoptview){
                if (!productoptview.IsValid || !productoptview.Enabled){
                    _depend.status = 'DE';
                }
            } 
        }        
 
        // remove the invalid options
        for (const _option of this.category.options){
            if (!_option.IsValid || !_option.Enabled){
                _option.status = 'DE';
            }
        }  
        
        // remove empty categories
        if (this.category.options.length == 0){
            this.category.status = 'DE';
        }
 
        return true;
    }
 
    async OnCancel(): Promise <boolean> {
        const _forms = this._formitems.toArray();
        for (const _form of _forms){
            if (_form && !(await _form.OnCancel())){
                return false;   // operation completed
            }
        }
 
        return true;
    } 
 
    /********************************/
    /* DISPLAY OPTIONS              */
    /********************************/ 
 
    OptionTrack(index: number, option: ProductOptView): any {
        return option?._uuid || null;
    }
 
    get CanUp(): boolean {
        if (!this.product || !this.category || (this.product.categories.length == 0)){
            return false;
        }
 
        const pos = 0;  // first array position
        return (this.product.categories[pos] != this.category);
    }
 
    get CanDown(): boolean {
        if (!this.product || !this.category || (this.product.categories.length == 0)){
            return false;
        }
 
        const pos = this.product.categories.length - 1;  // last array position
        return (this.product.categories[pos] != this.category);
    }
 
    /********************************/
    /* EVENT HANDLERS               */
    /********************************/    
    
    OnUpdated(){
        this.Updated.emit();
    }
 
    OnCategoryEdit(){
        if (this.category){
            this.Edit.emit(this.category);
        }
    }
 
    OnCategoryUp(): void {
        Iif (!this.product || !this.category){
            return;
        }
 
        const pos = this.product.categories.indexOf(this.category);
        if (pos != -1){
            const _element = this.product.categories[pos];
            this.product.categories.splice(pos+0, 1);
            this.product.categories.splice(pos-1, 0, _element);
        }
 
        for (let _idx=0; _idx < this.product.categories.length; _idx++){
            this.product.categories[_idx].sort = _idx;
        }
 
        this.Updated.emit();
    }
 
    OnCategoryDown(): void {
        Iif (!this.product || !this.category){
            return;
        }
 
        const pos = this.product.categories.indexOf(this.category);
        if (pos != -1){
            const _element = this.product.categories[pos];
            this.product.categories.splice(pos+0, 1);
            this.product.categories.splice(pos+1, 0, _element);
        }
 
        for (let _idx=0; _idx < this.product.categories.length; _idx++){
            this.product.categories[_idx].sort = _idx;
        }
 
        this.Updated.emit();
    }
 
    OnOptionDrop(event: CdkDragDrop<ProductOptView[]>, options: ProductOptView[]): void {
        const _prev = event.previousIndex;
        const _next = event.currentIndex;
        
        const _element = options[_prev];
        options.splice(_prev, 1);
        options.splice(_next, 0, _element);
        
        for (let _idx=0; _idx < options.length; _idx++){
            options[_idx].sort = _idx;
        }
 
        this.Updated.emit();
        
        this.change.markForCheck();
    }
 
    OnAddOption(){
        if (!this.category){
            return;
        }
 
        const _option = new ProductOpt(null, this.data);
        Iif (_option){
            _option.product = this.category.product.object;
            _option.category = this.category.object;
            _option.sort = this.category.options.length;
            _option.status = 'AC';
 
            this.category.AddOption(_option);
        }
    }
}