application-amicale/utils/PlanningEventManager.js

86 lines
3.4 KiB
JavaScript
Raw Normal View History

export default class PlanningEventManager {
static isEventBefore(event1: Object, event2: Object) {
let date1 = new Date();
let date2 = new Date();
2020-03-09 19:00:12 +01:00
let timeArray = PlanningEventManager.getEventStartTime(event1).split(":");
date1.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
2020-03-09 19:00:12 +01:00
timeArray = PlanningEventManager.getEventStartTime(event2).split(":");
date2.setHours(parseInt(timeArray[0]), parseInt(timeArray[1]));
return date1 < date2;
}
static getEventStartDate(event: Object) {
return event.date_begin.split(" ")[0];
}
static getEventStartTime(event: Object) {
if (event !== undefined && Object.keys(event).length > 0 && event.date_begin !== null)
2020-03-09 19:00:12 +01:00
return PlanningEventManager.formatTime(event.date_begin.split(" ")[1]);
else
return "";
}
static getEventEndTime(event: Object) {
if (event !== undefined && Object.keys(event).length > 0 && event.date_end !== null)
2020-03-09 19:00:12 +01:00
return PlanningEventManager.formatTime(event.date_end.split(" ")[1]);
else
return "";
}
static getFormattedTime(event: Object) {
2020-03-09 19:00:12 +01:00
if (PlanningEventManager.getEventEndTime(event) !== "")
return PlanningEventManager.getEventStartTime(event) + " - " + PlanningEventManager.getEventEndTime(event);
else
2020-03-09 19:00:12 +01:00
return PlanningEventManager.getEventStartTime(event);
}
static formatTime(time: string) {
let array = time.split(':');
return array[0] + ':' + array[1];
}
2020-03-09 19:00:12 +01:00
/**
* Convert the date string given by in the event list json to a date object
* @param dateString
* @return {Date}
*/
static stringToDate(dateString: ?string): ?Date {
let date = new Date();
if (dateString === undefined || dateString === null)
date = undefined;
else if (dateString.split(' ').length > 1) {
let timeStr = dateString.split(' ')[1];
date.setHours(parseInt(timeStr.split(':')[0]), parseInt(timeStr.split(':')[1]), 0);
} else
date = undefined;
return date;
}
static padStr(i: number) {
return (i < 10) ? "0" + i : "" + i;
}
static getFormattedEventTime(event: Object): string {
let formattedStr = '';
let startDate = PlanningEventManager.stringToDate(event['date_begin']);
let endDate = PlanningEventManager.stringToDate(event['date_end']);
if (startDate !== undefined && startDate !== null && endDate !== undefined && endDate !== null)
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes()) +
' - ' + PlanningEventManager.padStr(endDate.getHours()) + ':' + PlanningEventManager.padStr(endDate.getMinutes());
else if (startDate !== undefined && startDate !== null)
formattedStr = PlanningEventManager.padStr(startDate.getHours()) + ':' + PlanningEventManager.padStr(startDate.getMinutes());
return formattedStr
}
2020-03-21 11:24:30 +01:00
static isDescriptionEmpty (description: ?string) {
2020-03-20 22:50:28 +01:00
if (description !== undefined && description !== null) {
return description
2020-03-21 11:24:30 +01:00
.split('<p>').join('') // Equivalent to a replace all
.split('</p>').join('')
.split('<br>').join('').trim() === '';
2020-03-20 22:50:28 +01:00
} else
return true;
}
}