lupine.web 1.1.2 → 1.1.4
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/package.json +1 -1
- package/src/core/initialize.ts +9 -5
- package/src/core/render-component.ts +1 -1
- package/src/lib/index.ts +1 -0
- package/src/lib/web-config.ts +79 -0
- package/src/lib/web-env.ts +47 -46
package/package.json
CHANGED
package/src/core/initialize.ts
CHANGED
|
@@ -8,9 +8,10 @@ import { callPageLoadedEvent } from './page-loaded-events';
|
|
|
8
8
|
import { initServerCookies } from './server-cookie';
|
|
9
9
|
import { IToClientDelivery } from '../models';
|
|
10
10
|
import { getMetaDataObject, getMetaDataTags, getPageTitle } from './bind-meta';
|
|
11
|
-
import { initWebEnv
|
|
11
|
+
import { initWebEnv } from '../lib/web-env';
|
|
12
12
|
import { _lupineJs, PageProps, PageResultType, setRenderPageProps } from './export-lupine';
|
|
13
13
|
import { isFrontEnd } from '../lib/is-frontend';
|
|
14
|
+
import { WebConfig } from '../lib/web-config';
|
|
14
15
|
|
|
15
16
|
const logger = new Logger('initialize');
|
|
16
17
|
|
|
@@ -26,7 +27,8 @@ const generatePage = async (props: PageProps, toClientDelivery: IToClientDeliver
|
|
|
26
27
|
setRenderPageProps(props);
|
|
27
28
|
|
|
28
29
|
initWebEnv(toClientDelivery.getWebEnv());
|
|
29
|
-
|
|
30
|
+
WebConfig.initFromData(toClientDelivery.getWebSetting());
|
|
31
|
+
// initWebSetting(toClientDelivery.getWebSetting());
|
|
30
32
|
initServerCookies(toClientDelivery.getServerCookie());
|
|
31
33
|
// callPageResetEvent();
|
|
32
34
|
callPageLoadedEvent();
|
|
@@ -57,12 +59,14 @@ const generatePage = async (props: PageProps, toClientDelivery: IToClientDeliver
|
|
|
57
59
|
};
|
|
58
60
|
_lupineJs.generatePage = generatePage;
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
const _initSaved = {
|
|
63
|
+
pageInitialized: false,
|
|
64
|
+
};
|
|
61
65
|
// this is called in the FE when the document is loaded
|
|
62
66
|
// to avoid circular reference, bindLinks can't call initializePage directly
|
|
63
67
|
export const initializePage = async (newUrl?: string) => {
|
|
64
|
-
const currentPageInitialized =
|
|
65
|
-
|
|
68
|
+
const currentPageInitialized = _initSaved.pageInitialized;
|
|
69
|
+
_initSaved.pageInitialized = true;
|
|
66
70
|
logger.log('initializePage: ', newUrl);
|
|
67
71
|
if (newUrl) {
|
|
68
72
|
window.history.pushState({ urlPath: newUrl }, '', newUrl);
|
|
@@ -209,7 +209,7 @@ export const renderComponent = (type: any, props: any, uniqueClassName?: string,
|
|
|
209
209
|
props._html.push(`<style id="sty-${newUniqueClassName}">${cssText}</style>`); // sty means style, and updateStyles has the same name
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
if (newProps.children) {
|
|
212
|
+
if (newProps.children || newProps.children === 0) {
|
|
213
213
|
// if (newProps._lb) {
|
|
214
214
|
// assignLabels(newProps._lb, newProps.children);
|
|
215
215
|
// }
|
package/src/lib/index.ts
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { getRenderPageProps } from 'lupine.web';
|
|
2
|
+
|
|
3
|
+
// for mobile app, it needs the url to fetch the config for the first time
|
|
4
|
+
export const bindWebConfigApi = (webConfigApi: string) => {
|
|
5
|
+
WebConfig.webConfigApi = webConfigApi;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class WebConfig {
|
|
9
|
+
static webConfigApi = '';
|
|
10
|
+
static initialized = false;
|
|
11
|
+
static cfg: { [key: string]: string } = {};
|
|
12
|
+
|
|
13
|
+
// called from generatePage (SSR)
|
|
14
|
+
static initFromData(cfg: { [key: string]: string }) {
|
|
15
|
+
this.initialized = true;
|
|
16
|
+
this.cfg = cfg;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static async init(force?: boolean) {
|
|
20
|
+
if (this.initialized && !force) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
this.initialized = true;
|
|
24
|
+
|
|
25
|
+
// For web, it's injected in the html by SSR, but for mobile, it's fetched from api
|
|
26
|
+
if (typeof document === 'object' && !force) {
|
|
27
|
+
const json = document.querySelector('#web-setting')?.textContent;
|
|
28
|
+
if (json) {
|
|
29
|
+
this.cfg = JSON.parse(json);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!this.webConfigApi) {
|
|
35
|
+
console.error('WebConfig webConfigApi is not set');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const url = getRenderPageProps().renderPageFunctions.baseUrl(this.webConfigApi);
|
|
39
|
+
const data = await getRenderPageProps().renderPageFunctions.fetchData(url);
|
|
40
|
+
if (data && data.json && data.json.status === 'ok') {
|
|
41
|
+
this.cfg = data.json.result;
|
|
42
|
+
} else {
|
|
43
|
+
console.error(data?.json?.message || 'Failed to get web config');
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static async get(key: string, defaultValue: number): Promise<number>;
|
|
48
|
+
static async get(key: string, defaultValue: string): Promise<string>;
|
|
49
|
+
static async get(key: string, defaultValue: boolean): Promise<boolean>;
|
|
50
|
+
static async get(key: string, defaultValue: object): Promise<object>;
|
|
51
|
+
static async get(key: string, defaultValue?: any): Promise<any> {
|
|
52
|
+
await WebConfig.init();
|
|
53
|
+
|
|
54
|
+
const v = WebConfig.cfg[key];
|
|
55
|
+
if (typeof v === 'undefined') {
|
|
56
|
+
return defaultValue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (typeof defaultValue === 'number') {
|
|
60
|
+
return Number.parseInt(v!);
|
|
61
|
+
}
|
|
62
|
+
if (typeof defaultValue === 'boolean') {
|
|
63
|
+
return v!.toLocaleLowerCase() === 'true' || v === '1';
|
|
64
|
+
}
|
|
65
|
+
if (typeof defaultValue === 'object') {
|
|
66
|
+
if (typeof v === 'object') {
|
|
67
|
+
return v;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(v!);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
console.error(`WebConfig JSON.parse error: `, error);
|
|
73
|
+
}
|
|
74
|
+
return defaultValue;
|
|
75
|
+
}
|
|
76
|
+
// if empty, then return default value
|
|
77
|
+
return v || defaultValue;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/lib/web-env.ts
CHANGED
|
@@ -47,52 +47,53 @@ function initWebEnv(webEnv: { [key: string]: string }) {
|
|
|
47
47
|
_webEnvInitialized = true;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
// _webSetting is for
|
|
51
|
-
// _webSetting is
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
function webSetting(key: string, defaultValue:
|
|
56
|
-
function webSetting(key: string, defaultValue:
|
|
57
|
-
function webSetting(key: string, defaultValue:
|
|
58
|
-
function webSetting(key: string, defaultValue:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
50
|
+
// _webSetting is removed to WebConfig because for mobile app, it's async to load webconfig from an api
|
|
51
|
+
// // _webSetting is for dynamic settings that can be changed without redeploying the app
|
|
52
|
+
// // _webSetting is json format so the returning can be an object
|
|
53
|
+
// const _webSetting: { [key: string]: string } = {};
|
|
54
|
+
// let _webSettingInitialized = false;
|
|
55
|
+
// function webSetting(key: string, defaultValue: number): number;
|
|
56
|
+
// function webSetting(key: string, defaultValue: string): string;
|
|
57
|
+
// function webSetting(key: string, defaultValue: boolean): boolean;
|
|
58
|
+
// function webSetting(key: string, defaultValue: object): object;
|
|
59
|
+
// function webSetting(key: string, defaultValue: any): any {
|
|
60
|
+
// // for SSR, the webSetting should be initialized. But for the FE, it should be initialized by the webSetting script tag
|
|
61
|
+
// if (!_webSettingInitialized) {
|
|
62
|
+
// const json = document.querySelector('#web-setting')?.textContent;
|
|
63
|
+
// if (json) {
|
|
64
|
+
// _webSettingInitialized = true;
|
|
65
|
+
// initWebSetting(JSON.parse(json));
|
|
66
|
+
// }
|
|
67
|
+
// }
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
// !_webSettingInitialized && console.warn('webSetting has not been initialized yet!');
|
|
70
|
+
// if (typeof _webSetting[key] === 'undefined') {
|
|
71
|
+
// return defaultValue;
|
|
72
|
+
// }
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
// this is only called from the server side for SSR
|
|
93
|
-
function initWebSetting(webSetting: { [key: string]: string }) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
74
|
+
// if (typeof defaultValue === 'number') {
|
|
75
|
+
// return Number.parseInt(_webSetting[key]!);
|
|
76
|
+
// }
|
|
77
|
+
// if (typeof defaultValue === 'boolean') {
|
|
78
|
+
// return _webSetting[key]!.toLocaleLowerCase() === 'true' || _webSetting[key] === '1';
|
|
79
|
+
// }
|
|
80
|
+
// if (typeof defaultValue === 'object') {
|
|
81
|
+
// if (typeof _webSetting[key] === 'object') {
|
|
82
|
+
// return _webSetting[key];
|
|
83
|
+
// }
|
|
84
|
+
// try {
|
|
85
|
+
// return JSON.parse(_webSetting[key]!);
|
|
86
|
+
// } catch (error) {
|
|
87
|
+
// console.error(`webSetting JSON.parse error: `, error);
|
|
88
|
+
// }
|
|
89
|
+
// return defaultValue;
|
|
90
|
+
// }
|
|
91
|
+
// return _webSetting[key] || defaultValue;
|
|
92
|
+
// }
|
|
93
|
+
// // this is only called from the server side for SSR
|
|
94
|
+
// function initWebSetting(webSetting: { [key: string]: string }) {
|
|
95
|
+
// Object.assign(_webSetting, webSetting);
|
|
96
|
+
// _webSettingInitialized = true;
|
|
97
|
+
// }
|
|
97
98
|
|
|
98
|
-
export { initWebEnv, webEnv
|
|
99
|
+
export { initWebEnv, webEnv };
|