Skip to content

Commit 9c6df48

Browse files
MapTo0vladitasev
authored andcommitted
feat: add configuration for first day of the week (#627)
1 parent 5d9a1ac commit 9c6df48

File tree

9 files changed

+72
-10
lines changed

9 files changed

+72
-10
lines changed

docs/Configuration.md

+33-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ 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](#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
15+
[noConflict](#noConflict) | true, false | Object | false | When set to true, all events will be fired with a "ui5-" prefix only
16+
[formatSettings](#formatSettings)| See the [Format settings](#formatSettings) section below | Empty object | Allows to override locale-specific configuration
1617

17-
`*` When the `rtl` setting is set to `true`, UI5 Web Components will adjust their styling accordingly.
18+
<a name="rtl"></a>
19+
###RTL
20+
21+
When the `rtl` setting is set to `true`, UI5 Web Components will adjust their styling accordingly.
1822
However, you should also set the HTML attribute `dir` to `rtl` on the `body` or `html` or any other relevant region of your application
1923
so that the rest of your application is also affected.
2024

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`).
25+
<a name="noConflict"></a>
26+
### No conflict
27+
28+
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`).
2229
For example, when the `ui5-switch` is toggled, it fires a `change` event, but also a `ui5-change` event.
2330

2431
The `noConflict` configuration setting allows certain control over this behavior:
@@ -39,6 +46,24 @@ The `noConflict` configuration setting allows certain control over this behavior
3946
You can still use them by listening to `ui5-selectionChange` and `ui5-headerClick`, but the names `selectionChange` and `headerClick` will be
4047
free for use by other UI components and libraries without name collision.
4148

49+
<a name="formatSettings"></a>
50+
### Format settings
51+
52+
For example, to force the first day of week to Sunday, no matter the locale:
53+
54+
```json
55+
{
56+
"formatSettings": {
57+
"firstDayOfWeek": 0
58+
}
59+
}
60+
```
61+
62+
Setting | Values | Default value | Description
63+
------------ | ----------------------------------------------- | ------------- | -------------------------------------------------------------
64+
firstDayOfWeek | 0 (Sunday) through 6 (Saturday) | *depends on locale* | When set, overrides the locale's default value
65+
66+
4267
## Configuration script
4368

4469
In order to provide configuration settings, include the following ```<script>``` element in your HTML page:
@@ -50,6 +75,9 @@ In order to provide configuration settings, include the following ```<script>```
5075
"compactSize": true,
5176
"language": "ja",
5277
"calendarType": "Japanese",
78+
"formatSettings": {
79+
"firstDayOfWeek": 0
80+
},
5381
"theme": "sap_belize_hcb",
5482
"noConflict": {
5583
"events": ["selectionChange", "headerClick"]
@@ -74,4 +102,5 @@ import { getCompactSize } from "@ui5/webcomponents-base/dist/config/CompactSize.
74102
import { getRTL } from "@ui5/webcomponents-base/dist/config/RTL.js";
75103
import { getLanguage } from "@ui5/webcomponents-base/dist/config/Language.js";
76104
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
105+
import { getFirstDayOfWeek } from "@ui5/webcomponents-base/dist/config/FormatSettings.js";
77106
```

packages/base/src/InitialConfiguration.js

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const initialConfig = {
77
compactSize: false,
88
calendarType: null,
99
noConflict: false, // no URL
10+
formatSettings: {},
1011
};
1112

1213
/* General settings */
@@ -40,6 +41,11 @@ const getCalendarType = () => {
4041
return initialConfig.calendarType;
4142
};
4243

44+
const getFormatSettings = () => {
45+
initConfiguration();
46+
return initialConfig.formatSettings;
47+
};
48+
4349
const booleanMapping = new Map();
4450
booleanMapping.set("true", true);
4551
booleanMapping.set("false", false);
@@ -109,4 +115,5 @@ export {
109115
getCompactSize,
110116
getNoConflict,
111117
getCalendarType,
118+
getFormatSettings,
112119
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getFormatSettings } from "../InitialConfiguration.js";
2+
3+
const formatSettings = getFormatSettings();
4+
5+
const getFirstDayOfWeek = () => {
6+
return formatSettings.firstDayOfWeek;
7+
};
8+
9+
export { getFirstDayOfWeek }; // eslint-disable-line

packages/base/src/dates/CalendarUtils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import UniversalDate from "@ui5/webcomponents-core/dist/sap/ui/core/date/UniversalDate.js";
22
import Locale from "@ui5/webcomponents-core/dist/sap/ui/core/Locale.js";
33
import LocaleData from "@ui5/webcomponents-core/dist/sap/ui/core/LocaleData.js";
4+
import { getFirstDayOfWeek } from "../config/FormatSettings.js";
45

56
const calculateWeekNumber = (oDate, iYear, oLocale, oLocaleData) => {
67
let iWeekNum = 0;
78
let iWeekDay = 0;
8-
const iFirstDayOfWeek = oLocaleData.getFirstDayOfWeek();
9+
const confFirstDayOfWeek = getFirstDayOfWeek();
10+
const iFirstDayOfWeek = Number.isInteger(confFirstDayOfWeek) ? confFirstDayOfWeek : oLocaleData.getFirstDayOfWeek();
911

1012
// search Locale for containing "en-US", since sometimes
1113
// when any user settings have been defined, subtag "sapufmt" is added to the locale name
@@ -53,7 +55,8 @@ const calculateWeekNumber = (oDate, iYear, oLocale, oLocaleData) => {
5355
const getFirstDateOfWeek = oDate => {
5456
const oUniversalDate = new UniversalDate(oDate.getTime()),
5557
oLocaleData = LocaleData.getInstance(new Locale("en")),
56-
iCLDRFirstWeekDay = oLocaleData.getFirstDayOfWeek();
58+
confFirstDayOfWeek = getFirstDayOfWeek(),
59+
iCLDRFirstWeekDay = Number.isInteger(confFirstDayOfWeek) ? confFirstDayOfWeek : oLocaleData.getFirstDayOfWeek();
5760

5861

5962
const oWeek = UniversalDate.getWeekByDate(oUniversalDate.getCalendarType(), oUniversalDate.getUTCFullYear(),

packages/base/src/shims/Core-shim.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { inject as injectCore } from "@ui5/webcomponents-core/dist/sap/ui/core/C
22
import * as FormatSettings from "../FormatSettings.js";
33
import { getLanguage } from "../config/Language.js";
44
import { getCalendarType } from "../config/CalendarType.js";
5+
import { getFirstDayOfWeek } from "../config/FormatSettings.js";
56
import getDesigntimePropertyAsArray from "../util/getDesigntimePropertyAsArray.js";
67

78
/**
@@ -12,6 +13,7 @@ import getDesigntimePropertyAsArray from "../util/getDesigntimePropertyAsArray.j
1213
const Configuration = {
1314
getLanguage,
1415
getCalendarType,
16+
getFirstDayOfWeek,
1517
getSupportedLanguages: () => {
1618
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$");
1719
},

packages/core/overlay/sap/ui/core/date/UniversalDate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ sap.ui.define(['sap/ui/base/Object', 'sap/ui/core/LocaleData', 'sap/ui/core/date
258258
var oLocale = sap.ui.getCore().getFormatSettings().getFormatLocale(),
259259
oLocaleData = LocaleData.getInstance(oLocale),
260260
iMinDays = oLocaleData.getMinimalDaysInFirstWeek(),
261-
iFirstDayOfWeek = oLocaleData.getFirstDayOfWeek(),
261+
iFirstDayOfWeek = sap.ui.getConfiguration().getFirstDateOfWeek() || oLocaleData.getFirstDayOfWeek(),
262262
oFirstDay = new clDate(clDate.UTC(iYear, 0, 1)),
263263
iDayCount = 7;
264264
// Find the first day of the first week of the year

packages/core/overlay/sap/ui/core/format/DateFormat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ sap.ui.define([
21802180
DateFormat.prototype._adaptDayOfWeek = function(iDayOfWeek) {
21812181
// day of week depends on the format locale
21822182
// the DateFormat's locale is independent
2183-
var iFirstDayOfWeek = LocaleData.getInstance(sap.ui.getCore().getFormatSettings().getFormatLocale()).getFirstDayOfWeek();
2183+
var iFirstDayOfWeek = sap.ui.getCore().getConfiguration().getFirstDayOfWeek() || LocaleData.getInstance(sap.ui.getCore().getFormatSettings().getFormatLocale()).getFirstDayOfWeek();
21842184
var iDayNumberOfWeek = iDayOfWeek - (iFirstDayOfWeek - 1);
21852185

21862186
if (iDayNumberOfWeek <= 0) {

packages/main/src/DayPicker.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
22
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
33
import { getLocale } from "@ui5/webcomponents-base/dist/LocaleProvider.js";
4+
import { getFirstDayOfWeek } from "@ui5/webcomponents-base/dist/config/FormatSettings.js";
45
import { getCalendarType } from "@ui5/webcomponents-base/dist/config/CalendarType.js";
56
import { getFormatLocale } from "@ui5/webcomponents-base/dist/FormatSettings.js";
67
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
@@ -433,7 +434,8 @@ class DayPicker extends UI5Element {
433434
}
434435

435436
_getFirstDayOfWeek() {
436-
return this._oLocaleData.getFirstDayOfWeek();
437+
const confFirstDayOfWeek = getFirstDayOfWeek();
438+
return Number.isInteger(confFirstDayOfWeek) ? confFirstDayOfWeek : this._oLocaleData.getFirstDayOfWeek();
437439
}
438440

439441
get styles() {

packages/main/test/sap/ui/webcomponents/main/pages/DatePicker.html

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
1919
crossorigin="anonymous"></script>
2020

21+
<script data-id="sap-ui-config" type="application/json">
22+
{
23+
"rtl": false,
24+
"compactSize": false,
25+
"formatSettings": {
26+
"firstDayOfWeek": 0
27+
}
28+
}
29+
</script>
30+
2131
<style>
2232
.sapUiTheme-sap_fiori3 body {
2333
background: #f7f7f7;
@@ -126,4 +136,4 @@ <h3>japanese calendar type</h3>
126136
});
127137
</script>
128138
</body>
129-
</html>
139+
</html>

0 commit comments

Comments
 (0)