UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

197 lines (194 loc) 4.55 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.1 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { AbstractHelper } from './abstract-helper.mjs'; import { TypeOption } from '../types/b24-helper.mjs'; import { Type } from '../tools/type.mjs'; import { Text } from '../tools/text.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class OptionsManager extends AbstractHelper { static { __name(this, "OptionsManager"); } _data; _type; // region static //// static getSupportTypes() { return [ TypeOption.NotSet, TypeOption.JsonArray, TypeOption.JsonObject, TypeOption.FloatVal, TypeOption.IntegerVal, TypeOption.BoolYN, TypeOption.StringVal ]; } static prepareArrayList(list) { if (Type.isArray(list)) { return list; } if (Type.isObject(list)) { return Object.values(list); } return []; } // endregion //// // region Init //// constructor(b24, type) { super(b24); this._type = type; this._data = /* @__PURE__ */ new Map(); } get data() { return this._data; } reset() { this.data.clear(); } /** * @inheritDoc */ async initData(data) { this.reset(); if (Type.isObject(data)) { for (const [key, value] of Object.entries(data)) { this.data.set(key, value); } } } // endregion //// // region Get //// getJsonArray(key, defValue = []) { if (!this.data.has(key)) { return defValue; } let data = this.data.get(key); try { data = JSON.parse(data); if (!Type.isArray(data) && !Type.isObject(data)) { data = defValue; } } catch (error) { this.getLogger().error("Failed JSON parse", { error }); data = defValue; } return OptionsManager.prepareArrayList(data); } getJsonObject(key, defValue = {}) { if (!this.data.has(key)) { return defValue; } let data = this.data.get(key); try { data = JSON.parse(data); } catch (error) { this.getLogger().error("Failed JSON parse", { error }); data = defValue; } if (!Type.isObject(data)) { data = defValue; } return data; } getFloat(key, defValue = 0) { if (!this.data.has(key)) { return defValue; } return Text.toNumber(this.data.get(key)); } getInteger(key, defValue = 0) { if (!this.data.has(key)) { return defValue; } return Text.toInteger(this.data.get(key)); } getBoolYN(key, defValue = true) { if (!this.data.has(key)) { return defValue; } return Text.toBoolean(this.data.get(key)); } getBoolNY(key, defValue = false) { if (!this.data.has(key)) { return defValue; } return Text.toBoolean(this.data.get(key)); } getString(key, defValue = "") { if (!this.data.has(key)) { return defValue; } return this.data.get(key).toString(); } getDate(key, defValue = null) { if (!this.data.has(key)) { return defValue; } try { const result = Text.toDateTime(this.data.get(key).toString()); if (result.isValid) { return result; } else { return defValue; } } catch { return defValue; } } // endregion //// // region Tools //// encode(value) { return JSON.stringify(value); } decode(data, defaultValue) { try { if (data.length > 0) { return JSON.parse(data); } return defaultValue; } catch (error) { this.getLogger().error("Failed JSON parse", { error }); } return defaultValue; } // endregion //// // region Save //// getMethodSave() { switch (this._type) { case "app": return "app.option.set"; case "user": return "user.option.set"; } } async save(options, optionsPull) { const commands = []; commands.push({ method: this.getMethodSave(), params: { options } }); if (Type.isObject(optionsPull)) { commands.push({ method: "pull.application.event.add", params: { COMMAND: optionsPull?.command, PARAMS: optionsPull?.params, MODULE_ID: optionsPull?.moduleId } }); } return this._b24.callBatch(commands, true); } // endregion //// } export { OptionsManager }; //# sourceMappingURL=options-manager.mjs.map