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

0% Statements 0/81
0% Branches 0/31
0% Functions 0/23
0% Lines 0/74

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { Directive, Component, forwardRef } from '@angular/core';
import { AfterContentInit, OnInit, OnDestroy } from '@angular/core';
import { Input, HostBinding } from '@angular/core';
import { ContentChildren, QueryList } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Subscription } from 'rxjs';
 
import { configService, Configuration } from '@unpispas/upp-base';
 
/**
 * Enumeration for grid configuration keys.
 * Defines the keys used in the configuration object for the UppGrid component.
 */
export enum UppGridConfiguration {
    /**
     * Represents the key for the grid's view mode.
     * This determines whether the grid is displayed in 'GRID' or 'LIST' view mode.
     * - **Default**: `'GRID'`
     */
    viewmode = 'viewmode',
 
    /**
     * Represents the key for the grid's thumbnail size.
     * This determines the scaling factor for the grid items, where the value is 
     * typically a number representing a percentage or factor of the base size.
     * - **Default**: `10` (100% scale)
     */    
    thumbsize = 'thumbsize'
}
 
/**
 * A component to display when the grid is empty.
 * The content is projected from the parent component.
 */
@Component({
    selector: 'upp-grid-empty',
    template: `<ng-content></ng-content>`,
    styleUrls: []
})
export class UppGridEmptyComponent {
    // nothing to do
}
 
/**
 * Directive for components that handle view mode changes.
 */
@Directive({
    selector: '[uppViewMode]'    
})
export class ViewModeDirective {
    protected _viewmode: 'GRID' | 'LIST' = 'GRID';
 
    /**
     * Gets the current view mode.
     */    
    get viewMode(){
        return this._viewmode;
    }
 
    /**
     * Sets the view mode and updates child items if the value has changed.
     * @param value - The new view mode ('GRID' or 'LIST').
     */   
    set viewMode(value: 'GRID' | 'LIST'){
        this._viewmode = value;
    }
}
 
/**
 * Represents an individual item in the grid.
 * This component supports view mode changes ('GRID' or 'LIST').
 */
@Component({
    selector: 'upp-grid-item',
    providers: [{ 
        provide: ViewModeDirective, 
        useExisting: forwardRef(() => UppGridItemComponent) 
    }],
    template: `
        <div uppViewMode class="thumbnail-item">
            <ng-content></ng-content>
        </div>
    `,
    styleUrls: [],
})
export class UppGridItemComponent extends ViewModeDirective implements AfterContentInit {
    /** identifies the list of upp-grid-item in the template */
    @ContentChildren(ViewModeDirective, { descendants: true }) items!: QueryList<ViewModeDirective>;
 
    /**
     * Lifecycle hook that runs after content initialization.
     * Ensures any child items are updated with the current view mode.
     */
    ngAfterContentInit() {
        this.items.changes.subscribe(() => {
            this._updateItems();
        });
 
        this._updateItems();
    }
 
    /**
     * Updates child items with the current view mode.
     * @private
     */    
    private _updateItems() {
        Iif (this.items?.length > 0) {
            for(const _item of this.items){
                _item.viewMode = this._viewmode;
            };
        }
    }    
 
    /**
     * Gets the current view mode.
     */ 
    override get viewMode(): 'GRID' | 'LIST' {
        return super.viewMode;
    }
 
    /**
     * Sets the view mode and updates child items if the value has changed.
     * @param value - The new view mode ('GRID' or 'LIST').
     */   
    override set viewMode(value: 'GRID' | 'LIST'){
        Iif (super.viewMode != value){
            super.viewMode = value;
            this._updateItems();
        }
    }
}
 
/**
 * Represents a grid component that dynamically handles items and configuration.
 * Supports two view modes ('GRID' and 'LIST') and scaling via a `thumsize` value.
 */
