Skip to content

Commit 86ad25b

Browse files
authored
refactor: make configuration initial only (#638)
BREAKING CHANGE: Theming.js no longer has getTheme and setTheme methods. Use Configuration.js instead.
1 parent 9985634 commit 86ad25b

File tree

103 files changed

+400
-2523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+400
-2523
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ import "@ui5/webcomponents/dist/Label.js"; // loads ui5-label
8585
## Configure
8686
UI5 Web Components have built-in internationalization and globalization support. Language, compact/cozy switch, date/time settings and theme can be changed with parameters.
8787

88-
To provide configuration settings, create a ```script``` tag having ```data-id="sap-ui-config"``` and ```type="application/json"```:
88+
To provide configuration settings, create a ```script``` tag having ```data-ui5-config``` and ```type="application/json"```:
8989

9090
```html
91-
<script data-id="sap-ui-config" type="application/json">
91+
<script data-ui5-config type="application/json">
9292
{
9393
"theme": "sap_belize",
9494
"language": "EN"
@@ -101,7 +101,7 @@ To provide configuration settings, create a ```script``` tag having ```data-id="
101101
UI5 Web Components support right-to-left text direction (RTL). To enable RTL globally, provide the option ```rtl: true``` in the configuration ```script``` tag:
102102

103103
```html
104-
<script data-id="sap-ui-config" type="application/json">
104+
<script data-ui5-config type="application/json">
105105
{
106106
"language": "en",
107107
"rtl": true
@@ -113,7 +113,7 @@ UI5 Web Components support right-to-left text direction (RTL). To enable RTL glo
113113
UI5 Web Components supports ```Compact``` and ```Cozy``` mode. It is set to ```Cozy``` by default. To enable ```Compact``` globally, provide the option ```compactSize: true``` in the configuration ```script``` tag:
114114

115115
```html
116-
<script data-id="sap-ui-config" type="application/json">
116+
<script data-ui5-config type="application/json">
117117
{
118118
"compactSize": true
119119
}
@@ -124,7 +124,7 @@ UI5 Web Components supports ```Compact``` and ```Cozy``` mode. It is set to ```C
124124
UI5 Web Components support different calendar types (Gregorian, Islamic, Japanese, Buddhist and Persian). To change them, provide the option ```calendarType: "Islamic"``` in the configuration ```script``` tag:
125125

126126
```html
127-
<script data-id="sap-ui-config" type="application/json">
127+
<script data-ui5-config type="application/json">
128128
{
129129
"calendarType": "Islamic"
130130
}

docs/Configuration.md

+32-3
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,51 @@ There are several configuration settings that affect all UI5 Web Components glob
99
------------ | ----------------------------------------------- | ------------- | -------------------------------------------------------------
1010
theme | sap_fiori_3, sap_belize, sap_belize_hcb | sap_fiori_3 | Visual theme
1111
language | en, de, es, etc... | en | Language to be used for translatable texts
12-
rtl | true, false | false | When true, sets global text direction to right-to-left
12+
rtl* | true, false | false | When true, sets global text direction to right-to-left
1313
compactSize | true, false | false | When set, enforces compact density (smaller margins/paddings)
1414
calendarType | Gregorian, Islamic, Buddhist, Japanese, Persian | Gregorian | Default calendar type for date-related web components
15+
noConflict** | true, false | Object | false | When set to true, all events will be fired with a "ui5-" prefix only
16+
17+
`*` When the `rtl` setting is set to `true`, UI5 Web Components will adjust their styling accordingly.
18+
However, you should also set the HTML attribute `dir` to `rtl` on the `body` or `html` or any other relevant region of your application
19+
so that the rest of your application is also affected.
20+
21+
`**` By default UI5 Web Components fire all custom events twice - once with the documented name (f.e. `change`), and once more with a `ui5-` prefix (f.e. `ui5-change`).
22+
For example, when the `ui5-switch` is toggled, it fires a `change` event, but also a `ui5-change` event.
23+
24+
The `noConflict` configuration setting allows certain control over this behavior:
25+
- When `false` (default value) all custom events are fired with and without the `ui5-` prefix.
26+
- When `true` all custom events are fired with the `ui5-` prefix **only**.
27+
This is handy for example if the name of some event happens to collide with the name of an event provided by a third-party library.
28+
- When an object is supplied, just the specified events will be fired with the `ui5-` prefix **only**.
29+
All other events will be fired normally - once with the prefix, and once without.
30+
The format of this object is as follows:
31+
```json
32+
{
33+
"events": ["selectionChange", "headerClick"]
34+
}
35+
```
36+
*Please note that other keys may be added to this object in the future for the purpose of name conflict resolution.*
37+
38+
In the above example, only the `selectionChange` and `headerClick` events will not be fired without a prefix.
39+
You can still use them by listening to `ui5-selectionChange` and `ui5-headerClick`, but the names `selectionChange` and `headerClick` will be
40+
free for use by other UI components and libraries without name collision.
1541

1642
## Configuration script
1743

1844
In order to provide configuration settings, include the following ```<script>``` element in your HTML page:
1945

2046
```html
21-
<script data-id="sap-ui-config" type="application/json">
47+
<script data-ui5-config type="application/json">
2248
{
2349
"rtl": true,
2450
"compactSize": true,
2551
"language": "ja",
2652
"calendarType": "Japanese",
27-
"theme": "sap_belize_hcb"
53+
"theme": "sap_belize_hcb",
54+
"noConflict": {
55+
"events": ["selectionChange", "headerClick"]
56+
}
2857
}
2958
</script>
3059
```

docs/PublicModuleImports.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ import "@ui5/webcomponents/dist/ThemePropertiesProvider.js";
116116

117117
and
118118
```js
119-
import { setTheme } from "@ui5/webcomponents-base/dist/Theming.js";
119+
import { setTheme } from "@ui5/webcomponents-base/dist/Configuration.js";
120120
```
121121
(for changing the theme at runtime)
122122

@@ -145,7 +145,7 @@ By importing the second module, you get the:
145145
method that allows you to change the theme during runtime, if necessary.
146146
Example:
147147
```js
148-
import { setTheme } from "@ui5/webcomponents-base/dist/Theming.js";
148+
import { setTheme } from "@ui5/webcomponents-base/dist/Configuration.js";
149149
setTheme("sap_belize_hcb");
150150
```
151151

packages/base/src/CSS.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getTheme } from "./Configuration.js";
21
import { getEffectiveStyle } from "./Theming.js";
2+
import { getTheme } from "./config/Theme.js";
33
import { injectWebComponentStyle } from "./theming/StyleInjection.js";
44

