|
@@ -1,5 +1,8 @@
|
1
|
|
-
|
2
|
1
|
export default class PlanningEventManager {
|
|
2
|
+
|
|
3
|
+ // Regex used to check date string validity
|
|
4
|
+ static dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
|
5
|
+
|
3
|
6
|
static isEventBefore(event1: Object, event2: Object) {
|
4
|
7
|
let date1 = new Date();
|
5
|
8
|
let date2 = new Date();
|
|
@@ -41,39 +44,104 @@ export default class PlanningEventManager {
|
41
|
44
|
}
|
42
|
45
|
|
43
|
46
|
/**
|
44
|
|
- * Convert the date string given by in the event list json to a date object
|
45
|
|
- * @param dateString
|
46
|
|
- * @return {Date}
|
|
47
|
+ * Checks if the given date string is in the format
|
|
48
|
+ * YYYY-MM-DD HH:MM:SS
|
|
49
|
+ *
|
|
50
|
+ * @param dateString The string to check
|
|
51
|
+ * @return {boolean}
|
47
|
52
|
*/
|
48
|
|
- static stringToDate(dateString: ?string): ?Date {
|
|
53
|
+ static isDateStringFormatValid(dateString: ?string) {
|
|
54
|
+ return dateString !== undefined
|
|
55
|
+ && dateString !== null
|
|
56
|
+ && PlanningEventManager.dateRegExp.test(dateString);
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ /**
|
|
60
|
+ * Converts the given date string to a date object.<br>
|
|
61
|
+ * Accepted format: YYYY-MM-DD HH:MM:SS
|
|
62
|
+ *
|
|
63
|
+ * @param dateString The string to convert
|
|
64
|
+ * @return {Date} The date object or undefined if the given string is invalid
|
|
65
|
+ */
|
|
66
|
+ static stringToDate(dateString: ?string): Date | undefined {
|
49
|
67
|
let date = new Date();
|
50
|
|
- if (dateString === undefined || dateString === null)
|
51
|
|
- date = undefined;
|
52
|
|
- else if (dateString.split(' ').length > 1) {
|
53
|
|
- let timeStr = dateString.split(' ')[1];
|
54
|
|
- date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
|
|
68
|
+ if (PlanningEventManager.isDateStringFormatValid(dateString)) {
|
|
69
|
+ let stringArray = dateString.split(' ');
|
|
70
|
+ let dateArray = stringArray[0].split('-');
|
|
71
|
+ let timeArray = stringArray[1].split(':');
|
|
72
|
+ date.setFullYear(
|
|
73
|
+ parseInt(dateArray[0]),
|
|
74
|
+ parseInt(dateArray[1]) - 1, // Month range from 0 to 11
|
|
75
|
+ parseInt(dateArray[2])
|
|
76
|
+ );
|
|
77
|
+ date.setHours(
|
|
78
|
+ parseInt(timeArray[0]),
|
|
79
|
+ parseInt(timeArray[1]),
|
|
80
|
+ parseInt(timeArray[2]),
|
|
81
|
+ 0,
|
|
82
|
+ );
|
55
|
83
|
} else
|
56
|
84
|
date = undefined;
|
|
85
|
+
|
57
|
86
|
return date;
|
58
|
87
|
}
|
59
|
88
|
|
60
|
|
- static padStr(i: number) {
|
61
|
|
- return (i < 10) ? "0" + i : "" + i;
|
|
89
|
+ /**
|
|
90
|
+ * Returns a padded string for the given number if it is lower than 10
|
|
91
|
+ *
|
|
92
|
+ * @param i The string to be converted
|
|
93
|
+ * @return {string}
|
|
94
|
+ */
|
|
95
|
+ static toPaddedString(i: number): string {
|
|
96
|
+ return (i < 10 && i >= 0) ? "0" + i.toString(10) : i.toString(10);
|
62
|
97
|
}
|
63
|
98
|
|
64
|
|
- static getFormattedEventTime(event: Object): string {
|
65
|
|
- let formattedStr = '';
|
66
|
|
- let startDate = PlanningEventManager.stringToDate(event['date_begin']);
|
67
|
|
- let endDate = PlanningEventManager.stringToDate(event['date_end']);
|
68
|
|
- if (startDate !== undefined && startDate !== null && endDate !== undefined && endDate !== null)
|
69
|
|
- formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes()) +
|
70
|
|
- ' - ' + PlanningEventManager.padStr(endDate.getHours()) + ':' + PlanningEventManager.padStr(endDate.getMinutes());
|
71
|
|
- else if (startDate !== undefined && startDate !== null)
|
72
|
|
- formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes());
|
|
99
|
+ /**
|
|
100
|
+ * Returns a string corresponding to the event start and end times in the following format:
|
|
101
|
+ *
|
|
102
|
+ * HH:MM - HH:MM
|
|
103
|
+ *
|
|
104
|
+ * If the end date is not specified or is equal to start time, only start time will be shown.
|
|
105
|
+ *
|
|
106
|
+ * If the end date is not on the same day, 00:00 will be shown as end time
|
|
107
|
+ *
|
|
108
|
+ * @param start Start time in YYYY-MM-DD HH:MM:SS format
|
|
109
|
+ * @param end End time in YYYY-MM-DD HH:MM:SS format
|
|
110
|
+ * @return {string} Formatted string or "/ - /" on error
|
|
111
|
+ */
|
|
112
|
+ static getFormattedEventTime(start: ?string, end: ?string): string {
|
|
113
|
+ let formattedStr = '/ - /';
|
|
114
|
+ let startDate = PlanningEventManager.stringToDate(start);
|
|
115
|
+ let endDate = PlanningEventManager.stringToDate(end);
|
|
116
|
+
|
|
117
|
+ if (startDate !== undefined && endDate !== undefined && startDate.getTime() !== endDate.getTime()) {
|
|
118
|
+ formattedStr = PlanningEventManager.toPaddedString(startDate.getHours()) + ':'
|
|
119
|
+ + PlanningEventManager.toPaddedString(startDate.getMinutes()) + ' - ';
|
|
120
|
+ if (endDate.getFullYear() > startDate.getFullYear()
|
|
121
|
+ || endDate.getMonth() > startDate.getMonth()
|
|
122
|
+ || endDate.getDate() > startDate.getDate())
|
|
123
|
+ formattedStr += '00:00';
|
|
124
|
+ else
|
|
125
|
+ formattedStr += PlanningEventManager.toPaddedString(endDate.getHours()) + ':'
|
|
126
|
+ + PlanningEventManager.toPaddedString(endDate.getMinutes());
|
|
127
|
+ } else if (startDate !== undefined)
|
|
128
|
+ formattedStr =
|
|
129
|
+ PlanningEventManager.toPaddedString(startDate.getHours()) + ':'
|
|
130
|
+ + PlanningEventManager.toPaddedString(startDate.getMinutes());
|
|
131
|
+
|
73
|
132
|
return formattedStr
|
74
|
133
|
}
|
75
|
134
|
|
76
|
|
- static isDescriptionEmpty (description: ?string) {
|
|
135
|
+ /**
|
|
136
|
+ * Checks if the given description can be considered empty.
|
|
137
|
+ * <br>
|
|
138
|
+ * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
|
|
139
|
+ *
|
|
140
|
+ *
|
|
141
|
+ * @param description The text to check
|
|
142
|
+ * @return {boolean}
|
|
143
|
+ */
|
|
144
|
+ static isDescriptionEmpty(description: ?string): boolean {
|
77
|
145
|
if (description !== undefined && description !== null) {
|
78
|
146
|
return description
|
79
|
147
|
.split('<p>').join('') // Equivalent to a replace all
|