@aarhus-university/au-lib-react-components 11.5.0 → 11.6.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.
Files changed (100) hide show
  1. package/.eslintrc.js +34 -34
  2. package/.storybook/main.js +20 -20
  3. package/.storybook/preview.js +17 -17
  4. package/README.md +19 -19
  5. package/__tests__/jest/AUButtonComponent.test.tsx +165 -165
  6. package/__tests__/jest/AUErrorComponent.test.tsx +142 -142
  7. package/__tests__/jest/AUModalComponent.test.tsx +186 -186
  8. package/__tests__/jest/AUNotificationComponent.test.tsx +115 -115
  9. package/__tests__/jest/AUSpinnerComponent.test.tsx +57 -57
  10. package/__tests__/jest/AUToolbarComponent.test.tsx +46 -46
  11. package/__tests__/jest/context.test.ts +25 -25
  12. package/__tests__/jest/helpers.test.ts +15 -15
  13. package/__tests__/jest/setupTests.ts +2 -2
  14. package/babel.config.js +8 -8
  15. package/build/umd/all.css +2 -2
  16. package/build/umd/all.js +1 -1
  17. package/build/umd/alphabox.js +1 -1
  18. package/build/umd/databox.js +1 -1
  19. package/build/umd/diagramme.js +1 -1
  20. package/build/umd/flowbox.js +1 -1
  21. package/build/umd/universe.js +1 -1
  22. package/build-storybook.log +367 -367
  23. package/package.json +114 -114
  24. package/src/components/AUAlertComponent.tsx +113 -102
  25. package/src/components/AUAutoSuggestComponent.js +158 -158
  26. package/src/components/AUButtonComponent.tsx +87 -84
  27. package/src/components/AUCalendarComponent.tsx +493 -493
  28. package/src/components/AUContentToggleComponent.tsx +47 -47
  29. package/src/components/AUDatepickerComponent.tsx +121 -121
  30. package/src/components/AUEditFilterComponent.tsx +24 -0
  31. package/src/components/AUErrorComponent.tsx +78 -78
  32. package/src/components/AUMobilePrefixComponent.tsx +15 -15
  33. package/src/components/AUModalComponent.tsx +68 -68
  34. package/src/components/AUNotificationComponent.tsx +43 -43
  35. package/src/components/AUReceiptComponent.tsx +33 -33
  36. package/src/components/AUSpinnerComponent.tsx +36 -36
  37. package/src/components/AUStepComponent.tsx +47 -47
  38. package/src/components/AUSubNavComponent.tsx +48 -48
  39. package/src/components/AUSubmitButtonContainerComponent.tsx +29 -29
  40. package/src/components/AUTabbedContentComponent.tsx +145 -145
  41. package/src/components/AUTableComponent.tsx +24 -24
  42. package/src/components/AUToastComponent.tsx +104 -104
  43. package/src/components/AUToolbarComponent.tsx +101 -101
  44. package/src/components/AUTruncatorComponent.tsx +124 -124
  45. package/src/components/wrapping/AUEmbedComponent.js +47 -47
  46. package/src/layout-2016/components/alphabox/AlphaBoxComponent.js +143 -143
  47. package/src/layout-2016/components/alphabox/AlphaBoxContentComponent.js +136 -136
  48. package/src/layout-2016/components/common/AUCollapsibleComponent.js +152 -152
  49. package/src/layout-2016/components/common/AUSpinnerComponent.js +103 -103
  50. package/src/layout-2016/components/databox/DataBoxAlphabetComponent.js +144 -144
  51. package/src/layout-2016/components/databox/DataBoxAssociationComponent.js +122 -122
  52. package/src/layout-2016/components/databox/DataBoxButtonComponent.js +157 -157
  53. package/src/layout-2016/components/databox/DataBoxComponent.js +297 -297
  54. package/src/layout-2016/components/databox/DataBoxGroupingComponent.js +64 -64
  55. package/src/layout-2016/components/databox/DataBoxSearchResultComponent.js +36 -36
  56. package/src/layout-2016/components/databox/DataBoxStackedAssociationComponent.js +54 -54
  57. package/src/layout-2016/components/databox/DataBoxSuggestionComponent.js +39 -39
  58. package/src/layout-2016/components/diagramme/AUDiagrammeComponent.js +309 -309
  59. package/src/layout-2016/components/flowbox/FlowBoxComponent.js +126 -126
  60. package/src/layout-2016/components/flowbox/FlowBoxPhoneComponent.js +104 -104
  61. package/src/layout-2016/components/profile/AUProfileAvatar2016Component.js +103 -103
  62. package/src/layout-2016/components/universe/StaffTopComponent.js +363 -363
  63. package/src/layout-2016/components/universe/StudentTopComponent.js +137 -137
  64. package/src/layout-2016/components/universe/UniverseContainerComponent.js +65 -65
  65. package/src/layout-2016/lib/all.js +3 -3
  66. package/src/layout-2016/lib/au-alphabox.js +100 -100
  67. package/src/layout-2016/lib/au-databox.js +400 -400
  68. package/src/layout-2016/lib/au-diagramme.js +85 -85
  69. package/src/layout-2016/lib/au-flowbox.js +93 -93
  70. package/src/layout-2016/lib/universe.js +9 -9
  71. package/src/lib/context.tsx +51 -51
  72. package/src/lib/dates.ts +50 -50
  73. package/src/lib/helpers.ts +208 -208
  74. package/src/lib/hooks.ts +75 -75
  75. package/src/lib/i18n.ts +600 -600
  76. package/src/lib/portals.tsx +145 -119
  77. package/src/lib/tracking.ts +69 -69
  78. package/src/lib/wrapping.ts +21 -21
  79. package/src/styles/_settings.scss +10 -10
  80. package/src/styles/alphabox.scss +222 -222
  81. package/src/styles/app.scss +7 -7
  82. package/src/styles/autosuggest.scss +57 -57
  83. package/src/styles/databox.scss +563 -563
  84. package/src/styles/diagramme.scss +119 -119
  85. package/src/styles/flowbox.scss +72 -72
  86. package/src/styles/maps.scss +395 -395
  87. package/stories/AUAlertComponent.stories.tsx +120 -60
  88. package/stories/AUButtonComponent.stories.tsx +127 -127
  89. package/stories/AUContentToggleComponent.stories.tsx +63 -63
  90. package/stories/AUEditFilterComponent.stories.tsx +29 -0
  91. package/stories/AUErrorComponent.stories.tsx +98 -98
  92. package/stories/AUModalComponent.stories.tsx +84 -83
  93. package/stories/AUNotificationComponent.stories.tsx +116 -116
  94. package/stories/AUSpinnerComponent.stories.tsx +41 -41
  95. package/stories/AUStepComponent.stories.tsx +40 -40
  96. package/stories/AUToolbarComponent.stories.tsx +326 -326
  97. package/stories/AUTruncatorComponent.stories.tsx +101 -101
  98. package/stories/lib/helpers.tsx +128 -128
  99. package/tsconfig.json +46 -46
  100. package/webpack.config.js +89 -89
