@karta.io/utils 1.0.0-alpha.7 → 1.0.0-alpha.8

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 CHANGED
@@ -826,6 +826,219 @@ function useGlobalScroll() {
826
826
  };
827
827
  }
828
828
 
829
+ const CONFIRM_TEXT = "Are you sure you want to close? \nAll changes will be lost.";
830
+ let dialogs = [];
831
+ function useDialog() {
832
+ let appContext;
833
+ const logger = useLogger("use-dialog");
834
+ const {
835
+ isLocked: isGlobalScrollLocked,
836
+ lock: lockGlobalScroll,
837
+ unlock: unlockGlobalScroll
838
+ } = useGlobalScroll();
839
+ const route = vueRouter.useRoute();
840
+ const router = vueRouter.useRouter();
841
+ vue.onMounted(() => {
842
+ appContext = vue.getCurrentInstance()?.appContext;
843
+ });
844
+ const open = ({
845
+ layer = 0,
846
+ component,
847
+ props
848
+ }) => {
849
+ const uuid = genUUID();
850
+ const createComponent = () => {
851
+ const newProps = {
852
+ ...props,
853
+ layer
854
+ };
855
+ const el2 = document.createElement("div");
856
+ const vnode2 = vue.createVNode(component, {
857
+ ...newProps,
858
+ modelValue: true,
859
+ uuid
860
+ });
861
+ vnode2.appContext = { ...appContext };
862
+ if (!vnode2?.props?.["onUpdate:modelValue"]) {
863
+ vnode2.props["onUpdate:modelValue"] = (value) => {
864
+ if (vnode2 && vnode2.component) {
865
+ vnode2.component.props.modelValue = value;
866
+ if (!value && dialogs.length === 1 && isGlobalScrollLocked.value) {
867
+ unlockGlobalScroll();
868
+ }
869
+ }
870
+ };
871
+ }
872
+ vnode2.close = () => {
873
+ if (vnode2 && vnode2.component) {
874
+ vnode2.component.props.modelValue = false;
875
+ }
876
+ };
877
+ vnode2.props.onDestroy = () => {
878
+ vue.render(null, el2);
879
+ dialogs = dialogs.filter((dialog) => dialog.uuid !== uuid);
880
+ logger.info("Dialogs after destroy", dialogs);
881
+ };
882
+ const name = vnode2?.type?.name ?? "Unknown component";
883
+ const newDialog2 = {
884
+ uuid,
885
+ name,
886
+ vnode: vnode2,
887
+ layer
888
+ };
889
+ logger.log(`Open new dialog: "${name}"`);
890
+ if (!isGlobalScrollLocked.value) {
891
+ lockGlobalScroll();
892
+ }
893
+ return {
894
+ vnode: vnode2,
895
+ el: el2,
896
+ newDialog: newDialog2
897
+ };
898
+ };
899
+ if (!dialogs.length) {
900
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
901
+ vue.render(vnode2, el2);
902
+ dialogs = [newDialog2];
903
+ logger.info("Dialogs after open", dialogs);
904
+ return uuid;
905
+ }
906
+ if (layer === 0) {
907
+ closeAll();
908
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
909
+ vue.render(vnode2, el2);
910
+ dialogs = [newDialog2];
911
+ logger.info("Dialogs after open", dialogs);
912
+ return uuid;
913
+ }
914
+ if (!dialogs.some((dialog) => dialog.layer === layer)) {
915
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
916
+ vue.render(vnode2, el2);
917
+ dialogs = [...dialogs, newDialog2];
918
+ logger.info("Dialogs after open", dialogs);
919
+ return uuid;
920
+ }
921
+ const dialogOnCurrentLayer = dialogs.find((dialog) => dialog.layer === layer);
922
+ close(dialogOnCurrentLayer?.uuid || "");
923
+ const { vnode, el, newDialog } = createComponent();
924
+ vue.render(vnode, el);
925
+ dialogs = [...dialogs, newDialog];
926
+ logger.info("Dialogs after open", dialogs);
927
+ return uuid;
928
+ };
929
+ const close = (uuid) => {
930
+ const dialog = dialogs.find((item) => item.uuid === uuid);
931
+ logger.log(`Close dialog: "${uuid}"`);
932
+ if (dialogs.length === 1 && isGlobalScrollLocked.value) {
933
+ unlockGlobalScroll();
934
+ }
935
+ dialog?.vnode.close();
936
+ };
937
+ const closeAll = () => {
938
+ dialogs.forEach((dialog) => {
939
+ dialog?.vnode.close();
940
+ });
941
+ if (isGlobalScrollLocked.value) {
942
+ unlockGlobalScroll();
943
+ }
944
+ };
945
+ const getInstance = (uuid) => {
946
+ return dialogs.find((item) => item.uuid === uuid)?.vnode;
947
+ };
948
+ const isOnTop = (uuid) => {
949
+ if (!dialogs.length)
950
+ return false;
951
+ const dialogsSorted = [...dialogs].sort((a, b) => b.layer - a.layer);
952
+ return dialogsSorted[0].uuid === uuid;
953
+ };
954
+ const showConfirm = (text = CONFIRM_TEXT) => {
955
+ return window.confirm(text);
956
+ };
957
+ const openByHash = ({
958
+ layer = 0,
959
+ component,
960
+ props
961
+ }) => {
962
+ const data = {
963
+ name: component.name,
964
+ layer,
965
+ props
966
+ };
967
+ router.push({
968
+ name: route.name || void 0,
969
+ query: route.query,
970
+ hash: `#${window.btoa(JSON.stringify(data))}`
971
+ });
972
+ };
973
+ const watchHash = ({
974
+ component,
975
+ events
976
+ }) => {
977
+ logger.info(`start watching route.hash for dialog *${component.name}*`);
978
+ const stopWatch = vue.watch(
979
+ () => route.hash,
980
+ async (newHash) => {
981
+ if (!newHash) {
982
+ closeAll();
983
+ return;
984
+ }
985
+ let dataFromHash;
986
+ try {
987
+ dataFromHash = JSON.parse(window.atob(newHash.slice(1)));
988
+ } catch (error) {
989
+ logger.error("*watchHash* cant parse hash");
990
+ }
991
+ if (typeof dataFromHash !== "object")
992
+ return;
993
+ if (!dataFromHash.name) {
994
+ logger.error("*watchHash* cant find component name in hash");
995
+ return;
996
+ }
997
+ if (dataFromHash.name !== component.name)
998
+ return;
999
+ logger.info("*watchHash* data from hash", dataFromHash);
1000
+ await vue.nextTick();
1001
+ open({
1002
+ component,
1003
+ layer: dataFromHash.layer || 0,
1004
+ props: {
1005
+ "onUpdate:modelValue": (value) => {
1006
+ if (value)
1007
+ return;
1008
+ router.push({
1009
+ name: route.name || void 0,
1010
+ query: route.query,
1011
+ hash: void 0
1012
+ });
1013
+ },
1014
+ ...dataFromHash.props,
1015
+ ...events
1016
+ }
1017
+ });
1018
+ },
1019
+ { immediate: true }
1020
+ );
1021
+ const stopWatchHash = () => {
1022
+ logger.info(`stop watching route.hash for dialog *${component.name}*`);
1023
+ stopWatch();
1024
+ };
1025
+ vue.onBeforeUnmount(() => {
1026
+ stopWatchHash();
1027
+ });
1028
+ return stopWatchHash;
1029
+ };
1030
+ return {
1031
+ open,
1032
+ getInstance,
1033
+ close,
1034
+ closeAll,
1035
+ showConfirm,
1036
+ isOnTop,
1037
+ openByHash,
1038
+ watchHash
1039
+ };
1040
+ }
1041
+
829
1042
  const QS_STRINGIFY_PARAMS = {
830
1043
  arrayFormat: "comma",
831
1044
  encode: false,
@@ -1228,6 +1441,7 @@ exports.useBreakpoint = useBreakpoint;
1228
1441
  exports.useClassName = useClassName;
1229
1442
  exports.useDayjs = useDayjs;
1230
1443
  exports.useDetectBrowser = useDetectBrowser;
1444
+ exports.useDialog = useDialog;
1231
1445
  exports.useFilters = useFilters;
1232
1446
  exports.useGlobalScroll = useGlobalScroll;
1233
1447
  exports.useLogger = useLogger;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as vue from 'vue';
2
- import { MaybeRef, Ref, Component, EmitsOptions, VNode } from 'vue';
2
+ import { MaybeRef, Component, EmitsOptions, VNode, Ref } from 'vue';
3
3
  import { AxiosRequestConfig } from 'axios';
4
4
  export { AxiosError as HttpError, AxiosPromise as HttpPromise, AxiosRequestConfig as HttpRequestConfig, AxiosResponse as HttpResponse } from 'axios';
5
5
  import { MaybeRef as MaybeRef$1 } from '@vueuse/core';
@@ -244,6 +244,31 @@ type VersionsList = {
244
244
  };
245
245
  declare function useDetectBrowser(versions: VersionsList, userAgent?: any): BrowserInfo;
246
246
 
247
+ interface VNodeCustom$1 extends VNode {
248
+ close(): void;
249
+ }
250
+ declare function useDialog(): {
251
+ open: ({ layer, component, props, }: {
252
+ component: Component;
253
+ props?: any;
254
+ layer?: number;
255
+ }) => string;
256
+ getInstance: (uuid: string) => VNodeCustom$1;
257
+ close: (uuid: string) => void;
258
+ closeAll: () => void;
259
+ showConfirm: (text?: string) => boolean;
260
+ isOnTop: (uuid: string) => boolean;
261
+ openByHash: ({ layer, component, props, }: {
262
+ component: Component;
263
+ props?: any;
264
+ layer?: number;
265
+ }) => void;
266
+ watchHash: ({ component, events, }: {
267
+ component: Component;
268
+ events?: EmitsOptions;
269
+ }) => any;
270
+ };
271
+
247
272
  declare function useGlobalScroll(): {
248
273
  isLocked: vue.Ref<boolean>;
249
274
  lock: () => void;
@@ -282,6 +307,9 @@ declare function useLogger(scope: string, options?: Options): logdown.Logger;
282
307
  interface VNodeCustom extends VNode {
283
308
  close(): void;
284
309
  }
310
+ /**
311
+ * @deprecated use useDialog
312
+ */
285
313
  declare function usePanel(): {
286
314
  open: ({ layer, component, props, }: {
287
315
  component: Component;
@@ -318,4 +346,4 @@ declare function isTypeUrl(message?: string): RuleItem;
318
346
  declare function isTypeNumber(value?: number, message?: string): RuleItem;
319
347
  declare function isStrictLength(value?: number, message?: string): RuleItem;
320
348
 
321
- export { AppComponentsResolver, BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, convertEnumToOptions, formatMoney, genHash, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, mapDataObjectForFilters, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
349
+ export { AppComponentsResolver, BrowserInfo, Dictionary, ResponseError, UiComponentsResolver, VersionsList, camelizeKeys, convertEnumToOptions, formatMoney, genHash, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, mapDataObjectForFilters, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useDialog, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
package/dist/index.mjs CHANGED
@@ -16,7 +16,7 @@ import updateLocale from 'dayjs/plugin/updateLocale.js';
16
16
  import isToday from 'dayjs/plugin/isToday.js';
17
17
  import customParseFormat from 'dayjs/plugin/customParseFormat.js';
18
18
  import { detect } from 'detect-browser';
19
- import { useRouter, useRoute } from 'vue-router';
19
+ import { useRoute, useRouter } from 'vue-router';
20
20
  import qs from 'qs';
21
21
 
22
22
  function AppComponentsResolver(options = {}) {
@@ -808,6 +808,219 @@ function useGlobalScroll() {
808
808
  };
809
809
  }
810
810
 
811
+ const CONFIRM_TEXT = "Are you sure you want to close? \nAll changes will be lost.";
812
+ let dialogs = [];
813
+ function useDialog() {
814
+ let appContext;
815
+ const logger = useLogger("use-dialog");
816
+ const {
817
+ isLocked: isGlobalScrollLocked,
818
+ lock: lockGlobalScroll,
819
+ unlock: unlockGlobalScroll
820
+ } = useGlobalScroll();
821
+ const route = useRoute();
822
+ const router = useRouter();
823
+ onMounted(() => {
824
+ appContext = getCurrentInstance()?.appContext;
825
+ });
826
+ const open = ({
827
+ layer = 0,
828
+ component,
829
+ props
830
+ }) => {
831
+ const uuid = genUUID();
832
+ const createComponent = () => {
833
+ const newProps = {
834
+ ...props,
835
+ layer
836
+ };
837
+ const el2 = document.createElement("div");
838
+ const vnode2 = createVNode(component, {
839
+ ...newProps,
840
+ modelValue: true,
841
+ uuid
842
+ });
843
+ vnode2.appContext = { ...appContext };
844
+ if (!vnode2?.props?.["onUpdate:modelValue"]) {
845
+ vnode2.props["onUpdate:modelValue"] = (value) => {
846
+ if (vnode2 && vnode2.component) {
847
+ vnode2.component.props.modelValue = value;
848
+ if (!value && dialogs.length === 1 && isGlobalScrollLocked.value) {
849
+ unlockGlobalScroll();
850
+ }
851
+ }
852
+ };
853
+ }
854
+ vnode2.close = () => {
855
+ if (vnode2 && vnode2.component) {
856
+ vnode2.component.props.modelValue = false;
857
+ }
858
+ };
859
+ vnode2.props.onDestroy = () => {
860
+ render(null, el2);
861
+ dialogs = dialogs.filter((dialog) => dialog.uuid !== uuid);
862
+ logger.info("Dialogs after destroy", dialogs);
863
+ };
864
+ const name = vnode2?.type?.name ?? "Unknown component";
865
+ const newDialog2 = {
866
+ uuid,
867
+ name,
868
+ vnode: vnode2,
869
+ layer
870
+ };
871
+ logger.log(`Open new dialog: "${name}"`);
872
+ if (!isGlobalScrollLocked.value) {
873
+ lockGlobalScroll();
874
+ }
875
+ return {
876
+ vnode: vnode2,
877
+ el: el2,
878
+ newDialog: newDialog2
879
+ };
880
+ };
881
+ if (!dialogs.length) {
882
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
883
+ render(vnode2, el2);
884
+ dialogs = [newDialog2];
885
+ logger.info("Dialogs after open", dialogs);
886
+ return uuid;
887
+ }
888
+ if (layer === 0) {
889
+ closeAll();
890
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
891
+ render(vnode2, el2);
892
+ dialogs = [newDialog2];
893
+ logger.info("Dialogs after open", dialogs);
894
+ return uuid;
895
+ }
896
+ if (!dialogs.some((dialog) => dialog.layer === layer)) {
897
+ const { vnode: vnode2, el: el2, newDialog: newDialog2 } = createComponent();
898
+ render(vnode2, el2);
899
+ dialogs = [...dialogs, newDialog2];
900
+ logger.info("Dialogs after open", dialogs);
901
+ return uuid;
902
+ }
903
+ const dialogOnCurrentLayer = dialogs.find((dialog) => dialog.layer === layer);
904
+ close(dialogOnCurrentLayer?.uuid || "");
905
+ const { vnode, el, newDialog } = createComponent();
906
+ render(vnode, el);
907
+ dialogs = [...dialogs, newDialog];
908
+ logger.info("Dialogs after open", dialogs);
909
+ return uuid;
910
+ };
911
+ const close = (uuid) => {
912
+ const dialog = dialogs.find((item) => item.uuid === uuid);
913
+ logger.log(`Close dialog: "${uuid}"`);
914
+ if (dialogs.length === 1 && isGlobalScrollLocked.value) {
915
+ unlockGlobalScroll();
916
+ }
917
+ dialog?.vnode.close();
918
+ };
919
+ const closeAll = () => {
920
+ dialogs.forEach((dialog) => {
921
+ dialog?.vnode.close();
922
+ });
923
+ if (isGlobalScrollLocked.value) {
924
+ unlockGlobalScroll();
925
+ }
926
+ };
927
+ const getInstance = (uuid) => {
928
+ return dialogs.find((item) => item.uuid === uuid)?.vnode;
929
+ };
930
+ const isOnTop = (uuid) => {
931
+ if (!dialogs.length)
932
+ return false;
933
+ const dialogsSorted = [...dialogs].sort((a, b) => b.layer - a.layer);
934
+ return dialogsSorted[0].uuid === uuid;
935
+ };
936
+ const showConfirm = (text = CONFIRM_TEXT) => {
937
+ return window.confirm(text);
938
+ };
939
+ const openByHash = ({
940
+ layer = 0,
941
+ component,
942
+ props
943
+ }) => {
944
+ const data = {
945
+ name: component.name,
946
+ layer,
947
+ props
948
+ };
949
+ router.push({
950
+ name: route.name || void 0,
951
+ query: route.query,
952
+ hash: `#${window.btoa(JSON.stringify(data))}`
953
+ });
954
+ };
955
+ const watchHash = ({
956
+ component,
957
+ events
958
+ }) => {
959
+ logger.info(`start watching route.hash for dialog *${component.name}*`);
960
+ const stopWatch = watch(
961
+ () => route.hash,
962
+ async (newHash) => {
963
+ if (!newHash) {
964
+ closeAll();
965
+ return;
966
+ }
967
+ let dataFromHash;
968
+ try {
969
+ dataFromHash = JSON.parse(window.atob(newHash.slice(1)));
970
+ } catch (error) {
971
+ logger.error("*watchHash* cant parse hash");
972
+ }
973
+ if (typeof dataFromHash !== "object")
974
+ return;
975
+ if (!dataFromHash.name) {
976
+ logger.error("*watchHash* cant find component name in hash");
977
+ return;
978
+ }
979
+ if (dataFromHash.name !== component.name)
980
+ return;
981
+ logger.info("*watchHash* data from hash", dataFromHash);
982
+ await nextTick();
983
+ open({
984
+ component,
985
+ layer: dataFromHash.layer || 0,
986
+ props: {
987
+ "onUpdate:modelValue": (value) => {
988
+ if (value)
989
+ return;
990
+ router.push({
991
+ name: route.name || void 0,
992
+ query: route.query,
993
+ hash: void 0
994
+ });
995
+ },
996
+ ...dataFromHash.props,
997
+ ...events
998
+ }
999
+ });
1000
+ },
1001
+ { immediate: true }
1002
+ );
1003
+ const stopWatchHash = () => {
1004
+ logger.info(`stop watching route.hash for dialog *${component.name}*`);
1005
+ stopWatch();
1006
+ };
1007
+ onBeforeUnmount(() => {
1008
+ stopWatchHash();
1009
+ });
1010
+ return stopWatchHash;
1011
+ };
1012
+ return {
1013
+ open,
1014
+ getInstance,
1015
+ close,
1016
+ closeAll,
1017
+ showConfirm,
1018
+ isOnTop,
1019
+ openByHash,
1020
+ watchHash
1021
+ };
1022
+ }
1023
+
811
1024
  const QS_STRINGIFY_PARAMS = {
812
1025
  arrayFormat: "comma",
813
1026
  encode: false,
@@ -1175,4 +1388,4 @@ function isStrictLength(value = 3, message) {
1175
1388
  };
1176
1389
  }
1177
1390
 
1178
- export { AppComponentsResolver, UiComponentsResolver, camelizeKeys, convertEnumToOptions, formatMoney, genHash, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, mapDataObjectForFilters, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
1391
+ export { AppComponentsResolver, UiComponentsResolver, camelizeKeys, convertEnumToOptions, formatMoney, genHash, genRandomNumber, genUUID, getCardNumber, getCountryFromPhone, getPaginationLimit, getPaginationOffset, getPaginationPages, getPaginationParams, isElementIgnored, isEmptyObject, isMax, isMin, isRequired, isStrictLength, isTypeEmail, isTypeNumber, isTypeUrl, mapDataObjectForFilters, parsePhone, snakelizeKeys, unformatNumber, useAbortController, useAsyncItems, useAsyncMultiselect, useAsyncSelect, useBreakpoint, useClassName, useDayjs, useDetectBrowser, useDialog, useFilters, useGlobalScroll, useLogger, usePanel, validateNumber, validatePhone };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karta.io/utils",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "karta.io utils for admin and application",
5
5
  "author": "Vitaly Blokhin <karambafe@gmail.com>",
6
6
  "homepage": "",
@@ -24,7 +24,7 @@
24
24
  "vue": "3.3.x"
25
25
  },
26
26
  "dependencies": {
27
- "@karta.io/common-data": "^1.0.0-alpha.7",
27
+ "@karta.io/common-data": "^1.0.0-alpha.8",
28
28
  "@tanstack/vue-query": "4.33.1",
29
29
  "@vueuse/core": "10.3.0",
30
30
  "async-validator": "4.2.5",