55
const styleMap = new Map();

packages/base/src/Configuration.js

+12-140
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,21 @@
1-
import CalendarType from "@ui5/webcomponents-core/dist/sap/ui/core/CalendarType.js";
2-
import getDesigntimePropertyAsArray from "./util/getDesigntimePropertyAsArray.js";
1+
import { getTheme, setTheme } from "./config/Theme.js";
2+
import { getNoConflict, setNoConflict } from "./config/NoConflict.js";
3+
import { getCompactSize } from "./config/CompactSize.js";
4+
import { getRTL } from "./config/RTL.js";
5+
import { getLanguage, getSupportedLanguages } from "./config/Language.js";
6+
import { getCalendarType } from "./config/CalendarType.js";
37

4-
let initialized = false;
5-
6-
const CONFIGURATION = {
7-
theme: "sap_fiori_3",
8-
rtl: null,
9-
language: null,
10-
compactSize: false,
11-
supportedLanguages: null,
12-
calendarType: null,
13-
derivedRTL: null,
14-
"xx-wc-no-conflict": false, // no URL
15-
};
16-
17-
/* General settings */
18-
const getTheme = () => {
19-
initConfiguration();
20-
return CONFIGURATION.theme;
21-
};
22-
23-
const getRTL = () => {
24-
initConfiguration();
25-
return CONFIGURATION.rtl;
26-
};
27-
28-
const getLanguage = () => {
29-
initConfiguration();
30-
return CONFIGURATION.language;
31-
};
32-
33-
const getCompactSize = () => {
34-
initConfiguration();
35-
return CONFIGURATION.compactSize;
36-
};
37-
38-
const getSupportedLanguages = () => {
39-
return getDesigntimePropertyAsArray("$core-i18n-locales:,ar,bg,ca,cs,da,de,el,en,es,et,fi,fr,hi,hr,hu,it,iw,ja,ko,lt,lv,nl,no,pl,pt,ro,ru,sh,sk,sl,sv,th,tr,uk,vi,zh_CN,zh_TW$");
40-
};
41-
42-
const getWCNoConflict = () => {
43-
initConfiguration();
44-
return CONFIGURATION["xx-wc-no-conflict"];
45-
};
46-
47-
const _setWCNoConflict = value => {
48-
CONFIGURATION["xx-wc-no-conflict"] = value;
49-
};
50-
51-
/* Calendar stuff */
52-
const getCalendarType = () => {
53-
initConfiguration();
54-
if (CONFIGURATION.calendarType) {
55-
const type = Object.keys(CalendarType).filter(calType => calType === CONFIGURATION.calendarType)[0];
56-
57-
if (type) {
58-
return type;
59-
}
60-
}
61-
62-
return CalendarType.Gregorian;
63-
};
64-
65-
const getOriginInfo = () => {};
66-
67-
const getLocale = () => {
68-
initConfiguration();
69-
return CONFIGURATION.language;
70-
};
71-
72-
const _setTheme = themeName => {
73-
CONFIGURATION.theme = themeName;
74-
};
75-
76-
const booleanMapping = new Map();
77-
booleanMapping.set("true", true);
78-
booleanMapping.set("false", false);
79-
80-
let runtimeConfig = {};
81-
82-
const parseConfigurationScript = () => {
83-
const configScript = document.querySelector("[data-id='sap-ui-config']");
84-
let configJSON;
85-
86-
if (configScript) {
87-
try {
88-
configJSON = JSON.parse(configScript.innerHTML);
89-
} catch (е) {
90-
console.warn("Incorrect data-sap-ui-config format. Please use JSON"); /* eslint-disable-line */
91-
}
92-
93-
if (configJSON) {
94-
runtimeConfig = Object.assign({}, configJSON);
95-
}
96-
}
97-
};
98-
99-
const parseURLParameters = () => {
100-
const params = new URLSearchParams(window.location.search);
101-
102-
params.forEach((value, key) => {
103-
if (!key.startsWith("sap-ui")) {
104-
return;
105-
}
106-
107-
const lowerCaseValue = value.toLowerCase();
108-
109-
const param = key.split("sap-ui-")[1];
110-
111-
if (booleanMapping.has(value)) {
112-
value = booleanMapping.get(lowerCaseValue);
113-
}
114-
115-
runtimeConfig[param] = value;
116-
});
117-
};
118-
119-
const applyConfigurations = () => {
120-
Object.keys(runtimeConfig).forEach(key => {
121-
CONFIGURATION[key] = runtimeConfig[key];
122-
});
123-
};
124-
125-
const initConfiguration = () => {
126-
if (initialized) {
127-
return;
128-
}
129-
130-
parseConfigurationScript();
131-
parseURLParameters();
132-
applyConfigurations();
133-
134-
initialized = true;
135-
};
8+
const getOriginInfo = () => {}; // needed for the shim
1369

