diff --git a/src/app/config/defaults.ts b/src/app/config/defaults.ts index d343455..a37b891 100644 --- a/src/app/config/defaults.ts +++ b/src/app/config/defaults.ts @@ -18,43 +18,56 @@ export const labelLayouts = { }), }; -// Default form field and data values -export const defaults: AppDefaults = new AppDefaults({ +export const defaultOptions: AppDefaultsOptions = { barCodeNumLength: 9, // Number of digits in Bar Code. - barCodeRegExp: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long. Manually-entered ID numbers will be exactly 9 digits long. + barCodeRegExp: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long. + // Manually-entered ID numbers will be exactly 9 digits long. countsCollection: 'counts', // Name of collection for Line Counts in Firebase. dateDisplayFormat: 'MM/dd/yyyy, hh:mm aa', // Format for dates when displayed to user. dateEncodedFormat: 'yyyyMMddHHmm', // Format for dates when encoded in IDs for database records. initialsLength: 5, initialsRegExp: /^[a-zA-Z]{2,5}$/, labelLayout: labelLayouts.round_32mm_1up, // Which label layout to use for printing. Can be overridden by user setting. - lineCountRegExp: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records. + lineCountRegExp: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records. locationId: '0000', // Default location ID. Can be overridden by user setting. - locationIdRegExp: /^[\d]{4}$/, // ID format for Line Count records. + locationIdRegExp: /^[\d]{4}$/, // ID format for Line Count records. numCopies: 1, // Default number of copies of labels to print. Can be overridden by user setting. - qrCodeRegExp: /^[\d]{9}-[a-zA-Z]+-[\d]{12}-[\d]{4}$/, // ID format for QR Code records. + qrCodeRegExp: /^[\d]{9}-[a-zA-Z]+-[\d]{12}-[\d]{4}$/, // ID format for QR Code records. samplesCollection: 'samples', // Name of collection for Line Counts in Firebase. -}); +}; +// Default form field and data values +export const defaults: AppDefaults = new AppDefaults(defaultOptions); + +// Deserializes settings from local storage and returns AppDefaults instance +export const getStoredSettings = (): AppDefaults => { + // tslint:disable-next-line:no-eval + return new AppDefaults(eval(`(${localStorage.getItem('settings')})`)); +}; + +// Returns true if settings are found in local storage +export const hasStoredSettings = (): boolean => { + return !!localStorage.getItem('settings'); +}; + +// Returns settings from local storage, or defaults if none have been saved yet. export const getSettings = (): AppDefaults => { - const storedSettings = localStorage.getItem('settings'); - - if (storedSettings) { - // tslint:disable-next-line:no-eval - return new AppDefaults(eval(`(${storedSettings})`)); + if (hasStoredSettings()) { + return getStoredSettings(); } else { - localStorage.setItem('settings', serializeJs(defaults)); - return defaults; + return saveSettings(defaults); } }; +// Serializes given settings and stores them in local storage export const saveSettings = (newSettings: AppDefaultsOptions): AppDefaults => { - const settings: AppDefaults = createClone()(getSettings()); + const settings: AppDefaults = createClone({circles: true})(defaults); Object.keys(newSettings).forEach(k => { + console.log(`${k}:`, newSettings[k]); settings[k] = newSettings[k]; }); localStorage.setItem('settings', serializeJs(settings)); - return getSettings(); + return settings; }; diff --git a/src/app/interfaces/appDefaults.interface.ts b/src/app/interfaces/appDefaults.interface.ts index 751d9c5..2e0da58 100644 --- a/src/app/interfaces/appDefaults.interface.ts +++ b/src/app/interfaces/appDefaults.interface.ts @@ -1,3 +1,4 @@ +import {defaultOptions} from '../config/defaults'; import {LabelLayout} from './labelLayout.interface'; export interface AppDefaultsOptions { @@ -34,17 +35,18 @@ export class AppDefaults { samplesCollection: string; constructor(options: AppDefaultsOptions) { - console.log('options', options); - const keys = Object.keys(options); + const keys = Object.keys(defaultOptions); keys.forEach(k => { if (k.includes('RegExp')) { if (typeof options[k] === 'string') { this[k] = new RegExp(options[k]); - } else { + } else if (options[k] instanceof RegExp) { this[k] = options[k]; + } else { + this[k] = defaultOptions[k]; } } else { - this[k] = options[k]; + this[k] = options[k] || defaultOptions[k]; } }); } diff --git a/src/app/navbar/navbar.component.html b/src/app/navbar/navbar.component.html index 79cd657..d387e81 100644 --- a/src/app/navbar/navbar.component.html +++ b/src/app/navbar/navbar.component.html @@ -1,7 +1,7 @@ - + diff --git a/src/app/navbar/navbar.component.ts b/src/app/navbar/navbar.component.ts index 7671f6a..24b51fe 100644 --- a/src/app/navbar/navbar.component.ts +++ b/src/app/navbar/navbar.component.ts @@ -1,4 +1,5 @@ import {Component, Input, OnInit} from '@angular/core'; +import {getSettings} from '../config/defaults'; import {TestingLocation} from '../interfaces/testingLocation.interface'; @Component({ @@ -8,8 +9,12 @@ import {TestingLocation} from '../interfaces/testingLocation.interface'; }) export class NavbarComponent implements OnInit { @Input() testingLocation: TestingLocation; + locationId: string; - constructor() { } + constructor() { + const settings = getSettings(); + this.locationId = settings.locationId; + } ngOnInit(): void { } diff --git a/src/app/settings/settings.component.ts b/src/app/settings/settings.component.ts index ad1f90d..af9e4c5 100644 --- a/src/app/settings/settings.component.ts +++ b/src/app/settings/settings.component.ts @@ -39,7 +39,9 @@ export class SettingsComponent implements OnInit { save() { saveSettings({ - + labelLayout: labelLayouts[this.labelLayoutFormControl.value], + numCopies: this.numCopiesFormControl.value, + locationId: this.locationIdFormControl.value, }); } }