chayns-api 1.0.0
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/.babelrc +30 -0
- package/.eslintrc +17 -0
- package/.github/workflows/deploy_docs.yml +28 -0
- package/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/cjs/calls/dialogs/alert.js +19 -0
- package/dist/cjs/calls/dialogs/chaynsDialog.js +59 -0
- package/dist/cjs/calls/dialogs/close.js +13 -0
- package/dist/cjs/calls/dialogs/communication.js +71 -0
- package/dist/cjs/calls/dialogs/confirm.js +33 -0
- package/dist/cjs/calls/dialogs/date.js +187 -0
- package/dist/cjs/calls/dialogs/dropUpAlert.js +12 -0
- package/dist/cjs/calls/dialogs/fileSelect.js +39 -0
- package/dist/cjs/calls/dialogs/iFrame.js +43 -0
- package/dist/cjs/calls/dialogs/index.js +178 -0
- package/dist/cjs/calls/dialogs/input.js +33 -0
- package/dist/cjs/calls/dialogs/mediaSelect.js +27 -0
- package/dist/cjs/calls/dialogs/open.js +19 -0
- package/dist/cjs/calls/dialogs/select.js +55 -0
- package/dist/cjs/calls/dialogs/signature.js +24 -0
- package/dist/cjs/calls/dialogs/toast.js +18 -0
- package/dist/cjs/calls/dialogs/utils/callback.js +12 -0
- package/dist/cjs/calls/dialogs/utils/environment.js +10 -0
- package/dist/cjs/calls/dialogs/utils/is.js +21 -0
- package/dist/cjs/calls/getUserInfo.js +40 -0
- package/dist/cjs/calls/index.js +300 -0
- package/dist/cjs/calls/sendMessage.js +120 -0
- package/dist/cjs/calls/visibilityChangeListener.js +38 -0
- package/dist/cjs/client.js +93 -0
- package/dist/cjs/components/ChaynsContext.js +12 -0
- package/dist/cjs/components/ChaynsModuleProvider.js +457 -0
- package/dist/cjs/components/ChaynsProvider.js +116 -0
- package/dist/cjs/components/WaitUntil.js +31 -0
- package/dist/cjs/components/withCompatMode.js +55 -0
- package/dist/cjs/helper/apiListenerHelper.js +34 -0
- package/dist/cjs/hooks/geoLocationListener.js +50 -0
- package/dist/cjs/hooks/index.js +153 -0
- package/dist/cjs/hooks/scrollListener.js +102 -0
- package/dist/cjs/hooks/useAccessToken.js +28 -0
- package/dist/cjs/hooks/useCurrentPage.js +13 -0
- package/dist/cjs/hooks/useCustomData.js +13 -0
- package/dist/cjs/hooks/useDevice.js +13 -0
- package/dist/cjs/hooks/useEnvironment.js +13 -0
- package/dist/cjs/hooks/useFunctions.js +16 -0
- package/dist/cjs/hooks/useIsAdminMode.js +16 -0
- package/dist/cjs/hooks/useLanguage.js +13 -0
- package/dist/cjs/hooks/usePages.js +38 -0
- package/dist/cjs/hooks/useParameters.js +13 -0
- package/dist/cjs/hooks/useSite.js +13 -0
- package/dist/cjs/hooks/useUser.js +14 -0
- package/dist/cjs/hooks/useValues.js +16 -0
- package/dist/cjs/hooks/windowMetricsListener.js +58 -0
- package/dist/cjs/host/ChaynsHost.js +102 -0
- package/dist/cjs/host/iframe/HostIframe.js +133 -0
- package/dist/cjs/host/iframe/utils/useUpdateData.js +21 -0
- package/dist/cjs/host/module/ModuleHost.js +82 -0
- package/dist/cjs/host/module/PagemakerFrame.js +105 -0
- package/dist/cjs/host/module/utils/loadComponent.js +61 -0
- package/dist/cjs/host/module/utils/useDynamicScript.js +61 -0
- package/dist/cjs/index.js +117 -0
- package/dist/cjs/types/DynamicApiImport.d.js +5 -0
- package/dist/cjs/types/IChaynsReact.js +235 -0
- package/dist/cjs/types/dialog.js +5 -0
- package/dist/cjs/util/appCall.js +23 -0
- package/dist/cjs/util/deviceHelper.js +71 -0
- package/dist/cjs/util/heightHelper.js +40 -0
- package/dist/cjs/util/postIframeForm.js +32 -0
- package/dist/cjs/wrapper/AppWrapper.js +244 -0
- package/dist/cjs/wrapper/FrameWrapper.js +288 -0
- package/dist/cjs/wrapper/ModuleFederationWrapper.js +61 -0
- package/dist/cjs/wrapper/SsrWrapper.js +30 -0
- package/dist/esm/calls/dialogs/alert.js +15 -0
- package/dist/esm/calls/dialogs/chaynsDialog.js +48 -0
- package/dist/esm/calls/dialogs/close.js +7 -0
- package/dist/esm/calls/dialogs/communication.js +66 -0
- package/dist/esm/calls/dialogs/confirm.js +30 -0
- package/dist/esm/calls/dialogs/date.js +211 -0
- package/dist/esm/calls/dialogs/dropUpAlert.js +7 -0
- package/dist/esm/calls/dialogs/fileSelect.js +32 -0
- package/dist/esm/calls/dialogs/iFrame.js +37 -0
- package/dist/esm/calls/dialogs/index.js +14 -0
- package/dist/esm/calls/dialogs/input.js +26 -0
- package/dist/esm/calls/dialogs/mediaSelect.js +22 -0
- package/dist/esm/calls/dialogs/open.js +13 -0
- package/dist/esm/calls/dialogs/select.js +47 -0
- package/dist/esm/calls/dialogs/signature.js +18 -0
- package/dist/esm/calls/dialogs/toast.js +13 -0
- package/dist/esm/calls/dialogs/utils/callback.js +7 -0
- package/dist/esm/calls/dialogs/utils/environment.js +3 -0
- package/dist/esm/calls/dialogs/utils/is.js +12 -0
- package/dist/esm/calls/getUserInfo.js +33 -0
- package/dist/esm/calls/index.js +323 -0
- package/dist/esm/calls/sendMessage.js +111 -0
- package/dist/esm/calls/visibilityChangeListener.js +30 -0
- package/dist/esm/client.js +8 -0
- package/dist/esm/components/ChaynsContext.js +4 -0
- package/dist/esm/components/ChaynsModuleProvider.js +517 -0
- package/dist/esm/components/ChaynsProvider.js +109 -0
- package/dist/esm/components/WaitUntil.js +25 -0
- package/dist/esm/components/withCompatMode.js +47 -0
- package/dist/esm/helper/apiListenerHelper.js +25 -0
- package/dist/esm/hooks/geoLocationListener.js +43 -0
- package/dist/esm/hooks/index.js +16 -0
- package/dist/esm/hooks/scrollListener.js +95 -0
- package/dist/esm/hooks/useAccessToken.js +22 -0
- package/dist/esm/hooks/useCurrentPage.js +7 -0
- package/dist/esm/hooks/useCustomData.js +7 -0
- package/dist/esm/hooks/useDevice.js +6 -0
- package/dist/esm/hooks/useEnvironment.js +6 -0
- package/dist/esm/hooks/useFunctions.js +9 -0
- package/dist/esm/hooks/useIsAdminMode.js +9 -0
- package/dist/esm/hooks/useLanguage.js +6 -0
- package/dist/esm/hooks/usePages.js +32 -0
- package/dist/esm/hooks/useParameters.js +6 -0
- package/dist/esm/hooks/useSite.js +6 -0
- package/dist/esm/hooks/useUser.js +7 -0
- package/dist/esm/hooks/useValues.js +9 -0
- package/dist/esm/hooks/windowMetricsListener.js +51 -0
- package/dist/esm/host/ChaynsHost.js +95 -0
- package/dist/esm/host/iframe/HostIframe.js +124 -0
- package/dist/esm/host/iframe/utils/useUpdateData.js +14 -0
- package/dist/esm/host/module/ModuleHost.js +74 -0
- package/dist/esm/host/module/PagemakerFrame.js +97 -0
- package/dist/esm/host/module/utils/loadComponent.js +55 -0
- package/dist/esm/host/module/utils/useDynamicScript.js +50 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/types/DynamicApiImport.d.js +1 -0
- package/dist/esm/types/IChaynsReact.js +215 -0
- package/dist/esm/types/dialog.js +1 -0
- package/dist/esm/util/appCall.js +16 -0
- package/dist/esm/util/deviceHelper.js +62 -0
- package/dist/esm/util/heightHelper.js +32 -0
- package/dist/esm/util/postIframeForm.js +26 -0
- package/dist/esm/wrapper/AppWrapper.js +241 -0
- package/dist/esm/wrapper/FrameWrapper.js +279 -0
- package/dist/esm/wrapper/ModuleFederationWrapper.js +56 -0
- package/dist/esm/wrapper/SsrWrapper.js +23 -0
- package/dist/types/calls/dialogs/alert.d.ts +1 -0
- package/dist/types/calls/dialogs/chaynsDialog.d.ts +24 -0
- package/dist/types/calls/dialogs/close.d.ts +1 -0
- package/dist/types/calls/dialogs/communication.d.ts +3 -0
- package/dist/types/calls/dialogs/confirm.d.ts +13 -0
- package/dist/types/calls/dialogs/date.d.ts +96 -0
- package/dist/types/calls/dialogs/dropUpAlert.d.ts +5 -0
- package/dist/types/calls/dialogs/fileSelect.d.ts +16 -0
- package/dist/types/calls/dialogs/iFrame.d.ts +10 -0
- package/dist/types/calls/dialogs/index.d.ts +14 -0
- package/dist/types/calls/dialogs/input.d.ts +15 -0
- package/dist/types/calls/dialogs/mediaSelect.d.ts +8 -0
- package/dist/types/calls/dialogs/open.d.ts +1 -0
- package/dist/types/calls/dialogs/select.d.ts +6 -0
- package/dist/types/calls/dialogs/signature.d.ts +7 -0
- package/dist/types/calls/dialogs/toast.d.ts +1 -0
- package/dist/types/calls/dialogs/utils/callback.d.ts +1 -0
- package/dist/types/calls/dialogs/utils/environment.d.ts +3 -0
- package/dist/types/calls/dialogs/utils/is.d.ts +4 -0
- package/dist/types/calls/getUserInfo.d.ts +9 -0
- package/dist/types/calls/index.d.ts +231 -0
- package/dist/types/calls/sendMessage.d.ts +13 -0
- package/dist/types/calls/visibilityChangeListener.d.ts +9 -0
- package/dist/types/client.d.ts +7 -0
- package/dist/types/components/ChaynsContext.d.ts +3 -0
- package/dist/types/components/ChaynsModuleProvider.d.ts +170 -0
- package/dist/types/components/ChaynsProvider.d.ts +13 -0
- package/dist/types/components/WaitUntil.d.ts +7 -0
- package/dist/types/components/withCompatMode.d.ts +13 -0
- package/dist/types/helper/apiListenerHelper.d.ts +6 -0
- package/dist/types/hooks/geoLocationListener.d.ts +18 -0
- package/dist/types/hooks/index.d.ts +16 -0
- package/dist/types/hooks/scrollListener.d.ts +28 -0
- package/dist/types/hooks/useAccessToken.d.ts +5 -0
- package/dist/types/hooks/useCurrentPage.d.ts +4 -0
- package/dist/types/hooks/useCustomData.d.ts +4 -0
- package/dist/types/hooks/useDevice.d.ts +5 -0
- package/dist/types/hooks/useEnvironment.d.ts +5 -0
- package/dist/types/hooks/useFunctions.d.ts +5 -0
- package/dist/types/hooks/useIsAdminMode.d.ts +4 -0
- package/dist/types/hooks/useLanguage.d.ts +5 -0
- package/dist/types/hooks/usePages.d.ts +14 -0
- package/dist/types/hooks/useParameters.d.ts +5 -0
- package/dist/types/hooks/useSite.d.ts +5 -0
- package/dist/types/hooks/useUser.d.ts +5 -0
- package/dist/types/hooks/useValues.d.ts +5 -0
- package/dist/types/hooks/windowMetricsListener.d.ts +11 -0
- package/dist/types/host/ChaynsHost.d.ts +27 -0
- package/dist/types/host/iframe/HostIframe.d.ts +24 -0
- package/dist/types/host/iframe/utils/useUpdateData.d.ts +3 -0
- package/dist/types/host/module/ModuleHost.d.ts +23 -0
- package/dist/types/host/module/PagemakerFrame.d.ts +22 -0
- package/dist/types/host/module/utils/loadComponent.d.ts +1 -0
- package/dist/types/host/module/utils/useDynamicScript.d.ts +9 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/types/IChaynsReact.d.ts +590 -0
- package/dist/types/types/dialog.d.ts +41 -0
- package/dist/types/util/appCall.d.ts +2 -0
- package/dist/types/util/deviceHelper.d.ts +7 -0
- package/dist/types/util/heightHelper.d.ts +1 -0
- package/dist/types/util/postIframeForm.d.ts +1 -0
- package/dist/types/wrapper/AppWrapper.d.ts +16 -0
- package/dist/types/wrapper/FrameWrapper.d.ts +15 -0
- package/dist/types/wrapper/ModuleFederationWrapper.d.ts +10 -0
- package/dist/types/wrapper/SsrWrapper.d.ts +11 -0
- package/package.json +76 -0
- package/toolkit.config.js +52 -0
- package/tsconfig.json +56 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const listenerMapping = {};
|
|
2
|
+
let counter = 0;
|
|
3
|
+
export const addApiListener = (key, callback) => {
|
|
4
|
+
const id = ++counter;
|
|
5
|
+
if (!listenerMapping[key]) {
|
|
6
|
+
listenerMapping[key] = {};
|
|
7
|
+
}
|
|
8
|
+
const shouldInitialize = Object.keys(listenerMapping[key]).length === 0;
|
|
9
|
+
listenerMapping[key][counter] = callback;
|
|
10
|
+
return {
|
|
11
|
+
id,
|
|
12
|
+
shouldInitialize
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export const removeApiListener = (key, id) => {
|
|
16
|
+
if (listenerMapping[key]) {
|
|
17
|
+
delete listenerMapping[key][id];
|
|
18
|
+
}
|
|
19
|
+
return Object.keys(listenerMapping[key]).length === 0;
|
|
20
|
+
};
|
|
21
|
+
export const dispatchApiEvent = (key, value) => {
|
|
22
|
+
if (listenerMapping[key]) {
|
|
23
|
+
Object.values(listenerMapping[key]).forEach(l => l(value));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { ChaynsFunctionsContext } from '../components/ChaynsContext';
|
|
4
|
+
/**
|
|
5
|
+
* @category Hooks
|
|
6
|
+
*/
|
|
7
|
+
export const useGeoLocationListener = () => {
|
|
8
|
+
const addListener = useContextSelector(ChaynsFunctionsContext, v => v.addGeoLocationListener);
|
|
9
|
+
const removeListener = useContextSelector(ChaynsFunctionsContext, v => v.removeGeoLocationListener);
|
|
10
|
+
const promiseRef = useRef();
|
|
11
|
+
return useCallback((value, callback) => {
|
|
12
|
+
promiseRef.current = addListener(value, callback);
|
|
13
|
+
return () => {
|
|
14
|
+
var _promiseRef$current;
|
|
15
|
+
void ((_promiseRef$current = promiseRef.current) === null || _promiseRef$current === void 0 ? void 0 : _promiseRef$current.then(removeListener));
|
|
16
|
+
};
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
}, []);
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @category Hooks
|
|
22
|
+
*/
|
|
23
|
+
export const useGeoLocation = function () {
|
|
24
|
+
let {
|
|
25
|
+
enabled = true,
|
|
26
|
+
timeout = 0,
|
|
27
|
+
silent = false
|
|
28
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
29
|
+
const [value, setValue] = useState(null);
|
|
30
|
+
const addListener = useGeoLocationListener();
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (enabled) {
|
|
33
|
+
return addListener({
|
|
34
|
+
timeout,
|
|
35
|
+
silent
|
|
36
|
+
}, setValue);
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}, [enabled, timeout, addListener, silent]);
|
|
40
|
+
return {
|
|
41
|
+
value
|
|
42
|
+
};
|
|
43
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { useGeoLocation, useGeoLocationListener } from './geoLocationListener';
|
|
2
|
+
export { useScrollPosition, useScrollListener, useScrollOffsetBottom, useScrollOffsetTop } from './scrollListener';
|
|
3
|
+
export { useIsAdminMode } from './useIsAdminMode';
|
|
4
|
+
export { useUser } from './useUser';
|
|
5
|
+
export { useAccessToken } from './useAccessToken';
|
|
6
|
+
export { useWindowMetrics, useWindowMetricsListener } from './windowMetricsListener';
|
|
7
|
+
export { useDevice } from './useDevice';
|
|
8
|
+
export { useSite } from './useSite';
|
|
9
|
+
export { useParameters } from './useParameters';
|
|
10
|
+
export { usePages, usePage } from './usePages';
|
|
11
|
+
export { useEnvironment } from './useEnvironment';
|
|
12
|
+
export { useLanguage } from './useLanguage';
|
|
13
|
+
export { useValues } from './useValues';
|
|
14
|
+
export { useFunctions } from './useFunctions';
|
|
15
|
+
export { useCurrentPage } from './useCurrentPage';
|
|
16
|
+
export { useCustomData } from './useCustomData';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { ChaynsFunctionsContext } from '../components/ChaynsContext';
|
|
4
|
+
import { useWindowMetrics } from './windowMetricsListener';
|
|
5
|
+
/**
|
|
6
|
+
* @category Hooks
|
|
7
|
+
*/
|
|
8
|
+
export const useScrollListener = () => {
|
|
9
|
+
const addListener = useContextSelector(ChaynsFunctionsContext, v => v.addScrollListener);
|
|
10
|
+
const removeListener = useContextSelector(ChaynsFunctionsContext, v => v.removeScrollListener);
|
|
11
|
+
const promiseRef = useRef();
|
|
12
|
+
return useCallback((value, callback) => {
|
|
13
|
+
promiseRef.current = addListener(value, callback);
|
|
14
|
+
return () => {
|
|
15
|
+
var _promiseRef$current;
|
|
16
|
+
void ((_promiseRef$current = promiseRef.current) === null || _promiseRef$current === void 0 ? void 0 : _promiseRef$current.then(removeListener));
|
|
17
|
+
};
|
|
18
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
19
|
+
}, []);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @category Hooks
|
|
23
|
+
*/
|
|
24
|
+
export const useScrollPosition = function () {
|
|
25
|
+
let {
|
|
26
|
+
enabled = true,
|
|
27
|
+
throttle = 200
|
|
28
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
29
|
+
const [value, setValue] = useState({
|
|
30
|
+
scrollY: null,
|
|
31
|
+
scrollX: null
|
|
32
|
+
});
|
|
33
|
+
const addListener = useScrollListener();
|
|
34
|
+
const getScrollPosition = useContextSelector(ChaynsFunctionsContext, v => v.getScrollPosition);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (enabled) {
|
|
37
|
+
return addListener({
|
|
38
|
+
throttle
|
|
39
|
+
}, setValue);
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
43
|
+
}, [enabled, throttle, addListener]);
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
void getScrollPosition().then(setValue);
|
|
46
|
+
}, [getScrollPosition]);
|
|
47
|
+
return value;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* @category Hooks
|
|
51
|
+
*/
|
|
52
|
+
export const useScrollOffsetTop = function () {
|
|
53
|
+
let {
|
|
54
|
+
enabled = true,
|
|
55
|
+
throttle = 200
|
|
56
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
57
|
+
const scrollPosition = useScrollPosition({
|
|
58
|
+
enabled,
|
|
59
|
+
throttle
|
|
60
|
+
});
|
|
61
|
+
const {
|
|
62
|
+
offsetTop,
|
|
63
|
+
topBarHeight
|
|
64
|
+
} = useWindowMetrics({
|
|
65
|
+
enabled
|
|
66
|
+
});
|
|
67
|
+
if (typeof (scrollPosition === null || scrollPosition === void 0 ? void 0 : scrollPosition.scrollY) === 'number') {
|
|
68
|
+
return Math.max(0, scrollPosition.scrollY - (offsetTop - topBarHeight));
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @category Hooks
|
|
74
|
+
*/
|
|
75
|
+
export const useScrollOffsetBottom = function () {
|
|
76
|
+
let {
|
|
77
|
+
enabled = true,
|
|
78
|
+
throttle = 200
|
|
79
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
80
|
+
const scrollPosition = useScrollPosition({
|
|
81
|
+
enabled,
|
|
82
|
+
throttle
|
|
83
|
+
});
|
|
84
|
+
const {
|
|
85
|
+
offsetTop,
|
|
86
|
+
windowHeight,
|
|
87
|
+
bottomBarHeight
|
|
88
|
+
} = useWindowMetrics({
|
|
89
|
+
enabled
|
|
90
|
+
});
|
|
91
|
+
if (typeof (scrollPosition === null || scrollPosition === void 0 ? void 0 : scrollPosition.scrollY) === 'number') {
|
|
92
|
+
return windowHeight - Math.max(0, offsetTop - scrollPosition.scrollY + bottomBarHeight);
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useContextSelector } from "use-context-selector";
|
|
3
|
+
import { ChaynsFunctionsContext } from "../components/ChaynsContext";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @category Hooks
|
|
7
|
+
*/
|
|
8
|
+
export const useAccessToken = accessToken => {
|
|
9
|
+
const [token, setToken] = useState(null);
|
|
10
|
+
const [externalToken, setExternalToken] = useState(null);
|
|
11
|
+
const getAccessToken = useContextSelector(ChaynsFunctionsContext, s => s.getAccessToken);
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
if (accessToken !== null && accessToken !== void 0 && accessToken.external) {
|
|
14
|
+
void getAccessToken({
|
|
15
|
+
external: true
|
|
16
|
+
}).then(setExternalToken);
|
|
17
|
+
} else {
|
|
18
|
+
void getAccessToken().then(setToken);
|
|
19
|
+
}
|
|
20
|
+
}, [accessToken === null || accessToken === void 0 ? void 0 : accessToken.external, getAccessToken]);
|
|
21
|
+
return accessToken !== null && accessToken !== void 0 && accessToken.external ? externalToken === null || externalToken === void 0 ? void 0 : externalToken.accessToken : token === null || token === void 0 ? void 0 : token.accessToken;
|
|
22
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @category Hooks
|
|
6
|
+
*/
|
|
7
|
+
export const useCurrentPage = () => useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.currentPage);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @category Hooks
|
|
6
|
+
*/
|
|
7
|
+
export const useCustomData = () => useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.customData);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useDevice = () => useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.device);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useEnvironment = () => useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.environment);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContextSelector } from "use-context-selector";
|
|
2
|
+
import { ChaynsFunctionsContext } from "../components/ChaynsContext";
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useFunctions = () => {
|
|
7
|
+
const t = useContextSelector(ChaynsFunctionsContext, f => f || {});
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useIsAdminMode = () => {
|
|
7
|
+
var _useContextSelector;
|
|
8
|
+
return (_useContextSelector = useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.isAdminModeActive)) !== null && _useContextSelector !== void 0 ? _useContextSelector : false;
|
|
9
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useLanguage = () => useContextSelector(ChaynsContext, v => v === null || v === void 0 ? void 0 : v.language);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const usePages = function () {
|
|
7
|
+
let {
|
|
8
|
+
siteId
|
|
9
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
|
|
10
|
+
siteId: undefined
|
|
11
|
+
};
|
|
12
|
+
const pages = useContextSelector(ChaynsContext, v => v.pages);
|
|
13
|
+
if (siteId) {
|
|
14
|
+
pages.filter(tapp => tapp.siteId === siteId);
|
|
15
|
+
}
|
|
16
|
+
return pages;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* @category Hooks
|
|
20
|
+
*/
|
|
21
|
+
export const usePage = _ref => {
|
|
22
|
+
let {
|
|
23
|
+
id,
|
|
24
|
+
siteId
|
|
25
|
+
} = _ref;
|
|
26
|
+
const pages = useContextSelector(ChaynsContext, v => v.pages);
|
|
27
|
+
if (id) {
|
|
28
|
+
var _pages$find;
|
|
29
|
+
return (_pages$find = pages.find(x => x.id === id && (!siteId || x.siteId === siteId))) !== null && _pages$find !== void 0 ? _pages$find : null;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useParameters = () => useContextSelector(ChaynsContext, v => (v === null || v === void 0 ? void 0 : v.parameters) || []);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
export const useUser = () => useContextSelector(ChaynsContext, v => (v === null || v === void 0 ? void 0 : v.user) || {});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { ChaynsContext } from '../components/ChaynsContext';
|
|
3
|
+
/**
|
|
4
|
+
* @category Hooks
|
|
5
|
+
*/
|
|
6
|
+
export const useValues = () => {
|
|
7
|
+
const t = useContextSelector(ChaynsContext, v => v || {});
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useContextSelector } from 'use-context-selector';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { ChaynsFunctionsContext } from '../components/ChaynsContext';
|
|
4
|
+
import { ScreenSize } from '../types/IChaynsReact';
|
|
5
|
+
/**
|
|
6
|
+
* @category Hooks
|
|
7
|
+
*/
|
|
8
|
+
export const useWindowMetricsListener = () => {
|
|
9
|
+
const addListener = useContextSelector(ChaynsFunctionsContext, v => v.addWindowMetricsListener);
|
|
10
|
+
const removeListener = useContextSelector(ChaynsFunctionsContext, v => v.removeWindowMetricsListener);
|
|
11
|
+
const promiseRef = useRef();
|
|
12
|
+
return useCallback(callback => {
|
|
13
|
+
promiseRef.current = addListener(callback);
|
|
14
|
+
return () => {
|
|
15
|
+
var _promiseRef$current;
|
|
16
|
+
void ((_promiseRef$current = promiseRef.current) === null || _promiseRef$current === void 0 ? void 0 : _promiseRef$current.then(removeListener));
|
|
17
|
+
};
|
|
18
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
19
|
+
}, []);
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @category Hooks
|
|
23
|
+
*/
|
|
24
|
+
export const useWindowMetrics = function () {
|
|
25
|
+
let {
|
|
26
|
+
enabled = true
|
|
27
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
28
|
+
const [value, setValue] = useState({
|
|
29
|
+
bottomBarHeight: 0,
|
|
30
|
+
offsetTop: 0,
|
|
31
|
+
pageHeight: 0,
|
|
32
|
+
pageWidth: 0,
|
|
33
|
+
topBarHeight: 0,
|
|
34
|
+
windowHeight: 0,
|
|
35
|
+
pageSize: ScreenSize.XS
|
|
36
|
+
});
|
|
37
|
+
const addListener = useWindowMetricsListener();
|
|
38
|
+
const getWindowMetrics = useContextSelector(ChaynsFunctionsContext, v => v.getWindowMetrics);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (enabled) {
|
|
41
|
+
return addListener(v => {
|
|
42
|
+
setValue(v);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}, [enabled, addListener]);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
void getWindowMetrics().then(setValue);
|
|
49
|
+
}, [getWindowMetrics]);
|
|
50
|
+
return value;
|
|
51
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import HostIframe from './iframe/HostIframe';
|
|
3
|
+
import ModuleHost from './module/ModuleHost';
|
|
4
|
+
const ChaynsHost = _ref => {
|
|
5
|
+
let {
|
|
6
|
+
type,
|
|
7
|
+
iFrameProps,
|
|
8
|
+
functions,
|
|
9
|
+
src,
|
|
10
|
+
iFrameRef = undefined,
|
|
11
|
+
children = undefined,
|
|
12
|
+
system,
|
|
13
|
+
// shallow data
|
|
14
|
+
pages,
|
|
15
|
+
language,
|
|
16
|
+
isAdminModeActive,
|
|
17
|
+
site,
|
|
18
|
+
user,
|
|
19
|
+
currentPage,
|
|
20
|
+
device,
|
|
21
|
+
parameters,
|
|
22
|
+
customData,
|
|
23
|
+
environment
|
|
24
|
+
} = _ref;
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'client-iframe':
|
|
27
|
+
return /*#__PURE__*/React.createElement(HostIframe, {
|
|
28
|
+
iFrameRef: iFrameRef,
|
|
29
|
+
iFrameProps: iFrameProps,
|
|
30
|
+
pages: pages,
|
|
31
|
+
isAdminModeActive: isAdminModeActive,
|
|
32
|
+
site: site,
|
|
33
|
+
user: user,
|
|
34
|
+
device: device,
|
|
35
|
+
currentPage: currentPage,
|
|
36
|
+
functions: functions,
|
|
37
|
+
src: src,
|
|
38
|
+
language: language,
|
|
39
|
+
parameters: parameters,
|
|
40
|
+
environment: environment,
|
|
41
|
+
customData: customData
|
|
42
|
+
});
|
|
43
|
+
case 'client-module':
|
|
44
|
+
return /*#__PURE__*/React.createElement(ModuleHost, {
|
|
45
|
+
system: system,
|
|
46
|
+
pages: pages,
|
|
47
|
+
isAdminModeActive: isAdminModeActive,
|
|
48
|
+
site: site,
|
|
49
|
+
user: user,
|
|
50
|
+
device: device,
|
|
51
|
+
currentPage: currentPage,
|
|
52
|
+
functions: functions,
|
|
53
|
+
language: language,
|
|
54
|
+
parameters: parameters,
|
|
55
|
+
customData: customData,
|
|
56
|
+
environment: environment
|
|
57
|
+
});
|
|
58
|
+
case 'server-iframe':
|
|
59
|
+
return /*#__PURE__*/React.createElement(HostIframe, {
|
|
60
|
+
iFrameRef: iFrameRef,
|
|
61
|
+
iFrameProps: iFrameProps,
|
|
62
|
+
pages: pages,
|
|
63
|
+
isAdminModeActive: isAdminModeActive,
|
|
64
|
+
site: site,
|
|
65
|
+
user: user,
|
|
66
|
+
device: device,
|
|
67
|
+
currentPage: currentPage,
|
|
68
|
+
functions: functions,
|
|
69
|
+
src: src,
|
|
70
|
+
postForm: true,
|
|
71
|
+
language: language,
|
|
72
|
+
parameters: parameters,
|
|
73
|
+
environment: environment,
|
|
74
|
+
customData: customData
|
|
75
|
+
});
|
|
76
|
+
case 'server-module':
|
|
77
|
+
return /*#__PURE__*/React.createElement(ModuleHost, {
|
|
78
|
+
system: system,
|
|
79
|
+
pages: pages,
|
|
80
|
+
isAdminModeActive: isAdminModeActive,
|
|
81
|
+
site: site,
|
|
82
|
+
user: user,
|
|
83
|
+
device: device,
|
|
84
|
+
currentPage: currentPage,
|
|
85
|
+
functions: functions,
|
|
86
|
+
language: language,
|
|
87
|
+
parameters: parameters,
|
|
88
|
+
customData: customData,
|
|
89
|
+
environment: environment
|
|
90
|
+
}, children);
|
|
91
|
+
default:
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
export default ChaynsHost;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
2
|
+
import React, { useEffect, useRef } from 'react';
|
|
3
|
+
import * as comlink from 'comlink';
|
|
4
|
+
import postIframeForm from '../../util/postIframeForm';
|
|
5
|
+
import useUpdateData from './utils/useUpdateData';
|
|
6
|
+
const HostIframe = _ref => {
|
|
7
|
+
let {
|
|
8
|
+
iFrameProps,
|
|
9
|
+
src,
|
|
10
|
+
postForm = false,
|
|
11
|
+
iFrameRef,
|
|
12
|
+
// shallow data
|
|
13
|
+
pages,
|
|
14
|
+
isAdminModeActive,
|
|
15
|
+
site,
|
|
16
|
+
user,
|
|
17
|
+
currentPage,
|
|
18
|
+
functions,
|
|
19
|
+
device,
|
|
20
|
+
language,
|
|
21
|
+
parameters,
|
|
22
|
+
environment,
|
|
23
|
+
customData
|
|
24
|
+
} = _ref;
|
|
25
|
+
const eventTarget = useRef();
|
|
26
|
+
const ref = useRef();
|
|
27
|
+
const currentDataRef = useRef();
|
|
28
|
+
if (!eventTarget.current) {
|
|
29
|
+
eventTarget.current = global.document ? document.createElement('div') : undefined; // global.EventTarget ? new EventTarget() : undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const setHeight = value => {
|
|
33
|
+
if (ref.current) {
|
|
34
|
+
ref.current.height = `${value}px`;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// region initialData
|
|
39
|
+
const initialData = {
|
|
40
|
+
site,
|
|
41
|
+
isAdminModeActive,
|
|
42
|
+
pages,
|
|
43
|
+
currentPage,
|
|
44
|
+
device,
|
|
45
|
+
user,
|
|
46
|
+
language,
|
|
47
|
+
parameters,
|
|
48
|
+
environment,
|
|
49
|
+
customData
|
|
50
|
+
};
|
|
51
|
+
// endregion
|
|
52
|
+
|
|
53
|
+
currentDataRef.current = initialData;
|
|
54
|
+
|
|
55
|
+
// region postIframeForm
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
(async () => {
|
|
58
|
+
if (postForm) {
|
|
59
|
+
var _await$functions$getA;
|
|
60
|
+
const accessToken = (_await$functions$getA = await functions.getAccessToken()) !== null && _await$functions$getA !== void 0 ? _await$functions$getA : {};
|
|
61
|
+
void postIframeForm(src, JSON.stringify({
|
|
62
|
+
...initialData,
|
|
63
|
+
pages: undefined,
|
|
64
|
+
...accessToken
|
|
65
|
+
}), 'chayns', iFrameProps.name);
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
+
}, []);
|
|
70
|
+
// endregion
|
|
71
|
+
|
|
72
|
+
// region expose data and functions to iframe
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
var _ref$current;
|
|
75
|
+
if ((_ref$current = ref.current) !== null && _ref$current !== void 0 && _ref$current.contentWindow) {
|
|
76
|
+
const obj = {
|
|
77
|
+
[iFrameProps.name]: {
|
|
78
|
+
functions: {
|
|
79
|
+
...functions,
|
|
80
|
+
setHeight
|
|
81
|
+
},
|
|
82
|
+
addDataListener: cb => {
|
|
83
|
+
if (eventTarget.current) eventTarget.current.addEventListener('data_update', e => e.detail && cb(e.detail));
|
|
84
|
+
},
|
|
85
|
+
getInitialData: () => currentDataRef.current
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
comlink.expose(obj, comlink.windowEndpoint(ref.current.contentWindow));
|
|
89
|
+
ref.current.contentWindow.postMessage('chayns-api-host-ready', '*');
|
|
90
|
+
// https://github.com/GoogleChromeLabs/comlink/pull/469 might be better approach once released
|
|
91
|
+
return () => {
|
|
92
|
+
delete obj[iFrameProps.name];
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
97
|
+
}, []);
|
|
98
|
+
// endregion
|
|
99
|
+
|
|
100
|
+
// region dispatch data update
|
|
101
|
+
useUpdateData(eventTarget.current, 'isAdminModeActive', isAdminModeActive);
|
|
102
|
+
useUpdateData(eventTarget.current, 'user', user);
|
|
103
|
+
useUpdateData(eventTarget.current, 'site', site);
|
|
104
|
+
useUpdateData(eventTarget.current, 'pages', pages);
|
|
105
|
+
useUpdateData(eventTarget.current, 'language', language);
|
|
106
|
+
useUpdateData(eventTarget.current, 'parameters', parameters);
|
|
107
|
+
useUpdateData(eventTarget.current, 'environment', environment);
|
|
108
|
+
useUpdateData(eventTarget.current, 'customData', customData);
|
|
109
|
+
// endregion
|
|
110
|
+
|
|
111
|
+
return /*#__PURE__*/React.createElement("iframe", _extends({
|
|
112
|
+
ref: r => {
|
|
113
|
+
ref.current = r;
|
|
114
|
+
if (iFrameRef) {
|
|
115
|
+
// eslint-disable-next-line no-param-reassign
|
|
116
|
+
iFrameRef.current = r;
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
title: " "
|
|
120
|
+
}, iFrameProps, {
|
|
121
|
+
src: postForm ? undefined : src
|
|
122
|
+
}));
|
|
123
|
+
};
|
|
124
|
+
export default HostIframe;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
const useUpdateData = (target, type, value) => {
|
|
3
|
+
useEffect(() => {
|
|
4
|
+
if (target) {
|
|
5
|
+
target.dispatchEvent(new CustomEvent('data_update', {
|
|
6
|
+
detail: {
|
|
7
|
+
type,
|
|
8
|
+
value
|
|
9
|
+
}
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
}, [target, type, value]);
|
|
13
|
+
};
|
|
14
|
+
export default useUpdateData;
|