@Component({
    selector: 'upp-grid',
    templateUrl: './upp-grid.html',
    styleUrls: ['./upp-grid.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class UppGridComponent implements OnInit, AfterContentInit, OnDestroy {
    /**
     * The name of the grid configuration.
     * This input is required to load configuration settings.
     */    
    @Input() name: string | null = null;
 
    /**
     * The desired card size
     * SMALL for square cards and LARGE for rectangular cards
     */    
    @Input() size: 'SMALL' | 'LARGE' = 'SMALL';
 
    private _config: Configuration | null = null;
    private _onconfig_subscription: Subscription | null = null;
 
    private _viewmode: 'GRID' | 'LIST' = 'GRID';
    private _thumsize = 10;
 
    /**
     * Gets the current view mode of the grid.
     */
    get ViewMode(){
        return this._viewmode;
    }
 
    /**
     * Computes the scaling factor based on the `thumsize` value.
     */    
    get ScaleMode(){
        return this._thumsize / 10;
    }
        
    /**
     * Checks if the grid is empty (i.e., no child items are present).
     */    
    get IsEmpty(){
        return !this.items || (this.items.length == 0);
    }
 
    constructor(private config: configService, private change: ChangeDetectorRef){
        // nothing to do
    }
 
    /**
     * Dynamically calculates the CSS classes for the grid based on the `size` inputs.
     * 
     * @returns A string representing the classes to apply to the panel.
     */    
    @HostBinding('class') get gridClasses(): string {
        Iif (['SMALL', 'LARGE'].includes(this.size)){
            const sizeClass = `--${this.size.toLowerCase()}`;
            return `${sizeClass}`.trim();    
        }
 
        return '--small';   // default value
    }
 
    /**
     * Refreshes the grid configuration, updating the view mode and thumbnail size.
     * Detects and applies changes only when necessary.
     * @private
     */    
    private _refreshConfiguration(){
        Iif (this._config){
            const _oldviewmode = this._viewmode;     // keep previous value to check for changes
            const _newviewmode = this._config.get(UppGridConfiguration.viewmode) as 'GRID' | 'LIST' || this._viewmode;
            Iif (['GRID', 'LIST'].includes(_newviewmode)){
                this._viewmode = _newviewmode;
            }
 
            Iif (_oldviewmode != this._viewmode){
                this._updateItems();
            }
 
            const _newthumsize = Number(this._config.get(UppGridConfiguration.thumbsize)) || this._thumsize; 
            Iif (!isNaN(_newthumsize)) {
                this._thumsize = _newthumsize;
            }
 
            this.change.markForCheck();
        }
    }
 
    /**
     * Obtains the configuration instance for this specific grid (by name).
     * @private
     */   
    private get configuration(){
        Iif ((this.name) && (this._config == null)){
            this._config = this.config.getConfiguration(this.name);
        }
        return this._config;
    }
 
    /**
     * Lifecycle hook that initializes the grid configuration and subscribes to updates.
     */
    ngOnInit(){
        const _config = this.configuration;
        Iif (_config){
            // initialize the configuration values
            Iif (_config.get(UppGridConfiguration.viewmode) == null){
                _config.set(UppGridConfiguration.viewmode, this._viewmode);
            }
 
            Iif (_config.get(UppGridConfiguration.thumbsize) == null){
                _config.set(UppGridConfiguration.thumbsize, this._thumsize);
            }
 
            _config.commit();
 
            this._onconfig_subscription = _config.OnUpdated.subscribe(
            () => {
                this._refreshConfiguration();
            });
 
            this._refreshConfiguration();    
        }
    }
 
    /**
     * Lifecycle hook that cleans up resources when the component is destroyed.
     */
    ngOnDestroy(): void {
        Iif (this._onconfig_subscription){
            this._onconfig_subscription.unsubscribe();
            this._onconfig_subscription = null;
        }
    }
 
    @ContentChildren(ViewModeDirective, { descendants: true }) items!: QueryList<ViewModeDirective>;
 
    /**
     * Lifecycle hook that updates child items after content initialization.
     */    
    ngAfterContentInit() {
        this.items.changes.subscribe(() => {
            this._updateItems();
        });
 
        this._updateItems();
    }
    
    /**
     * Updates all child grid items with the current view mode.
     * @private
     */    
    private _updateItems() {
        Iif (this.items?.length > 0) {
            for(const _item of this.items){
                _item.viewMode = this._viewmode;
            };
        }   
        
        this.change.markForCheck();
    }
}