Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Planning.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // @flow
  2. export type eventObject = {
  3. id: number,
  4. title: string,
  5. logo: string,
  6. date_begin: string,
  7. date_end: string,
  8. description: string,
  9. club: string,
  10. category_id: number,
  11. url: string,
  12. };
  13. // Regex used to check date string validity
  14. const dateRegExp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/;
  15. /**
  16. * Gets the current day string representation in the format
  17. * YYYY-MM-DD
  18. *
  19. * @return {string} The string representation
  20. */
  21. export function getCurrentDateString(): string {
  22. return dateToString(new Date(Date.now()));
  23. }
  24. /**
  25. * Checks if the given date is before the other.
  26. *
  27. * @param event1Date Event 1 date in format YYYY-MM-DD HH:MM
  28. * @param event2Date Event 2 date in format YYYY-MM-DD HH:MM
  29. * @return {boolean}
  30. */
  31. export function isEventBefore(event1Date: string, event2Date: string): boolean {
  32. let date1 = stringToDate(event1Date);
  33. let date2 = stringToDate(event2Date);
  34. if (date1 !== null && date2 !== null)
  35. return date1 < date2;
  36. else
  37. return false;
  38. }
  39. /**
  40. * Gets only the date part of the given event date string in the format
  41. * YYYY-MM-DD HH:MM
  42. *
  43. * @param dateString The string to get the date from
  44. * @return {string|null} Date in format YYYY:MM:DD or null if given string is invalid
  45. */
  46. export function getDateOnlyString(dateString: string): string | null {
  47. if (isEventDateStringFormatValid(dateString))
  48. return dateString.split(" ")[0];
  49. else
  50. return null;
  51. }
  52. /**
  53. * Checks if the given date string is in the format
  54. * YYYY-MM-DD HH:MM
  55. *
  56. * @param dateString The string to check
  57. * @return {boolean}
  58. */
  59. export function isEventDateStringFormatValid(dateString: ?string): boolean {
  60. return dateString !== undefined
  61. && dateString !== null
  62. && dateRegExp.test(dateString);
  63. }
  64. /**
  65. * Converts the given date string to a date object.<br>
  66. * Accepted format: YYYY-MM-DD HH:MM
  67. *
  68. * @param dateString The string to convert
  69. * @return {Date|null} The date object or null if the given string is invalid
  70. */
  71. export function stringToDate(dateString: string): Date | null {
  72. let date = new Date();
  73. if (isEventDateStringFormatValid(dateString)) {
  74. let stringArray = dateString.split(' ');
  75. let dateArray = stringArray[0].split('-');
  76. let timeArray = stringArray[1].split(':');
  77. date.setFullYear(
  78. parseInt(dateArray[0]),
  79. parseInt(dateArray[1]) - 1, // Month range from 0 to 11
  80. parseInt(dateArray[2])
  81. );
  82. date.setHours(
  83. parseInt(timeArray[0]),
  84. parseInt(timeArray[1]),
  85. 0,
  86. 0,
  87. );
  88. } else
  89. date = null;
  90. return date;
  91. }
  92. /**
  93. * Converts a date object to a string in the format
  94. * YYYY-MM-DD HH-MM-SS
  95. *
  96. * @param date The date object to convert
  97. * @return {string} The converted string
  98. */
  99. export function dateToString(date: Date): string {
  100. const day = String(date.getDate()).padStart(2, '0');
  101. const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
  102. const year = date.getFullYear();
  103. const hours = String(date.getHours()).padStart(2, '0');
  104. const minutes = String(date.getMinutes()).padStart(2, '0');
  105. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
  106. }
  107. /**
  108. * Returns a string corresponding to the event start and end times in the following format:
  109. *
  110. * HH:MM - HH:MM
  111. *
  112. * If the end date is not specified or is equal to start time, only start time will be shown.
  113. *
  114. * If the end date is not on the same day, 23:59 will be shown as end time
  115. *
  116. * @param start Start time in YYYY-MM-DD HH:MM:SS format
  117. * @param end End time in YYYY-MM-DD HH:MM:SS format
  118. * @return {string} Formatted string or "/ - /" on error
  119. */
  120. export function getFormattedEventTime(start: string, end: string): string {
  121. let formattedStr = '/ - /';
  122. let startDate = stringToDate(start);
  123. let endDate = stringToDate(end);
  124. if (startDate !== null && endDate !== null && startDate.getTime() !== endDate.getTime()) {
  125. formattedStr = String(startDate.getHours()).padStart(2, '0') + ':'
  126. + String(startDate.getMinutes()).padStart(2, '0') + ' - ';
  127. if (endDate.getFullYear() > startDate.getFullYear()
  128. || endDate.getMonth() > startDate.getMonth()
  129. || endDate.getDate() > startDate.getDate())
  130. formattedStr += '23:59';
  131. else
  132. formattedStr += String(endDate.getHours()).padStart(2, '0') + ':'
  133. + String(endDate.getMinutes()).padStart(2, '0');
  134. } else if (startDate !== null)
  135. formattedStr =
  136. String(startDate.getHours()).padStart(2, '0') + ':'
  137. + String(startDate.getMinutes()).padStart(2, '0');
  138. return formattedStr
  139. }
  140. /**
  141. * Checks if the given description can be considered empty.
  142. * <br>
  143. * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
  144. *
  145. *
  146. * @param description The text to check
  147. * @return {boolean}
  148. */
  149. export function isDescriptionEmpty(description: ?string): boolean {
  150. if (description !== undefined && description !== null) {
  151. return description
  152. .split('<p>').join('') // Equivalent to a replace all
  153. .split('</p>').join('')
  154. .split('<br>').join('').trim() === '';
  155. } else
  156. return true;
  157. }
  158. /**
  159. * Generates an object with an empty array for each key.
  160. * Each key is a date string in the format
  161. * YYYY-MM-DD
  162. *
  163. * @param numberOfMonths The number of months to create, starting from the current date
  164. * @return {Object}
  165. */
  166. export function generateEmptyCalendar(numberOfMonths: number): Object {
  167. let end = new Date(Date.now());
  168. end.setMonth(end.getMonth() + numberOfMonths);
  169. let daysOfYear = {};
  170. for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
  171. const dateString = getDateOnlyString(
  172. dateToString(new Date(d)));
  173. if (dateString !== null)
  174. daysOfYear[dateString] = []
  175. }
  176. return daysOfYear;
  177. }
  178. /**
  179. * Generates an object with an array of eventObject at each key.
  180. * Each key is a date string in the format
  181. * YYYY-MM-DD.
  182. *
  183. * If no event is available at the given key, the array will be empty
  184. *
  185. * @param eventList The list of events to map to the agenda
  186. * @param numberOfMonths The number of months to create the agenda for
  187. * @return {Object}
  188. */
  189. export function generateEventAgenda(eventList: Array<eventObject>, numberOfMonths: number): Object {
  190. let agendaItems = generateEmptyCalendar(numberOfMonths);
  191. for (let i = 0; i < eventList.length; i++) {
  192. const dateString = getDateOnlyString(eventList[i].date_begin);
  193. if (dateString !== null) {
  194. const eventArray = agendaItems[dateString];
  195. if (eventArray !== undefined)
  196. pushEventInOrder(eventArray, eventList[i]);
  197. }
  198. }
  199. return agendaItems;
  200. }
  201. /**
  202. * Adds events to the given array depending on their starting date.
  203. *
  204. * Events starting before are added at the front.
  205. *
  206. * @param eventArray The array to hold sorted events
  207. * @param event The event to add to the array
  208. */
  209. export function pushEventInOrder(eventArray: Array<eventObject>, event: eventObject): Object {
  210. if (eventArray.length === 0)
  211. eventArray.push(event);
  212. else {
  213. for (let i = 0; i < eventArray.length; i++) {
  214. if (isEventBefore(event.date_begin, eventArray[i].date_begin)) {
  215. eventArray.splice(i, 0, event);
  216. break;
  217. } else if (i === eventArray.length - 1) {
  218. eventArray.push(event);
  219. break;
  220. }
  221. }
  222. }
  223. }