@karta.io/utils 0.35.5 → 1.0.0-alpha.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/README.md +1 -3
- package/dist/index.cjs +407 -18
- package/dist/index.d.ts +86 -15
- package/dist/index.mjs +405 -20
- package/package.json +10 -12
package/README.md
CHANGED
|
@@ -5,9 +5,7 @@
|
|
|
5
5
|
## Хелперы:
|
|
6
6
|
|
|
7
7
|
- camelizeKeys
|
|
8
|
-
- createClassName
|
|
9
8
|
- formatMoney
|
|
10
|
-
- genLoadingTableRows
|
|
11
9
|
- genRandomNumber
|
|
12
10
|
- genUUID
|
|
13
11
|
- getCardNumber
|
|
@@ -19,7 +17,7 @@
|
|
|
19
17
|
- snakelizeKeys
|
|
20
18
|
- unformatNumber
|
|
21
19
|
- validateNumber
|
|
22
|
-
-
|
|
20
|
+
- AppComponentsResolver
|
|
23
21
|
- UiComponentsResolver
|
|
24
22
|
|
|
25
23
|
## Хуки:
|
package/dist/index.cjs
CHANGED
|
@@ -5,8 +5,8 @@ const commonData = require('@karta.io/common-data');
|
|
|
5
5
|
const libphonenumberJs = require('libphonenumber-js');
|
|
6
6
|
const snakeCase = require('lodash/snakeCase.js');
|
|
7
7
|
const vue = require('vue');
|
|
8
|
-
const vueQuery = require('@tanstack/vue-query');
|
|
9
8
|
const core = require('@vueuse/core');
|
|
9
|
+
const vueQuery = require('@tanstack/vue-query');
|
|
10
10
|
const get = require('lodash/get.js');
|
|
11
11
|
const has = require('lodash/has.js');
|
|
12
12
|
const logdown = require('logdown');
|
|
@@ -17,6 +17,8 @@ const updateLocale = require('dayjs/plugin/updateLocale.js');
|
|
|
17
17
|
const isToday = require('dayjs/plugin/isToday.js');
|
|
18
18
|
const customParseFormat = require('dayjs/plugin/customParseFormat.js');
|
|
19
19
|
const detectBrowser = require('detect-browser');
|
|
20
|
+
const vueRouter = require('vue-router');
|
|
21
|
+
const qs = require('qs');
|
|
20
22
|
|
|
21
23
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
22
24
|
|
|
@@ -31,6 +33,17 @@ const tz__default = /*#__PURE__*/_interopDefaultCompat(tz);
|
|
|
31
33
|
const updateLocale__default = /*#__PURE__*/_interopDefaultCompat(updateLocale);
|
|
32
34
|
const isToday__default = /*#__PURE__*/_interopDefaultCompat(isToday);
|
|
33
35
|
const customParseFormat__default = /*#__PURE__*/_interopDefaultCompat(customParseFormat);
|
|
36
|
+
const qs__default = /*#__PURE__*/_interopDefaultCompat(qs);
|
|
37
|
+
|
|
38
|
+
function AppComponentsResolver(options = {}) {
|
|
39
|
+
const { prefix = "" } = options;
|
|
40
|
+
return (name) => {
|
|
41
|
+
const componentName = prefix && name.startsWith(prefix) ? name.substring(prefix.length) : name;
|
|
42
|
+
const isAppComponent = componentName.startsWith("App");
|
|
43
|
+
if (isAppComponent)
|
|
44
|
+
return { name: componentName, from: "@karta.io/app-components" };
|
|
45
|
+
};
|
|
46
|
+
}
|
|
34
47
|
|
|
35
48
|
function camelizeKeys(object) {
|
|
36
49
|
if (Array.isArray(object))
|
|
@@ -47,14 +60,6 @@ function camelizeKeys(object) {
|
|
|
47
60
|
return object;
|
|
48
61
|
}
|
|
49
62
|
|
|
50
|
-
function createClassName(className) {
|
|
51
|
-
return function f(...classNames) {
|
|
52
|
-
const result = classNames.join("");
|
|
53
|
-
const create = (...args) => args.length ? f.apply(null, [result, ...args])() : result;
|
|
54
|
-
return create;
|
|
55
|
-
}(className);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
63
|
function formatMoney(value, currency = commonData.CURRENCY_DEFAULT, options = {}) {
|
|
59
64
|
const convertedValue = Number(value);
|
|
60
65
|
if (value === null || Number.isNaN(convertedValue))
|
|
@@ -70,11 +75,6 @@ function formatMoney(value, currency = commonData.CURRENCY_DEFAULT, options = {}
|
|
|
70
75
|
return formatter.format(convertedValue);
|
|
71
76
|
}
|
|
72
77
|
|
|
73
|
-
function genLoadingTableRows(columns, limit) {
|
|
74
|
-
const row = columns.reduce((acc, curr) => ({ ...acc, [curr.key]: void 0 }), {});
|
|
75
|
-
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
76
|
-
}
|
|
77
|
-
|
|
78
78
|
function genRandomNumber(min, max) {
|
|
79
79
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
80
80
|
}
|
|
@@ -273,6 +273,50 @@ function useAbortController(callback) {
|
|
|
273
273
|
};
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
+
function useAsyncItems({
|
|
277
|
+
apiMethod,
|
|
278
|
+
queryKey,
|
|
279
|
+
params,
|
|
280
|
+
config = void 0
|
|
281
|
+
}) {
|
|
282
|
+
const getQueryKey = () => [queryKey, params];
|
|
283
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
284
|
+
const { isLoading, isFetching, isError, data, error } = vueQuery.useQuery({
|
|
285
|
+
queryKey: getQueryKey(),
|
|
286
|
+
queryFn: () => call(core.toValue(params), config)
|
|
287
|
+
});
|
|
288
|
+
const items = vue.computed(() => data.value?.results || []);
|
|
289
|
+
const count = vue.ref(0);
|
|
290
|
+
vue.watch(
|
|
291
|
+
data,
|
|
292
|
+
(newValue) => {
|
|
293
|
+
if (newValue === void 0 || newValue.count === void 0)
|
|
294
|
+
return;
|
|
295
|
+
count.value = newValue.count;
|
|
296
|
+
},
|
|
297
|
+
{ immediate: true }
|
|
298
|
+
);
|
|
299
|
+
const queryClient = vueQuery.useQueryClient();
|
|
300
|
+
const refetch = () => {
|
|
301
|
+
queryClient.invalidateQueries({ queryKey: getQueryKey() });
|
|
302
|
+
};
|
|
303
|
+
const reload = () => {
|
|
304
|
+
queryClient.resetQueries({ queryKey: getQueryKey() });
|
|
305
|
+
};
|
|
306
|
+
return {
|
|
307
|
+
isLoading,
|
|
308
|
+
isFetching,
|
|
309
|
+
isError,
|
|
310
|
+
data,
|
|
311
|
+
error,
|
|
312
|
+
items,
|
|
313
|
+
count,
|
|
314
|
+
refetch,
|
|
315
|
+
reload,
|
|
316
|
+
abort
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
|
|
276
320
|
function isEnabled(options) {
|
|
277
321
|
if (options?.debug !== void 0)
|
|
278
322
|
return options.debug;
|
|
@@ -325,6 +369,7 @@ function useAsyncMultiselect({
|
|
|
325
369
|
selectedValues: core.toValue(selectedValues),
|
|
326
370
|
params: JSON.stringify(core.toValue(params))
|
|
327
371
|
});
|
|
372
|
+
const search = vue.ref("");
|
|
328
373
|
const { call, abort } = useAbortController(apiMethod);
|
|
329
374
|
const loadWAC = () => call({
|
|
330
375
|
page: reactiveData.page,
|
|
@@ -423,6 +468,7 @@ function useAsyncMultiselect({
|
|
|
423
468
|
reactiveData.selectedValues = core.toValue(selectedValues);
|
|
424
469
|
}, delay);
|
|
425
470
|
const updateSearch = (value) => {
|
|
471
|
+
search.value = value;
|
|
426
472
|
updateSearchWithDebounce(value);
|
|
427
473
|
};
|
|
428
474
|
if (vue.isRef(selectedValues)) {
|
|
@@ -460,7 +506,7 @@ function useAsyncMultiselect({
|
|
|
460
506
|
return {
|
|
461
507
|
items,
|
|
462
508
|
options,
|
|
463
|
-
search
|
|
509
|
+
search,
|
|
464
510
|
hasNextPage,
|
|
465
511
|
isFetching,
|
|
466
512
|
isLoading,
|
|
@@ -492,6 +538,7 @@ function useAsyncSelect({
|
|
|
492
538
|
selectedValue: core.toValue(selectedValue),
|
|
493
539
|
params: JSON.stringify(core.toValue(params))
|
|
494
540
|
});
|
|
541
|
+
const search = vue.ref("");
|
|
495
542
|
const { call, abort } = useAbortController(apiMethod);
|
|
496
543
|
const loadWAC = () => call({
|
|
497
544
|
page: reactiveData.page,
|
|
@@ -567,6 +614,7 @@ function useAsyncSelect({
|
|
|
567
614
|
reactiveData.page = 1;
|
|
568
615
|
}, delay);
|
|
569
616
|
const updateSearch = (value) => {
|
|
617
|
+
search.value = value;
|
|
570
618
|
updateSearchWithDebounce(value);
|
|
571
619
|
};
|
|
572
620
|
if (vue.isRef(selectedValue)) {
|
|
@@ -587,7 +635,7 @@ function useAsyncSelect({
|
|
|
587
635
|
return {
|
|
588
636
|
items,
|
|
589
637
|
options,
|
|
590
|
-
search
|
|
638
|
+
search,
|
|
591
639
|
hasNextPage,
|
|
592
640
|
isFetching,
|
|
593
641
|
isLoading,
|
|
@@ -596,6 +644,28 @@ function useAsyncSelect({
|
|
|
596
644
|
};
|
|
597
645
|
}
|
|
598
646
|
|
|
647
|
+
function useBreakpoint() {
|
|
648
|
+
const breakpoints = core.useBreakpoints(commonData.BREAKPOINTS);
|
|
649
|
+
const isMobile = breakpoints.smaller("sm");
|
|
650
|
+
const isDesktop = breakpoints.greater("md");
|
|
651
|
+
const isGreaterXxs = breakpoints.greater("xxs");
|
|
652
|
+
const isGreaterXs = breakpoints.greater("xs");
|
|
653
|
+
const isGreaterSm = breakpoints.greater("sm");
|
|
654
|
+
const isGreaterMd = breakpoints.greater("md");
|
|
655
|
+
const isGreaterLg = breakpoints.greater("lg");
|
|
656
|
+
const isGreaterXl = breakpoints.greater("xl");
|
|
657
|
+
return {
|
|
658
|
+
isMobile,
|
|
659
|
+
isDesktop,
|
|
660
|
+
isGreaterXxs,
|
|
661
|
+
isGreaterXs,
|
|
662
|
+
isGreaterSm,
|
|
663
|
+
isGreaterMd,
|
|
664
|
+
isGreaterLg,
|
|
665
|
+
isGreaterXl
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
|
|
599
669
|
function _bem(block, blockSuffix, element, modifier, modifierValue) {
|
|
600
670
|
let cls = block;
|
|
601
671
|
if (blockSuffix)
|
|
@@ -709,6 +779,322 @@ function useGlobalScroll() {
|
|
|
709
779
|
};
|
|
710
780
|
}
|
|
711
781
|
|
|
782
|
+
const QS_STRINGIFY_PARAMS = {
|
|
783
|
+
arrayFormat: "comma",
|
|
784
|
+
encode: false,
|
|
785
|
+
skipNulls: true,
|
|
786
|
+
addQueryPrefix: true
|
|
787
|
+
};
|
|
788
|
+
const isOpen = vue.ref(false);
|
|
789
|
+
function useFilters({
|
|
790
|
+
config,
|
|
791
|
+
initialFilters
|
|
792
|
+
}) {
|
|
793
|
+
const router = vueRouter.useRouter();
|
|
794
|
+
const route = vueRouter.useRoute();
|
|
795
|
+
const parseRouteQuery = (query) => qs__default.parse(query ?? route.query);
|
|
796
|
+
const filtersFromQuery = vue.computed(() => {
|
|
797
|
+
const parsedRouteQuery = parseRouteQuery();
|
|
798
|
+
if (!parsedRouteQuery.filters)
|
|
799
|
+
return void 0;
|
|
800
|
+
return Object.entries(config).reduce((accum, [currentKey, currentValue]) => {
|
|
801
|
+
if (!parsedRouteQuery.filters[currentKey])
|
|
802
|
+
return accum;
|
|
803
|
+
if (currentValue.type === "ARRAY" /* Array */) {
|
|
804
|
+
const splitValues = parsedRouteQuery.filters[currentKey].split(",");
|
|
805
|
+
const checkedValue = currentValue?.possibleValues ? splitValues.filter((k) => currentValue?.possibleValues?.includes(k)) : splitValues;
|
|
806
|
+
if (!checkedValue?.length)
|
|
807
|
+
return accum;
|
|
808
|
+
return {
|
|
809
|
+
...accum,
|
|
810
|
+
[currentKey]: checkedValue
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
return {
|
|
814
|
+
...accum,
|
|
815
|
+
[currentKey]: parsedRouteQuery.filters[currentKey]
|
|
816
|
+
};
|
|
817
|
+
}, {});
|
|
818
|
+
});
|
|
819
|
+
const filtersCount = vue.computed(
|
|
820
|
+
() => filtersFromQuery.value ? Object.keys(filtersFromQuery.value).length : 0
|
|
821
|
+
);
|
|
822
|
+
const setQueryFromFilters = (filters) => {
|
|
823
|
+
const parsedRouteQuery = parseRouteQuery();
|
|
824
|
+
const filteredValue = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
|
|
825
|
+
if (!currentValue?.length)
|
|
826
|
+
return accum;
|
|
827
|
+
return { ...accum, [currentKey]: currentValue };
|
|
828
|
+
}, {});
|
|
829
|
+
const query = qs__default.stringify(
|
|
830
|
+
{
|
|
831
|
+
...parsedRouteQuery,
|
|
832
|
+
filters: filteredValue,
|
|
833
|
+
page: void 0
|
|
834
|
+
},
|
|
835
|
+
QS_STRINGIFY_PARAMS
|
|
836
|
+
);
|
|
837
|
+
router.push(query);
|
|
838
|
+
};
|
|
839
|
+
const getValidatedParams = (query) => {
|
|
840
|
+
const parsedRouteQuery = parseRouteQuery(query);
|
|
841
|
+
if (query?.filters) {
|
|
842
|
+
const filters = {
|
|
843
|
+
...query.filters,
|
|
844
|
+
...filtersFromQuery.value
|
|
845
|
+
};
|
|
846
|
+
return { ...parsedRouteQuery, filters };
|
|
847
|
+
}
|
|
848
|
+
return { ...parsedRouteQuery, filters: filtersFromQuery.value };
|
|
849
|
+
};
|
|
850
|
+
const open = () => {
|
|
851
|
+
isOpen.value = true;
|
|
852
|
+
};
|
|
853
|
+
const clear = () => setQueryFromFilters({});
|
|
854
|
+
const filtersModel = vue.ref({
|
|
855
|
+
...initialFilters,
|
|
856
|
+
...filtersFromQuery.value
|
|
857
|
+
});
|
|
858
|
+
const updateOne = (key, value) => {
|
|
859
|
+
setQueryFromFilters({
|
|
860
|
+
...filtersModel.value,
|
|
861
|
+
[key]: value ? String(value) : void 0
|
|
862
|
+
});
|
|
863
|
+
};
|
|
864
|
+
const updateMany = (filters) => {
|
|
865
|
+
const newFilters = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
|
|
866
|
+
return {
|
|
867
|
+
...accum,
|
|
868
|
+
[currentKey]: currentValue ? String(currentValue) : void 0
|
|
869
|
+
};
|
|
870
|
+
}, {});
|
|
871
|
+
setQueryFromFilters({
|
|
872
|
+
...filtersModel.value,
|
|
873
|
+
...newFilters
|
|
874
|
+
});
|
|
875
|
+
};
|
|
876
|
+
vue.watch(filtersFromQuery, (newFilters) => {
|
|
877
|
+
if (!newFilters) {
|
|
878
|
+
filtersModel.value = { ...initialFilters };
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
filtersModel.value = { ...initialFilters, ...newFilters };
|
|
882
|
+
});
|
|
883
|
+
return {
|
|
884
|
+
isOpen,
|
|
885
|
+
filtersModel,
|
|
886
|
+
filtersFromQuery,
|
|
887
|
+
filtersCount,
|
|
888
|
+
updateOne,
|
|
889
|
+
updateMany,
|
|
890
|
+
open,
|
|
891
|
+
clear,
|
|
892
|
+
setQueryFromFilters,
|
|
893
|
+
getValidatedParams
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
let panels = [];
|
|
898
|
+
function usePanel() {
|
|
899
|
+
let appContext;
|
|
900
|
+
const logger = useLogger("use-panel");
|
|
901
|
+
const {
|
|
902
|
+
isLocked: isGlobalScrollLocked,
|
|
903
|
+
lock: lockGlobalScroll,
|
|
904
|
+
unlock: unlockGlobalScroll
|
|
905
|
+
} = useGlobalScroll();
|
|
906
|
+
const route = vueRouter.useRoute();
|
|
907
|
+
const router = vueRouter.useRouter();
|
|
908
|
+
vue.onMounted(() => {
|
|
909
|
+
appContext = vue.getCurrentInstance()?.appContext;
|
|
910
|
+
});
|
|
911
|
+
const open = ({
|
|
912
|
+
layer = 0,
|
|
913
|
+
component,
|
|
914
|
+
props
|
|
915
|
+
}) => {
|
|
916
|
+
const uuid = genUUID();
|
|
917
|
+
const createComponent = () => {
|
|
918
|
+
const newProps = {
|
|
919
|
+
...props,
|
|
920
|
+
layer
|
|
921
|
+
};
|
|
922
|
+
const el2 = document.createElement("div");
|
|
923
|
+
const vnode2 = vue.createVNode(component, {
|
|
924
|
+
...newProps,
|
|
925
|
+
modelValue: true,
|
|
926
|
+
uuid
|
|
927
|
+
});
|
|
928
|
+
vnode2.appContext = { ...appContext };
|
|
929
|
+
if (!vnode2?.props?.["onUpdate:modelValue"]) {
|
|
930
|
+
vnode2.props["onUpdate:modelValue"] = (value) => {
|
|
931
|
+
if (vnode2 && vnode2.component) {
|
|
932
|
+
vnode2.component.props.modelValue = value;
|
|
933
|
+
if (!value && panels.length === 1 && isGlobalScrollLocked.value) {
|
|
934
|
+
unlockGlobalScroll();
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
vnode2.close = () => {
|
|
940
|
+
if (vnode2 && vnode2.component) {
|
|
941
|
+
vnode2.component.props.modelValue = false;
|
|
942
|
+
}
|
|
943
|
+
};
|
|
944
|
+
vnode2.props.onDestroy = () => {
|
|
945
|
+
vue.render(null, el2);
|
|
946
|
+
panels = panels.filter((panel) => panel.uuid !== uuid);
|
|
947
|
+
logger.info("Panels after destroy", panels);
|
|
948
|
+
};
|
|
949
|
+
const name = vnode2?.type?.name ?? "Unknown component";
|
|
950
|
+
const newPanel2 = {
|
|
951
|
+
uuid,
|
|
952
|
+
name,
|
|
953
|
+
vnode: vnode2,
|
|
954
|
+
layer
|
|
955
|
+
};
|
|
956
|
+
logger.log(`Open new panel: "${name}"`);
|
|
957
|
+
if (!isGlobalScrollLocked.value) {
|
|
958
|
+
lockGlobalScroll();
|
|
959
|
+
}
|
|
960
|
+
return {
|
|
961
|
+
vnode: vnode2,
|
|
962
|
+
el: el2,
|
|
963
|
+
newPanel: newPanel2
|
|
964
|
+
};
|
|
965
|
+
};
|
|
966
|
+
if (!panels.length) {
|
|
967
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
968
|
+
vue.render(vnode2, el2);
|
|
969
|
+
panels = [newPanel2];
|
|
970
|
+
logger.info("Panels after open", panels);
|
|
971
|
+
return uuid;
|
|
972
|
+
}
|
|
973
|
+
if (layer === 0) {
|
|
974
|
+
closeAll();
|
|
975
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
976
|
+
vue.render(vnode2, el2);
|
|
977
|
+
panels = [newPanel2];
|
|
978
|
+
logger.info("Panels after open", panels);
|
|
979
|
+
return uuid;
|
|
980
|
+
}
|
|
981
|
+
if (!panels.some((panel) => panel.layer === layer)) {
|
|
982
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
983
|
+
vue.render(vnode2, el2);
|
|
984
|
+
panels = [...panels, newPanel2];
|
|
985
|
+
logger.info("Panels after open", panels);
|
|
986
|
+
return uuid;
|
|
987
|
+
}
|
|
988
|
+
const panelOnCurrentLayer = panels.find((panel) => panel.layer === layer);
|
|
989
|
+
close(panelOnCurrentLayer?.uuid || "");
|
|
990
|
+
const { vnode, el, newPanel } = createComponent();
|
|
991
|
+
vue.render(vnode, el);
|
|
992
|
+
panels = [...panels, newPanel];
|
|
993
|
+
logger.info("Panels after open", panels);
|
|
994
|
+
return uuid;
|
|
995
|
+
};
|
|
996
|
+
const close = (uuid) => {
|
|
997
|
+
const panel = panels.find((item) => item.uuid === uuid);
|
|
998
|
+
logger.log(`Close panel: "${uuid}"`);
|
|
999
|
+
if (panels.length === 1 && isGlobalScrollLocked.value) {
|
|
1000
|
+
unlockGlobalScroll();
|
|
1001
|
+
}
|
|
1002
|
+
panel?.vnode.close();
|
|
1003
|
+
};
|
|
1004
|
+
const closeAll = () => {
|
|
1005
|
+
panels.forEach((panel) => {
|
|
1006
|
+
panel?.vnode.close();
|
|
1007
|
+
});
|
|
1008
|
+
if (isGlobalScrollLocked.value) {
|
|
1009
|
+
unlockGlobalScroll();
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
const getInstance = (uuid) => {
|
|
1013
|
+
return panels.find((item) => item.uuid === uuid)?.vnode;
|
|
1014
|
+
};
|
|
1015
|
+
const openByHash = ({
|
|
1016
|
+
layer = 0,
|
|
1017
|
+
component,
|
|
1018
|
+
props
|
|
1019
|
+
}) => {
|
|
1020
|
+
const data = {
|
|
1021
|
+
name: component.name,
|
|
1022
|
+
layer,
|
|
1023
|
+
props
|
|
1024
|
+
};
|
|
1025
|
+
router.push({
|
|
1026
|
+
name: route.name || void 0,
|
|
1027
|
+
query: route.query,
|
|
1028
|
+
hash: `#${window.btoa(JSON.stringify(data))}`
|
|
1029
|
+
});
|
|
1030
|
+
};
|
|
1031
|
+
const watchHash = ({
|
|
1032
|
+
component,
|
|
1033
|
+
events
|
|
1034
|
+
}) => {
|
|
1035
|
+
logger.info(`start watching route.hash for panel *${component.name}*`);
|
|
1036
|
+
const stopWatch = vue.watch(
|
|
1037
|
+
() => route.hash,
|
|
1038
|
+
async (newHash) => {
|
|
1039
|
+
if (!newHash) {
|
|
1040
|
+
closeAll();
|
|
1041
|
+
return;
|
|
1042
|
+
}
|
|
1043
|
+
let dataFromHash;
|
|
1044
|
+
try {
|
|
1045
|
+
dataFromHash = JSON.parse(window.atob(newHash.slice(1)));
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
logger.error("*watchHash* cant parse hash");
|
|
1048
|
+
}
|
|
1049
|
+
if (typeof dataFromHash !== "object")
|
|
1050
|
+
return;
|
|
1051
|
+
if (!dataFromHash.name) {
|
|
1052
|
+
logger.error("*watchHash* cant find component name in hash");
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
if (dataFromHash.name !== component.name)
|
|
1056
|
+
return;
|
|
1057
|
+
logger.info("*watchHash* data from hash", dataFromHash);
|
|
1058
|
+
await vue.nextTick();
|
|
1059
|
+
open({
|
|
1060
|
+
component,
|
|
1061
|
+
layer: dataFromHash.layer || 0,
|
|
1062
|
+
props: {
|
|
1063
|
+
"onUpdate:modelValue": (value) => {
|
|
1064
|
+
if (value)
|
|
1065
|
+
return;
|
|
1066
|
+
router.push({
|
|
1067
|
+
name: route.name || void 0,
|
|
1068
|
+
query: route.query,
|
|
1069
|
+
hash: void 0
|
|
1070
|
+
});
|
|
1071
|
+
},
|
|
1072
|
+
...dataFromHash.props,
|
|
1073
|
+
...events
|
|
1074
|
+
}
|
|
1075
|
+
});
|
|
1076
|
+
},
|
|
1077
|
+
{ immediate: true }
|
|
1078
|
+
);
|
|
1079
|
+
const stopWatchHash = () => {
|
|
1080
|
+
logger.info(`stop watching route.hash for panel *${component.name}*`);
|
|
1081
|
+
stopWatch();
|
|
1082
|
+
};
|
|
1083
|
+
vue.onBeforeUnmount(() => {
|
|
1084
|
+
stopWatchHash();
|
|
1085
|
+
});
|
|
1086
|
+
return stopWatchHash;
|
|
1087
|
+
};
|
|
1088
|
+
return {
|
|
1089
|
+
open,
|
|
1090
|
+
getInstance,
|
|
1091
|
+
close,
|
|
1092
|
+
closeAll,
|
|
1093
|
+
openByHash,
|
|
1094
|
+
watchHash
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
|
|
712
1098
|
function validatePhone(rule, value) {
|
|
713
1099
|
if (!value)
|
|
714
1100
|
return true;
|
|
@@ -760,11 +1146,10 @@ function isStrictLength(value = 3, message) {
|
|
|
760
1146
|
};
|
|
761
1147
|
}
|
|
762
1148
|
|
|
1149
|
+
exports.AppComponentsResolver = AppComponentsResolver;
|
|
763
1150
|
exports.UiComponentsResolver = UiComponentsResolver;
|
|
764
1151
|
exports.camelizeKeys = camelizeKeys;
|
|
765
|
-
exports.createClassName = createClassName;
|
|
766
1152
|
exports.formatMoney = formatMoney;
|
|
767
|
-
exports.genLoadingTableRows = genLoadingTableRows;
|
|
768
1153
|
exports.genRandomNumber = genRandomNumber;
|
|
769
1154
|
exports.genUUID = genUUID;
|
|
770
1155
|
exports.getCardNumber = getCardNumber;
|
|
@@ -786,12 +1171,16 @@ exports.parsePhone = parsePhone;
|
|
|
786
1171
|
exports.snakelizeKeys = snakelizeKeys;
|
|
787
1172
|
exports.unformatNumber = unformatNumber;
|
|
788
1173
|
exports.useAbortController = useAbortController;
|
|
1174
|
+
exports.useAsyncItems = useAsyncItems;
|
|
789
1175
|
exports.useAsyncMultiselect = useAsyncMultiselect;
|
|
790
1176
|
exports.useAsyncSelect = useAsyncSelect;
|
|
1177
|
+
exports.useBreakpoint = useBreakpoint;
|
|
791
1178
|
exports.useClassName = useClassName;
|
|
792
1179
|
exports.useDayjs = useDayjs;
|
|
793
1180
|
exports.useDetectBrowser = useDetectBrowser;
|
|
1181
|
+
exports.useFilters = useFilters;
|
|
794
1182
|
exports.useGlobalScroll = useGlobalScroll;
|
|
795
1183
|
exports.useLogger = useLogger;
|
|
1184
|
+
exports.usePanel = usePanel;
|
|
796
1185
|
exports.validateNumber = validateNumber;
|
|
797
1186
|
exports.validatePhone = validatePhone;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import * as vue from 'vue';
|
|
2
|
-
import { MaybeRef } from '
|
|
2
|
+
import { MaybeRef, Ref, Component, EmitsOptions, VNode } from 'vue';
|
|
3
|
+
import { AxiosRequestConfig } from 'axios';
|
|
4
|
+
export { AxiosError as HttpError, AxiosPromise as HttpPromise, AxiosRequestConfig as HttpRequestConfig, AxiosResponse as HttpResponse } from 'axios';
|
|
5
|
+
import { MaybeRef as MaybeRef$1 } from '@vueuse/core';
|
|
3
6
|
import dayjs from 'dayjs';
|
|
4
7
|
import { Browser } from 'detect-browser';
|
|
5
8
|
import logdown from 'logdown';
|
|
6
9
|
import { RuleItem } from 'async-validator';
|
|
7
10
|
|
|
11
|
+
interface AppComponentsResolverOptions {
|
|
12
|
+
prefix?: string;
|
|
13
|
+
}
|
|
14
|
+
declare function AppComponentsResolver(options?: AppComponentsResolverOptions): any;
|
|
15
|
+
|
|
8
16
|
interface Dictionary {
|
|
9
17
|
[keys: string | number]: any;
|
|
10
18
|
}
|
|
@@ -17,18 +25,11 @@ interface ResponseError {
|
|
|
17
25
|
|
|
18
26
|
declare function camelizeKeys(object: Dictionary): Dictionary;
|
|
19
27
|
|
|
20
|
-
declare function createClassName(className: string): (...classNames: string[]) => string;
|
|
21
|
-
|
|
22
28
|
interface Options$2 extends Intl.NumberFormatOptions {
|
|
23
29
|
locale?: string;
|
|
24
30
|
}
|
|
25
31
|
declare function formatMoney(value: string | number | null | undefined, currency?: string, options?: Options$2): string;
|
|
26
32
|
|
|
27
|
-
declare function genLoadingTableRows(columns: Dictionary[], limit: number): {
|
|
28
|
-
id: number | undefined;
|
|
29
|
-
key?: undefined;
|
|
30
|
-
}[];
|
|
31
|
-
|
|
32
33
|
declare function genRandomNumber(min: number, max: number): number;
|
|
33
34
|
|
|
34
35
|
declare function genUUID(): string;
|
|
@@ -106,6 +107,24 @@ declare function useAbortController<T extends (...args: any[]) => any>(callback:
|
|
|
106
107
|
isAborted(error: ResponseError): boolean;
|
|
107
108
|
};
|
|
108
109
|
|
|
110
|
+
declare function useAsyncItems<T extends (...args: any[]) => any>({ apiMethod, queryKey, params, config, }: {
|
|
111
|
+
apiMethod: T;
|
|
112
|
+
queryKey: string;
|
|
113
|
+
params: MaybeRef<Parameters<T>[0]>;
|
|
114
|
+
config?: AxiosRequestConfig;
|
|
115
|
+
}): {
|
|
116
|
+
isLoading: vue.Ref<false> | vue.Ref<true>;
|
|
117
|
+
isFetching: vue.Ref<boolean>;
|
|
118
|
+
isError: vue.Ref<false> | vue.Ref<true>;
|
|
119
|
+
data: vue.Ref<any>;
|
|
120
|
+
error: vue.Ref<unknown>;
|
|
121
|
+
items: vue.ComputedRef<Awaited<ReturnType<T>>["results"]>;
|
|
122
|
+
count: vue.Ref<number>;
|
|
123
|
+
refetch: () => void;
|
|
124
|
+
reload: () => void;
|
|
125
|
+
abort: () => void;
|
|
126
|
+
};
|
|
127
|
+
|
|
109
128
|
interface ReactiveData$1 {
|
|
110
129
|
search: string;
|
|
111
130
|
page: number;
|
|
@@ -115,8 +134,8 @@ interface ReactiveData$1 {
|
|
|
115
134
|
declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValues, params, queryKey, dataItemsKey, dataItemIdKey, dataCountKey, delay, onUpdated, }: {
|
|
116
135
|
apiMethod: T;
|
|
117
136
|
mapOptionCallback?: K;
|
|
118
|
-
selectedValues?: MaybeRef<Parameters<T>[0]['includedIds']>;
|
|
119
|
-
params?: MaybeRef<Partial<Parameters<T>[0]>>;
|
|
137
|
+
selectedValues?: MaybeRef$1<Parameters<T>[0]['includedIds']>;
|
|
138
|
+
params?: MaybeRef$1<Partial<Parameters<T>[0]>>;
|
|
120
139
|
queryKey: string;
|
|
121
140
|
dataItemsKey?: string;
|
|
122
141
|
dataItemIdKey?: string;
|
|
@@ -126,7 +145,7 @@ declare function useAsyncMultiselect<T extends (...args: any[]) => any, K extend
|
|
|
126
145
|
}): {
|
|
127
146
|
items: vue.ComputedRef<any[]>;
|
|
128
147
|
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
129
|
-
search: vue.
|
|
148
|
+
search: vue.Ref<string>;
|
|
130
149
|
hasNextPage: vue.ComputedRef<boolean>;
|
|
131
150
|
isFetching: vue.Ref<boolean>;
|
|
132
151
|
isLoading: vue.Ref<false> | vue.Ref<true>;
|
|
@@ -143,8 +162,8 @@ interface ReactiveData {
|
|
|
143
162
|
declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (params: any) => any>({ apiMethod, mapOptionCallback, selectedValue, params, queryKey, dataItemsKey, dataCountKey, delay, onUpdated, }: {
|
|
144
163
|
apiMethod: T;
|
|
145
164
|
mapOptionCallback?: K;
|
|
146
|
-
selectedValue?: MaybeRef<Parameters<T>[0]['includedIds']>;
|
|
147
|
-
params?: MaybeRef<Partial<Parameters<T>[0]>>;
|
|
165
|
+
selectedValue?: MaybeRef$1<Parameters<T>[0]['includedIds']>;
|
|
166
|
+
params?: MaybeRef$1<Partial<Parameters<T>[0]>>;
|
|
148
167
|
queryKey: string;
|
|
149
168
|
dataItemsKey?: string;
|
|
150
169
|
dataCountKey?: string;
|
|
@@ -153,7 +172,7 @@ declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (pa
|
|
|
153
172
|
}): {
|
|
154
173
|
items: vue.ComputedRef<any[]>;
|
|
155
174
|
options: vue.ComputedRef<ReturnType<K>[]>;
|
|
156
|
-
search: vue.
|
|
175
|
+
search: vue.Ref<string>;
|
|
157
176
|
hasNextPage: vue.ComputedRef<boolean>;
|
|
158
177
|
isFetching: vue.Ref<boolean>;
|
|
159
178
|
isLoading: vue.Ref<false> | vue.Ref<true>;
|
|
@@ -161,6 +180,17 @@ declare function useAsyncSelect<T extends (...args: any[]) => any, K extends (pa
|
|
|
161
180
|
updateSearch: (value: string) => void;
|
|
162
181
|
};
|
|
163
182
|
|
|
183
|
+
declare function useBreakpoint(): {
|
|
184
|
+
isMobile: vue.Ref<boolean>;
|
|
185
|
+
isDesktop: vue.Ref<boolean>;
|
|
186
|
+
isGreaterXxs: vue.Ref<boolean>;
|
|
187
|
+
isGreaterXs: vue.Ref<boolean>;
|
|
188
|
+
isGreaterSm: vue.Ref<boolean>;
|
|
189
|
+
isGreaterMd: vue.Ref<boolean>;
|
|
190
|
+
isGreaterLg: vue.Ref<boolean>;
|
|
191
|
+
isGreaterXl: vue.Ref<boolean>;
|
|
192
|
+
};
|
|
193
|
+
|
|
164
194
|
declare function useClassName(block: string): {
|
|
165
195
|
b: (blockSuffix?: string) => string;
|
|
166
196
|
e: (element?: string) => string;
|
|
@@ -200,6 +230,24 @@ declare function useGlobalScroll(): {
|
|
|
200
230
|
unlock: () => void;
|
|
201
231
|
};
|
|
202
232
|
|
|
233
|
+
declare function useFilters<T extends Record<string, any>, K extends Record<string, any>>({ config, initialFilters, }: {
|
|
234
|
+
config: T;
|
|
235
|
+
initialFilters: K;
|
|
236
|
+
}): {
|
|
237
|
+
isOpen: Ref<boolean>;
|
|
238
|
+
filtersModel: Ref<K>;
|
|
239
|
+
filtersFromQuery: vue.ComputedRef<{}>;
|
|
240
|
+
filtersCount: vue.ComputedRef<number>;
|
|
241
|
+
updateOne: (key: keyof K, value: any) => void;
|
|
242
|
+
updateMany: (filters: Record<string, any>) => void;
|
|
243
|
+
open: () => void;
|
|
244
|
+
clear: () => void;
|
|
245
|
+
setQueryFromFilters: (filters: Record<string, any>) => void;
|
|
246
|
+
getValidatedParams: (query: Record<string, any>) => {
|
|
247
|
+
filters: any;
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
|
|
203
251
|
/**
|
|
204
252
|
* @see https://github.com/caiogondim/logdown.js#readme
|
|
205
253
|
*/
|
|
@@ -211,6 +259,29 @@ interface Options {
|
|
|
211
259
|
}
|
|
212
260
|
declare function useLogger(scope: string, options?: Options): logdown.Logger;
|
|
213
261
|
|
|
262
|
+
interface VNodeCustom extends VNode {
|
|
263
|
+
close(): void;
|
|
264
|
+
}
|
|
265
|
+
declare function usePanel(): {
|
|
266
|
+
open: ({ layer, component, props, }: {
|
|
267
|
+
component: Component;
|
|
268
|
+
props?: any;
|
|
269
|
+
layer?: number;
|
|
270
|
+
}) => string;
|
|
271
|
+
getInstance: (uuid: string) => VNodeCustom;
|
|
272
|
+
close: (uuid: string) => void;
|
|
273
|
+
closeAll: () => void;
|
|
274
|
+
openByHash: ({ layer, component, props, }: {
|
|
275
|
+
component: Component;
|
|
276
|
+
props?: any;
|
|
277
|
+
layer?: number;
|
|
278
|
+
}) => void;
|
|
279
|
+
watchHash: ({ component, events, }: {
|
|
280
|
+
component: Component;
|
|
281
|
+
events?: EmitsOptions;
|
|
282
|
+
}) => any;
|
|
283
|
+
};
|
|
284
|
+
|
|
214
285
|
declare function validatePhone(rule: string, value: string): boolean;
|
|
215
286
|
|
|
216
287
|
declare function isRequired(value?: boolean, message?: string): RuleItem;
|
|
@@ -227,4 +298,4 @@ declare function isTypeUrl(message?: string): RuleItem;
|
|
|
227
298
|
declare function isTypeNumber(value?: number, message?: string): RuleItem;
|
|
228
299
|
declare function isStrictLength(value?: number, message?: string): RuleItem;
|
|
229
300
|
|
|
230
|
-
export { BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys,
|
|
301
|
+
export { AppComponentsResolver, BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import camelCase from 'lodash/camelCase.js';
|
|
2
|
-
import { CURRENCY_DEFAULT } from '@karta.io/common-data';
|
|
2
|
+
import { CURRENCY_DEFAULT, BREAKPOINTS } from '@karta.io/common-data';
|
|
3
3
|
import { parsePhoneNumberFromString } from 'libphonenumber-js';
|
|
4
4
|
import snakeCase from 'lodash/snakeCase.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
5
|
+
import { computed, ref, watch, reactive, isRef, nextTick, onBeforeUnmount, onMounted, getCurrentInstance, render, createVNode } from 'vue';
|
|
6
|
+
import { toValue, useDebounceFn, useBreakpoints } from '@vueuse/core';
|
|
7
|
+
import { useQuery, useQueryClient } from '@tanstack/vue-query';
|
|
8
8
|
import get from 'lodash/get.js';
|
|
9
9
|
import has from 'lodash/has.js';
|
|
10
10
|
import logdown from 'logdown';
|
|
@@ -15,6 +15,18 @@ import updateLocale from 'dayjs/plugin/updateLocale.js';
|
|
|
15
15
|
import isToday from 'dayjs/plugin/isToday.js';
|
|
16
16
|
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
17
17
|
import { detect } from 'detect-browser';
|
|
18
|
+
import { useRouter, useRoute } from 'vue-router';
|
|
19
|
+
import qs from 'qs';
|
|
20
|
+
|
|
21
|
+
function AppComponentsResolver(options = {}) {
|
|
22
|
+
const { prefix = "" } = options;
|
|
23
|
+
return (name) => {
|
|
24
|
+
const componentName = prefix && name.startsWith(prefix) ? name.substring(prefix.length) : name;
|
|
25
|
+
const isAppComponent = componentName.startsWith("App");
|
|
26
|
+
if (isAppComponent)
|
|
27
|
+
return { name: componentName, from: "@karta.io/app-components" };
|
|
28
|
+
};
|
|
29
|
+
}
|
|
18
30
|
|
|
19
31
|
function camelizeKeys(object) {
|
|
20
32
|
if (Array.isArray(object))
|
|
@@ -31,14 +43,6 @@ function camelizeKeys(object) {
|
|
|
31
43
|
return object;
|
|
32
44
|
}
|
|
33
45
|
|
|
34
|
-
function createClassName(className) {
|
|
35
|
-
return function f(...classNames) {
|
|
36
|
-
const result = classNames.join("");
|
|
37
|
-
const create = (...args) => args.length ? f.apply(null, [result, ...args])() : result;
|
|
38
|
-
return create;
|
|
39
|
-
}(className);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
46
|
function formatMoney(value, currency = CURRENCY_DEFAULT, options = {}) {
|
|
43
47
|
const convertedValue = Number(value);
|
|
44
48
|
if (value === null || Number.isNaN(convertedValue))
|
|
@@ -54,11 +58,6 @@ function formatMoney(value, currency = CURRENCY_DEFAULT, options = {}) {
|
|
|
54
58
|
return formatter.format(convertedValue);
|
|
55
59
|
}
|
|
56
60
|
|
|
57
|
-
function genLoadingTableRows(columns, limit) {
|
|
58
|
-
const row = columns.reduce((acc, curr) => ({ ...acc, [curr.key]: void 0 }), {});
|
|
59
|
-
return Array.from(Array(limit).keys()).map((item) => ({ id: item, ...row }));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
61
|
function genRandomNumber(min, max) {
|
|
63
62
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
64
63
|
}
|
|
@@ -257,6 +256,50 @@ function useAbortController(callback) {
|
|
|
257
256
|
};
|
|
258
257
|
}
|
|
259
258
|
|
|
259
|
+
function useAsyncItems({
|
|
260
|
+
apiMethod,
|
|
261
|
+
queryKey,
|
|
262
|
+
params,
|
|
263
|
+
config = void 0
|
|
264
|
+
}) {
|
|
265
|
+
const getQueryKey = () => [queryKey, params];
|
|
266
|
+
const { call, abort } = useAbortController(apiMethod);
|
|
267
|
+
const { isLoading, isFetching, isError, data, error } = useQuery({
|
|
268
|
+
queryKey: getQueryKey(),
|
|
269
|
+
queryFn: () => call(toValue(params), config)
|
|
270
|
+
});
|
|
271
|
+
const items = computed(() => data.value?.results || []);
|
|
272
|
+
const count = ref(0);
|
|
273
|
+
watch(
|
|
274
|
+
data,
|
|
275
|
+
(newValue) => {
|
|
276
|
+
if (newValue === void 0 || newValue.count === void 0)
|
|
277
|
+
return;
|
|
278
|
+
count.value = newValue.count;
|
|
279
|
+
},
|
|
280
|
+
{ immediate: true }
|
|
281
|
+
);
|
|
282
|
+
const queryClient = useQueryClient();
|
|
283
|
+
const refetch = () => {
|
|
284
|
+
queryClient.invalidateQueries({ queryKey: getQueryKey() });
|
|
285
|
+
};
|
|
286
|
+
const reload = () => {
|
|
287
|
+
queryClient.resetQueries({ queryKey: getQueryKey() });
|
|
288
|
+
};
|
|
289
|
+
return {
|
|
290
|
+
isLoading,
|
|
291
|
+
isFetching,
|
|
292
|
+
isError,
|
|
293
|
+
data,
|
|
294
|
+
error,
|
|
295
|
+
items,
|
|
296
|
+
count,
|
|
297
|
+
refetch,
|
|
298
|
+
reload,
|
|
299
|
+
abort
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
260
303
|
function isEnabled(options) {
|
|
261
304
|
if (options?.debug !== void 0)
|
|
262
305
|
return options.debug;
|
|
@@ -309,6 +352,7 @@ function useAsyncMultiselect({
|
|
|
309
352
|
selectedValues: toValue(selectedValues),
|
|
310
353
|
params: JSON.stringify(toValue(params))
|
|
311
354
|
});
|
|
355
|
+
const search = ref("");
|
|
312
356
|
const { call, abort } = useAbortController(apiMethod);
|
|
313
357
|
const loadWAC = () => call({
|
|
314
358
|
page: reactiveData.page,
|
|
@@ -407,6 +451,7 @@ function useAsyncMultiselect({
|
|
|
407
451
|
reactiveData.selectedValues = toValue(selectedValues);
|
|
408
452
|
}, delay);
|
|
409
453
|
const updateSearch = (value) => {
|
|
454
|
+
search.value = value;
|
|
410
455
|
updateSearchWithDebounce(value);
|
|
411
456
|
};
|
|
412
457
|
if (isRef(selectedValues)) {
|
|
@@ -444,7 +489,7 @@ function useAsyncMultiselect({
|
|
|
444
489
|
return {
|
|
445
490
|
items,
|
|
446
491
|
options,
|
|
447
|
-
search
|
|
492
|
+
search,
|
|
448
493
|
hasNextPage,
|
|
449
494
|
isFetching,
|
|
450
495
|
isLoading,
|
|
@@ -476,6 +521,7 @@ function useAsyncSelect({
|
|
|
476
521
|
selectedValue: toValue(selectedValue),
|
|
477
522
|
params: JSON.stringify(toValue(params))
|
|
478
523
|
});
|
|
524
|
+
const search = ref("");
|
|
479
525
|
const { call, abort } = useAbortController(apiMethod);
|
|
480
526
|
const loadWAC = () => call({
|
|
481
527
|
page: reactiveData.page,
|
|
@@ -551,6 +597,7 @@ function useAsyncSelect({
|
|
|
551
597
|
reactiveData.page = 1;
|
|
552
598
|
}, delay);
|
|
553
599
|
const updateSearch = (value) => {
|
|
600
|
+
search.value = value;
|
|
554
601
|
updateSearchWithDebounce(value);
|
|
555
602
|
};
|
|
556
603
|
if (isRef(selectedValue)) {
|
|
@@ -571,7 +618,7 @@ function useAsyncSelect({
|
|
|
571
618
|
return {
|
|
572
619
|
items,
|
|
573
620
|
options,
|
|
574
|
-
search
|
|
621
|
+
search,
|
|
575
622
|
hasNextPage,
|
|
576
623
|
isFetching,
|
|
577
624
|
isLoading,
|
|
@@ -580,6 +627,28 @@ function useAsyncSelect({
|
|
|
580
627
|
};
|
|
581
628
|
}
|
|
582
629
|
|
|
630
|
+
function useBreakpoint() {
|
|
631
|
+
const breakpoints = useBreakpoints(BREAKPOINTS);
|
|
632
|
+
const isMobile = breakpoints.smaller("sm");
|
|
633
|
+
const isDesktop = breakpoints.greater("md");
|
|
634
|
+
const isGreaterXxs = breakpoints.greater("xxs");
|
|
635
|
+
const isGreaterXs = breakpoints.greater("xs");
|
|
636
|
+
const isGreaterSm = breakpoints.greater("sm");
|
|
637
|
+
const isGreaterMd = breakpoints.greater("md");
|
|
638
|
+
const isGreaterLg = breakpoints.greater("lg");
|
|
639
|
+
const isGreaterXl = breakpoints.greater("xl");
|
|
640
|
+
return {
|
|
641
|
+
isMobile,
|
|
642
|
+
isDesktop,
|
|
643
|
+
isGreaterXxs,
|
|
644
|
+
isGreaterXs,
|
|
645
|
+
isGreaterSm,
|
|
646
|
+
isGreaterMd,
|
|
647
|
+
isGreaterLg,
|
|
648
|
+
isGreaterXl
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
|
|
583
652
|
function _bem(block, blockSuffix, element, modifier, modifierValue) {
|
|
584
653
|
let cls = block;
|
|
585
654
|
if (blockSuffix)
|
|
@@ -693,6 +762,322 @@ function useGlobalScroll() {
|
|
|
693
762
|
};
|
|
694
763
|
}
|
|
695
764
|
|
|
765
|
+
const QS_STRINGIFY_PARAMS = {
|
|
766
|
+
arrayFormat: "comma",
|
|
767
|
+
encode: false,
|
|
768
|
+
skipNulls: true,
|
|
769
|
+
addQueryPrefix: true
|
|
770
|
+
};
|
|
771
|
+
const isOpen = ref(false);
|
|
772
|
+
function useFilters({
|
|
773
|
+
config,
|
|
774
|
+
initialFilters
|
|
775
|
+
}) {
|
|
776
|
+
const router = useRouter();
|
|
777
|
+
const route = useRoute();
|
|
778
|
+
const parseRouteQuery = (query) => qs.parse(query ?? route.query);
|
|
779
|
+
const filtersFromQuery = computed(() => {
|
|
780
|
+
const parsedRouteQuery = parseRouteQuery();
|
|
781
|
+
if (!parsedRouteQuery.filters)
|
|
782
|
+
return void 0;
|
|
783
|
+
return Object.entries(config).reduce((accum, [currentKey, currentValue]) => {
|
|
784
|
+
if (!parsedRouteQuery.filters[currentKey])
|
|
785
|
+
return accum;
|
|
786
|
+
if (currentValue.type === "ARRAY" /* Array */) {
|
|
787
|
+
const splitValues = parsedRouteQuery.filters[currentKey].split(",");
|
|
788
|
+
const checkedValue = currentValue?.possibleValues ? splitValues.filter((k) => currentValue?.possibleValues?.includes(k)) : splitValues;
|
|
789
|
+
if (!checkedValue?.length)
|
|
790
|
+
return accum;
|
|
791
|
+
return {
|
|
792
|
+
...accum,
|
|
793
|
+
[currentKey]: checkedValue
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
...accum,
|
|
798
|
+
[currentKey]: parsedRouteQuery.filters[currentKey]
|
|
799
|
+
};
|
|
800
|
+
}, {});
|
|
801
|
+
});
|
|
802
|
+
const filtersCount = computed(
|
|
803
|
+
() => filtersFromQuery.value ? Object.keys(filtersFromQuery.value).length : 0
|
|
804
|
+
);
|
|
805
|
+
const setQueryFromFilters = (filters) => {
|
|
806
|
+
const parsedRouteQuery = parseRouteQuery();
|
|
807
|
+
const filteredValue = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
|
|
808
|
+
if (!currentValue?.length)
|
|
809
|
+
return accum;
|
|
810
|
+
return { ...accum, [currentKey]: currentValue };
|
|
811
|
+
}, {});
|
|
812
|
+
const query = qs.stringify(
|
|
813
|
+
{
|
|
814
|
+
...parsedRouteQuery,
|
|
815
|
+
filters: filteredValue,
|
|
816
|
+
page: void 0
|
|
817
|
+
},
|
|
818
|
+
QS_STRINGIFY_PARAMS
|
|
819
|
+
);
|
|
820
|
+
router.push(query);
|
|
821
|
+
};
|
|
822
|
+
const getValidatedParams = (query) => {
|
|
823
|
+
const parsedRouteQuery = parseRouteQuery(query);
|
|
824
|
+
if (query?.filters) {
|
|
825
|
+
const filters = {
|
|
826
|
+
...query.filters,
|
|
827
|
+
...filtersFromQuery.value
|
|
828
|
+
};
|
|
829
|
+
return { ...parsedRouteQuery, filters };
|
|
830
|
+
}
|
|
831
|
+
return { ...parsedRouteQuery, filters: filtersFromQuery.value };
|
|
832
|
+
};
|
|
833
|
+
const open = () => {
|
|
834
|
+
isOpen.value = true;
|
|
835
|
+
};
|
|
836
|
+
const clear = () => setQueryFromFilters({});
|
|
837
|
+
const filtersModel = ref({
|
|
838
|
+
...initialFilters,
|
|
839
|
+
...filtersFromQuery.value
|
|
840
|
+
});
|
|
841
|
+
const updateOne = (key, value) => {
|
|
842
|
+
setQueryFromFilters({
|
|
843
|
+
...filtersModel.value,
|
|
844
|
+
[key]: value ? String(value) : void 0
|
|
845
|
+
});
|
|
846
|
+
};
|
|
847
|
+
const updateMany = (filters) => {
|
|
848
|
+
const newFilters = Object.entries(filters).reduce((accum, [currentKey, currentValue]) => {
|
|
849
|
+
return {
|
|
850
|
+
...accum,
|
|
851
|
+
[currentKey]: currentValue ? String(currentValue) : void 0
|
|
852
|
+
};
|
|
853
|
+
}, {});
|
|
854
|
+
setQueryFromFilters({
|
|
855
|
+
...filtersModel.value,
|
|
856
|
+
...newFilters
|
|
857
|
+
});
|
|
858
|
+
};
|
|
859
|
+
watch(filtersFromQuery, (newFilters) => {
|
|
860
|
+
if (!newFilters) {
|
|
861
|
+
filtersModel.value = { ...initialFilters };
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
filtersModel.value = { ...initialFilters, ...newFilters };
|
|
865
|
+
});
|
|
866
|
+
return {
|
|
867
|
+
isOpen,
|
|
868
|
+
filtersModel,
|
|
869
|
+
filtersFromQuery,
|
|
870
|
+
filtersCount,
|
|
871
|
+
updateOne,
|
|
872
|
+
updateMany,
|
|
873
|
+
open,
|
|
874
|
+
clear,
|
|
875
|
+
setQueryFromFilters,
|
|
876
|
+
getValidatedParams
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
let panels = [];
|
|
881
|
+
function usePanel() {
|
|
882
|
+
let appContext;
|
|
883
|
+
const logger = useLogger("use-panel");
|
|
884
|
+
const {
|
|
885
|
+
isLocked: isGlobalScrollLocked,
|
|
886
|
+
lock: lockGlobalScroll,
|
|
887
|
+
unlock: unlockGlobalScroll
|
|
888
|
+
} = useGlobalScroll();
|
|
889
|
+
const route = useRoute();
|
|
890
|
+
const router = useRouter();
|
|
891
|
+
onMounted(() => {
|
|
892
|
+
appContext = getCurrentInstance()?.appContext;
|
|
893
|
+
});
|
|
894
|
+
const open = ({
|
|
895
|
+
layer = 0,
|
|
896
|
+
component,
|
|
897
|
+
props
|
|
898
|
+
}) => {
|
|
899
|
+
const uuid = genUUID();
|
|
900
|
+
const createComponent = () => {
|
|
901
|
+
const newProps = {
|
|
902
|
+
...props,
|
|
903
|
+
layer
|
|
904
|
+
};
|
|
905
|
+
const el2 = document.createElement("div");
|
|
906
|
+
const vnode2 = createVNode(component, {
|
|
907
|
+
...newProps,
|
|
908
|
+
modelValue: true,
|
|
909
|
+
uuid
|
|
910
|
+
});
|
|
911
|
+
vnode2.appContext = { ...appContext };
|
|
912
|
+
if (!vnode2?.props?.["onUpdate:modelValue"]) {
|
|
913
|
+
vnode2.props["onUpdate:modelValue"] = (value) => {
|
|
914
|
+
if (vnode2 && vnode2.component) {
|
|
915
|
+
vnode2.component.props.modelValue = value;
|
|
916
|
+
if (!value && panels.length === 1 && isGlobalScrollLocked.value) {
|
|
917
|
+
unlockGlobalScroll();
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
vnode2.close = () => {
|
|
923
|
+
if (vnode2 && vnode2.component) {
|
|
924
|
+
vnode2.component.props.modelValue = false;
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
vnode2.props.onDestroy = () => {
|
|
928
|
+
render(null, el2);
|
|
929
|
+
panels = panels.filter((panel) => panel.uuid !== uuid);
|
|
930
|
+
logger.info("Panels after destroy", panels);
|
|
931
|
+
};
|
|
932
|
+
const name = vnode2?.type?.name ?? "Unknown component";
|
|
933
|
+
const newPanel2 = {
|
|
934
|
+
uuid,
|
|
935
|
+
name,
|
|
936
|
+
vnode: vnode2,
|
|
937
|
+
layer
|
|
938
|
+
};
|
|
939
|
+
logger.log(`Open new panel: "${name}"`);
|
|
940
|
+
if (!isGlobalScrollLocked.value) {
|
|
941
|
+
lockGlobalScroll();
|
|
942
|
+
}
|
|
943
|
+
return {
|
|
944
|
+
vnode: vnode2,
|
|
945
|
+
el: el2,
|
|
946
|
+
newPanel: newPanel2
|
|
947
|
+
};
|
|
948
|
+
};
|
|
949
|
+
if (!panels.length) {
|
|
950
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
951
|
+
render(vnode2, el2);
|
|
952
|
+
panels = [newPanel2];
|
|
953
|
+
logger.info("Panels after open", panels);
|
|
954
|
+
return uuid;
|
|
955
|
+
}
|
|
956
|
+
if (layer === 0) {
|
|
957
|
+
closeAll();
|
|
958
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
959
|
+
render(vnode2, el2);
|
|
960
|
+
panels = [newPanel2];
|
|
961
|
+
logger.info("Panels after open", panels);
|
|
962
|
+
return uuid;
|
|
963
|
+
}
|
|
964
|
+
if (!panels.some((panel) => panel.layer === layer)) {
|
|
965
|
+
const { vnode: vnode2, el: el2, newPanel: newPanel2 } = createComponent();
|
|
966
|
+
render(vnode2, el2);
|
|
967
|
+
panels = [...panels, newPanel2];
|
|
968
|
+
logger.info("Panels after open", panels);
|
|
969
|
+
return uuid;
|
|
970
|
+
}
|
|
971
|
+
const panelOnCurrentLayer = panels.find((panel) => panel.layer === layer);
|
|
972
|
+
close(panelOnCurrentLayer?.uuid || "");
|
|
973
|
+
const { vnode, el, newPanel } = createComponent();
|
|
974
|
+
render(vnode, el);
|
|
975
|
+
panels = [...panels, newPanel];
|
|
976
|
+
logger.info("Panels after open", panels);
|
|
977
|
+
return uuid;
|
|
978
|
+
};
|
|
979
|
+
const close = (uuid) => {
|
|
980
|
+
const panel = panels.find((item) => item.uuid === uuid);
|
|
981
|
+
logger.log(`Close panel: "${uuid}"`);
|
|
982
|
+
if (panels.length === 1 && isGlobalScrollLocked.value) {
|
|
983
|
+
unlockGlobalScroll();
|
|
984
|
+
}
|
|
985
|
+
panel?.vnode.close();
|
|
986
|
+
};
|
|
987
|
+
const closeAll = () => {
|
|
988
|
+
panels.forEach((panel) => {
|
|
989
|
+
panel?.vnode.close();
|
|
990
|
+
});
|
|
991
|
+
if (isGlobalScrollLocked.value) {
|
|
992
|
+
unlockGlobalScroll();
|
|
993
|
+
}
|
|
994
|
+
};
|
|
995
|
+
const getInstance = (uuid) => {
|
|
996
|
+
return panels.find((item) => item.uuid === uuid)?.vnode;
|
|
997
|
+
};
|
|
998
|
+
const openByHash = ({
|
|
999
|
+
layer = 0,
|
|
1000
|
+
component,
|
|
1001
|
+
props
|
|
1002
|
+
}) => {
|
|
1003
|
+
const data = {
|
|
1004
|
+
name: component.name,
|
|
1005
|
+
layer,
|
|
1006
|
+
props
|
|
1007
|
+
};
|
|
1008
|
+
router.push({
|
|
1009
|
+
name: route.name || void 0,
|
|
1010
|
+
query: route.query,
|
|
1011
|
+
hash: `#${window.btoa(JSON.stringify(data))}`
|
|
1012
|
+
});
|
|
1013
|
+
};
|
|
1014
|
+
const watchHash = ({
|
|
1015
|
+
component,
|
|
1016
|
+
events
|
|
1017
|
+
}) => {
|
|
1018
|
+
logger.info(`start watching route.hash for panel *${component.name}*`);
|
|
1019
|
+
const stopWatch = watch(
|
|
1020
|
+
() => route.hash,
|
|
1021
|
+
async (newHash) => {
|
|
1022
|
+
if (!newHash) {
|
|
1023
|
+
closeAll();
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
let dataFromHash;
|
|
1027
|
+
try {
|
|
1028
|
+
dataFromHash = JSON.parse(window.atob(newHash.slice(1)));
|
|
1029
|
+
} catch (error) {
|
|
1030
|
+
logger.error("*watchHash* cant parse hash");
|
|
1031
|
+
}
|
|
1032
|
+
if (typeof dataFromHash !== "object")
|
|
1033
|
+
return;
|
|
1034
|
+
if (!dataFromHash.name) {
|
|
1035
|
+
logger.error("*watchHash* cant find component name in hash");
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
if (dataFromHash.name !== component.name)
|
|
1039
|
+
return;
|
|
1040
|
+
logger.info("*watchHash* data from hash", dataFromHash);
|
|
1041
|
+
await nextTick();
|
|
1042
|
+
open({
|
|
1043
|
+
component,
|
|
1044
|
+
layer: dataFromHash.layer || 0,
|
|
1045
|
+
props: {
|
|
1046
|
+
"onUpdate:modelValue": (value) => {
|
|
1047
|
+
if (value)
|
|
1048
|
+
return;
|
|
1049
|
+
router.push({
|
|
1050
|
+
name: route.name || void 0,
|
|
1051
|
+
query: route.query,
|
|
1052
|
+
hash: void 0
|
|
1053
|
+
});
|
|
1054
|
+
},
|
|
1055
|
+
...dataFromHash.props,
|
|
1056
|
+
...events
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
},
|
|
1060
|
+
{ immediate: true }
|
|
1061
|
+
);
|
|
1062
|
+
const stopWatchHash = () => {
|
|
1063
|
+
logger.info(`stop watching route.hash for panel *${component.name}*`);
|
|
1064
|
+
stopWatch();
|
|
1065
|
+
};
|
|
1066
|
+
onBeforeUnmount(() => {
|
|
1067
|
+
stopWatchHash();
|
|
1068
|
+
});
|
|
1069
|
+
return stopWatchHash;
|
|
1070
|
+
};
|
|
1071
|
+
return {
|
|
1072
|
+
open,
|
|
1073
|
+
getInstance,
|
|
1074
|
+
close,
|
|
1075
|
+
closeAll,
|
|
1076
|
+
openByHash,
|
|
1077
|
+
watchHash
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
|
|
696
1081
|
function validatePhone(rule, value) {
|
|
697
1082
|
if (!value)
|
|
698
1083
|
return true;
|
|
@@ -744,4 +1129,4 @@ function isStrictLength(value = 3, message) {
|
|
|
744
1129
|
};
|
|
745
1130
|
}
|
|
746
1131
|
|
|
747
|
-
export { UiComponentsResolver, camelizeKeys,
|
|
1132
|
+
export { AppComponentsResolver, UiComponentsResolver, camelizeKeys, formatMoney, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karta.io/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"description": "karta.io utils for admin and application",
|
|
5
5
|
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -21,23 +21,21 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@tanstack/vue-query": "4.x.x",
|
|
25
24
|
"vue": "3.3.x"
|
|
26
25
|
},
|
|
27
|
-
"peerDependenciesMeta": {
|
|
28
|
-
"@tanstack/vue-query": {
|
|
29
|
-
"optional": true
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
26
|
"dependencies": {
|
|
33
|
-
"@karta.io/common-data": "^0.
|
|
34
|
-
"@
|
|
27
|
+
"@karta.io/common-data": "^1.0.0-alpha.1",
|
|
28
|
+
"@tanstack/vue-query": "4.33.1",
|
|
29
|
+
"@vueuse/core": "10.3.0",
|
|
35
30
|
"async-validator": "4.2.5",
|
|
31
|
+
"axios": "1.4.0",
|
|
36
32
|
"dayjs": "1.11.8",
|
|
37
33
|
"detect-browser": "5.3.0",
|
|
38
34
|
"libphonenumber-js": "1.10.36",
|
|
39
35
|
"lodash": "4.17.21",
|
|
40
|
-
"logdown": "3.3.1"
|
|
36
|
+
"logdown": "3.3.1",
|
|
37
|
+
"qs": "6.11.2",
|
|
38
|
+
"vue-router": "4.x.x"
|
|
41
39
|
},
|
|
42
40
|
"devDependencies": {
|
|
43
41
|
"@qvant/prettier-config": "0.1.0",
|
|
@@ -47,8 +45,8 @@
|
|
|
47
45
|
"eslint": "8.43.0",
|
|
48
46
|
"eslint-config-airbnb-base": "15.0.0",
|
|
49
47
|
"eslint-plugin-import": "2.27.5",
|
|
50
|
-
"prettier": "
|
|
51
|
-
"typescript": "5.
|
|
48
|
+
"prettier": "3.0.1",
|
|
49
|
+
"typescript": "5.1.6",
|
|
52
50
|
"unbuild": "1.2.1",
|
|
53
51
|
"vitest": "0.32.2",
|
|
54
52
|
"vue": "3.3.4"
|