File

projects/netgrif-components-core/src/lib/header/services/overflow.service.ts

Index

Properties
Methods
Accessors

Constructor

constructor(_viewIdService: ViewIdService)
Parameters :
Name Type Optional
_viewIdService ViewIdService No

Methods

Protected checkIsNan
checkIsNan(item, defaultValue)
Parameters :
Name Optional
item No
defaultValue No
Returns : any
Protected getViewId
getViewId()
Returns : string | undefined

the Id of the view, if the ViewIdService was injected. Returns undefined if the service was not injected.

Protected initializeItem
initializeItem(id: string, defaultValue)
Parameters :
Name Type Optional
id string No
defaultValue No
Returns : any
Public restoreLastState
restoreLastState()
Returns : void
Public saveNewState
saveNewState()
Returns : void
Public saveState
saveState()
Returns : void

Properties

Protected Readonly DEFAULT_COLUMN_COUNT
Type : number
Default value : 6
Protected Readonly DEFAULT_COLUMN_WIDTH
Type : number
Default value : 200

Accessors

overflowMode
getoverflowMode()
setoverflowMode(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
columnWidth
getcolumnWidth()
setcolumnWidth(value: number)
Parameters :
Name Type Optional
value number No
Returns : void
columnCount
getcolumnCount()
setcolumnCount(value: number)
Parameters :
Name Type Optional
value number No
Returns : void
initializedCount
getinitializedCount()
import {Injectable, Optional} from '@angular/core';
import {ViewIdService} from '../../user/services/view-id.service';

interface OverflowState {
    overflowMode: boolean;
    columnWidth: number;
    columnCount: number;
}

@Injectable()
export class OverflowService {

    protected readonly DEFAULT_COLUMN_WIDTH = 200;
    protected readonly DEFAULT_COLUMN_COUNT = 6;
    private _overflowMode: boolean;
    private _columnWidth: number;
    private _columnCount: number;
    private _state: OverflowState;
    private _initializedCount: boolean;

    constructor(@Optional() private _viewIdService: ViewIdService) {
        this._initializedCount = false;
        this._overflowMode = this.initializeItem('overflowMode', false) === 'true';
        this._columnCount = this.checkIsNan(this.initializeItem('columnCount', this.DEFAULT_COLUMN_COUNT), this.DEFAULT_COLUMN_COUNT);
        this._columnWidth = this.checkIsNan(this.initializeItem('columnWidth', this.DEFAULT_COLUMN_WIDTH), this.DEFAULT_COLUMN_WIDTH);
    }

    get overflowMode(): boolean {
        return this._overflowMode;
    }

    set overflowMode(value: boolean) {
        this._overflowMode = value;
    }

    get columnWidth(): number {
        return this._columnWidth;
    }

    set columnWidth(value: number) {
        this._columnWidth = value;
    }

    get columnCount(): number {
        return this._columnCount;
    }

    set columnCount(value: number) {
        this._columnCount = value;
    }

    get initializedCount(): boolean {
        return this._initializedCount;
    }

    public saveState(): void {
        this._state = {
            overflowMode: this._overflowMode,
            columnWidth: this._columnWidth,
            columnCount: this._columnCount,
        };
    }

    public saveNewState(): void {
        const viewId = this.getViewId();
        if (viewId) {
            localStorage.setItem(viewId + '-overflowMode', this._overflowMode + '');
            localStorage.setItem(viewId + '-columnCount', this._columnCount + '');
            localStorage.setItem(viewId + '-columnWidth', this._columnWidth + '');
        }
        this._state = undefined;
    }

    public restoreLastState(): void {
        this._overflowMode = this._state.overflowMode;
        this._columnWidth = this._state.columnWidth;
        this._columnCount = this._state.columnCount;
        this._state = undefined;
    }

    protected initializeItem(id: string, defaultValue) {
        const viewId = this.getViewId();
        if (!viewId) {
            return defaultValue;
        }

        const item = localStorage.getItem(viewId + '-' + id);
        if (item !== null) {
            if (id.includes('columnCount')) {
                this._initializedCount = true;
            }
            return item;
        } else {
            return defaultValue;
        }
    }

    protected checkIsNan(item, defaultValue) {
        return isNaN(parseInt(item, 10)) ? defaultValue : parseInt(item, 10);
    }

    /**
     * @returns the Id of the view, if the ViewIdService was injected. Returns `undefined` if the service was not injected.
     */
    protected getViewId(): string | undefined {
        if (this._viewIdService) {
            return this._viewIdService.viewId;
        }
        return undefined;
    }
}

result-matching ""

    No results matching ""