@karta.io/utils 0.35.0 → 0.35.2
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/dist/index.cjs +140 -41
- package/dist/index.d.ts +21 -3
- package/dist/index.mjs +137 -41
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -7,6 +7,9 @@ const snakeCase = require('lodash/snakeCase.js');
|
|
|
7
7
|
const vue = require('vue');
|
|
8
8
|
const vueQuery = require('@tanstack/vue-query');
|
|
9
9
|
const core = require('@vueuse/core');
|
|
10
|
+
const get = require('lodash/get.js');
|
|
11
|
+
const has = require('lodash/has.js');
|
|
12
|
+
const logdown = require('logdown');
|
|
10
13
|
const dayjs = require('dayjs');
|
|
11
14
|
const utc = require('dayjs/plugin/utc.js');
|
|
12
15
|
const tz = require('dayjs/plugin/timezone.js');
|
|
@@ -14,19 +17,20 @@ const updateLocale = require('dayjs/plugin/updateLocale.js');
|
|
|
14
17
|
const isToday = require('dayjs/plugin/isToday.js');
|
|
15
18
|
const customParseFormat = require('dayjs/plugin/customParseFormat.js');
|
|
16
19
|
const detectBrowser = require('detect-browser');
|
|
17
|
-
const logdown = require('logdown');
|
|
18
20
|
|
|
19
21
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
20
22
|
|
|
21
23
|
const camelCase__default = /*#__PURE__*/_interopDefaultCompat(camelCase);
|
|
22
24
|
const snakeCase__default = /*#__PURE__*/_interopDefaultCompat(snakeCase);
|
|
25
|
+
const get__default = /*#__PURE__*/_interopDefaultCompat(get);
|
|
26
|
+
const has__default = /*#__PURE__*/_interopDefaultCompat(has);
|
|
27
|
+
const logdown__default = /*#__PURE__*/_interopDefaultCompat(logdown);
|
|
23
28
|
const dayjs__default = /*#__PURE__*/_interopDefaultCompat(dayjs);
|
|
24
29
|
const utc__default = /*#__PURE__*/_interopDefaultCompat(utc);
|
|
25
30
|
const tz__default = /*#__PURE__*/_interopDefaultCompat(tz);
|
|
26
31
|
const updateLocale__default = /*#__PURE__*/_interopDefaultCompat(updateLocale);
|
|
27
32
|
const isToday__default = /*#__PURE__*/_interopDefaultCompat(isToday);
|
|
28
33
|
const customParseFormat__default = /*#__PURE__*/_interopDefaultCompat(customParseFormat);
|
|
29
|
-
const logdown__default = /*#__PURE__*/_interopDefaultCompat(logdown);
|
|
30
34
|
|
|
31
35
|
function camelizeKeys(object) {
|
|
32
36
|
if (Array.isArray(object))
|
|
@@ -269,6 +273,34 @@ function useAbortController(callback) {
|
|
|
269
273
|
};
|
|
270
274
|
}
|
|
271
275
|
|
|
276
|
+
function isEnabled(options) {
|
|
277
|
+
if (options?.debug !== void 0)
|
|
278
|
+
return options.debug;
|
|
279
|
+
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
280
|
+
if (isBrowser) {
|
|
281
|
+
const debug = localStorage.getItem("debug");
|
|
282
|
+
if (debug === "true")
|
|
283
|
+
return true;
|
|
284
|
+
if (debug === "false")
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
288
|
+
if (isNode) {
|
|
289
|
+
const debug = process.env.NODE_DEBUG;
|
|
290
|
+
if (debug === "true")
|
|
291
|
+
return true;
|
|
292
|
+
if (debug === "false")
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
function useLogger(scope, options) {
|
|
298
|
+
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
299
|
+
const logger = logdown__default(prefix, { prefixColor: options?.color });
|
|
300
|
+
logger.state.isEnabled = isEnabled(options);
|
|
301
|
+
return logger;
|
|
302
|
+
}
|
|
303
|
+
|
|
272
304
|
function useAsyncMultiselect({
|
|
273
305
|
apiMethod,
|
|
274
306
|
mapOptionCallback = void 0,
|
|
@@ -281,6 +313,12 @@ function useAsyncMultiselect({
|
|
|
281
313
|
delay = 300,
|
|
282
314
|
onUpdated = void 0
|
|
283
315
|
}) {
|
|
316
|
+
const logger = useLogger("UTILS", {
|
|
317
|
+
tag: "useAsyncMultiselect",
|
|
318
|
+
color: "#b54708",
|
|
319
|
+
debug: true
|
|
320
|
+
});
|
|
321
|
+
const paramsForLogger = { queryKey, dataItemsKey, dataItemIdKey, dataCountKey };
|
|
284
322
|
const reactiveData = vue.reactive({
|
|
285
323
|
search: "",
|
|
286
324
|
page: 1,
|
|
@@ -304,20 +342,48 @@ function useAsyncMultiselect({
|
|
|
304
342
|
vue.watch(
|
|
305
343
|
data,
|
|
306
344
|
(newValue) => {
|
|
307
|
-
if (!newValue
|
|
345
|
+
if (!newValue)
|
|
346
|
+
return;
|
|
347
|
+
if (!has__default(newValue, dataItemsKey)) {
|
|
348
|
+
logger.warn(
|
|
349
|
+
`Unable to find key *"${dataItemsKey}"* (property "dataItemsKey") in data object from API.`,
|
|
350
|
+
paramsForLogger
|
|
351
|
+
);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (!has__default(newValue, dataCountKey)) {
|
|
355
|
+
logger.warn(
|
|
356
|
+
`Unable to find key *"${dataCountKey}"* (property "dataCountKey") in data object from API.`,
|
|
357
|
+
paramsForLogger
|
|
358
|
+
);
|
|
308
359
|
return;
|
|
309
|
-
|
|
310
|
-
|
|
360
|
+
}
|
|
361
|
+
if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0)
|
|
362
|
+
return;
|
|
363
|
+
count.value = get__default(newValue, dataCountKey, 0);
|
|
364
|
+
const newItems = get__default(newValue, dataItemsKey);
|
|
311
365
|
itemsByPages.value = {
|
|
312
366
|
...reactiveData.page !== 1 && itemsByPages.value,
|
|
313
367
|
[reactiveData.page]: newItems
|
|
314
368
|
};
|
|
315
369
|
if (reactiveData.page === 1 && reactiveData.selectedValues?.length && !reactiveData.search) {
|
|
370
|
+
if (newItems.some((item) => !has__default(item, dataItemIdKey))) {
|
|
371
|
+
logger.warn(
|
|
372
|
+
`Unable to find key *"${dataItemIdKey}"* (property "dataItemIdKey") in items from API.`,
|
|
373
|
+
'This key must be match with "optionValue" in UiMultiselect.',
|
|
374
|
+
paramsForLogger
|
|
375
|
+
);
|
|
376
|
+
}
|
|
316
377
|
selectedItemsCached.value = newItems.filter(
|
|
317
|
-
(item) => reactiveData.selectedValues.includes(item
|
|
378
|
+
(item) => reactiveData.selectedValues.includes(get__default(item, dataItemIdKey))
|
|
318
379
|
);
|
|
319
380
|
}
|
|
320
|
-
onUpdated?.(
|
|
381
|
+
onUpdated?.({
|
|
382
|
+
search: reactiveData.search,
|
|
383
|
+
page: reactiveData.page,
|
|
384
|
+
selectedValues: reactiveData.selectedValues,
|
|
385
|
+
params: reactiveData.params ? JSON.parse(reactiveData.params) : void 0
|
|
386
|
+
});
|
|
321
387
|
},
|
|
322
388
|
{ immediate: true }
|
|
323
389
|
);
|
|
@@ -332,7 +398,7 @@ function useAsyncMultiselect({
|
|
|
332
398
|
);
|
|
333
399
|
const itemsAsArray = [...selectedItemsCached.value, ...itemsByPagesAsArray];
|
|
334
400
|
const itemsAsMap = itemsAsArray.reduce((acc, item) => {
|
|
335
|
-
const key = item
|
|
401
|
+
const key = get__default(item, dataItemIdKey, JSON.stringify(item));
|
|
336
402
|
return acc.set(key, item);
|
|
337
403
|
}, /* @__PURE__ */ new Map());
|
|
338
404
|
return [...itemsAsMap.values()];
|
|
@@ -347,10 +413,14 @@ function useAsyncMultiselect({
|
|
|
347
413
|
const hasNextPage = vue.computed(() => count.value > items.value.length);
|
|
348
414
|
const loadPage = (newPage = 1) => {
|
|
349
415
|
reactiveData.page = newPage;
|
|
416
|
+
if (newPage === 1) {
|
|
417
|
+
reactiveData.selectedValues = core.toValue(selectedValues);
|
|
418
|
+
}
|
|
350
419
|
};
|
|
351
420
|
const updateSearchWithDebounce = core.useDebounceFn((value) => {
|
|
352
421
|
reactiveData.search = value;
|
|
353
422
|
reactiveData.page = 1;
|
|
423
|
+
reactiveData.selectedValues = core.toValue(selectedValues);
|
|
354
424
|
}, delay);
|
|
355
425
|
const updateSearch = (value) => {
|
|
356
426
|
updateSearchWithDebounce(value);
|
|
@@ -360,19 +430,28 @@ function useAsyncMultiselect({
|
|
|
360
430
|
if (!newSelectedValues.length) {
|
|
361
431
|
selectedItemsCached.value = [];
|
|
362
432
|
} else {
|
|
433
|
+
if (items.value.some((item) => !has__default(item, dataItemIdKey))) {
|
|
434
|
+
logger.warn(
|
|
435
|
+
`Unable to find key *"${dataItemIdKey}"* (property "dataItemIdKey") in items from API.`,
|
|
436
|
+
'This key must be match with "optionValue" in UiMultiselect.',
|
|
437
|
+
paramsForLogger
|
|
438
|
+
);
|
|
439
|
+
}
|
|
363
440
|
selectedItemsCached.value = items.value.filter(
|
|
364
|
-
(item) => newSelectedValues.includes(item
|
|
441
|
+
(item) => newSelectedValues.includes(get__default(item, dataItemIdKey))
|
|
365
442
|
);
|
|
366
443
|
}
|
|
367
444
|
await vue.nextTick();
|
|
368
|
-
reactiveData.page
|
|
369
|
-
|
|
445
|
+
if (reactiveData.page === 1) {
|
|
446
|
+
reactiveData.selectedValues = newSelectedValues;
|
|
447
|
+
}
|
|
370
448
|
});
|
|
371
449
|
}
|
|
372
450
|
if (vue.isRef(params)) {
|
|
373
451
|
vue.watch(params, (newParams) => {
|
|
374
452
|
reactiveData.page = 1;
|
|
375
453
|
reactiveData.params = JSON.stringify(newParams);
|
|
454
|
+
reactiveData.selectedValues = core.toValue(selectedValues);
|
|
376
455
|
});
|
|
377
456
|
}
|
|
378
457
|
vue.onBeforeUnmount(() => {
|
|
@@ -401,6 +480,12 @@ function useAsyncSelect({
|
|
|
401
480
|
delay = 300,
|
|
402
481
|
onUpdated = void 0
|
|
403
482
|
}) {
|
|
483
|
+
const logger = useLogger("UTILS", {
|
|
484
|
+
tag: "useAsyncSelect",
|
|
485
|
+
color: "#b54708",
|
|
486
|
+
debug: true
|
|
487
|
+
});
|
|
488
|
+
const paramsForLogger = { queryKey, dataItemsKey, dataCountKey };
|
|
404
489
|
const reactiveData = vue.reactive({
|
|
405
490
|
search: "",
|
|
406
491
|
page: 1,
|
|
@@ -423,14 +508,36 @@ function useAsyncSelect({
|
|
|
423
508
|
vue.watch(
|
|
424
509
|
data,
|
|
425
510
|
(newValue) => {
|
|
426
|
-
if (!newValue
|
|
511
|
+
if (!newValue)
|
|
427
512
|
return;
|
|
428
|
-
|
|
513
|
+
if (!has__default(newValue, dataItemsKey)) {
|
|
514
|
+
logger.warn(
|
|
515
|
+
`Unable to find key *"${dataItemsKey}"* (property "dataItemsKey") in data object from API.`,
|
|
516
|
+
paramsForLogger
|
|
517
|
+
);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
if (!has__default(newValue, dataCountKey)) {
|
|
521
|
+
logger.warn(
|
|
522
|
+
`Unable to find key *"${dataCountKey}"* (property "dataCountKey") in data object from API.`,
|
|
523
|
+
paramsForLogger
|
|
524
|
+
);
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (!get__default(newValue, dataItemsKey) || get__default(newValue, dataCountKey) === void 0)
|
|
528
|
+
return;
|
|
529
|
+
count.value = get__default(newValue, dataCountKey, 0);
|
|
530
|
+
const newItems = get__default(newValue, dataItemsKey);
|
|
429
531
|
itemsByPages.value = {
|
|
430
532
|
...reactiveData.page !== 1 && itemsByPages.value,
|
|
431
|
-
[reactiveData.page]:
|
|
533
|
+
[reactiveData.page]: newItems
|
|
432
534
|
};
|
|
433
|
-
onUpdated?.(
|
|
535
|
+
onUpdated?.({
|
|
536
|
+
search: reactiveData.search,
|
|
537
|
+
page: reactiveData.page,
|
|
538
|
+
selectedValue: reactiveData.selectedValue,
|
|
539
|
+
params: reactiveData.params ? JSON.parse(reactiveData.params) : void 0
|
|
540
|
+
});
|
|
434
541
|
},
|
|
435
542
|
{ immediate: true }
|
|
436
543
|
);
|
|
@@ -583,32 +690,23 @@ function useDetectBrowser(versions, userAgent) {
|
|
|
583
690
|
});
|
|
584
691
|
}
|
|
585
692
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
return false;
|
|
604
|
-
}
|
|
605
|
-
return false;
|
|
606
|
-
}
|
|
607
|
-
function useLogger(scope, options) {
|
|
608
|
-
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
609
|
-
const logger = logdown__default(prefix, { prefixColor: options?.color });
|
|
610
|
-
logger.state.isEnabled = isEnabled(options);
|
|
611
|
-
return logger;
|
|
693
|
+
const isLocked = vue.ref(false);
|
|
694
|
+
function useGlobalScroll() {
|
|
695
|
+
const lock = () => {
|
|
696
|
+
isLocked.value = true;
|
|
697
|
+
document.body.style.paddingRight = `${window.innerWidth - document.documentElement.clientWidth}px`;
|
|
698
|
+
document.body.style.overflow = "hidden";
|
|
699
|
+
};
|
|
700
|
+
const unlock = () => {
|
|
701
|
+
isLocked.value = false;
|
|
702
|
+
document.body.style.overflow = "auto";
|
|
703
|
+
document.body.style.paddingRight = "0px";
|
|
704
|
+
};
|
|
705
|
+
return {
|
|
706
|
+
isLocked,
|
|
707
|
+
lock,
|
|
708
|
+
unlock
|
|
709
|
+
};
|
|
612
710
|
}
|
|
613
711
|
|
|
614
712
|
function validatePhone(rule, value) {
|
|
@@ -693,6 +791,7 @@ exports.useAsyncSelect = useAsyncSelect;
|
|
|
693
791
|
exports.useClassName = useClassName;
|
|
694
792
|
exports.useDayjs = useDayjs;
|
|
695
793
|
exports.useDetectBrowser = useDetectBrowser;
|
|
794
|
+
exports.useGlobalScroll = useGlobalScroll;
|
|
696
795
|
exports.useLogger = useLogger;
|
|
697
796
|
exports.validateNumber = validateNumber;
|
|
698
797
|
exports.validatePhone = validatePhone;
|
package/dist/index.d.ts
CHANGED
|
@@ -106,6 +106,12 @@ declare function useAbortController<T extends (...args: any[]) => any>(callback:
|
|
|
106
106
|
isAborted(error: ResponseError): boolean;
|
|
107
107
|
};
|
|
108
108
|
|
|
109
|
+
interface ReactiveData$1 {
|
|
110
|
+
search: string;
|
|
111
|
+
page: number;
|
|
112
|
+
selectedValues?: any[];
|
|
113
|
+
params?: any;
|
|
114
|
+
}
|
|
109
115
|
declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValues, params, queryKey, dataItemsKey, dataItemIdKey, dataCountKey, delay, onUpdated, }: {
|
|
110
116
|
apiMethod: T;
|
|
111
117
|
mapOptionCallback?: K;
|
|
@@ -116,7 +122,7 @@ declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extend
|
|
|
116
122
|
dataItemIdKey?: string;
|
|
117
123
|
dataCountKey?: string;
|
|
118
124
|
delay?: number;
|
|
119
|
-
onUpdated?: () => void;
|
|
125
|
+
onUpdated?: (data: ReactiveData$1) => void;
|
|
120
126
|
}): {
|
|
121
127
|
items: vue.ComputedRef<any[]>;
|
|
122
128
|
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
@@ -128,6 +134,12 @@ declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extend
|
|
|
128
134
|
updateSearch: (value: string) => void;
|
|
129
135
|
};
|
|
130
136
|
|
|
137
|
+
interface ReactiveData {
|
|
138
|
+
search: string;
|
|
139
|
+
page: number;
|
|
140
|
+
selectedValue?: any[];
|
|
141
|
+
params?: any;
|
|
142
|
+
}
|
|
131
143
|
declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValue, params, queryKey, dataItemsKey, dataCountKey, delay, onUpdated, }: {
|
|
132
144
|
apiMethod: T;
|
|
133
145
|
mapOptionCallback?: K;
|
|
@@ -137,7 +149,7 @@ declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (pa
|
|
|
137
149
|
dataItemsKey?: string;
|
|
138
150
|
dataCountKey?: string;
|
|
139
151
|
delay?: number;
|
|
140
|
-
onUpdated?: () => void;
|
|
152
|
+
onUpdated?: (data: ReactiveData) => void;
|
|
141
153
|
}): {
|
|
142
154
|
items: vue.ComputedRef<any[]>;
|
|
143
155
|
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
@@ -182,6 +194,12 @@ type VersionsList = {
|
|
|
182
194
|
};
|
|
183
195
|
declare function useDetectBrowser(versions: VersionsList, userAgent?: any): BrowserInfo;
|
|
184
196
|
|
|
197
|
+
declare function useGlobalScroll(): {
|
|
198
|
+
isLocked: vue.Ref<boolean>;
|
|
199
|
+
lock: () => void;
|
|
200
|
+
unlock: () => void;
|
|
201
|
+
};
|
|
202
|
+
|
|
185
203
|
/**
|
|
186
204
|
* @see https://github.com/caiogondim/logdown.js#readme
|
|
187
205
|
*/
|
|
@@ -209,4 +227,4 @@ declare function isTypeUrl(message?: string): RuleItem;
|
|
|
209
227
|
declare function isTypeNumber(value?: number, message?: string): RuleItem;
|
|
210
228
|
declare function isStrictLength(value?: number, message?: string): RuleItem;
|
|
211
229
|
|
|
212
|
-
export { BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone };
|
|
230
|
+
export { BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useGlobalScroll, useLogger, validateNumber, validatePhone };
|
package/dist/index.mjs
CHANGED
|
@@ -5,6 +5,9 @@ import snakeCase from 'lodash/snakeCase.js';
|
|
|
5
5
|
import { reactive, ref, watch, computed, isRef, nextTick, onBeforeUnmount } from 'vue';
|
|
6
6
|
import { useQuery } from '@tanstack/vue-query';
|
|
7
7
|
import { toValue, useDebounceFn } from '@vueuse/core';
|
|
8
|
+
import get from 'lodash/get.js';
|
|
9
|
+
import has from 'lodash/has.js';
|
|
10
|
+
import logdown from 'logdown';
|
|
8
11
|
import dayjs from 'dayjs';
|
|
9
12
|
import utc from 'dayjs/plugin/utc.js';
|
|
10
13
|
import tz from 'dayjs/plugin/timezone.js';
|
|
@@ -12,7 +15,6 @@ import updateLocale from 'dayjs/plugin/updateLocale.js';
|
|
|
12
15
|
import isToday from 'dayjs/plugin/isToday.js';
|
|
13
16
|
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
14
17
|
import { detect } from 'detect-browser';
|
|
15
|
-
import logdown from 'logdown';
|
|
16
18
|
|
|
17
19
|
function camelizeKeys(object) {
|
|
18
20
|
if (Array.isArray(object))
|
|
@@ -255,6 +257,34 @@ function useAbortController(callback) {
|
|
|
255
257
|
};
|
|
256
258
|
}
|
|
257
259
|
|
|
260
|
+
function isEnabled(options) {
|
|
261
|
+
if (options?.debug !== void 0)
|
|
262
|
+
return options.debug;
|
|
263
|
+
const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
264
|
+
if (isBrowser) {
|
|
265
|
+
const debug = localStorage.getItem("debug");
|
|
266
|
+
if (debug === "true")
|
|
267
|
+
return true;
|
|
268
|
+
if (debug === "false")
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
const isNode = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
|
|
272
|
+
if (isNode) {
|
|
273
|
+
const debug = process.env.NODE_DEBUG;
|
|
274
|
+
if (debug === "true")
|
|
275
|
+
return true;
|
|
276
|
+
if (debug === "false")
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
function useLogger(scope, options) {
|
|
282
|
+
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
283
|
+
const logger = logdown(prefix, { prefixColor: options?.color });
|
|
284
|
+
logger.state.isEnabled = isEnabled(options);
|
|
285
|
+
return logger;
|
|
286
|
+
}
|
|
287
|
+
|
|
258
288
|
function useAsyncMultiselect({
|
|
259
289
|
apiMethod,
|
|
260
290
|
mapOptionCallback = void 0,
|
|
@@ -267,6 +297,12 @@ function useAsyncMultiselect({
|
|
|
267
297
|
delay = 300,
|
|
268
298
|
onUpdated = void 0
|
|
269
299
|
}) {
|
|
300
|
+
const logger = useLogger("UTILS", {
|
|
301
|
+
tag: "useAsyncMultiselect",
|
|
302
|
+
color: "#b54708",
|
|
303
|
+
debug: true
|
|
304
|
+
});
|
|
305
|
+
const paramsForLogger = { queryKey, dataItemsKey, dataItemIdKey, dataCountKey };
|
|
270
306
|
const reactiveData = reactive({
|
|
271
307
|
search: "",
|
|
272
308
|
page: 1,
|
|
@@ -290,20 +326,48 @@ function useAsyncMultiselect({
|
|
|
290
326
|
watch(
|
|
291
327
|
data,
|
|
292
328
|
(newValue) => {
|
|
293
|
-
if (!newValue
|
|
329
|
+
if (!newValue)
|
|
330
|
+
return;
|
|
331
|
+
if (!has(newValue, dataItemsKey)) {
|
|
332
|
+
logger.warn(
|
|
333
|
+
`Unable to find key *"${dataItemsKey}"* (property "dataItemsKey") in data object from API.`,
|
|
334
|
+
paramsForLogger
|
|
335
|
+
);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (!has(newValue, dataCountKey)) {
|
|
339
|
+
logger.warn(
|
|
340
|
+
`Unable to find key *"${dataCountKey}"* (property "dataCountKey") in data object from API.`,
|
|
341
|
+
paramsForLogger
|
|
342
|
+
);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (!get(newValue, dataItemsKey) || get(newValue, dataCountKey) === void 0)
|
|
294
346
|
return;
|
|
295
|
-
count.value = newValue
|
|
296
|
-
const newItems = newValue
|
|
347
|
+
count.value = get(newValue, dataCountKey, 0);
|
|
348
|
+
const newItems = get(newValue, dataItemsKey);
|
|
297
349
|
itemsByPages.value = {
|
|
298
350
|
...reactiveData.page !== 1 && itemsByPages.value,
|
|
299
351
|
[reactiveData.page]: newItems
|
|
300
352
|
};
|
|
301
353
|
if (reactiveData.page === 1 && reactiveData.selectedValues?.length && !reactiveData.search) {
|
|
354
|
+
if (newItems.some((item) => !has(item, dataItemIdKey))) {
|
|
355
|
+
logger.warn(
|
|
356
|
+
`Unable to find key *"${dataItemIdKey}"* (property "dataItemIdKey") in items from API.`,
|
|
357
|
+
'This key must be match with "optionValue" in UiMultiselect.',
|
|
358
|
+
paramsForLogger
|
|
359
|
+
);
|
|
360
|
+
}
|
|
302
361
|
selectedItemsCached.value = newItems.filter(
|
|
303
|
-
(item) => reactiveData.selectedValues.includes(item
|
|
362
|
+
(item) => reactiveData.selectedValues.includes(get(item, dataItemIdKey))
|
|
304
363
|
);
|
|
305
364
|
}
|
|
306
|
-
onUpdated?.(
|
|
365
|
+
onUpdated?.({
|
|
366
|
+
search: reactiveData.search,
|
|
367
|
+
page: reactiveData.page,
|
|
368
|
+
selectedValues: reactiveData.selectedValues,
|
|
369
|
+
params: reactiveData.params ? JSON.parse(reactiveData.params) : void 0
|
|
370
|
+
});
|
|
307
371
|
},
|
|
308
372
|
{ immediate: true }
|
|
309
373
|
);
|
|
@@ -318,7 +382,7 @@ function useAsyncMultiselect({
|
|
|
318
382
|
);
|
|
319
383
|
const itemsAsArray = [...selectedItemsCached.value, ...itemsByPagesAsArray];
|
|
320
384
|
const itemsAsMap = itemsAsArray.reduce((acc, item) => {
|
|
321
|
-
const key = item
|
|
385
|
+
const key = get(item, dataItemIdKey, JSON.stringify(item));
|
|
322
386
|
return acc.set(key, item);
|
|
323
387
|
}, /* @__PURE__ */ new Map());
|
|
324
388
|
return [...itemsAsMap.values()];
|
|
@@ -333,10 +397,14 @@ function useAsyncMultiselect({
|
|
|
333
397
|
const hasNextPage = computed(() => count.value > items.value.length);
|
|
334
398
|
const loadPage = (newPage = 1) => {
|
|
335
399
|
reactiveData.page = newPage;
|
|
400
|
+
if (newPage === 1) {
|
|
401
|
+
reactiveData.selectedValues = toValue(selectedValues);
|
|
402
|
+
}
|
|
336
403
|
};
|
|
337
404
|
const updateSearchWithDebounce = useDebounceFn((value) => {
|
|
338
405
|
reactiveData.search = value;
|
|
339
406
|
reactiveData.page = 1;
|
|
407
|
+
reactiveData.selectedValues = toValue(selectedValues);
|
|
340
408
|
}, delay);
|
|
341
409
|
const updateSearch = (value) => {
|
|
342
410
|
updateSearchWithDebounce(value);
|
|
@@ -346,19 +414,28 @@ function useAsyncMultiselect({
|
|
|
346
414
|
if (!newSelectedValues.length) {
|
|
347
415
|
selectedItemsCached.value = [];
|
|
348
416
|
} else {
|
|
417
|
+
if (items.value.some((item) => !has(item, dataItemIdKey))) {
|
|
418
|
+
logger.warn(
|
|
419
|
+
`Unable to find key *"${dataItemIdKey}"* (property "dataItemIdKey") in items from API.`,
|
|
420
|
+
'This key must be match with "optionValue" in UiMultiselect.',
|
|
421
|
+
paramsForLogger
|
|
422
|
+
);
|
|
423
|
+
}
|
|
349
424
|
selectedItemsCached.value = items.value.filter(
|
|
350
|
-
(item) => newSelectedValues.includes(item
|
|
425
|
+
(item) => newSelectedValues.includes(get(item, dataItemIdKey))
|
|
351
426
|
);
|
|
352
427
|
}
|
|
353
428
|
await nextTick();
|
|
354
|
-
reactiveData.page
|
|
355
|
-
|
|
429
|
+
if (reactiveData.page === 1) {
|
|
430
|
+
reactiveData.selectedValues = newSelectedValues;
|
|
431
|
+
}
|
|
356
432
|
});
|
|
357
433
|
}
|
|
358
434
|
if (isRef(params)) {
|
|
359
435
|
watch(params, (newParams) => {
|
|
360
436
|
reactiveData.page = 1;
|
|
361
437
|
reactiveData.params = JSON.stringify(newParams);
|
|
438
|
+
reactiveData.selectedValues = toValue(selectedValues);
|
|
362
439
|
});
|
|
363
440
|
}
|
|
364
441
|
onBeforeUnmount(() => {
|
|
@@ -387,6 +464,12 @@ function useAsyncSelect({
|
|
|
387
464
|
delay = 300,
|
|
388
465
|
onUpdated = void 0
|
|
389
466
|
}) {
|
|
467
|
+
const logger = useLogger("UTILS", {
|
|
468
|
+
tag: "useAsyncSelect",
|
|
469
|
+
color: "#b54708",
|
|
470
|
+
debug: true
|
|
471
|
+
});
|
|
472
|
+
const paramsForLogger = { queryKey, dataItemsKey, dataCountKey };
|
|
390
473
|
const reactiveData = reactive({
|
|
391
474
|
search: "",
|
|
392
475
|
page: 1,
|
|
@@ -409,14 +492,36 @@ function useAsyncSelect({
|
|
|
409
492
|
watch(
|
|
410
493
|
data,
|
|
411
494
|
(newValue) => {
|
|
412
|
-
if (!newValue
|
|
495
|
+
if (!newValue)
|
|
496
|
+
return;
|
|
497
|
+
if (!has(newValue, dataItemsKey)) {
|
|
498
|
+
logger.warn(
|
|
499
|
+
`Unable to find key *"${dataItemsKey}"* (property "dataItemsKey") in data object from API.`,
|
|
500
|
+
paramsForLogger
|
|
501
|
+
);
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (!has(newValue, dataCountKey)) {
|
|
505
|
+
logger.warn(
|
|
506
|
+
`Unable to find key *"${dataCountKey}"* (property "dataCountKey") in data object from API.`,
|
|
507
|
+
paramsForLogger
|
|
508
|
+
);
|
|
413
509
|
return;
|
|
414
|
-
|
|
510
|
+
}
|
|
511
|
+
if (!get(newValue, dataItemsKey) || get(newValue, dataCountKey) === void 0)
|
|
512
|
+
return;
|
|
513
|
+
count.value = get(newValue, dataCountKey, 0);
|
|
514
|
+
const newItems = get(newValue, dataItemsKey);
|
|
415
515
|
itemsByPages.value = {
|
|
416
516
|
...reactiveData.page !== 1 && itemsByPages.value,
|
|
417
|
-
[reactiveData.page]:
|
|
517
|
+
[reactiveData.page]: newItems
|
|
418
518
|
};
|
|
419
|
-
onUpdated?.(
|
|
519
|
+
onUpdated?.({
|
|
520
|
+
search: reactiveData.search,
|
|
521
|
+
page: reactiveData.page,
|
|
522
|
+
selectedValue: reactiveData.selectedValue,
|
|
523
|
+
params: reactiveData.params ? JSON.parse(reactiveData.params) : void 0
|
|
524
|
+
});
|
|
420
525
|
},
|
|
421
526
|
{ immediate: true }
|
|
422
527
|
);
|
|
@@ -569,32 +674,23 @@ function useDetectBrowser(versions, userAgent) {
|
|
|
569
674
|
});
|
|
570
675
|
}
|
|
571
676
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
return false;
|
|
590
|
-
}
|
|
591
|
-
return false;
|
|
592
|
-
}
|
|
593
|
-
function useLogger(scope, options) {
|
|
594
|
-
const prefix = options?.tag ? `[${scope.toUpperCase()}.${options.tag}]:` : `[${scope.toUpperCase()}]:`;
|
|
595
|
-
const logger = logdown(prefix, { prefixColor: options?.color });
|
|
596
|
-
logger.state.isEnabled = isEnabled(options);
|
|
597
|
-
return logger;
|
|
677
|
+
const isLocked = ref(false);
|
|
678
|
+
function useGlobalScroll() {
|
|
679
|
+
const lock = () => {
|
|
680
|
+
isLocked.value = true;
|
|
681
|
+
document.body.style.paddingRight = `${window.innerWidth - document.documentElement.clientWidth}px`;
|
|
682
|
+
document.body.style.overflow = "hidden";
|
|
683
|
+
};
|
|
684
|
+
const unlock = () => {
|
|
685
|
+
isLocked.value = false;
|
|
686
|
+
document.body.style.overflow = "auto";
|
|
687
|
+
document.body.style.paddingRight = "0px";
|
|
688
|
+
};
|
|
689
|
+
return {
|
|
690
|
+
isLocked,
|
|
691
|
+
lock,
|
|
692
|
+
unlock
|
|
693
|
+
};
|
|
598
694
|
}
|
|
599
695
|
|
|
600
696
|
function validatePhone(rule, value) {
|
|
@@ -648,4 +744,4 @@ function isStrictLength(value = 3, message) {
|
|
|
648
744
|
};
|
|
649
745
|
}
|
|
650
746
|
|
|
651
|
-
export { UiComponentsResolver, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useLogger, validateNumber, validatePhone };
|
|
747
|
+
export { UiComponentsResolver, camelizeKeys, createClassName, formatMoney, genLoadingTableRows, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncMultiselect, useAsyncSelect, useClassName, useDayjs, useDetectBrowser, useGlobalScroll, useLogger, validateNumber, validatePhone };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karta.io/utils",
|
|
3
|
-
"version": "0.35.
|
|
3
|
+
"version": "0.35.2",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@karta.io/common-data": "^0.35.
|
|
33
|
+
"@karta.io/common-data": "^0.35.2",
|
|
34
34
|
"@vueuse/core": "10.2.1",
|
|
35
35
|
"async-validator": "4.2.5",
|
|
36
36
|
"dayjs": "1.11.8",
|