@aarhus-university/au-lib-react-components 9.11.18 → 9.12.1

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 (148) hide show
  1. package/.eslintrc +18 -18
  2. package/README.md +20 -20
  3. package/babel.config.js +6 -6
  4. package/build/dev.html +328 -328
  5. package/build/umd/all.css +2 -2
  6. package/build/umd/all.css.map +1 -1
  7. package/build/umd/all.js +1 -1
  8. package/build/umd/alphabox.js +1 -1
  9. package/build/umd/alphabox.js.map +1 -1
  10. package/build/umd/auAuth.js +1 -1
  11. package/build/umd/auAuth.js.map +1 -1
  12. package/build/umd/databox.js +2 -2
  13. package/build/umd/databox.js.LICENSE.txt +5 -5
  14. package/build/umd/databox.js.map +1 -1
  15. package/build/umd/diagramme.js +1 -1
  16. package/build/umd/flowbox.js +1 -1
  17. package/build/umd/flowbox.js.map +1 -1
  18. package/build/umd/news.js +1 -1
  19. package/build/umd/news.js.map +1 -1
  20. package/build/umd/universe.js +1 -1
  21. package/build/umd/universe.js.map +1 -1
  22. package/package.json +60 -60
  23. package/src/all.js +3 -3
  24. package/src/app.js +263 -263
  25. package/src/components/AUAlertComponent.js +158 -158
  26. package/src/components/AUAmountComponent.js +84 -84
  27. package/src/components/AUBannerComponent.js +99 -99
  28. package/src/components/AUCalendarComponent.js +393 -393
  29. package/src/components/AUCollapsibleComponent.js +173 -173
  30. package/src/components/AUDatepickerComponent.js +105 -105
  31. package/src/components/AUDiagrammeComponent.js +310 -310
  32. package/src/components/AUFilterCheckboxComponent.js +122 -122
  33. package/src/components/AUFocusComponent.js +55 -55
  34. package/src/components/AUModalComponent.js +94 -94
  35. package/src/components/AUPaginationComponent.js +103 -103
  36. package/src/components/AUSpinnerComponent.js +125 -125
  37. package/src/components/AutoSuggestComponent.js +157 -132
  38. package/src/components/alphabox/AlphaBoxComponent.js +145 -140
  39. package/src/components/alphabox/AlphaBoxContentComponent.js +136 -125
  40. package/src/components/context/AUUserContextComponent.js +91 -91
  41. package/src/components/context/ImpersonateComponent.js +54 -54
  42. package/src/components/databox/DataBoxAlphabetComponent.js +144 -144
  43. package/src/components/databox/DataBoxAssociationComponent.js +122 -122
  44. package/src/components/databox/DataBoxButtonComponent.js +153 -153
  45. package/src/components/databox/DataBoxComponent.js +297 -297
  46. package/src/components/databox/DataBoxGroupingComponent.js +62 -62
  47. package/src/components/databox/DataBoxSearchResultComponent.js +40 -40
  48. package/src/components/databox/DataBoxStackedAssociationComponent.js +58 -58
  49. package/src/components/databox/DataBoxSuggestionComponent.js +39 -39
  50. package/src/components/delphinus/AUAlertComponent.js +85 -85
  51. package/src/components/delphinus/AUCalendarComponent.js +487 -487
  52. package/src/components/delphinus/AUContentToggleComponent.js +32 -32
  53. package/src/components/delphinus/AUDatepickerComponent.js +164 -164
  54. package/src/components/delphinus/AULoginComponent.js +65 -65
  55. package/src/components/delphinus/AUModalComponent.js +94 -94
  56. package/src/components/delphinus/AUSpinnerComponent.js +114 -114
  57. package/src/components/delphinus/AUSubNavComponent.js +53 -53
  58. package/src/components/delphinus/AUTabbedContentComponent.js +150 -150
  59. package/src/components/delphinus/AUTableComponent.js +28 -28
  60. package/src/components/delphinus/AUToastComponent.js +91 -91
  61. package/src/components/delphinus/AUToolbarComponent.js +48 -48
  62. package/src/components/delphinus/hooks.js +28 -28
  63. package/src/components/flowbox/FlowBoxComponent.js +126 -126
  64. package/src/components/flowbox/FlowBoxPhoneComponent.js +106 -106
  65. package/src/components/form/AUMobilePrefixComponent.js +18 -18
  66. package/src/components/form/AUReceiptComponent.js +40 -40
  67. package/src/components/form/AUSubmitButtonContainerComponent.js +39 -39
  68. package/src/components/news/EventLayout1Component.js +94 -94
  69. package/src/components/news/EventLayout2Component.js +90 -90
  70. package/src/components/news/EventLayout3Component.js +68 -68
  71. package/src/components/news/NewsCategoriesComponent.js +21 -21
  72. package/src/components/news/NewsCollageBannerComponent.js +71 -71
  73. package/src/components/news/NewsCollageBannerRSSComponent.js +79 -79
  74. package/src/components/news/NewsColumnsComponent.js +125 -125
  75. package/src/components/news/NewsLanguageChangeComponent.js +74 -74
  76. package/src/components/news/NewsLayout1Component.js +80 -80
  77. package/src/components/news/NewsLayout2Component.js +80 -80
  78. package/src/components/news/NewsLayout3Component.js +81 -81
  79. package/src/components/news/NewsLayout4Component.js +80 -80
  80. package/src/components/news/NewsLayout5Component.js +61 -61
  81. package/src/components/news/NewsLayout6Component.js +55 -55
  82. package/src/components/news/NewsLayout7Component.js +58 -58
  83. package/src/components/news/NewsLayout8Component.js +57 -57
  84. package/src/components/news/NewsListComponent.js +291 -291
  85. package/src/components/news/NewsPopUpComponent.js +591 -591
  86. package/src/components/news/NewsRSSComponent.js +74 -74
  87. package/src/components/news/NewsSocialComponent.js +104 -104
  88. package/src/components/news/NewsSubHeaderComponent.js +19 -19
  89. package/src/components/password/AUChangePasswordComponent.js +190 -177
  90. package/src/components/password/AUCurrentPasswordComponent.js +72 -72
  91. package/src/components/password/AUNewPasswordComponent.js +143 -143
  92. package/src/components/password/AUPasswordActions.js +101 -101
  93. package/src/components/password/AUPasswordHooks.js +47 -47
  94. package/src/components/password/AUPasswordReducer.js +78 -78
  95. package/src/components/password/AUPasswordRequirementsComponent.js +29 -29
  96. package/src/components/profile/AUProfileActions.js +128 -128
  97. package/src/components/profile/AUProfileAvatar2016Component.js +99 -99
  98. package/src/components/profile/AUProfileAvatarComponent.js +83 -83
  99. package/src/components/profile/AUProfileAvatarV2Component.js +91 -91
  100. package/src/components/profile/AUProfileAvatarV3Component.js +80 -80
  101. package/src/components/profile/AUProfileContainerComponent.js +283 -283
  102. package/src/components/profile/AUProfileHooks.js +30 -30
  103. package/src/components/profile/AUProfileItemComponent.js +54 -54
  104. package/src/components/profile/AUProfileLanguageComponent.js +131 -131
  105. package/src/components/profile/AUProfileLoginComponent.js +41 -41
  106. package/src/components/profile/AUProfileMailComponent.js +307 -307
  107. package/src/components/profile/AUProfileMobileComponent.js +164 -164
  108. package/src/components/profile/AUProfileNameComponent.js +253 -253
  109. package/src/components/profile/AUProfileNextOfKinComponent.js +216 -216
  110. package/src/components/profile/AUProfileReducer.js +230 -230
  111. package/src/components/profile/AUProfileWidgetComponent.js +95 -95
  112. package/src/components/profile/AUProfileWidgetV2Component.js +116 -116
  113. package/src/components/profile/AUProfileWidgetV3Component.js +130 -130
  114. package/src/components/universe/StaffTopComponent.js +362 -362
  115. package/src/components/universe/StudentTopComponent.js +137 -137
  116. package/src/components/universe/UniverseContainerComponent.js +62 -62
  117. package/src/components/wrapping/AUEmbedComponent.js +47 -47
  118. package/src/config/webpack.dev.config.js +47 -47
  119. package/src/config/webpack.umd.config.js +86 -85
  120. package/src/index.js +6 -6
  121. package/src/lib/au-alphabox.js +102 -102
  122. package/src/lib/au-auth.js +227 -227
  123. package/src/lib/au-databox.js +403 -403
  124. package/src/lib/au-diagramme.js +87 -87
  125. package/src/lib/au-flowbox.js +95 -95
  126. package/src/lib/au-news.js +371 -371
  127. package/src/lib/helpers.js +222 -219
  128. package/src/lib/i18n.js +603 -601
  129. package/src/lib/menu.js +10 -10
  130. package/src/lib/tracking.js +65 -65
  131. package/src/lib/universe.js +9 -9
  132. package/src/lib/urlHandler.js +63 -63
  133. package/src/lib/validation.js +219 -181
  134. package/src/lib/wrapping.js +16 -16
  135. package/src/styles/_settings.scss +10 -10
  136. package/src/styles/alert.scss +39 -39
  137. package/src/styles/alphabox.scss +222 -208
  138. package/src/styles/app.scss +13 -13
  139. package/src/styles/autosuggest.scss +57 -57
  140. package/src/styles/calendar.scss +111 -111
  141. package/src/styles/databox.scss +563 -563
  142. package/src/styles/diagramme.scss +119 -119
  143. package/src/styles/filtercheckbox.scss +4 -4
  144. package/src/styles/flowbox.scss +72 -72
  145. package/src/styles/maps.scss +395 -395
  146. package/src/styles/modal.scss +34 -34
  147. package/src/styles/pagination.scss +10 -10
  148. package/src/styles/spinner.scss +30 -30
