@michalrakus/x-react-web-lib 1.36.0 → 1.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gulpfile.js CHANGED
@@ -30,7 +30,9 @@ function generateApi(cb) {
30
30
  "./lib/components/XLazyDataTable/XLazyDataTable",
31
31
  "./lib/components/XLazyDataTable/XMultilineRenderer",
32
32
 
33
+ "./lib/components/useXStateLocal",
33
34
  "./lib/components/useXStateSession",
35
+ "./lib/components/useXStateStorage",
34
36
  "./lib/components/useXToken",
35
37
  "./lib/components/XAutoComplete",
36
38
  "./lib/components/XAutoCompleteBase",
@@ -10,6 +10,7 @@ export interface XFormProps {
10
10
  id?: number;
11
11
  initValues?: object;
12
12
  onSaveOrCancel?: XOnSaveOrCancelProp;
13
+ isInDialog?: boolean;
13
14
  }
14
15
  export declare function Form(entity: string): <T extends new (...args: any[]) => {}>(constructor: T) => {
15
16
  new (...args: any[]): {
@@ -235,7 +235,8 @@ var XFormBase = /** @class */ (function (_super) {
235
235
  };
236
236
  // helper method
237
237
  XFormBase.prototype.isInDialog = function () {
238
- return this.props.onSaveOrCancel !== undefined;
238
+ var _a;
239
+ return (_a = this.props.isInDialog) !== null && _a !== void 0 ? _a : false;
239
240
  };
240
241
  // helper method
241
242
  XFormBase.prototype.isTabViewUsed = function () {
@@ -16,7 +16,7 @@ var XFormDialog = function (props) {
16
16
  };
17
17
  var form = (_a = props.dialogState.form) !== null && _a !== void 0 ? _a : props.form;
18
18
  return (react_1.default.createElement(dialog_1.Dialog, { key: "dialog-form", className: "x-dialog-without-header", visible: props.dialogState.opened, onHide: onHide }, form ? react_1.default.cloneElement(form, {
19
- id: props.dialogState.id, initValues: props.dialogState.initValues, onSaveOrCancel: props.dialogState.onSaveOrCancel
19
+ id: props.dialogState.id, initValues: props.dialogState.initValues, onSaveOrCancel: props.dialogState.onSaveOrCancel, isInDialog: true
20
20
  } /*, props.form.children*/) : null));
21
21
  };
22
22
  exports.XFormDialog = XFormDialog;
@@ -1,7 +1,7 @@
1
1
  import React, { ReactChild } from 'react';
2
2
  import { DataTableFilterMeta, DataTableFilterMetaData, DataTableOperatorFilterMetaData, DataTableRowExpansionTemplate, DataTableSortMeta } from 'primereact/datatable';
3
3
  import { ColumnBodyOptions, ColumnFilterElementTemplateOptions } from 'primereact/column';
4
- import { XViewStatusOrBoolean } from "../XUtils";
4
+ import { XStorageType, XViewStatusOrBoolean } from "../XUtils";
5
5
  import { XSearchBrowseParams } from "../XSearchBrowseParams";
6
6
  import { XAggregateFunction, XCustomFilter } from "../../serverApi/FindParam";
7
7
  import { XOnSaveOrCancelProp } from "../XFormBase";
@@ -58,6 +58,7 @@ export interface XLazyDataTableRef {
58
58
  }
59
59
  export interface XLazyDataTableProps {
60
60
  entity: string;
61
+ stateStorage?: XStorageType;
61
62
  stateKey?: string;
62
63
  label?: string;
63
64
  labelStyle?: React.CSSProperties;
@@ -145,8 +145,8 @@ var XOcfDropdown_1 = require("./XOcfDropdown");
145
145
  var XFieldSetBase_1 = require("../XFieldSet/XFieldSetBase");
146
146
  var XAutoCompleteBase_1 = require("../XAutoCompleteBase");
147
147
  var XInputTextBase_1 = require("../XInputTextBase");
148
- var useXStateSession_1 = require("../useXStateSession");
149
- var useXStateSessionBase_1 = require("../useXStateSessionBase");
148
+ var useXStateStorage_1 = require("../useXStateStorage");
149
+ var useXStateStorageBase_1 = require("../useXStateStorageBase");
150
150
  var _ = __importStar(require("lodash"));
151
151
  var XDocTemplateButton_1 = require("../../modules/docTemplates/XDocTemplateButton");
152
152
  var XFormDialog_1 = require("../XFormDialog");
@@ -162,12 +162,12 @@ var XStateKeySuffix;
162
162
  })(XStateKeySuffix || (exports.XStateKeySuffix = XStateKeySuffix = {}));
163
163
  exports.XLazyDataTable = (0, react_1.forwardRef)(function (_a, ref) {
164
164
  var _b;
165
- var _c = _a.paginator, paginator = _c === void 0 ? true : _c, _d = _a.rows, rows = _d === void 0 ? 30 : _d, _e = _a.filterDisplay, filterDisplay = _e === void 0 ? "row" : _e, _f = _a.autoFilter, autoFilter = _f === void 0 ? false : _f, _g = _a.showFilterButtons, showFilterButtons = _g === void 0 ? true : _g, _h = _a.fullTextSearch, fullTextSearch = _h === void 0 ? true : _h, _j = _a.multilineSwitch, multilineSwitch = _j === void 0 ? false : _j, _k = _a.multilineSwitchInitValue, multilineSwitchInitValue = _k === void 0 ? "allLines" : _k, _l = _a.multilineSwitchFewLinesCount, multilineSwitchFewLinesCount = _l === void 0 ? 2 : _l, _m = _a.scrollable, scrollable = _m === void 0 ? true : _m, _o = _a.scrollWidth, scrollWidth = _o === void 0 ? 'viewport' : _o, // nastavi sirku tabulky na (100vw - nieco) (ak bude obsah sirsi, zapne horizontalny scrollbar)
166
- _p = _a.scrollHeight, // nastavi sirku tabulky na (100vw - nieco) (ak bude obsah sirsi, zapne horizontalny scrollbar)
167
- scrollHeight = _p === void 0 ? 'viewport' : _p, // nastavi vysku tabulky na (100vh - nieco) (ak bude obsah vecsi, zapne vertikalny scrollbar)
168
- _q = _a.shrinkWidth, // nastavi vysku tabulky na (100vh - nieco) (ak bude obsah vecsi, zapne vertikalny scrollbar)
169
- shrinkWidth = _q === void 0 ? true : _q, propsRest = __rest(_a, ["paginator", "rows", "filterDisplay", "autoFilter", "showFilterButtons", "fullTextSearch", "multilineSwitch", "multilineSwitchInitValue", "multilineSwitchFewLinesCount", "scrollable", "scrollWidth", "scrollHeight", "shrinkWidth"]);
170
- var props = __assign({ paginator: paginator, rows: rows, filterDisplay: filterDisplay, autoFilter: autoFilter, showFilterButtons: showFilterButtons, fullTextSearch: fullTextSearch, multilineSwitch: multilineSwitch, multilineSwitchInitValue: multilineSwitchInitValue, multilineSwitchFewLinesCount: multilineSwitchFewLinesCount, scrollable: scrollable, scrollWidth: scrollWidth, scrollHeight: scrollHeight, shrinkWidth: shrinkWidth }, propsRest);
165
+ var _c = _a.stateStorage, stateStorage = _c === void 0 ? "session" : _c, _d = _a.paginator, paginator = _d === void 0 ? true : _d, _e = _a.rows, rows = _e === void 0 ? 30 : _e, _f = _a.filterDisplay, filterDisplay = _f === void 0 ? "row" : _f, _g = _a.autoFilter, autoFilter = _g === void 0 ? false : _g, _h = _a.showFilterButtons, showFilterButtons = _h === void 0 ? true : _h, _j = _a.fullTextSearch, fullTextSearch = _j === void 0 ? true : _j, _k = _a.multilineSwitch, multilineSwitch = _k === void 0 ? false : _k, _l = _a.multilineSwitchInitValue, multilineSwitchInitValue = _l === void 0 ? "allLines" : _l, _m = _a.multilineSwitchFewLinesCount, multilineSwitchFewLinesCount = _m === void 0 ? 2 : _m, _o = _a.scrollable, scrollable = _o === void 0 ? true : _o, _p = _a.scrollWidth, scrollWidth = _p === void 0 ? 'viewport' : _p, // nastavi sirku tabulky na (100vw - nieco) (ak bude obsah sirsi, zapne horizontalny scrollbar)
166
+ _q = _a.scrollHeight, // nastavi sirku tabulky na (100vw - nieco) (ak bude obsah sirsi, zapne horizontalny scrollbar)
167
+ scrollHeight = _q === void 0 ? 'viewport' : _q, // nastavi vysku tabulky na (100vh - nieco) (ak bude obsah vecsi, zapne vertikalny scrollbar)
168
+ _r = _a.shrinkWidth, // nastavi vysku tabulky na (100vh - nieco) (ak bude obsah vecsi, zapne vertikalny scrollbar)
169
+ shrinkWidth = _r === void 0 ? true : _r, propsRest = __rest(_a, ["stateStorage", "paginator", "rows", "filterDisplay", "autoFilter", "showFilterButtons", "fullTextSearch", "multilineSwitch", "multilineSwitchInitValue", "multilineSwitchFewLinesCount", "scrollable", "scrollWidth", "scrollHeight", "shrinkWidth"]);
170
+ var props = __assign({ stateStorage: stateStorage, paginator: paginator, rows: rows, filterDisplay: filterDisplay, autoFilter: autoFilter, showFilterButtons: showFilterButtons, fullTextSearch: fullTextSearch, multilineSwitch: multilineSwitch, multilineSwitchInitValue: multilineSwitchInitValue, multilineSwitchFewLinesCount: multilineSwitchFewLinesCount, scrollable: scrollable, scrollWidth: scrollWidth, scrollHeight: scrollHeight, shrinkWidth: shrinkWidth }, propsRest);
171
171
  // must be here, is used in createFiltersInit()
172
172
  var xEntity = XUtilsMetadataCommon_1.XUtilsMetadataCommon.getXEntity(props.entity);
173
173
  var createAggregateItems = function () {
@@ -320,7 +320,7 @@ exports.XLazyDataTable = (0, react_1.forwardRef)(function (_a, ref) {
320
320
  return "x-ldt-state-".concat((_a = props.stateKey) !== null && _a !== void 0 ? _a : props.entity, "-").concat(stateKeySuffix);
321
321
  };
322
322
  var removePagingFromStorage = function () {
323
- XUtils_1.XUtils.removeValueFromStorage(getStateKey(XStateKeySuffix.pagingFirst));
323
+ XUtils_1.XUtils.removeValueFromStorage(props.stateStorage, getStateKey(XStateKeySuffix.pagingFirst));
324
324
  };
325
325
  // premenne platne pre cely component (obdoba member premennych v class-e)
326
326
  var primeReactContext = react_1.default.useContext(api_1.PrimeReactContext); // probably does not work and deprecated PrimeReact.filterMatchModeOptions is used
@@ -338,15 +338,15 @@ exports.XLazyDataTable = (0, react_1.forwardRef)(function (_a, ref) {
338
338
  // ak mame fieldSet stlpce (stlpce ktore maju zadany fieldSetId a zobrazuju hodnoty podla fieldSet-u),
339
339
  // tak sem nacitame mapy umoznujuce ziskanie labelov zakliknutych field-ov
340
340
  // poznamka: uz by sa zislo mat vytvorene objekty (instancie) pre stlpce a do nich zapisovat pripadny XFieldSetMap, filter (teraz je vo "filters")
341
- var _r = __read((0, react_1.useState)({}), 2), xFieldSetMaps = _r[0], setXFieldSetMaps = _r[1];
342
- var _s = __read((0, react_1.useState)({ rowList: [], totalRecords: 0, aggregateValues: [] }), 2), value = _s[0], setValue = _s[1];
343
- var _t = __read((0, react_1.useState)(undefined), 2), expandedRows = _t[0], setExpandedRows = _t[1];
344
- var _u = __read((0, react_1.useState)(false), 2), loading = _u[0], setLoading = _u[1];
345
- var _v = __read((0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.pagingFirst), 0), 2), first = _v[0], setFirst = _v[1];
346
- var _w = __read((0, react_1.useState)(props.paginator ? props.rows : undefined), 2), rowsLocal = _w[0], setRowsLocal = _w[1];
341
+ var _s = __read((0, react_1.useState)({}), 2), xFieldSetMaps = _s[0], setXFieldSetMaps = _s[1];
342
+ var _t = __read((0, react_1.useState)({ rowList: [], totalRecords: 0, aggregateValues: [] }), 2), value = _t[0], setValue = _t[1];
343
+ var _u = __read((0, react_1.useState)(undefined), 2), expandedRows = _u[0], setExpandedRows = _u[1];
344
+ var _v = __read((0, react_1.useState)(false), 2), loading = _v[0], setLoading = _v[1];
345
+ var _w = __read((0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.pagingFirst), 0), 2), first = _w[0], setFirst = _w[1];
346
+ var _x = __read((0, react_1.useState)(props.paginator ? props.rows : undefined), 2), rowsLocal = _x[0], setRowsLocal = _x[1];
347
347
  // "filters" have special initialState function different from that used in useXStateSession
348
348
  var filtersInitialStateFunction = function () {
349
- var filtersInit = XUtils_1.XUtils.getValueFromStorage(getStateKey(XStateKeySuffix.filters), null);
349
+ var filtersInit = XUtils_1.XUtils.getValueFromStorage(props.stateStorage, getStateKey(XStateKeySuffix.filters), null);
350
350
  if (filtersInit != null) {
351
351
  // we have filters from session - if we have props.filters, we always override the values from session (values from props.filters have higher priority)
352
352
  filtersInit = overrideFilters(filtersInit, props.filters);
@@ -357,23 +357,23 @@ exports.XLazyDataTable = (0, react_1.forwardRef)(function (_a, ref) {
357
357
  }
358
358
  return filtersInit;
359
359
  };
360
- var _x = __read((0, useXStateSessionBase_1.useXStateSessionBase)(getStateKey(XStateKeySuffix.filters), filtersInitialStateFunction), 2), filters = _x[0], setFilters = _x[1]; // filtrovanie na "controlled manner" (moze sa sem nainicializovat nejaka hodnota)
360
+ var _y = __read((0, useXStateStorageBase_1.useXStateStorageBase)(props.stateStorage, getStateKey(XStateKeySuffix.filters), filtersInitialStateFunction), 2), filters = _y[0], setFilters = _y[1]; // filtrovanie na "controlled manner" (moze sa sem nainicializovat nejaka hodnota)
361
361
  var initFtsInputValue = props.fullTextSearch ? createInitFtsInputValue() : undefined;
362
- var _y = __read((0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.ftsInputValue), initFtsInputValue), 2), ftsInputValue = _y[0], setFtsInputValue = _y[1];
363
- var _z = __read((0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.optionalCustomFilter), undefined), 2), optionalCustomFilter = _z[0], setOptionalCustomFilter = _z[1];
364
- var _0 = __read((_b = props.multilineSwitchValue) !== null && _b !== void 0 ? _b : (0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.multilineSwitchValue), props.multilineSwitchInitValue), 2), multilineSwitchValue = _0[0], setMultilineSwitchValue = _0[1];
365
- var _1 = __read((0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.multiSortMeta), XUtilsCommon_1.XUtilsCommon.createMultiSortMeta(props.sortField)), 2), multiSortMeta = _1[0], setMultiSortMeta = _1[1];
366
- var _2 = __read((0, useXStateSession_1.useXStateSession)(getStateKey(XStateKeySuffix.selectedRow), null), 2), selectedRow = _2[0], setSelectedRow = _2[1];
362
+ var _z = __read((0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.ftsInputValue), initFtsInputValue), 2), ftsInputValue = _z[0], setFtsInputValue = _z[1];
363
+ var _0 = __read((0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.optionalCustomFilter), undefined), 2), optionalCustomFilter = _0[0], setOptionalCustomFilter = _0[1];
364
+ var _1 = __read((_b = props.multilineSwitchValue) !== null && _b !== void 0 ? _b : (0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.multilineSwitchValue), props.multilineSwitchInitValue), 2), multilineSwitchValue = _1[0], setMultilineSwitchValue = _1[1];
365
+ var _2 = __read((0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.multiSortMeta), XUtilsCommon_1.XUtilsCommon.createMultiSortMeta(props.sortField)), 2), multiSortMeta = _2[0], setMultiSortMeta = _2[1];
366
+ var _3 = __read((0, useXStateStorage_1.useXStateStorage)(props.stateStorage, getStateKey(XStateKeySuffix.selectedRow), null), 2), selectedRow = _3[0], setSelectedRow = _3[1];
367
367
  /**
368
368
  * @deprecated was used to reread data after save/cancel of the form when using XFormNavigator (deprecated)
369
369
  */
370
- var _3 = __read((0, react_1.useState)(false), 2), dataLoaded = _3[0], setDataLoaded = _3[1]; // priznak kde si zapiseme, ci uz sme nacitali data
371
- var _4 = __read((0, react_1.useState)({ dialogOpened: false }), 2), exportRowsDialogState = _4[0], setExportRowsDialogState = _4[1];
370
+ var _4 = __read((0, react_1.useState)(false), 2), dataLoaded = _4[0], setDataLoaded = _4[1]; // priznak kde si zapiseme, ci uz sme nacitali data
371
+ var _5 = __read((0, react_1.useState)({ dialogOpened: false }), 2), exportRowsDialogState = _5[0], setExportRowsDialogState = _5[1];
372
372
  //const [exportRowsDialogRowCount, setExportRowsDialogRowCount] = useState<number>(); // param pre dialog
373
- var _5 = __read((0, react_1.useState)(filters), 2), filtersAfterFiltering = _5[0], setFiltersAfterFiltering = _5[1]; // sem si odkladame stav filtra po kliknuti na button Filter (chceme exportovat presne to co vidno vyfiltrovane)
374
- var _6 = __read((0, react_1.useState)(ftsInputValue), 2), ftsInputValueAfterFiltering = _6[0], setFtsInputValueAfterFiltering = _6[1]; // tak isto ako filtersAfterFiltering
375
- var _7 = __read((0, react_1.useState)(optionalCustomFilter), 2), optionalCustomFilterAfterFiltering = _7[0], setOptionalCustomFilterAfterFiltering = _7[1]; // tak isto ako filtersAfterFiltering
376
- var _8 = __read((0, react_1.useState)({ opened: false }), 2), formDialogState = _8[0], setFormDialogState = _8[1];
373
+ var _6 = __read((0, react_1.useState)(filters), 2), filtersAfterFiltering = _6[0], setFiltersAfterFiltering = _6[1]; // sem si odkladame stav filtra po kliknuti na button Filter (chceme exportovat presne to co vidno vyfiltrovane)
374
+ var _7 = __read((0, react_1.useState)(ftsInputValue), 2), ftsInputValueAfterFiltering = _7[0], setFtsInputValueAfterFiltering = _7[1]; // tak isto ako filtersAfterFiltering
375
+ var _8 = __read((0, react_1.useState)(optionalCustomFilter), 2), optionalCustomFilterAfterFiltering = _8[0], setOptionalCustomFilterAfterFiltering = _8[1]; // tak isto ako filtersAfterFiltering
376
+ var _9 = __read((0, react_1.useState)({ opened: false }), 2), formDialogState = _9[0], setFormDialogState = _9[1];
377
377
  // parameter [] zabezpeci ze sa metoda zavola len po prvom renderingu (a nie po kazdej zmene stavu (zavolani setNieco()))
378
378
  (0, react_1.useEffect)(function () {
379
379
  // jednoduchy sposob - nepouzivame parameter props.displayed a priznak dataLoaded
@@ -1470,7 +1470,7 @@ exports.XLazyDataTable = (0, react_1.forwardRef)(function (_a, ref) {
1470
1470
  exportRows ? react_1.default.createElement(XButton_1.XButton, { key: "exportRows", icon: "pi pi-file-export", label: (0, XLocale_1.xLocaleOption)('exportRows'), onClick: onClickExport }) : null,
1471
1471
  props.docTemplates && !isMobile && props.searchBrowseParams === undefined ? react_1.default.createElement(XDocTemplateButton_1.XDocTemplateButton, { key: "docTemplates", entity: props.entity, rowId: selectedRow ? selectedRow[dataKey] : undefined, docTemplates: typeof props.docTemplates === 'function' ? props.docTemplates : undefined }) : null,
1472
1472
  props.appButtonsForRow && props.searchBrowseParams === undefined ? props.appButtonsForRow.map(function (xAppButton) { return react_1.default.createElement(XButton_1.XButton, { key: xAppButton.key, icon: xAppButton.icon, label: xAppButton.label, onClick: function () { return onClickAppButtonForRow(xAppButton.onClick); }, style: xAppButton.style }); }) : null,
1473
- props.appButtons && props.searchBrowseParams === undefined,
1473
+ props.searchBrowseParams === undefined ? props.appButtons : null,
1474
1474
  props.searchBrowseParams !== undefined ? react_1.default.createElement(XButton_1.XButton, { key: "choose", label: (0, XLocale_1.xLocaleOption)('chooseRow'), onClick: onClickChoose }) : null,
1475
1475
  props.editForm != undefined ? react_1.default.createElement(XFormDialog_1.XFormDialog, { key: "formDialog", dialogState: formDialogState }) : null,
1476
1476
  exportRows ? react_1.default.createElement(XExportRowsDialog_1.XExportRowsDialog, { key: "exportRowsDialog", dialogState: exportRowsDialogState, hideDialog: function () { return setExportRowsDialogState({ dialogOpened: false }); } }) : null),
@@ -22,6 +22,7 @@ export declare enum XViewStatus {
22
22
  Hidden = "hidden"
23
23
  }
24
24
  export type XViewStatusOrBoolean = XViewStatus | boolean;
25
+ export type XStorageType = "none" | "session" | "local";
25
26
  export interface IPostgresInterval {
26
27
  years?: number;
27
28
  months?: number;
@@ -110,10 +111,10 @@ export declare class XUtils {
110
111
  static isTableRowInserted(tableRow: any): boolean;
111
112
  static xViewStatus(xViewStatusOrBoolean: XViewStatusOrBoolean): XViewStatus;
112
113
  static options(valueStringList: string[]): SelectItem[];
113
- static saveValueIntoStorage(key: string, value: any): void;
114
- static getValueFromStorage(key: string, initValue: any): any;
115
- static removeValueFromStorage(key: string): void;
116
- static clearStorage(): void;
114
+ static saveValueIntoStorage(xStorageType: XStorageType, key: string, value: any): void;
115
+ static getValueFromStorage(xStorageType: XStorageType, key: string, initValue: any): any;
116
+ static removeValueFromStorage(xStorageType: XStorageType, key: string): void;
117
+ static clearStorage(xStorageType: XStorageType): void;
117
118
  static reloadIfNewVersion(): void;
118
119
  static reloadIfNewVersionNow(): Promise<void>;
119
120
  static isLocalhost(): boolean;
@@ -713,7 +713,7 @@ var XUtils = exports.XUtils = /** @class */ (function () {
713
713
  XUtils.options = function (valueStringList) {
714
714
  return valueStringList.map(function (valueString) { return { value: valueString, label: valueString }; });
715
715
  };
716
- XUtils.saveValueIntoStorage = function (key, value) {
716
+ XUtils.saveValueIntoStorage = function (xStorageType, key, value) {
717
717
  // value can be also string or null or undefined
718
718
  // if we don't have object that can be serialised to json, we create special object
719
719
  var valueObject;
@@ -727,12 +727,23 @@ var XUtils = exports.XUtils = /** @class */ (function () {
727
727
  // value is null or string or boolean or number or Date...
728
728
  valueObject = { _xValue: value };
729
729
  }
730
- sessionStorage.setItem(key, XUtilsCommon_1.XUtilsCommon.objectAsJSON(valueObject));
730
+ if (xStorageType === "session") {
731
+ sessionStorage.setItem(key, XUtilsCommon_1.XUtilsCommon.objectAsJSON(valueObject));
732
+ }
733
+ else if (xStorageType === "local") {
734
+ localStorage.setItem(key, XUtilsCommon_1.XUtilsCommon.objectAsJSON(valueObject));
735
+ }
731
736
  };
732
- XUtils.getValueFromStorage = function (key, initValue) {
737
+ XUtils.getValueFromStorage = function (xStorageType, key, initValue) {
733
738
  // if the value is not found in storage, initValue is returned
734
739
  var value;
735
- var item = sessionStorage.getItem(key);
740
+ var item = null;
741
+ if (xStorageType === "session") {
742
+ item = sessionStorage.getItem(key);
743
+ }
744
+ else if (xStorageType === "local") {
745
+ item = localStorage.getItem(key);
746
+ }
736
747
  if (item !== null) {
737
748
  try {
738
749
  var valueObject = JSON.parse(item);
@@ -750,7 +761,7 @@ var XUtils = exports.XUtils = /** @class */ (function () {
750
761
  }
751
762
  catch (e) {
752
763
  // exception should not happen
753
- console.log("XUtils.getValueFromStorage: Could not parse/process item from session. key = ".concat(key, ", item = ").concat(item, ". Error: ").concat(e));
764
+ console.log("XUtils.getValueFromStorage: Could not parse/process item from storage \"".concat(xStorageType, "\". key = ").concat(key, ", item = ").concat(item, ". Error: ").concat(e));
754
765
  value = initValue;
755
766
  }
756
767
  }
@@ -759,11 +770,21 @@ var XUtils = exports.XUtils = /** @class */ (function () {
759
770
  }
760
771
  return value;
761
772
  };
762
- XUtils.removeValueFromStorage = function (key) {
763
- sessionStorage.removeItem(key);
773
+ XUtils.removeValueFromStorage = function (xStorageType, key) {
774
+ if (xStorageType === "session") {
775
+ sessionStorage.removeItem(key);
776
+ }
777
+ else if (xStorageType === "local") {
778
+ localStorage.removeItem(key);
779
+ }
764
780
  };
765
- XUtils.clearStorage = function () {
766
- sessionStorage.clear();
781
+ XUtils.clearStorage = function (xStorageType) {
782
+ if (xStorageType === "session") {
783
+ sessionStorage.clear();
784
+ }
785
+ else if (xStorageType === "local") {
786
+ localStorage.clear();
787
+ }
767
788
  };
768
789
  // hleper method used for items of XLazyDataTable (shortcut ldt)
769
790
  // static getValueFromStorageLdt(entity: string, stateKeySuffix: XStateKeySuffix, initValue: any): any {
@@ -863,7 +884,8 @@ var XUtils = exports.XUtils = /** @class */ (function () {
863
884
  // data in session may not correspond with new structures in new version
864
885
  // e.g. if we add new column to XLazyDataTable, filter operator/value for this column new column is missing in data from session and application will crash
865
886
  // simple solution is to clear session
866
- XUtils.clearStorage();
887
+ XUtils.clearStorage("session");
888
+ XUtils.clearStorage("local");
867
889
  // page reload (like pressing F5 or Enter on url bar)
868
890
  // warning - if user has typed some data in form, the data will be lost
869
891
  window.location.reload();
@@ -0,0 +1,2 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ export declare function useXStateLocal<T>(key: string, initialState: T): [T, Dispatch<SetStateAction<T>>];
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useXStateLocal = void 0;
4
+ var useXStateStorage_1 = require("./useXStateStorage");
5
+ // TODO - initialState sholud be value | function returning T, and the function should be called in function "initialStateFunction"
6
+ function useXStateLocal(key, initialState) {
7
+ return (0, useXStateStorage_1.useXStateStorage)("local", key, initialState);
8
+ }
9
+ exports.useXStateLocal = useXStateLocal;
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useXStateSession = void 0;
4
- var XUtils_1 = require("./XUtils");
5
- var useXStateSessionBase_1 = require("./useXStateSessionBase");
4
+ var useXStateStorage_1 = require("./useXStateStorage");
6
5
  // TODO - initialState sholud be value | function returning T, and the function should be called in function "initialStateFunction"
7
6
  function useXStateSession(key, initialState) {
8
- return (0, useXStateSessionBase_1.useXStateSessionBase)(key, function () { return XUtils_1.XUtils.getValueFromStorage(key, initialState); });
7
+ return (0, useXStateStorage_1.useXStateStorage)("session", key, initialState);
9
8
  }
10
9
  exports.useXStateSession = useXStateSession;
@@ -0,0 +1,3 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ import { XStorageType } from "./XUtils";
3
+ export declare function useXStateStorage<T>(xStorageType: XStorageType, key: string, initialState: T): [T, Dispatch<SetStateAction<T>>];
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useXStateStorage = void 0;
4
+ var XUtils_1 = require("./XUtils");
5
+ var useXStateStorageBase_1 = require("./useXStateStorageBase");
6
+ // TODO - initialState sholud be value | function returning T, and the function should be called in function "initialStateFunction"
7
+ function useXStateStorage(xStorageType, key, initialState) {
8
+ return (0, useXStateStorageBase_1.useXStateStorageBase)(xStorageType, key, function () { return XUtils_1.XUtils.getValueFromStorage(xStorageType, key, initialState); });
9
+ }
10
+ exports.useXStateStorage = useXStateStorage;
@@ -0,0 +1,3 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ import { XStorageType } from "./XUtils";
3
+ export declare function useXStateStorageBase<T>(xStorageType: XStorageType, key: string, initialStateFunction: () => T): [T, Dispatch<SetStateAction<T>>];
@@ -16,17 +16,17 @@ var __read = (this && this.__read) || function (o, n) {
16
16
  return ar;
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.useXStateSessionBase = void 0;
19
+ exports.useXStateStorageBase = void 0;
20
20
  var react_1 = require("react");
21
21
  var XUtils_1 = require("./XUtils");
22
22
  // this base version enables to use custom version of function that computes initialState
23
- // (in usual case useXStateSession shoud be used)
24
- function useXStateSessionBase(key, initialStateFunction) {
23
+ // (in usual case useXStateStorage shoud be used)
24
+ function useXStateStorageBase(xStorageType, key, initialStateFunction) {
25
25
  var _a = __read((0, react_1.useState)(initialStateFunction), 2), value = _a[0], setValue = _a[1];
26
26
  var setValueIntoSession = function (value) {
27
27
  setValue(value);
28
- XUtils_1.XUtils.saveValueIntoStorage(key, value);
28
+ XUtils_1.XUtils.saveValueIntoStorage(xStorageType, key, value);
29
29
  };
30
30
  return [value, setValueIntoSession];
31
31
  }
32
- exports.useXStateSessionBase = useXStateSessionBase;
32
+ exports.useXStateStorageBase = useXStateStorageBase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@michalrakus/x-react-web-lib",
3
- "version": "1.36.0",
3
+ "version": "1.36.1",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "clean": "rimraf lib",
@@ -0,0 +1,3 @@
1
+ // generated by gulp
2
+
3
+ export * from './lib/components/useXStateLocal';
@@ -0,0 +1,5 @@
1
+ // generated by gulp
2
+
3
+ 'use strict';
4
+
5
+ module.exports = require('./lib/components/useXStateLocal.js');
@@ -0,0 +1,3 @@
1
+ // generated by gulp
2
+
3
+ export * from './lib/components/useXStateStorage';
@@ -0,0 +1,5 @@
1
+ // generated by gulp
2
+
3
+ 'use strict';
4
+
5
+ module.exports = require('./lib/components/useXStateStorage.js');
@@ -1,2 +0,0 @@
1
- import { Dispatch, SetStateAction } from "react";
2
- export declare function useXStateSessionBase<T>(key: string, initialStateFunction: () => T): [T, Dispatch<SetStateAction<T>>];