x-essential-lib 0.10.9 → 0.10.12

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.d.ts CHANGED
@@ -301,7 +301,7 @@ declare function popView(views: Ref<string[]>): void;
301
301
  declare function clearViews(views: Ref<string[]>): void;
302
302
  //#endregion
303
303
  //#region src/composables/microApp.d.ts
304
- interface States {
304
+ interface GlobalStates {
305
305
  dark: Ref<boolean | undefined>;
306
306
  locale: Ref<string>;
307
307
  activeOrg: Ref<string>;
@@ -310,7 +310,7 @@ interface States {
310
310
  lastAppPath: Ref<string>;
311
311
  views: Ref<string[]>;
312
312
  }
313
- declare function useMicroApp(states: States, customBack?: (m: RouteMeta) => void): void;
313
+ declare function useMicroApp(globalStates: GlobalStates, syncLocale: () => Promise<void>, base?: boolean, customBack?: (m: RouteMeta) => void): void;
314
314
  //#endregion
315
315
  //#region src/composables/permission.d.ts
316
316
  declare function usePermission(): {
package/dist/index.js CHANGED
@@ -4,9 +4,9 @@ import { Fragment, Transition, computed, createBlock, createCommentVNode, create
4
4
  import { useI18n } from "vue-i18n";
5
5
  import { useDisplay, useTheme } from "vuetify";
6
6
  import { useRoute, useRouter } from "vue-router";
7
- import axios from "axios";
8
7
  import { getErrorMessage } from "x-error-lib";
9
8
  import { createAxios as createAxios$1, messageError as messageError$1 } from "x-essential-lib";
9
+ import axios from "axios";
10
10
 
11
11
  //#region src/composables/color.ts
12
12
  function useColor() {
@@ -36,6 +36,22 @@ function useColor() {
36
36
  };
37
37
  }
38
38
 
39
+ //#endregion
40
+ //#region src/api/orgbase/instance.ts
41
+ const instance = createAxios$1({
42
+ baseUrl: "http://localhost/orgbase/",
43
+ onError: (error) => {
44
+ messageError$1(getErrorMessage(error));
45
+ }
46
+ });
47
+ if (window.API_URL) instance.defaults.baseURL = window.API_URL + "/orgbase/";
48
+
49
+ //#endregion
50
+ //#region src/api/orgbase/index.ts
51
+ function PullPermission(request) {
52
+ return instance.post("pullPermission", request);
53
+ }
54
+
39
55
  //#endregion
40
56
  //#region src/utils/axios.ts
41
57
  function createAxios(options) {
@@ -442,22 +458,6 @@ function injectViews() {
442
458
  return inject("viewsKey");
443
459
  }
444
460
 
445
- //#endregion
446
- //#region src/api/orgbase/instance.ts
447
- const instance = createAxios$1({
448
- baseUrl: "http://localhost/orgbase/",
449
- onError: (error) => {
450
- messageError$1(getErrorMessage(error));
451
- }
452
- });
453
- if (window.API_URL) instance.defaults.baseURL = window.API_URL + "/orgbase/";
454
-
455
- //#endregion
456
- //#region src/api/orgbase/index.ts
457
- function PullPermission(request) {
458
- return instance.post("pullPermission", request);
459
- }
460
-
461
461
  //#endregion
462
462
  //#region src/utils/router.ts
463
463
  const routeMetas = [
@@ -731,10 +731,10 @@ function clearViews(views) {
731
731
 
732
732
  //#endregion
733
733
  //#region src/composables/microApp.ts
734
- function useMicroApp(states, customBack) {
734
+ function useMicroApp(globalStates, syncLocale, base, customBack) {
735
735
  const route = useRoute();
736
736
  const router = useRouter();
737
- const { dark, locale, activeOrg, permissionObjects, permissionChecksum, lastAppPath, views } = states;
737
+ const { dark, locale, activeOrg, permissionObjects, permissionChecksum, lastAppPath, views } = globalStates;
738
738
  const prefDark = usePreferredDark();
739
739
  const finalDark = ref(false);
740
740
  watchEffect(() => {
@@ -743,6 +743,20 @@ function useMicroApp(states, customBack) {
743
743
  else d = prefDark.value;
744
744
  finalDark.value = d;
745
745
  });
746
+ const theme = useTheme();
747
+ watchEffect(async () => {
748
+ await theme.change(finalDark.value ? "dark" : "light");
749
+ });
750
+ let pending = true;
751
+ router.beforeEach(async (to, from) => {
752
+ await syncLocale();
753
+ pending = false;
754
+ return onBeforeEach(to, from, lastAppPath.value);
755
+ });
756
+ watch(locale, async () => {
757
+ if (pending) return;
758
+ await syncLocale();
759
+ });
746
760
  provideDark(finalDark);
747
761
  provideLocale(locale);
748
762
  provideActiveOrg(activeOrg);
@@ -750,10 +764,58 @@ function useMicroApp(states, customBack) {
750
764
  providePermissionChecksum(permissionChecksum);
751
765
  provideLastAppPath(lastAppPath);
752
766
  provideViews(views);
753
- const theme = useTheme();
754
- watchEffect(async () => {
755
- await theme.change(finalDark.value ? "dark" : "light");
756
- });
767
+ function extractPathFromUrl(url) {
768
+ return new URL(url).pathname;
769
+ }
770
+ function onBeforeAppChange(e) {
771
+ const lastAppPath = extractPathFromUrl(e.detail.oldUrl);
772
+ eventBus.emit("updateLastAppPath", lastAppPath);
773
+ }
774
+ if (base) {
775
+ onMounted(() => {
776
+ window.addEventListener("single-spa:before-app-change", onBeforeAppChange);
777
+ });
778
+ onBeforeUnmount(() => {
779
+ window.removeEventListener("single-spa:before-app-change", onBeforeAppChange);
780
+ });
781
+ }
782
+ function syncStatusBar() {
783
+ if (typeof StatusBar !== "object") return;
784
+ if (finalDark.value) StatusBar.styleLightContent();
785
+ else StatusBar.styleDefault();
786
+ }
787
+ const running = ref(true);
788
+ function onPause() {
789
+ running.value = false;
790
+ }
791
+ function onResume() {
792
+ running.value = true;
793
+ }
794
+ async function onBackButton() {
795
+ await handleBack();
796
+ }
797
+ function onDeviceReady() {
798
+ console.log("onDeviceReady");
799
+ document.addEventListener("pause", onPause, false);
800
+ document.addEventListener("resume", onResume, false);
801
+ document.addEventListener("backbutton", onBackButton, false);
802
+ setInterval(() => {
803
+ syncStatusBar();
804
+ }, 1e3);
805
+ navigator.splashscreen.hide();
806
+ }
807
+ if (base) {
808
+ onMounted(() => {
809
+ document.addEventListener("deviceready", onDeviceReady, false);
810
+ });
811
+ onBeforeUnmount(() => {
812
+ document.removeEventListener("deviceready", onDeviceReady, false);
813
+ });
814
+ watch(finalDark, (val) => {
815
+ window.dispatchEvent(new CustomEvent("darkChange", { detail: val }));
816
+ syncStatusBar();
817
+ }, { immediate: true });
818
+ }
757
819
  async function handleBack() {
758
820
  if (!isEmpty(views)) {
759
821
  if (!hasView(views, "waitDlg")) popView(views);
@@ -771,17 +833,48 @@ function useMicroApp(states, customBack) {
771
833
  return;
772
834
  }
773
835
  }
774
- async function onBackButton() {
775
- await handleBack();
836
+ if (base) {
837
+ onMounted(() => {
838
+ window.addEventListener("keydown", onKeyDown);
839
+ });
840
+ onBeforeUnmount(() => {
841
+ window.removeEventListener("keydown", onKeyDown);
842
+ });
843
+ }
844
+ let handle;
845
+ async function syncPermission() {
846
+ if (!running.value) return;
847
+ if (!api.get("accessToken")) return;
848
+ try {
849
+ const { update, permissions } = await PullPermission({
850
+ orgId: activeOrg.value,
851
+ checksum: permissionChecksum.value
852
+ });
853
+ if (!update) return;
854
+ eventBus.emit("updatePermission", [toPermissionObjects(permissions), permissions["checksum"] ?? ""]);
855
+ } catch (e) {
856
+ console.error(e);
857
+ }
858
+ }
859
+ async function startTimer() {
860
+ window.clearTimeout(handle);
861
+ handle = window.setTimeout(async () => {
862
+ await syncPermission();
863
+ if (handle) await startTimer();
864
+ }, 3e3);
865
+ }
866
+ function stopTimer() {
867
+ window.clearTimeout(handle);
868
+ handle = void 0;
869
+ }
870
+ if (base) {
871
+ onMounted(async () => {
872
+ await startTimer();
873
+ });
874
+ onBeforeUnmount(() => {
875
+ stopTimer();
876
+ });
776
877
  }
777
- onMounted(() => {
778
- window.addEventListener("keydown", onKeyDown);
779
- eventBus.on("backbutton", onBackButton);
780
- });
781
- onBeforeUnmount(() => {
782
- window.removeEventListener("keydown", onKeyDown);
783
- eventBus.off("backbutton", onBackButton);
784
- });
785
878
  function onUpdateDark(val) {
786
879
  dark.value = val;
787
880
  }
@@ -792,20 +885,27 @@ function useMicroApp(states, customBack) {
792
885
  activeOrg.value = val;
793
886
  }
794
887
  function onUpdatePermission(val) {
795
- const v = val;
796
- permissionObjects.value = v[0];
797
- permissionChecksum.value = v[1];
888
+ permissionObjects.value = val[0];
889
+ permissionChecksum.value = val[1];
798
890
  }
799
891
  function onUpdateLastAppPath(val) {
800
892
  lastAppPath.value = val;
801
893
  }
894
+ function onSyncGlobalStates() {
895
+ eventBus.emit("updateDark", dark.value);
896
+ eventBus.emit("updateLocale", locale.value);
897
+ eventBus.emit("updateActiveOrg", activeOrg.value);
898
+ eventBus.emit("updatePermission", [permissionObjects.value, permissionChecksum.value]);
899
+ eventBus.emit("updateLastAppPath", lastAppPath.value);
900
+ }
802
901
  onBeforeMount(() => {
803
902
  eventBus.on("updateDark", onUpdateDark);
804
903
  eventBus.on("updateLocale", onUpdateLocale);
805
904
  eventBus.on("updateActiveOrg", onUpdateActiveOrg);
806
905
  eventBus.on("updatePermission", onUpdatePermission);
807
906
  eventBus.on("updateLastAppPath", onUpdateLastAppPath);
808
- eventBus.emit("syncGlobalState");
907
+ if (base) eventBus.on("syncGlobalStates", onSyncGlobalStates);
908
+ else eventBus.emit("syncGlobalStates");
809
909
  });
810
910
  onBeforeUnmount(() => {
811
911
  eventBus.off("updateDark", onUpdateDark);
@@ -813,6 +913,7 @@ function useMicroApp(states, customBack) {
813
913
  eventBus.off("updateActiveOrg", onUpdateActiveOrg);
814
914
  eventBus.off("updatePermission", onUpdatePermission);
815
915
  eventBus.off("updateLastAppPath", onUpdateLastAppPath);
916
+ if (base) eventBus.off("syncGlobalStates", onSyncGlobalStates);
816
917
  });
817
918
  }
818
919
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-essential-lib",
3
- "version": "0.10.9",
3
+ "version": "0.10.12",
4
4
  "files": [
5
5
  "dist"
6
6
  ],