@@ -1,85 +1,85 @@
1
- /* eslint no-prototype-builtins: 0 */
2
- /* eslint no-restricted-syntax: 0 */
3
- /* eslint no-unused-vars: 0 */
4
- /* eslint-env browser */
5
- import React from 'react';
6
- import ReactDOM from 'react-dom';
7
- import AUDiagrammeComponent from '../components/diagramme/AUDiagrammeComponent';
8
-
9
- const defaultOptions = {
10
- config: {
11
- container: '.au_edutable_eddi_container',
12
- courseID: 0,
13
- track: 0,
14
- },
15
- };
16
-
17
- class AUDiagramme {
18
- constructor(lang, tables) {
19
- this.tables = tables;
20
- this.lang = lang;
21
- }
22
-
23
- static mergeOptions(_table) {
24
- const table = _table;
25
- if (!table.hasOwnProperty('config')) {
26
- table.config = {};
27
- }
28
-
29
- for (const conf in defaultOptions.config) {
30
- if (!table.config.hasOwnProperty(conf)) {
31
- table.config[conf] = defaultOptions.config[conf];
32
- }
33
- }
34
- }
35
-
36
- init() {
37
- const qsCourses = AU.helpers.getQuerystring('coursesprint');
38
- if (qsCourses !== '') {
39
- const coursesSplit = qsCourses.split(',');
40
- for (let j = 0; j < coursesSplit.length; j += 1) {
41
- const trackSplit = coursesSplit[j].split('|');
42
- if (trackSplit.length > 1) {
43
- this.tables.push({
44
- config: {
45
- container: `#print_course_${j}`,
46
- courseID: parseInt(trackSplit[0], 10),
47
- track: parseInt(trackSplit[1], 10),
48
- },
49
- });
50
- } else {
51
- this.tables.push({
52
- config: {
53
- container: `#print_course_${j}`,
54
- courseID: parseInt(trackSplit[0], 10),
55
- },
56
- });
57
- }
58
- ReactDOM.render(<div id={`print_course_${j}`} />, document.querySelector('body'));
59
- }
60
- }
61
- for (let i = 0; i < this.tables.length; i += 1) {
62
- const table = this.tables[i];
63
- table.id = i;
64
- AUDiagramme.mergeOptions(table);
65
- const url = `https://eddiprod.au.dk/EDDI/webservices/StudieoversigtService2.cfc?method=getLinjeListByDokordningId&tjekForVisningIstudieDiagram=true&allowOrigin=true&dokordningId=${table.config.courseID}`;
66
- fetch(url).then((promise) => promise.json()).then((data) => {
67
- const mountNode = document.querySelector(table.config.container);
68
- if (mountNode) {
69
- ReactDOM.render(
70
- <AUDiagrammeComponent
71
- lang={this.lang}
72
- response={data}
73
- course={table.config.courseID}
74
- track={table.config.track}
75
- print={qsCourses !== ''}
76
- />,
77
- mountNode,
78
- );
79
- }
80
- });
81
- }
82
- }
83
- }
84
-
85
- window.AUDiagramme = AUDiagramme;
1
+ /* eslint no-prototype-builtins: 0 */
2
+ /* eslint no-restricted-syntax: 0 */
3
+ /* eslint no-unused-vars: 0 */
4
+ /* eslint-env browser */
5
+ import React from 'react';
6
+ import ReactDOM from 'react-dom';
7
+ import AUDiagrammeComponent from '../components/diagramme/AUDiagrammeComponent';
8
+
9
+ const defaultOptions = {
10
+ config: {
11
+ container: '.au_edutable_eddi_container',
12
+ courseID: 0,
13
+ track: 0,
14
+ },
15
+ };
16
+
17
+ class AUDiagramme {
18
+ constructor(lang, tables) {
19
+ this.tables = tables;
20
+ this.lang = lang;
21
+ }
22
+
23
+ static mergeOptions(_table) {
24
+ const table = _table;
25
+ if (!table.hasOwnProperty('config')) {
26
+ table.config = {};
27
+ }
28
+
29
+ for (const conf in defaultOptions.config) {
30
+ if (!table.config.hasOwnProperty(conf)) {
31
+ table.config[conf] = defaultOptions.config[conf];
32
+ }
33
+ }
34
+ }
35
+
36
+ init() {
37
+ const qsCourses = AU.helpers.getQuerystring('coursesprint');
38
+ if (qsCourses !== '') {
39
+ const coursesSplit = qsCourses.split(',');
40
+ for (let j = 0; j < coursesSplit.length; j += 1) {
41
+ const trackSplit = coursesSplit[j].split('|');
42
+ if (trackSplit.length > 1) {
43
+ this.tables.push({
44
+ config: {
45
+ container: `#print_course_${j}`,
46
+ courseID: parseInt(trackSplit[0], 10),
47
+ track: parseInt(trackSplit[1], 10),
48
+ },
49
+ });
50
+ } else {
51
+ this.tables.push({
52
+ config: {
53
+ container: `#print_course_${j}`,
54
+ courseID: parseInt(trackSplit[0], 10),
55
+ },
56
+ });
57
+ }
58
+ ReactDOM.render(<div id={`print_course_${j}`} />, document.querySelector('body'));
59
+ }
60
+ }
61
+ for (let i = 0; i < this.tables.length; i += 1) {
62
+ const table = this.tables[i];
63
+ table.id = i;
64
+ AUDiagramme.mergeOptions(table);
65
+ const url = `https://eddiprod.au.dk/EDDI/webservices/StudieoversigtService2.cfc?method=getLinjeListByDokordningId&tjekForVisningIstudieDiagram=true&allowOrigin=true&dokordningId=${table.config.courseID}`;
66
+ fetch(url).then((promise) => promise.json()).then((data) => {
67
+ const mountNode = document.querySelector(table.config.container);
68
+ if (mountNode) {
69
+ ReactDOM.render(
70
+ <AUDiagrammeComponent
71
+ lang={this.lang}
72
+ response={data}
73
+ course={table.config.courseID}
74
+ track={table.config.track}
75
+ print={qsCourses !== ''}
76
+ />,
77
+ mountNode,
78
+ );
79
+ }
80
+ });
81
+ }
82
+ }
83
+ }
84
+
85
+ window.AUDiagramme = AUDiagramme;
@@ -1,93 +1,93 @@
1
- /* eslint-env browser */
2
- /* eslint no-prototype-builtins: 0 */
3
- /* eslint no-restricted-syntax: 0 */
4
- /* eslint no-unused-vars: 0 */
5
- import React from 'react';
6
- import ReactDOM from 'react-dom';
7
- import FlowBoxComponent from '../components/flowbox/FlowBoxComponent';
8
-
9
- const defaultOptions = {
10
- config: {
11
- container: '.au_flowbox',
12
- tracking: '/virtual/flowbox/',
13
- render: 'ul',
14
- },
15
- data: {
16
- mode: 'list',
17
- source: '.au_flowbox_list',
18
- headline: 'Overskrift',
19
- prepend: '',
20
- },
21
- };
22
-
23
- class AUFlowbox {
24
- constructor(lang, boxes) {
25
- this.boxes = boxes;
26
- this.lang = lang;
27
- }
28
-
29
- static mergeOptions(_box) {
30
- const box = _box;
31
- if (!box.hasOwnProperty('config')) {
32
- box.config = {};
33
- }
34
-
35
- for (const conf in defaultOptions.config) {
36
- if (!box.config.hasOwnProperty(conf)) {
37
- box.config[conf] = defaultOptions.config[conf];
38
- }
39
- }
40
-
41
- if (!box.hasOwnProperty('data')) {
42
- box.data = {};
43
- }
44
-
45
- for (const data in defaultOptions.data) {
46
- if (!box.data.hasOwnProperty(data)) {
47
- box.data[data] = defaultOptions.data[data];
48
- }
49
- }
50
- }
51
-
52
- static parse(box) {
53
- const parseList = (data, elements, selector) => {
54
- elements.forEach((e) => {
55
- const subElements = e.querySelectorAll(selector);
56
- subElements.forEach((s) => {
57
- const obj = {
58
- name: s.innerHTML,
59
- children: [],
60
- parent: data,
61
- };
62
-
63
- data.children.push(obj);
64
- const parent = s.parentElement;
65
- const children = parent.querySelectorAll(':scope > ul');
66
- if (children.length > 0) {
67
- parseList(obj, children, ':scope > li > p'); // Det her skal vi altid gøre
68
- }
69
- });
70
- });
71
- };
72
-
73
- parseList(box.item, document.querySelectorAll(box.data.source), window.flowboxScopeSelector || ':scope > li > p'); // Her kan selectoren være en anden, hvis der fx er global content i spil, så der kommer en <div> ind...
74
- }
75
-
76
- init() {
77
- for (let i = 0; i < this.boxes.length; i += 1) {
78
- const box = this.boxes[i];
79
- AUFlowbox.mergeOptions(box);
80
- box.id = i;
81
- box.item = {
82
- name: box.data.headline,
83
- children: [],
84
- parent: null,
85
- };
86
- AUFlowbox.parse(box);
87
- const mountNode = document.querySelector(box.config.container);
88
- ReactDOM.render(<FlowBoxComponent box={box} lang={this.lang} />, mountNode);
89
- }
90
- }
91
- }
92
-
93
- window.AUFlowbox = AUFlowbox;
1
+ /* eslint-env browser */
2
+ /* eslint no-prototype-builtins: 0 */
3
+ /* eslint no-restricted-syntax: 0 */
4
+ /* eslint no-unused-vars: 0 */
5
+ import React from 'react';
6
+ import ReactDOM from 'react-dom';
7
+ import FlowBoxComponent from '../components/flowbox/FlowBoxComponent';
8
+
9
+ const defaultOptions = {
10
+ config: {
11
+ container: '.au_flowbox',
12
+ tracking: '/virtual/flowbox/',
13
+ render: 'ul',
14
+ },
15
+ data: {
16
+ mode: 'list',
17
+ source: '.au_flowbox_list',
18
+ headline: 'Overskrift',
19
+ prepend: '',
20
+ },
21
+ };
22
+
23
+ class AUFlowbox {
24
+ constructor(lang, boxes) {
25
+ this.boxes = boxes;
26
+ this.lang = lang;
27
+ }
28
+
29
+ static mergeOptions(_box) {
30
+ const box = _box;
31
+ if (!box.hasOwnProperty('config')) {
32
+ box.config = {};
33
+ }
34
+
35
+ for (const conf in defaultOptions.config) {
36
+ if (!box.config.hasOwnProperty(conf)) {
37
+ box.config[conf] = defaultOptions.config[conf];
38
+ }
39
+ }
40
+
41
+ if (!box.hasOwnProperty('data')) {
42
+ box.data = {};
43
+ }
44
+
45
+ for (const data in defaultOptions.data) {
46
+ if (!box.data.hasOwnProperty(data)) {
47
+ box.data[data] = defaultOptions.data[data];
48
+ }
49
+ }
50
+ }
51
+
52
+ static parse(box) {
53
+ const parseList = (data, elements, selector) => {
54
+ elements.forEach((e) => {
55
+ const subElements = e.querySelectorAll(selector);
56
+ subElements.forEach((s) => {
57
+ const obj = {
58
+ name: s.innerHTML,
59
+ children: [],
60
+ parent: data,
61
+ };
62
+
63
+ data.children.push(obj);
64
+ const parent = s.parentElement;
65
+ const children = parent.querySelectorAll(':scope > ul');
66
+ if (children.length > 0) {
67
+ parseList(obj, children, ':scope > li > p'); // Det her skal vi altid gøre
68
+ }
69
+ });
70
+ });
71
+ };
72
+
73
+ parseList(box.item, document.querySelectorAll(box.data.source), window.flowboxScopeSelector || ':scope > li > p'); // Her kan selectoren være en anden, hvis der fx er global content i spil, så der kommer en <div> ind...
74
+ }
75
+
76
+ init() {
77
+ for (let i = 0; i < this.boxes.length; i += 1) {
78
+ const box = this.boxes[i];
79
+ AUFlowbox.mergeOptions(box);
80
+ box.id = i;
81
+ box.item = {
82
+ name: box.data.headline,
83
+ children: [],
84
+ parent: null,
85
+ };
86
+ AUFlowbox.parse(box);
87
+ const mountNode = document.querySelector(box.config.container);
88
+ ReactDOM.render(<FlowBoxComponent box={box} lang={this.lang} />, mountNode);
89
+ }
90
+ }
91
+ }
92
+
93
+ window.AUFlowbox = AUFlowbox;
@@ -1,9 +1,9 @@
1
- /* eslint-env browser */
2
- import React from 'react';
3
- import ReactDOM from 'react-dom';
4
- import UniverseContainerComponent from '../components/universe/UniverseContainerComponent';
5
-
6
- ReactDOM.render(
7
- <UniverseContainerComponent />,
8
- document.querySelector('.universeheader'),
9
- );
1
+ /* eslint-env browser */
2
+ import React from 'react';
3
+ import ReactDOM from 'react-dom';
4
+ import UniverseContainerComponent from '../components/universe/UniverseContainerComponent';
5
+
6
+ ReactDOM.render(
7
+ <UniverseContainerComponent />,
8
+ document.querySelector('.universeheader'),
9
+ );
@@ -1,51 +1,51 @@
1
- import React, { createContext, useContext } from 'react';
2
- import { setLocale } from './dates';
3
- import { getPortalByName } from './portals';
4
-
5
- export const LangContext = createContext<string>('da');
6
- export const useLangContext = (): string => {
7
- const lang = useContext(LangContext);
8
- setLocale(lang);
9
- return lang;
10
- };
11
-
12
- export const AuthenticationContext = createContext<boolean>(false);
13
- export const useAuthenticationContext = (): boolean => useContext(AuthenticationContext);
14
-
15
- export const UserContext = createContext<AU.IUserContext | null>(null);
16
- export const useUserContext = (): AU.IUserContext | null => {
17
- const user = useContext(UserContext);
18
- return user;
19
- };
20
-
21
- export const PortalContext = createContext<AU.IPortalContext | null>(null);
22
-
23
- export const AUPortal = {
24
- Provider: ({
25
- name,
26
- children,
27
- }: PortalContextProviderProps) => {
28
- const lang = useLangContext();
29
- const portal = getPortalByName(lang || 'da', name);
30
- return (
31
- <PortalContext.Provider value={portal}>
32
- <AuthenticationContext.Provider value={typeof window.authenticated === 'undefined' ? false : window.authenticated}>
33
- {children}
34
- </AuthenticationContext.Provider>
35
- </PortalContext.Provider>
36
- );
37
- },
38
- };
39
-
40
- export const usePortalContext = (): AU.IPortalContext | null => {
41
- const portal = useContext(PortalContext);
42
- return portal;
43
- };
44
-
45
- export const ImpersonationContext = createContext<AU.IImpersonationContext>({
46
- canImpersonate: false,
47
- isImpersonating: false,
48
- impersonatateError: false,
49
- });
50
- // eslint-disable-next-line max-len
51
- export const useImpersonationContext = (): AU.IImpersonationContext => useContext(ImpersonationContext);
1
+ import React, { createContext, useContext } from 'react';
2
+ import { setLocale } from './dates';
3
+ import { getPortalByName } from './portals';
4
+
5
+ export const LangContext = createContext<string>('da');
6
+ export const useLangContext = (): string => {
7
+ const lang = useContext(LangContext);
8
+ setLocale(lang);
9
+ return lang;
10
+ };
11
+
12
+ export const AuthenticationContext = createContext<boolean>(false);
13
+ export const useAuthenticationContext = (): boolean => useContext(AuthenticationContext);
14
+
15
+ export const UserContext = createContext<AU.IUserContext | null>(null);
16
+ export const useUserContext = (): AU.IUserContext | null => {
17
+ const user = useContext(UserContext);
18
+ return user;
19
+ };
20
+
21
+ export const PortalContext = createContext<AU.IPortalContext | null>(null);
22
+
23
+ export const AUPortal = {
24
+ Provider: ({
25
+ name,
26
+ children,
27
+ }: PortalContextProviderProps) => {
28
+ const lang = useLangContext();
29
+ const portal = getPortalByName(lang || 'da', name);
30
+ return (
31
+ <PortalContext.Provider value={portal}>
32
+ <AuthenticationContext.Provider value={typeof window.authenticated === 'undefined' ? false : window.authenticated}>
33
+ {children}
34
+ </AuthenticationContext.Provider>
35
+ </PortalContext.Provider>
36
+ );
37
+ },
38
+ };
39
+
40
+ export const usePortalContext = (): AU.IPortalContext | null => {
41
+ const portal = useContext(PortalContext);
42
+ return portal;
43
+ };
44
+
45
+ export const ImpersonationContext = createContext<AU.IImpersonationContext>({
46
+ canImpersonate: false,
47
+ isImpersonating: false,
48
+ impersonatateError: false,
49
+ });
50
+ // eslint-disable-next-line max-len
51
+ export const useImpersonationContext = (): AU.IImpersonationContext => useContext(ImpersonationContext);
package/src/lib/dates.ts CHANGED
@@ -1,50 +1,50 @@
1
- import dayjs from 'dayjs';
2
- import 'dayjs/locale/da';
3
- import isoWeek from 'dayjs/plugin/isoWeek';
4
- import weekday from 'dayjs/plugin/weekday';
5
- import advancedFormat from 'dayjs/plugin/advancedFormat';
6
- import updateLocale from 'dayjs/plugin/updateLocale';
7
-
8
- dayjs.extend(isoWeek);
9
- dayjs.extend(weekday);
10
- dayjs.extend(advancedFormat);
11
- dayjs.extend(updateLocale);
12
-
13
- dayjs.updateLocale('da', {
14
- weekdaysShort: 'søn_man_tir_ons_tor_fre_lør'.split('_'),
15
- monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
16
- });
17
-
18
- export const formatTime = (date: Date, format = 'HH:mm'): string => dayjs(date).format(format);
19
- export const getLocale = (): string => dayjs.locale();
20
- export const setLocale = (locale: string): string => dayjs.locale(locale);
21
- export const getThisYear = (): number => dayjs().year();
22
- export const getYearToString = (date: Date): string => formatTime(date, 'YYYY');
23
- export const getNumberOfWeeksInYear = (year: number): number => dayjs(`${year}-12-31`).isoWeek();
24
- export const skipWeeks = (date: Date, multiplier: number): Date => dayjs(date).clone().add(multiplier * 7, 'days').toDate();
25
- export const getWeek = (date: Date): number => dayjs(date).isoWeek();
26
- // eslint-disable-next-line max-len
27
- export const getDateByWeekday = (date: Date, index: number, locale = 'da'): Date => {
28
- const currentLocale = getLocale();
29
- setLocale(locale);
30
- const newDate = dayjs(date).weekday(index).toDate();
31
- if (currentLocale !== locale) {
32
- setLocale(currentLocale);
33
- }
34
- return newDate;
35
- };
36
- const dateIsSameMonthAsCurrent = (date: Date): boolean => formatTime(new Date(), 'M') === formatTime(date, 'M');
37
- export const getLongDate = (date: Date): string => formatTime(date, 'dddd, Do MMMM YYYY');
38
- export const getShortDate = (date: Date): string => formatTime(date, `ddd D${(dateIsSameMonthAsCurrent(date) ? '' : `${getLocale() === 'da' ? '.' : ''} MMM`)}`);
39
- export const isToday = (date: Date): boolean => dayjs(date).isSame(dayjs());
40
- export const multipleDaysFormatTime = (date: Date, index: number, format = 'ddd HH:mm', firstLetterToUpper = true): string => {
41
- if (index > 0) {
42
- const formatted = formatTime(date, format);
43
- if (firstLetterToUpper) {
44
- return formatted.charAt(0).toUpperCase() + formatted.slice(1);
45
- }
46
- return formatted;
47
- }
48
- return formatTime(date);
49
- };
50
- export const formatDayOfWeek = (date: Date): string => formatTime(date, 'dddd').charAt(0).toUpperCase() + formatTime(date, 'dddd').slice(1);
1
+ import dayjs from 'dayjs';
2
+ import 'dayjs/locale/da';
3
+ import isoWeek from 'dayjs/plugin/isoWeek';
4
+ import weekday from 'dayjs/plugin/weekday';
5
+ import advancedFormat from 'dayjs/plugin/advancedFormat';
6
+ import updateLocale from 'dayjs/plugin/updateLocale';
7
+
8
+ dayjs.extend(isoWeek);
9
+ dayjs.extend(weekday);
10
+ dayjs.extend(advancedFormat);
11
+ dayjs.extend(updateLocale);
12
+
13
+ dayjs.updateLocale('da', {
14
+ weekdaysShort: 'søn_man_tir_ons_tor_fre_lør'.split('_'),
15
+ monthsShort: 'jan_feb_mar_apr_maj_jun_jul_aug_sep_okt_nov_dec'.split('_'),
16
+ });
17
+
18
+ export const formatTime = (date: Date, format = 'HH:mm'): string => dayjs(date).format(format);
19
+ export const getLocale = (): string => dayjs.locale();
20
+ export const setLocale = (locale: string): string => dayjs.locale(locale);
21
+ export const getThisYear = (): number => dayjs().year();
22
+ export const getYearToString = (date: Date): string => formatTime(date, 'YYYY');
23
+ export const getNumberOfWeeksInYear = (year: number): number => dayjs(`${year}-12-31`).isoWeek();
24
+ export const skipWeeks = (date: Date, multiplier: number): Date => dayjs(date).clone().add(multiplier * 7, 'days').toDate();
25
+ export const getWeek = (date: Date): number => dayjs(date).isoWeek();
26
+ // eslint-disable-next-line max-len
27
+ export const getDateByWeekday = (date: Date, index: number, locale = 'da'): Date => {
28
+ const currentLocale = getLocale();
29
+ setLocale(locale);
30
+ const newDate = dayjs(date).weekday(index).toDate();
31
+ if (currentLocale !== locale) {
32
+ setLocale(currentLocale);
33
+ }
34
+ return newDate;
35
+ };
36
+ const dateIsSameMonthAsCurrent = (date: Date): boolean => formatTime(new Date(), 'M') === formatTime(date, 'M');
37
+ export const getLongDate = (date: Date): string => formatTime(date, 'dddd, Do MMMM YYYY');
38
+ export const getShortDate = (date: Date): string => formatTime(date, `ddd D${(dateIsSameMonthAsCurrent(date) ? '' : `${getLocale() === 'da' ? '.' : ''} MMM`)}`);
39
+ export const isToday = (date: Date): boolean => dayjs(date).isSame(dayjs());
40
+ export const multipleDaysFormatTime = (date: Date, index: number, format = 'ddd HH:mm', firstLetterToUpper = true): string => {
41
+ if (index > 0) {
42
+ const formatted = formatTime(date, format);
43
+ if (firstLetterToUpper) {
44
+ return formatted.charAt(0).toUpperCase() + formatted.slice(1);
45
+ }
46
+ return formatted;
47
+ }
48
+ return formatTime(date);
49
+ };
50
+ export const formatDayOfWeek = (date: Date): string => formatTime(date, 'dddd').charAt(0).toUpperCase() + formatTime(date, 'dddd').slice(1);