Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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()), false);
  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. * Gets only the time part of the given event date string in the format
  54. * YYYY-MM-DD HH:MM
  55. *
  56. * @param dateString The string to get the date from
  57. * @return {string|null} Time in format HH:MM or null if given string is invalid
  58. */
  59. export function getTimeOnlyString(dateString: string): string | null {
  60. if (isEventDateStringFormatValid(dateString))
  61. return dateString.split(" ")[1];
  62. else
  63. return null;
  64. }
  65. /**
  66. * Checks if the given date string is in the format
  67. * YYYY-MM-DD HH:MM
  68. *
  69. * @param dateString The string to check
  70. * @return {boolean}
  71. */
  72. export function isEventDateStringFormatValid(dateString: ?string): boolean {
  73. return dateString !== undefined
  74. && dateString !== null
  75. && dateRegExp.test(dateString);
  76. }
  77. /**
  78. * Converts the given date string to a date object.<br>
  79. * Accepted format: YYYY-MM-DD HH:MM
  80. *
  81. * @param dateString The string to convert
  82. * @return {Date|null} The date object or null if the given string is invalid
  83. */
  84. export function stringToDate(dateString: string): Date | null {
  85. let date = new Date();
  86. if (isEventDateStringFormatValid(dateString)) {
  87. let stringArray = dateString.split(' ');
  88. let dateArray = stringArray[0].split('-');
  89. let timeArray = stringArray[1].split(':');
  90. date.setFullYear(
  91. parseInt(dateArray[0]),
  92. parseInt(dateArray[1]) - 1, // Month range from 0 to 11
  93. parseInt(dateArray[2])
  94. );
  95. date.setHours(
  96. parseInt(timeArray[0]),
  97. parseInt(timeArray[1]),
  98. 0,
  99. 0,
  100. );
  101. } else
  102. date = null;
  103. return date;
  104. }
  105. /**
  106. * Converts a date object to a string in the format
  107. * YYYY-MM-DD HH-MM
  108. *
  109. * @param date The date object to convert
  110. * @param isUTC Whether to treat the date as UTC
  111. * @return {string} The converted string
  112. */
  113. export function dateToString(date: Date, isUTC: boolean): string {
  114. const day = String(date.getDate()).padStart(2, '0');
  115. const month = String(date.getMonth() + 1).padStart(2, '0'); //January is 0!
  116. const year = date.getFullYear();
  117. const h = isUTC ? date.getUTCHours() : date.getHours();
  118. const hours = String(h).padStart(2, '0');
  119. const minutes = String(date.getMinutes()).padStart(2, '0');
  120. return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes;
  121. }
  122. /**
  123. * Returns a string corresponding to the event start and end times in the following format:
  124. *
  125. * HH:MM - HH:MM
  126. *
  127. * If the end date is not specified or is equal to start time, only start time will be shown.
  128. *
  129. * If the end date is not on the same day, 23:59 will be shown as end time
  130. *
  131. * @param start Start time in YYYY-MM-DD HH:MM:SS format
  132. * @param end End time in YYYY-MM-DD HH:MM:SS format
  133. * @return {string} Formatted string or "/ - /" on error
  134. */
  135. export function getFormattedEventTime(start: string, end: string): string {
  136. let formattedStr = '/ - /';
  137. let startDate = stringToDate(start);
  138. let endDate = stringToDate(end);
  139. if (startDate !== null && endDate !== null && startDate.getTime() !== endDate.getTime()) {
  140. formattedStr = String(startDate.getHours()).padStart(2, '0') + ':'
  141. + String(startDate.getMinutes()).padStart(2, '0') + ' - ';
  142. if (endDate.getFullYear() > startDate.getFullYear()
  143. || endDate.getMonth() > startDate.getMonth()
  144. || endDate.getDate() > startDate.getDate())
  145. formattedStr += '23:59';
  146. else
  147. formattedStr += String(endDate.getHours()).padStart(2, '0') + ':'
  148. + String(endDate.getMinutes()).padStart(2, '0');
  149. } else if (startDate !== null)
  150. formattedStr =
  151. String(startDate.getHours()).padStart(2, '0') + ':'
  152. + String(startDate.getMinutes()).padStart(2, '0');
  153. return formattedStr
  154. }
  155. /**
  156. * Checks if the given description can be considered empty.
  157. * <br>
  158. * An empty description is composed only of whitespace, <b>br</b> or <b>p</b> tags
  159. *
  160. *
  161. * @param description The text to check
  162. * @return {boolean}
  163. */
  164. export function isDescriptionEmpty(description: ?string): boolean {
  165. if (description !== undefined && description !== null) {
  166. return description
  167. .split('<p>').join('') // Equivalent to a replace all
  168. .split('</p>').join('')
  169. .split('<br>').join('').trim() === '';
  170. } else
  171. return true;
  172. }
  173. /**
  174. * Generates an object with an empty array for each key.
  175. * Each key is a date string in the format
  176. * YYYY-MM-DD
  177. *
  178. * @param numberOfMonths The number of months to create, starting from the current date
  179. * @return {Object}
  180. */
  181. export function generateEmptyCalendar(numberOfMonths: number): Object {
  182. let end = new Date(Date.now());
  183. end.setMonth(end.getMonth() + numberOfMonths);
  184. let daysOfYear = {};
  185. for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) {
  186. const dateString = getDateOnlyString(
  187. dateToString(new Date(d), false));
  188. if (dateString !== null)
  189. daysOfYear[dateString] = []
  190. }
  191. return daysOfYear;
  192. }
  193. /**
  194. * Generates an object with an array of eventObject at each key.
  195. * Each key is a date string in the format
  196. * YYYY-MM-DD.
  197. *
  198. * If no event is available at the given key, the array will be empty
  199. *
  200. * @param eventList The list of events to map to the agenda
  201. * @param numberOfMonths The number of months to create the agenda for
  202. * @return {Object}
  203. */
  204. export function generateEventAgenda(eventList: Array<eventObject>, numberOfMonths: number): Object {
  205. let agendaItems = generateEmptyCalendar(numberOfMonths);
  206. for (let i = 0; i < eventList.length; i++) {
  207. const dateString = getDateOnlyString(eventList[i].date_begin);
  208. if (dateString !== null) {
  209. const eventArray = agendaItems[dateString];
  210. if (eventArray !== undefined)
  211. pushEventInOrder(eventArray, eventList[i]);
  212. }
  213. }
  214. return agendaItems;
  215. }
  216. /**
  217. * Adds events to the given array depending on their starting date.
  218. *
  219. * Events starting before are added at the front.
  220. *
  221. * @param eventArray The array to hold sorted events
  222. * @param event The event to add to the array
  223. */
  224. export function pushEventInOrder(eventArray: Array<eventObject>, event: eventObject): Object {
  225. if (eventArray.length === 0)
  226. eventArray.push(event);
  227. else {
  228. for (let i = 0; i < eventArray.length; i++) {
  229. if (isEventBefore(event.date_begin, eventArray[i].date_begin)) {
  230. eventArray.splice(i, 0, event);
  231. break;
  232. } else if (i === eventArray.length - 1) {
  233. eventArray.push(event);
  234. break;
  235. }
  236. }
  237. }
  238. }