13710
export {
13811
getTheme,
12+
setTheme,
13+
getNoConflict,
14+
setNoConflict,
15+
getCompactSize,
13916
getRTL,
14017
getLanguage,
141-
getCompactSize,
142-
getWCNoConflict,
143-
getCalendarType,
144-
getLocale,
145-
_setTheme,
146-
_setWCNoConflict,
14718
getSupportedLanguages,
19+
getCalendarType,
14820
getOriginInfo,
14921
};
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
let initialized = false;
2+
3+
const initialConfig = {
4+
theme: "sap_fiori_3",
5+
rtl: null,
6+
language: null,
7+
compactSize: false,
8+
calendarType: null,
9+
noConflict: false, // no URL
10+
};
11+
12+
/* General settings */
13+
const getTheme = () => {
14+
initConfiguration();
15+
return initialConfig.theme;
16+
};
17+
18+
const getRTL = () => {
19+
initConfiguration();
20+
return initialConfig.rtl;
21+
};
22+
23+
const getLanguage = () => {
24+
initConfiguration();
25+
return initialConfig.language;
26+
};
27+
28+
const getCompactSize = () => {
29+
initConfiguration();
30+
return initialConfig.compactSize;
31+
};
32+
33+
const getNoConflict = () => {
34+
initConfiguration();
35+
return initialConfig.noConflict;
36+
};
37+
38+
const getCalendarType = () => {
39+
initConfiguration();
40+
return initialConfig.calendarType;
41+
};
42+
43+
const booleanMapping = new Map();
44+
booleanMapping.set("true", true);
45+
booleanMapping.set("false", false);
46+
47+
let runtimeConfig = {};
48+
49+
const parseConfigurationScript = () => {
50+
const configScript = document.querySelector("[data-ui5-config]") || document.querySelector("[data-id='sap-ui-config']"); // for backward compatibility
51+
52+
let configJSON;
53+
54+
if (configScript) {
55+
try {
56+
configJSON = JSON.parse(configScript.innerHTML);
57+
} catch (err) {
58+
console.warn("Incorrect data-sap-ui-config format. Please use JSON"); /* eslint-disable-line */
59+
}
60+
61+
if (configJSON) {
62+
runtimeConfig = Object.assign({}, configJSON);
63+
}
64+
}
65+
};
66+
67+
const parseURLParameters = () => {
68+
const params = new URLSearchParams(window.location.search);
69+
70+
params.forEach((value, key) => {
71+
if (!key.startsWith("sap-ui")) {
72+
return;
73+
}
74+
75+
const lowerCaseValue = value.toLowerCase();
76+
77+
const param = key.split("sap-ui-")[1];
78+
79+
if (booleanMapping.has(value)) {
80+
value = booleanMapping.get(lowerCaseValue);
81+
}
82+
83+
runtimeConfig[param] = value;
84+
});
85+
};
86+
87+
const applyConfigurations = () => {
88+
Object.keys(runtimeConfig).forEach(key => {
89+
initialConfig[key] = runtimeConfig[key];
90+
});
91+
};
92+
93+
const initConfiguration = () => {
94+
if (initialized) {
95+
return;
96+
}
97+
98+
parseConfigurationScript();
99+
parseURLParameters();
100+
applyConfigurations();
101+
102+
initialized = true;
103+
};
104+
105+
export {
106+
getTheme,
107+
getRTL,
108+
getLanguage,
109+
getCompactSize,
110+
getNoConflict,
111+
getCalendarType,
112+
};

0 commit comments

Comments
 (0)