UNPKG

@itwin/core-frontend

Version:
73 lines 2.96 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module QuantityFormatting */ Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalUnitFormatProvider = void 0; const BaseUnitFormattingSettingsProvider_1 = require("./BaseUnitFormattingSettingsProvider"); /** Implementation of BaseUnitFormattingSettingsProvider that stores and retrieves data in local storage. * @beta */ class LocalUnitFormatProvider extends BaseUnitFormattingSettingsProvider_1.BaseUnitFormattingSettingsProvider { /** If `maintainOverridesPerIModel` is true, the base class will set up listeners to monitor active iModel * changes so the overrides for the QuantityFormatter properly match the overrides set up by the user. */ constructor(quantityFormatter, maintainOverridesPerIModel) { super(quantityFormatter, maintainOverridesPerIModel); } buildUnitSystemKey() { if (this.imodelConnection) return `unitsystem#i:${this.imodelConnection.iModelId}`; return `unitsystem#user`; } async retrieveUnitSystem(defaultKey) { const readUnitSystem = localStorage.getItem(this.buildUnitSystemKey()); if (readUnitSystem && readUnitSystem.length) { return readUnitSystem; } return defaultKey; } async storeUnitSystemKey(unitSystemKey) { try { localStorage.setItem(this.buildUnitSystemKey(), unitSystemKey); return true; } catch { return false; } } buildOverridesKey(quantityTypeKey) { if (this.imodelConnection) return `quantityTypeFormat#i:${this.imodelConnection.iModelId}#q:${quantityTypeKey}`; return `quantityTypeFormat#user#q:${quantityTypeKey}`; } async store(quantityTypeKey, overrideProps) { try { localStorage.setItem(this.buildOverridesKey(quantityTypeKey), JSON.stringify(overrideProps)); return true; } catch { return false; } } async retrieve(quantityTypeKey) { const storedFormat = localStorage.getItem(this.buildOverridesKey(quantityTypeKey)); if (storedFormat) { return JSON.parse(storedFormat); } return undefined; } async remove(quantityTypeKey) { const key = this.buildOverridesKey(quantityTypeKey); if (localStorage.getItem(key)) { localStorage.removeItem(key); return true; } return false; } } exports.LocalUnitFormatProvider = LocalUnitFormatProvider; //# sourceMappingURL=LocalUnitFormatProvider.js.map