kui-utils 0.0.23 → 0.0.25

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/cjs/index.js CHANGED
@@ -6,6 +6,7 @@ var React = require('react');
6
6
  var _ = require('lodash');
7
7
  var reactRouterDom = require('react-router-dom');
8
8
  var mobx = require('mobx');
9
+ var luxon = require('luxon');
9
10
 
10
11
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
12
 
@@ -2044,6 +2045,110 @@ var updateQueryParams = function (newLink) {
2044
2045
  window.history.pushState({ route: route }, "", route);
2045
2046
  };
2046
2047
 
2048
+ function cleanObjectForIndexDB(value) {
2049
+ if (typeof value === "undefined")
2050
+ return null;
2051
+ if (value instanceof File)
2052
+ return value;
2053
+ if (value instanceof luxon.DateTime)
2054
+ return value.toISO();
2055
+ if (mobx.isObservable(value)) {
2056
+ return cleanObjectForIndexDB(mobx.toJS(value));
2057
+ }
2058
+ if (Array.isArray(value)) {
2059
+ return value.map(function (item) { return cleanObjectForIndexDB(item); });
2060
+ }
2061
+ if (value !== null && typeof value === "object") {
2062
+ var plainObject = {};
2063
+ // eslint-disable-next-line no-restricted-syntax
2064
+ for (var key in value) {
2065
+ // eslint-disable-next-line no-prototype-builtins
2066
+ if (value.hasOwnProperty(key)) {
2067
+ var objectValue = value[key];
2068
+ if (typeof objectValue !== "function" && objectValue !== undefined) {
2069
+ plainObject[key] = cleanObjectForIndexDB(objectValue);
2070
+ }
2071
+ }
2072
+ }
2073
+ return plainObject;
2074
+ }
2075
+ return value;
2076
+ }
2077
+ var transactionQueue = Promise.resolve();
2078
+ function addToIndexDBWithQueue(db, storeName, data, loader) {
2079
+ transactionQueue = transactionQueue
2080
+ .then(function () {
2081
+ return new Promise(function (resolve, reject) {
2082
+ if (db && db.objectStoreNames.contains(storeName) && data) {
2083
+ var transaction = db.transaction(storeName, "readwrite");
2084
+ var store = transaction.objectStore(storeName);
2085
+ // eslint-disable-next-line no-param-reassign
2086
+ data.id = 1;
2087
+ var formattedData = cleanObjectForIndexDB(data);
2088
+ var request_1 = store.put(formattedData);
2089
+ request_1.onsuccess = function () {
2090
+ resolve(request_1);
2091
+ };
2092
+ request_1.onerror = function () {
2093
+ loader.setError("Не удалось сохранить данные в локальное хранилище");
2094
+ reject();
2095
+ };
2096
+ }
2097
+ });
2098
+ })
2099
+ .catch(function (error) {
2100
+ console.error("Ошибка в очереди транзакций:", error);
2101
+ });
2102
+ }
2103
+ var readFromIndexDB = function (db, name, loader) { return __awaiter(void 0, void 0, void 0, function () {
2104
+ var transaction, request_2;
2105
+ return __generator(this, function (_a) {
2106
+ if (db.objectStoreNames.contains(name)) {
2107
+ transaction = db.transaction(name, "readonly").objectStore(name);
2108
+ request_2 = transaction.getAll();
2109
+ return [2 /*return*/, new Promise(function (resolve, reject) {
2110
+ request_2.onerror = function () {
2111
+ reject();
2112
+ loader.setError("Не удалось сохранить данные в локальное хранилище");
2113
+ };
2114
+ request_2.onsuccess = function () {
2115
+ var _a;
2116
+ resolve((_a = request_2.result) === null || _a === void 0 ? void 0 : _a[0]);
2117
+ };
2118
+ })];
2119
+ }
2120
+ return [2 /*return*/, null];
2121
+ });
2122
+ }); };
2123
+ var initIndexDB = function (dbName, onupgradeneeded, loader, setIndexDB) {
2124
+ var dbRequest = indexedDB.open(dbName, 1);
2125
+ dbRequest.onerror = function () {
2126
+ loader.setError("Ошибка при открытии хранилища на устройстве. Данные не будут сохраняться локально");
2127
+ };
2128
+ dbRequest.onsuccess = function () {
2129
+ var db = dbRequest.result;
2130
+ setIndexDB(db);
2131
+ };
2132
+ dbRequest.onupgradeneeded = function () { return onupgradeneeded(dbRequest.result); };
2133
+ };
2134
+ var addIndexDBStore = function (db, name) {
2135
+ if (db && !db.objectStoreNames.contains(name)) {
2136
+ db.createObjectStore(name, { keyPath: "id" });
2137
+ }
2138
+ };
2139
+ var clearIndexStore = function (db, name) {
2140
+ if (db) {
2141
+ var transaction = db.transaction(name, "readwrite");
2142
+ var objectStore = transaction.objectStore(name);
2143
+ objectStore.clear();
2144
+ }
2145
+ };
2146
+ var clearIndexStores = function (db, stores) {
2147
+ Array.from(stores).forEach(function (name) {
2148
+ clearIndexStore(db, name);
2149
+ });
2150
+ };
2151
+
2047
2152
  function setCookie(name, value, options) {
2048
2153
  if (options === void 0) { options = {}; }
2049
2154
  var cookieOptions = __assign({ path: "/" }, options);
@@ -2264,9 +2369,8 @@ var MultistepForm = /** @class */ (function () {
2264
2369
  this.addValueToField = function (valueName, value, fieldName) {
2265
2370
  if (!_this.fields)
2266
2371
  _this.fields = {};
2267
- var _a = _this.getNestedField(fieldName), path = _a.path, fields = _a.fields;
2372
+ var path = _this.getNestedField(fieldName);
2268
2373
  path[valueName] = value;
2269
- _this.fields = fields;
2270
2374
  };
2271
2375
  this.getNestedField = function (fieldName) {
2272
2376
  if (!_this.fields)
@@ -2278,12 +2382,11 @@ var MultistepForm = /** @class */ (function () {
2278
2382
  // @ts-ignore
2279
2383
  _this.fields[fieldParts[0]] = [];
2280
2384
  }
2281
- var fields = _this.fields;
2282
- var path = fields;
2385
+ var path = _this.fields;
2283
2386
  fieldParts === null || fieldParts === void 0 ? void 0 : fieldParts.forEach(function (part) {
2284
2387
  path = path[part];
2285
2388
  });
2286
- return { path: path, fields: fields };
2389
+ return path;
2287
2390
  };
2288
2391
  this.nextStep = function () {
2289
2392
  _this.step += 1;
@@ -2370,14 +2473,18 @@ exports.MultistepForm = MultistepForm;
2370
2473
  exports.Paginator = Paginator;
2371
2474
  exports.SortingFilter = SortingFilter;
2372
2475
  exports.accountRegExp = accountRegExp;
2476
+ exports.addIndexDBStore = addIndexDBStore;
2373
2477
  exports.addLeadZero = addLeadZero;
2374
2478
  exports.addToArrayByCondition = addToArrayByCondition;
2479
+ exports.addToIndexDBWithQueue = addToIndexDBWithQueue;
2375
2480
  exports.applyMask = applyMask;
2376
2481
  exports.bicRegExp = bicRegExp;
2377
2482
  exports.callPromises = callPromises;
2378
2483
  exports.carNumberRegExp = carNumberRegExp;
2379
2484
  exports.checkIsIOS = checkIsIOS;
2380
2485
  exports.cleanEmptyFields = cleanEmptyFields;
2486
+ exports.clearIndexStore = clearIndexStore;
2487
+ exports.clearIndexStores = clearIndexStores;
2381
2488
  exports.clearNotValidFields = clearNotValidFields;
2382
2489
  exports.copyInfo = copyInfo;
2383
2490
  exports.corrAccountRegExp = corrAccountRegExp;
@@ -2396,12 +2503,14 @@ exports.getFileNameFromUrl = getFileNameFromUrl;
2396
2503
  exports.getNestedData = getNestedData;
2397
2504
  exports.getPhoneNumberFromPhoneParams = getPhoneNumberFromPhoneParams;
2398
2505
  exports.getPhoneParamsFromString = getPhoneParamsFromString;
2506
+ exports.initIndexDB = initIndexDB;
2399
2507
  exports.innRegExp = innRegExp;
2400
2508
  exports.isValidWithMaskExp = isValidWithMaskExp;
2401
2509
  exports.mediumPasswordRegExp = mediumPasswordRegExp;
2402
2510
  exports.monthYearRegExp = monthYearRegExp;
2403
2511
  exports.phoneRegExp = phoneRegExp;
2404
2512
  exports.promisesWithCallback = promisesWithCallback;
2513
+ exports.readFromIndexDB = readFromIndexDB;
2405
2514
  exports.resHandler = resHandler;
2406
2515
  exports.setCookie = setCookie;
2407
2516
  exports.simplePasswordRegExp = simplePasswordRegExp;