UNPKG

@tapsellorg/angular-material-library

Version:

Angular library for Tapsell

125 lines (119 loc) 6.36 kB
import * as i0 from '@angular/core'; import { Injectable, input, model, Directive, NgModule } from '@angular/core'; import { PghStorageFactory } from '@tapsellorg/angular-material-library/src/lib/common'; import { CommonModule } from '@angular/common'; class PghIndicatorPersistService { constructor() { this.localStorageFactory = PghStorageFactory.localStorageFactory(); } persist({ persistId, persistDays }) { const expirationDate = new Date(); expirationDate.setDate(expirationDate.getDate() + persistDays); this.localStorageFactory.setItem(PghIndicatorPersistService.getKey(persistId), JSON.stringify({ persistId, expirationDate })); } static getKey(persistId) { return `pghIndicator_${persistId}`; } getPersistData(persistId) { const rawData = this.localStorageFactory.getItem(PghIndicatorPersistService.getKey(persistId)); if (!rawData) return; try { const parsedData = JSON.parse(rawData); parsedData.expirationDate = new Date(parsedData.expirationDate); return parsedData; } catch (e) { return; } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorPersistService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorPersistService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorPersistService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [] }); class PghIndicatorDirective { constructor(_elementRef, indicatorPersistService) { this._elementRef = _elementRef; this.indicatorPersistService = indicatorPersistService; this.color = input('primary', { alias: 'pghIndicatorColor', }); this.persistData = input({ persistId: '', persistDays: 0, }, { alias: 'pghIndicatorPersistData', }); this.position = input('top end', { alias: 'pghIndicatorPosition', }); this.hidden = model(false, { alias: 'pghIndicatorHidden', }); this.element = _elementRef.nativeElement; } ngOnInit() { this.handlePersistence(); this.configLoaderAttributes(); } ngOnChanges(changes) { if (changes.color || changes.position) { this.configLoaderAttributes(); } } handlePersistence() { if (!this.persistData()) return; const { persistId, persistDays } = this.persistData(); if (!persistId || !persistDays) throw new Error('You must provide persistId and persistDays'); const persistServiceSavedData = this.indicatorPersistService.getPersistData(persistId); if (!persistServiceSavedData) { this.indicatorPersistService.persist(this.persistData()); } else if (persistServiceSavedData.expirationDate <= new Date()) { this.hidden.set(true); } } configLoaderAttributes() { if (this.hidden()) { this.element.setAttribute('data-pgh-indicator', 'hidden'); } else { this.element.setAttribute('data-pgh-indicator', `${this.position} ${this.color}`); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorDirective, deps: [{ token: i0.ElementRef }, { token: PghIndicatorPersistService }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "19.2.13", type: PghIndicatorDirective, isStandalone: false, selector: "[pghIndicator]", inputs: { color: { classPropertyName: "color", publicName: "pghIndicatorColor", isSignal: true, isRequired: false, transformFunction: null }, persistData: { classPropertyName: "persistData", publicName: "pghIndicatorPersistData", isSignal: true, isRequired: false, transformFunction: null }, position: { classPropertyName: "position", publicName: "pghIndicatorPosition", isSignal: true, isRequired: false, transformFunction: null }, hidden: { classPropertyName: "hidden", publicName: "pghIndicatorHidden", isSignal: true, isRequired: false, transformFunction: null } }, outputs: { hidden: "pghIndicatorHiddenChange" }, exportAs: ["pghIndicator"], usesOnChanges: true, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorDirective, decorators: [{ type: Directive, args: [{ selector: '[pghIndicator]', exportAs: 'pghIndicator', standalone: false, }] }], ctorParameters: () => [{ type: i0.ElementRef }, { type: PghIndicatorPersistService }] }); class PghIndicatorModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorModule, declarations: [PghIndicatorDirective], imports: [CommonModule], exports: [PghIndicatorDirective] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorModule, imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.13", ngImport: i0, type: PghIndicatorModule, decorators: [{ type: NgModule, args: [{ declarations: [PghIndicatorDirective], imports: [CommonModule], exports: [PghIndicatorDirective], }] }] }); /** * Generated bundle index. Do not edit. */ export { PghIndicatorDirective, PghIndicatorModule }; //# sourceMappingURL=tapsellorg-angular-material-library-src-lib-indicator.mjs.map