x-essential-lib 0.10.10 → 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, syncLocale: () => Promise<void>, 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, syncLocale, 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(() => {
@@ -764,6 +764,58 @@ function useMicroApp(states, syncLocale, customBack) {
764
764
  providePermissionChecksum(permissionChecksum);
765
765
  provideLastAppPath(lastAppPath);
766
766
  provideViews(views);
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
+ }
767
819
  async function handleBack() {
768
820
  if (!isEmpty(views)) {
769
821
  if (!hasView(views, "waitDlg")) popView(views);
@@ -781,17 +833,48 @@ function useMicroApp(states, syncLocale, customBack) {
781
833
  return;
782
834
  }
783
835
  }
784
- async function onBackButton() {
785
- 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
+ });
786
877
  }
787
- onMounted(() => {
788
- window.addEventListener("keydown", onKeyDown);
789
- eventBus.on("backbutton", onBackButton);
790
- });
791
- onBeforeUnmount(() => {
792
- window.removeEventListener("keydown", onKeyDown);
793
- eventBus.off("backbutton", onBackButton);
794
- });
795
878
  function onUpdateDark(val) {
796
879
  dark.value = val;
797
880
  }
@@ -808,13 +891,21 @@ function useMicroApp(states, syncLocale, customBack) {
808
891
  function onUpdateLastAppPath(val) {
809
892
  lastAppPath.value = val;
810
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
+ }
811
901
  onBeforeMount(() => {
812
902
  eventBus.on("updateDark", onUpdateDark);
813
903
  eventBus.on("updateLocale", onUpdateLocale);
814
904
  eventBus.on("updateActiveOrg", onUpdateActiveOrg);
815
905
  eventBus.on("updatePermission", onUpdatePermission);
816
906
  eventBus.on("updateLastAppPath", onUpdateLastAppPath);
817
- eventBus.emit("syncGlobalState");
907
+ if (base) eventBus.on("syncGlobalStates", onSyncGlobalStates);
908
+ else eventBus.emit("syncGlobalStates");
818
909
  });
819
910
  onBeforeUnmount(() => {
820
911
  eventBus.off("updateDark", onUpdateDark);
@@ -822,6 +913,7 @@ function useMicroApp(states, syncLocale, customBack) {
822
913
  eventBus.off("updateActiveOrg", onUpdateActiveOrg);
823
914
  eventBus.off("updatePermission", onUpdatePermission);
824
915
  eventBus.off("updateLastAppPath", onUpdateLastAppPath);
916
+ if (base) eventBus.off("syncGlobalStates", onSyncGlobalStates);
825
917
  });
826
918
  }
827
919
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "x-essential-lib",
3
- "version": "0.10.10",
3
+ "version": "0.10.12",
4
4
  "files": [
5
5
  "dist"
6
6
  ],