@@ -1,487 +1,487 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import moment from 'moment';
4
-
5
- const months = {
6
- da: ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'],
7
- en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
8
- };
9
- const dayTitles = {
10
- da: ['Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn'],
11
- en: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
12
- };
13
-
14
- const daysInMonth = (year, month) => new Date(year, month, 0).getDate();
15
-
16
- const addDays = (days, date) => new Date(date.getTime() + days * 60 * 24 * 60000);
17
-
18
- const calcEaster = (year) => {
19
- const a = year % 19;
20
- const b = Math.floor(year / 100);
21
- const c = year % 100;
22
- const d = Math.floor(b / 4);
23
- const e = b % 4;
24
- const f = Math.floor((b + 8) / 25);
25
- const g = Math.floor((b - f + 1) / 3);
26
- const h = (19 * a + b - d - g + 15) % 30;
27
- const i = Math.floor(c / 4);
28
- const k = c % 4;
29
- const l = (32 + 2 * e + 2 * i - h - k) % 7;
30
- const m = Math.floor((a + 11 * h + 22 * l) / 451);
31
- const n0 = (h + l + 7 * m + 114);
32
- const n = Math.floor(n0 / 31) - 1;
33
- const p = (n0 % 31) + 1;
34
- const date = new Date(year, n, p);
35
- return date;
36
- };
37
-
38
- const isHoliday = (date) => {
39
- const year = date.getFullYear();
40
- const holidays = [];
41
- const newYear = new Date(year, 0, 1);
42
- holidays.push(newYear);
43
- const easter = calcEaster(year);
44
- holidays.push(easter);
45
- const easterThu = addDays(-3, easter);
46
- holidays.push(easterThu);
47
- const easterFri = addDays(-2, easter);
48
- holidays.push(easterFri);
49
- const easter2 = addDays(1, easter);
50
- holidays.push(easter2);
51
- const stBededag = addDays(26, easter);
52
- holidays.push(stBededag);
53
- const kristiHf = addDays(39, easter);
54
- holidays.push(kristiHf);
55
- const whitsun = addDays(49, easter);
56
- holidays.push(whitsun);
57
- const whitsun2 = addDays(1, whitsun);
58
- holidays.push(whitsun2);
59
- const grundlov = new Date(year, 5, 5);
60
- holidays.push(grundlov);
61
- const christmas = new Date(year, 11, 25);
62
- holidays.push(christmas);
63
- const christmas2 = addDays(1, christmas);
64
- holidays.push(christmas2);
65
-
66
- return holidays.map((h) => h.toString()).find((x) => x === date.toString());
67
- };
68
-
69
- const dateAllowed = (date, limitStart, limitEnd) => {
70
- if (limitStart && !limitEnd) {
71
- return date >= limitStart;
72
- }
73
-
74
- if (!limitStart && limitEnd) {
75
- return date <= limitEnd;
76
- }
77
-
78
- if (limitStart && limitEnd) {
79
- return date >= limitStart && date <= limitEnd;
80
- }
81
-
82
- return true;
83
- }
84
-
85
- class AUCalendarComponent extends React.Component {
86
- constructor(props) {
87
- super(props);
88
-
89
- this.state = {
90
- day: props.day,
91
- month: props.month,
92
- year: props.year,
93
- hour: props.hour,
94
- minute: props.minute,
95
- selected: props.selected,
96
- };
97
-
98
- this.skipDays = [6, 0, 1, 2, 3, 4, 5];
99
- this.goBack = this.goBack.bind(this);
100
- this.goForward = this.goForward.bind(this);
101
-
102
- moment.locale(props.lang);
103
- }
104
-
105
- goBack() {
106
- const { month, year } = this.state;
107
- const newMonth = month - 1 < 1 ? 12 : (month - 1);
108
- const newYear = newMonth === 12 ? (year - 1) : year;
109
- this.setState({
110
- month: newMonth,
111
- year: newYear,
112
- });
113
- }
114
-
115
- goForward() {
116
- const { month, year } = this.state;
117
- const newMonth = month + 1 > 12 ? 1 : (month + 1);
118
- const newYear = newMonth === 1 ? (year + 1) : year;
119
- this.setState({
120
- month: newMonth,
121
- year: newYear,
122
- });
123
- }
124
-
125
- render() {
126
- const {
127
- day,
128
- month,
129
- year,
130
- hour,
131
- minute,
132
- selected,
133
- } = this.state;
134
- const {
135
- lang,
136
- onSelected,
137
- highlightWeekend,
138
- highlightHolidays,
139
- yearSpanMinus,
140
- yearSpanPlus,
141
- minuteInterval,
142
- showTime,
143
- closeable,
144
- onClose,
145
- controls,
146
- limitStart,
147
- limitEnd,
148
- } = this.props;
149
-
150
- const renderMonths = months[lang].map(
151
- (m, i) => (
152
- <option key={m} value={i + 1}>
153
- {m}
154
- </option>
155
- ),
156
- );
157
-
158
- let yearStart = yearSpanMinus;
159
- if (limitStart && limitStart.getFullYear() > (selected.getFullYear() - yearSpanMinus)) {
160
- yearStart = selected.getFullYear() - limitStart.getFullYear();
161
- }
162
-
163
- let yearEnd = yearSpanPlus;
164
- if (limitEnd && limitEnd.getFullYear() < (selected.getFullYear() + yearSpanPlus)) {
165
- yearEnd = limitEnd.getFullYear() + selected.getFullYear();
166
- }
167
-
168
- const yearArr = [];
169
- for (let y = year - yearStart; y <= year + yearEnd; y += 1) {
170
- yearArr.push(y);
171
- }
172
-
173
- const renderYears = yearArr.map(
174
- (y) => (
175
- <option key={y}>
176
- {y}
177
- </option>
178
- ),
179
- );
180
-
181
- const renderWeekDays = dayTitles[lang].map(
182
- (w) => (
183
- <span key={w} className="calendar__days__day">
184
- {w}
185
- </span>
186
- ),
187
- );
188
-
189
- const firstDayInMonth = new Date(year, month - 1, 1);
190
- const emptyDays = this.skipDays[firstDayInMonth.getDay()];
191
- const prevYear = month === 1 ? year - 1 : year;
192
- const prevMonth = month === 1 ? 12 : month - 1;
193
- const numberOfDaysInPrevMonth = daysInMonth(
194
- prevYear, prevMonth,
195
- );
196
-
197
- let rowCount = -1;
198
- const renderEmptyDaysBefore = Array(emptyDays).fill().map((e, i) => {
199
- rowCount = i;
200
- const beforeDay = numberOfDaysInPrevMonth - emptyDays + i + 1;
201
- const date = new Date(prevYear, prevMonth - 1, beforeDay);
202
- const isWeekend = (highlightWeekend && rowCount > 4)
203
- || (highlightHolidays
204
- && isHoliday(date));
205
- return (
206
- <button
207
- type="button"
208
- key={rowCount}
209
- disabled={limitStart && date < limitStart}
210
- className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
211
- onClick={() => {
212
- this.goBack();
213
- }}
214
- aria-label={moment(date).format('LL')}
215
- >
216
- {beforeDay}
217
- </button>
218
- );
219
- });
220
-
221
- const renderDays = Array(daysInMonth(year, month)).fill().map((d, i) => {
222
- const key = i; // suck it
223
- if (rowCount === 6) {
224
- rowCount = 0;
225
- } else {
226
- rowCount += 1;
227
- }
228
- const newDay = i + 1;
229
- const isSelected = (
230
- newDay === selected.getDate()
231
- && year === selected.getFullYear()
232
- && month === selected.getMonth() + 1
233
- );
234
- const isWeekend = (highlightWeekend && rowCount > 4)
235
- || (highlightHolidays && isHoliday(new Date(year, month - 1, newDay)));
236
- let className = 'calendar__days__date';
237
- if (isSelected) {
238
- className = `${className} calendar__days__date--selected`;
239
- }
240
- if (isWeekend) {
241
- className = `${className} calendar__days__date--weekend`;
242
- }
243
- const date = new Date(year, month - 1, newDay, hour, minute);
244
- return (
245
- <button
246
- key={key}
247
- disabled={(limitStart && date < limitStart) || (limitEnd && date > limitEnd)}
248
- type="button"
249
- className={className}
250
- onClick={() => {
251
- const newSelected = date;
252
- this.setState({
253
- day: newDay,
254
- selected: newSelected,
255
- }, () => {
256
- onSelected(newSelected);
257
- });
258
- }}
259
- aria-label={moment(date).format('LL')}
260
- >
261
- {i + 1}
262
- </button>
263
- );
264
- });
265
-
266
- const nextYear = month === 12 ? (year + 1) : year;
267
- const nextMonth = month === 12 ? 1 : (month + 1);
268
- const renderEmptyDaysAfter = Array(6 - rowCount).fill().map((e, i) => {
269
- const key = i;
270
- const afterDay = i + 1;
271
- if (rowCount === 6) {
272
- rowCount = 0;
273
- } else {
274
- rowCount += 1;
275
- }
276
- const date = new Date(nextYear, nextMonth - 1, afterDay);
277
- const isWeekend = (highlightWeekend && rowCount > 4)
278
- || (highlightHolidays
279
- && isHoliday(date));
280
- return (
281
- <button
282
- type="button"
283
- key={key}
284
- disabled={limitEnd && date > limitEnd}
285
- className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
286
- onClick={() => {
287
- this.goForward();
288
- }}
289
- aria-label={moment(date).format('LL')}
290
- >
291
- {afterDay}
292
- </button>
293
- );
294
- });
295
-
296
- const renderHours = Array(24).fill().map((h, i) => {
297
- const key = i;
298
- const optHour = i < 10 ? `0${i}` : i;
299
- return (
300
- <option key={key} value={i}>
301
- {optHour}
302
- </option>
303
- );
304
- });
305
-
306
- const renderMinutes = Array(60).fill().map((h, i) => {
307
- const key = i;
308
- if (i % minuteInterval === 0) {
309
- const optMinute = i < 10 ? `0${i}` : i;
310
- return (
311
- <option key={key} value={i}>
312
- {optMinute}
313
- </option>
314
- );
315
- }
316
- return null;
317
- });
318
-
319
- return (
320
- <div id={`${controls}-calendar`} className="calendar" aria-controls={controls}>
321
- <div className="calendar__year-month">
322
- <div className="form__field">
323
- <select
324
- value={month}
325
- title={lang === 'da' ? 'Vælg måned' : 'Select month'}
326
- onChange={(e) => {
327
- const newMonth = parseInt(e.target.value, 10);
328
- const newSelected = new Date(year, newMonth - 1, day, hour, minute);
329
- if (dateAllowed(newSelected, limitStart, limitEnd)) {
330
- this.setState({
331
- month: newMonth,
332
- selected: newSelected,
333
- }, () => {
334
- onSelected(newSelected);
335
- });
336
- } else {
337
- this.setState({
338
- month: newMonth,
339
- });
340
- }
341
- }}
342
- >
343
- {renderMonths}
344
- </select>
345
- </div>
346
- <div className="form__field">
347
- <select
348
- value={year}
349
- title={lang === 'da' ? 'Vælg år' : 'Select year'}
350
- onChange={(e) => {
351
- const newYear = parseInt(e.target.value, 10);
352
- const newSelected = new Date(newYear, month - 1, day, hour, minute);
353
- if (dateAllowed(newSelected, limitStart, limitEnd)) {
354
- this.setState({
355
- year: newYear,
356
- selected: newSelected,
357
- }, () => {
358
- onSelected(newSelected);
359
- });
360
- } else {
361
- this.setState({
362
- year: newYear,
363
- });
364
- }
365
- }}
366
- >
367
- {renderYears}
368
- </select>
369
- </div>
370
- {
371
- closeable && (
372
- <button
373
- className="button button--icon--hide-label button--small button--icon"
374
- type="button"
375
- data-icon=""
376
- onClick={onClose}
377
- >
378
- {lang === 'da' ? 'Luk' : 'Close'}
379
- </button>
380
- )
381
- }
382
- </div>
383
- <div className="calendar__days">
384
- {renderWeekDays}
385
- {renderEmptyDaysBefore}
386
- {renderDays}
387
- {renderEmptyDaysAfter}
388
- </div>
389
- {
390
- showTime && (
391
- <div className="calendar__time">
392
- <div className="form__field">
393
- <select
394
- value={hour}
395
- title={lang === 'da' ? 'Vælg time' : 'Select hour'}
396
- onChange={(e) => {
397
- const newHour = parseInt(e.target.value, 10);
398
- const newSelected = new Date(year, month - 1, day, newHour, minute);
399
- if (dateAllowed(newSelected, limitStart, limitEnd)) {
400
- this.setState({
401
- hour: newHour,
402
- selected: newSelected,
403
- }, () => {
404
- onSelected(newSelected);
405
- });
406
- } else {
407
- this.setState({
408
- hour: newHour,
409
- });
410
- }
411
- }}
412
- >
413
- {renderHours}
414
- </select>
415
- </div>
416
- <div className="form__field">
417
- <select
418
- value={minute}
419
- title={lang === 'da' ? 'Vælg minut' : 'Select minute'}
420
- onChange={(e) => {
421
- const newMinute = parseInt(e.target.value, 10);
422
- const newSelected = new Date(year, month - 1, day, hour, newMinute);
423
- if (dateAllowed(newSelected, limitStart, limitEnd)) {
424
- this.setState({
425
- minute: newMinute,
426
- selected: newSelected,
427
- }, () => {
428
- onSelected(newSelected);
429
- });
430
- } else {
431
- this.setState({
432
- minut: newMinute,
433
- });
434
- }
435
- }}
436
- >
437
- {renderMinutes}
438
- </select>
439
- </div>
440
- </div>
441
- )
442
- }
443
- </div>
444
- );
445
- }
446
- }
447
-
448
- AUCalendarComponent.defaultProps = {
449
- lang: 'da',
450
- selected: new Date(),
451
- onSelected: () => { },
452
- highlightWeekend: true,
453
- highlightHolidays: true,
454
- yearSpanMinus: 5,
455
- yearSpanPlus: 5,
456
- minuteInterval: 5,
457
- showTime: true,
458
- closeable: false,
459
- onClose: () => { },
460
- limitStart: null,
461
- limitEnd: null,
462
- };
463
-
464
- AUCalendarComponent.propTypes = {
465
- lang: PropTypes.string,
466
- selected: PropTypes.instanceOf(Date),
467
- day: PropTypes.number.isRequired,
468
- month: PropTypes.number.isRequired,
469
- year: PropTypes.number.isRequired,
470
- hour: PropTypes.number.isRequired,
471
- minute: PropTypes.number.isRequired,
472
- highlightWeekend: PropTypes.bool,
473
- highlightHolidays: PropTypes.bool,
474
- onSelected: PropTypes.func,
475
- yearSpanMinus: PropTypes.number,
476
- yearSpanPlus: PropTypes.number,
477
- minuteInterval: PropTypes.number,
478
- showTime: PropTypes.bool,
479
- closeable: PropTypes.bool,
480
- onClose: PropTypes.func,
481
- controls: PropTypes.string.isRequired,
482
- limitStart: PropTypes.instanceOf(Date),
483
- limitEnd: PropTypes.instanceOf(Date),
484
- };
485
-
486
- AUCalendarComponent.displayName = 'AUCalendarComponent';
487
- export default AUCalendarComponent;
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import moment from 'moment';
4
+
5
+ const months = {
6
+ da: ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December'],
7
+ en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
8
+ };
9
+ const dayTitles = {
10
+ da: ['Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn'],
11
+ en: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
12
+ };
13
+
14
+ const daysInMonth = (year, month) => new Date(year, month, 0).getDate();
15
+
16
+ const addDays = (days, date) => new Date(date.getTime() + days * 60 * 24 * 60000);
17
+
18
+ const calcEaster = (year) => {
19
+ const a = year % 19;
20
+ const b = Math.floor(year / 100);
21
+ const c = year % 100;
22
+ const d = Math.floor(b / 4);
23
+ const e = b % 4;
24
+ const f = Math.floor((b + 8) / 25);
25
+ const g = Math.floor((b - f + 1) / 3);
26
+ const h = (19 * a + b - d - g + 15) % 30;
27
+ const i = Math.floor(c / 4);
28
+ const k = c % 4;
29
+ const l = (32 + 2 * e + 2 * i - h - k) % 7;
30
+ const m = Math.floor((a + 11 * h + 22 * l) / 451);
31
+ const n0 = (h + l + 7 * m + 114);
32
+ const n = Math.floor(n0 / 31) - 1;
33
+ const p = (n0 % 31) + 1;
34
+ const date = new Date(year, n, p);
35
+ return date;
36
+ };
37
+
38
+ const isHoliday = (date) => {
39
+ const year = date.getFullYear();
40
+ const holidays = [];
41
+ const newYear = new Date(year, 0, 1);
42
+ holidays.push(newYear);
43
+ const easter = calcEaster(year);
44
+ holidays.push(easter);
45
+ const easterThu = addDays(-3, easter);
46
+ holidays.push(easterThu);
47
+ const easterFri = addDays(-2, easter);
48
+ holidays.push(easterFri);
49
+ const easter2 = addDays(1, easter);
50
+ holidays.push(easter2);
51
+ const stBededag = addDays(26, easter);
52
+ holidays.push(stBededag);
53
+ const kristiHf = addDays(39, easter);
54
+ holidays.push(kristiHf);
55
+ const whitsun = addDays(49, easter);
56
+ holidays.push(whitsun);
57
+ const whitsun2 = addDays(1, whitsun);
58
+ holidays.push(whitsun2);
59
+ const grundlov = new Date(year, 5, 5);
60
+ holidays.push(grundlov);
61
+ const christmas = new Date(year, 11, 25);
62
+ holidays.push(christmas);
63
+ const christmas2 = addDays(1, christmas);
64
+ holidays.push(christmas2);
65
+
66
+ return holidays.map((h) => h.toString()).find((x) => x === date.toString());
67
+ };
68
+
69
+ const dateAllowed = (date, limitStart, limitEnd) => {
70
+ if (limitStart && !limitEnd) {
71
+ return date >= limitStart;
72
+ }
73
+
74
+ if (!limitStart && limitEnd) {
75
+ return date <= limitEnd;
76
+ }
77
+
78
+ if (limitStart && limitEnd) {
79
+ return date >= limitStart && date <= limitEnd;
80
+ }
81
+
82
+ return true;
83
+ }
84
+
85
+ class AUCalendarComponent extends React.Component {
86
+ constructor(props) {
87
+ super(props);
88
+
89
+ this.state = {
90
+ day: props.day,
91
+ month: props.month,
92
+ year: props.year,
93
+ hour: props.hour,
94
+ minute: props.minute,
95
+ selected: props.selected,
96
+ };
97
+
98
+ this.skipDays = [6, 0, 1, 2, 3, 4, 5];
99
+ this.goBack = this.goBack.bind(this);
100
+ this.goForward = this.goForward.bind(this);
101
+
102
+ moment.locale(props.lang);
103
+ }
104
+
105
+ goBack() {
106
+ const { month, year } = this.state;
107
+ const newMonth = month - 1 < 1 ? 12 : (month - 1);
108
+ const newYear = newMonth === 12 ? (year - 1) : year;
109
+ this.setState({
110
+ month: newMonth,
111
+ year: newYear,
112
+ });
113
+ }
114
+
115
+ goForward() {
116
+ const { month, year } = this.state;
117
+ const newMonth = month + 1 > 12 ? 1 : (month + 1);
118
+ const newYear = newMonth === 1 ? (year + 1) : year;
119
+ this.setState({
120
+ month: newMonth,
121
+ year: newYear,
122
+ });
123
+ }
124
+
125
+ render() {
126
+ const {
127
+ day,
128
+ month,
129
+ year,
130
+ hour,
131
+ minute,
132
+ selected,
133
+ } = this.state;
134
+ const {
135
+ lang,
136
+ onSelected,
137
+ highlightWeekend,
138
+ highlightHolidays,
139
+ yearSpanMinus,
140
+ yearSpanPlus,
141
+ minuteInterval,
142
+ showTime,
143
+ closeable,
144
+ onClose,
145
+ controls,
146
+ limitStart,
147
+ limitEnd,
148
+ } = this.props;
149
+
150
+ const renderMonths = months[lang].map(
151
+ (m, i) => (
152
+ <option key={m} value={i + 1}>
153
+ {m}
154
+ </option>
155
+ ),
156
+ );
157
+
158
+ let yearStart = yearSpanMinus;
159
+ if (limitStart && limitStart.getFullYear() > (selected.getFullYear() - yearSpanMinus)) {
160
+ yearStart = selected.getFullYear() - limitStart.getFullYear();
161
+ }
162
+
163
+ let yearEnd = yearSpanPlus;
164
+ if (limitEnd && limitEnd.getFullYear() < (selected.getFullYear() + yearSpanPlus)) {
165
+ yearEnd = limitEnd.getFullYear() + selected.getFullYear();
166
+ }
167
+
168
+ const yearArr = [];
169
+ for (let y = year - yearStart; y <= year + yearEnd; y += 1) {
170
+ yearArr.push(y);
171
+ }
172
+
173
+ const renderYears = yearArr.map(
174
+ (y) => (
175
+ <option key={y}>
176
+ {y}
177
+ </option>
178
+ ),
179
+ );
180
+
181
+ const renderWeekDays = dayTitles[lang].map(
182
+ (w) => (
183
+ <span key={w} className="calendar__days__day">
184
+ {w}
185
+ </span>
186
+ ),
187
+ );
188
+
189
+ const firstDayInMonth = new Date(year, month - 1, 1);
190
+ const emptyDays = this.skipDays[firstDayInMonth.getDay()];
191
+ const prevYear = month === 1 ? year - 1 : year;
192
+ const prevMonth = month === 1 ? 12 : month - 1;
193
+ const numberOfDaysInPrevMonth = daysInMonth(
194
+ prevYear, prevMonth,
195
+ );
196
+
197
+ let rowCount = -1;
198
+ const renderEmptyDaysBefore = Array(emptyDays).fill().map((e, i) => {
199
+ rowCount = i;
200
+ const beforeDay = numberOfDaysInPrevMonth - emptyDays + i + 1;
201
+ const date = new Date(prevYear, prevMonth - 1, beforeDay);
202
+ const isWeekend = (highlightWeekend && rowCount > 4)
203
+ || (highlightHolidays
204
+ && isHoliday(date));
205
+ return (
206
+ <button
207
+ type="button"
208
+ key={rowCount}
209
+ disabled={limitStart && date < limitStart}
210
+ className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
211
+ onClick={() => {
212
+ this.goBack();
213
+ }}
214
+ aria-label={moment(date).format('LL')}
215
+ >
216
+ {beforeDay}
217
+ </button>
218
+ );
219
+ });
220
+
221
+ const renderDays = Array(daysInMonth(year, month)).fill().map((d, i) => {
222
+ const key = i; // suck it
223
+ if (rowCount === 6) {
224
+ rowCount = 0;
225
+ } else {
226
+ rowCount += 1;
227
+ }
228
+ const newDay = i + 1;
229
+ const isSelected = (
230
+ newDay === selected.getDate()
231
+ && year === selected.getFullYear()
232
+ && month === selected.getMonth() + 1
233
+ );
234
+ const isWeekend = (highlightWeekend && rowCount > 4)
235
+ || (highlightHolidays && isHoliday(new Date(year, month - 1, newDay)));
236
+ let className = 'calendar__days__date';
237
+ if (isSelected) {
238
+ className = `${className} calendar__days__date--selected`;
239
+ }
240
+ if (isWeekend) {
241
+ className = `${className} calendar__days__date--weekend`;
242
+ }
243
+ const date = new Date(year, month - 1, newDay, hour, minute);
244
+ return (
245
+ <button
246
+ key={key}
247
+ disabled={(limitStart && date < limitStart) || (limitEnd && date > limitEnd)}
248
+ type="button"
249
+ className={className}
250
+ onClick={() => {
251
+ const newSelected = date;
252
+ this.setState({
253
+ day: newDay,
254
+ selected: newSelected,
255
+ }, () => {
256
+ onSelected(newSelected);
257
+ });
258
+ }}
259
+ aria-label={moment(date).format('LL')}
260
+ >
261
+ {i + 1}
262
+ </button>
263
+ );
264
+ });
265
+
266
+ const nextYear = month === 12 ? (year + 1) : year;
267
+ const nextMonth = month === 12 ? 1 : (month + 1);
268
+ const renderEmptyDaysAfter = Array(6 - rowCount).fill().map((e, i) => {
269
+ const key = i;
270
+ const afterDay = i + 1;
271
+ if (rowCount === 6) {
272
+ rowCount = 0;
273
+ } else {
274
+ rowCount += 1;
275
+ }
276
+ const date = new Date(nextYear, nextMonth - 1, afterDay);
277
+ const isWeekend = (highlightWeekend && rowCount > 4)
278
+ || (highlightHolidays
279
+ && isHoliday(date));
280
+ return (
281
+ <button
282
+ type="button"
283
+ key={key}
284
+ disabled={limitEnd && date > limitEnd}
285
+ className={`calendar__days__date ${isWeekend ? 'calendar__days__date--weekend calendar__days__date--empty' : 'calendar__days__date--empty'}`}
286
+ onClick={() => {
287
+ this.goForward();
288
+ }}
289
+ aria-label={moment(date).format('LL')}
290
+ >
291
+ {afterDay}
292
+ </button>
293
+ );
294
+ });
295
+
296
+ const renderHours = Array(24).fill().map((h, i) => {
297
+ const key = i;
298
+ const optHour = i < 10 ? `0${i}` : i;
299
+ return (
300
+ <option key={key} value={i}>
301
+ {optHour}
302
+ </option>
303
+ );
304
+ });
305
+
306
+ const renderMinutes = Array(60).fill().map((h, i) => {
307
+ const key = i;
308
+ if (i % minuteInterval === 0) {
309
+ const optMinute = i < 10 ? `0${i}` : i;
310
+ return (
311
+ <option key={key} value={i}>
312
+ {optMinute}
313
+ </option>
314
+ );
315
+ }
316
+ return null;
317
+ });
318
+
319
+ return (
320
+ <div id={`${controls}-calendar`} className="calendar" aria-controls={controls}>
321
+ <div className="calendar__year-month">
322
+ <div className="form__field">
323
+ <select
324
+ value={month}
325
+ title={lang === 'da' ? 'Vælg måned' : 'Select month'}
326
+ onChange={(e) => {
327
+ const newMonth = parseInt(e.target.value, 10);
328
+ const newSelected = new Date(year, newMonth - 1, day, hour, minute);
329
+ if (dateAllowed(newSelected, limitStart, limitEnd)) {
330
+ this.setState({
331
+ month: newMonth,
332
+ selected: newSelected,
333
+ }, () => {
334
+ onSelected(newSelected);
335
+ });
336
+ } else {
337
+ this.setState({
338
+ month: newMonth,
339
+ });
340
+ }
341
+ }}
342
+ >
343
+ {renderMonths}
344
+ </select>
345
+ </div>
346
+ <div className="form__field">
347
+ <select
348
+ value={year}
349
+ title={lang === 'da' ? 'Vælg år' : 'Select year'}
350
+ onChange={(e) => {
351
+ const newYear = parseInt(e.target.value, 10);
352
+ const newSelected = new Date(newYear, month - 1, day, hour, minute);
353
+ if (dateAllowed(newSelected, limitStart, limitEnd)) {
354
+ this.setState({
355
+ year: newYear,
356
+ selected: newSelected,
357
+ }, () => {
358
+ onSelected(newSelected);
359
+ });
360
+ } else {
361
+ this.setState({
362
+ year: newYear,
363
+ });
364
+ }
365
+ }}
366
+ >
367
+ {renderYears}
368
+ </select>
369
+ </div>
370
+ {
371
+ closeable && (
372
+ <button
373
+ className="button button--icon--hide-label button--small button--icon"
374
+ type="button"
375
+ data-icon=""
376
+ onClick={onClose}
377
+ >
378
+ {lang === 'da' ? 'Luk' : 'Close'}
379
+ </button>
380
+ )
381
+ }
382
+ </div>
383
+ <div className="calendar__days">
384
+ {renderWeekDays}
385
+ {renderEmptyDaysBefore}
386
+ {renderDays}
387
+ {renderEmptyDaysAfter}
388
+ </div>
389
+ {
390
+ showTime && (
391
+ <div className="calendar__time">
392
+ <div className="form__field">
393
+ <select
394
+ value={hour}
395
+ title={lang === 'da' ? 'Vælg time' : 'Select hour'}
396
+ onChange={(e) => {
397
+ const newHour = parseInt(e.target.value, 10);
398
+ const newSelected = new Date(year, month - 1, day, newHour, minute);
399
+ if (dateAllowed(newSelected, limitStart, limitEnd)) {
400
+ this.setState({
401
+ hour: newHour,
402
+ selected: newSelected,
403
+ }, () => {
404
+ onSelected(newSelected);
405
+ });
406
+ } else {
407
+ this.setState({
408
+ hour: newHour,
409
+ });
410
+ }
411
+ }}
412
+ >
413
+ {renderHours}
414
+ </select>
415
+ </div>
416
+ <div className="form__field">
417
+ <select
418
+ value={minute}
419
+ title={lang === 'da' ? 'Vælg minut' : 'Select minute'}
420
+ onChange={(e) => {
421
+ const newMinute = parseInt(e.target.value, 10);
422
+ const newSelected = new Date(year, month - 1, day, hour, newMinute);
423
+ if (dateAllowed(newSelected, limitStart, limitEnd)) {
424
+ this.setState({
425
+ minute: newMinute,
426
+ selected: newSelected,
427
+ }, () => {
428
+ onSelected(newSelected);
429
+ });
430
+ } else {
431
+ this.setState({
432
+ minut: newMinute,
433
+ });
434
+ }
435
+ }}
436
+ >
437
+ {renderMinutes}
438
+ </select>
439
+ </div>
440
+ </div>
441
+ )
442
+ }
443
+ </div>
444
+ );
445
+ }
446
+ }
447
+
448
+ AUCalendarComponent.defaultProps = {
449
+ lang: 'da',
450
+ selected: new Date(),
451
+ onSelected: () => { },
452
+ highlightWeekend: true,
453
+ highlightHolidays: true,
454
+ yearSpanMinus: 5,
455
+ yearSpanPlus: 5,
456
+ minuteInterval: 5,
457
+ showTime: true,
458
+ closeable: false,
459
+ onClose: () => { },
460
+ limitStart: null,
461
+ limitEnd: null,
462
+ };
463
+
464
+ AUCalendarComponent.propTypes = {
465
+ lang: PropTypes.string,
466
+ selected: PropTypes.instanceOf(Date),
467
+ day: PropTypes.number.isRequired,
468
+ month: PropTypes.number.isRequired,
469
+ year: PropTypes.number.isRequired,
470
+ hour: PropTypes.number.isRequired,
471
+ minute: PropTypes.number.isRequired,
472
+ highlightWeekend: PropTypes.bool,
473
+ highlightHolidays: PropTypes.bool,
474
+ onSelected: PropTypes.func,
475
+ yearSpanMinus: PropTypes.number,
476
+ yearSpanPlus: PropTypes.number,
477
+ minuteInterval: PropTypes.number,
478
+ showTime: PropTypes.bool,
479
+ closeable: PropTypes.bool,
480
+ onClose: PropTypes.func,
481
+ controls: PropTypes.string.isRequired,
482
+ limitStart: PropTypes.instanceOf(Date),
483
+ limitEnd: PropTypes.instanceOf(Date),
484
+ };
485
+
486
+ AUCalendarComponent.displayName = 'AUCalendarComponent';
487
+ export default AUCalendarComponent;