script.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* --- Surcharge objets --- */
  2. String.prototype.capitalize = function() {
  3. return this[0].toUpperCase() + this.slice(1);
  4. }
  5. /* --- Fonctions utilitaires --- */
  6. function getCookie(key) {
  7. const split = document.cookie.split(';');
  8. let cookies = {};
  9. split.forEach(elt => {
  10. const val = elt.trim().split('=');
  11. cookies[val[0]] = val[1];
  12. });
  13. if(key !== undefined) {
  14. return cookies[key];
  15. }
  16. return cookies;
  17. }
  18. function pagination(id, pageNum, perPage, total, tabElt, paginationElt, dataPage, tableClass, last) {
  19. pageNum = parseInt(pageNum);
  20. perPage = parseInt(perPage);
  21. total = parseInt(total);
  22. tableClass = tableClass || 'striped';
  23. if (pageNum < 1) {
  24. pageNum = 1;
  25. }
  26. // Création du tableau
  27. let tab = `<table id="pagination-${id}-table" class="${tableClass}">`;
  28. let first = true;
  29. let keys = [];
  30. dataPage.forEach(elt => {
  31. if(first) {
  32. tab += '<thead><tr>';
  33. let lastKey;
  34. for (const key in elt) {
  35. // Garde la derniere clef pour la fin
  36. if (last != key) {
  37. keys.push(key);
  38. tab += `<th>${key.capitalize()}</th>`;
  39. } else {
  40. lastKey = key;
  41. }
  42. }
  43. // Si il y a une derniere clef on l'ajoute
  44. if (lastKey) {
  45. keys.push(lastKey);
  46. tab += `<th>${lastKey.capitalize()}</th>`;
  47. }
  48. tab += '</tr></thead><tbody>';
  49. first = false;
  50. }
  51. tab += '<tr>';
  52. keys.forEach(key => {
  53. // Garde la valeur de la derniere clef pour la fin
  54. if (last != key) {
  55. tab += `<td style="width: ${Math.round((1/keys.length)*100)}%;">${elt[key]}</td>`;
  56. }
  57. });
  58. // Ajoute la valeur de la derniere clef
  59. if (last && keys.indexOf(last) !== -1) {
  60. tab += `<td style="width: ${Math.round((1/keys.length)*100)}%;">${elt[last]}</td>`;
  61. }
  62. tab += '</tr>';
  63. });
  64. tab += '</tbody></table>';
  65. // Creation de la pagination
  66. const nbPage = Math.ceil(total / perPage);
  67. if (pageNum > nbPage) {
  68. // Si il n' y a pas assez de page pour aller à la page demandé on va au maximum
  69. pageNum = nbPage
  70. }
  71. let pagination = `<ul class="pagination" data-active="${pageNum}">`;
  72. if(pageNum == 1) {
  73. pagination += '<li class="disabled"><span><i class="material-icons">chevron_left</i></span></li>';
  74. } else {
  75. pagination += `<li class="waves-effect"><span class="pagination-${id}-prev" data-page="${pageNum - 1}"><i class="material-icons">chevron_left</i></span></li>`;
  76. }
  77. for(let i = 1; i <= nbPage; i++) {
  78. if(pageNum === i) {
  79. pagination += `<li class="active"><span>${i}</span></li>`;
  80. } else {
  81. pagination += `<li class="waves-effect"><span class="pagination-${id}-number" data-page="${i}">${i}</span></li>`;
  82. }
  83. }
  84. if(pageNum == nbPage || total == 0) {
  85. pagination += '<li class="disabled"><span><i class="material-icons">chevron_right</i></span></li>';
  86. } else {
  87. pagination += `<li class="waves-effect"><span class="pagination-${id}-next" data-page="${pageNum + 1}"><i class="material-icons">chevron_right</i></span></li>`;
  88. }
  89. pagination += '</ul>';
  90. // Affichage
  91. tabElt.html(tab);
  92. paginationElt.html(pagination);
  93. // Retourne la page courante
  94. return pageNum;
  95. }
  96. /* --- Initialisation modules --- */
  97. $(document).ready(function(){
  98. // Chargement menu mobile
  99. $('.sidenav').sidenav();
  100. // Select
  101. $('select').formSelect();
  102. // Tooltip
  103. $('.tooltipped').tooltip();
  104. // Tabs
  105. $('.tabs').tabs();
  106. // Modal
  107. M.Modal.init($('.popup'), {
  108. dismissible: false,
  109. startingTop: '30%',
  110. endingTop: '35%',
  111. onOpenEnd: (modal) => {
  112. setTimeout(() => {
  113. M.Modal.getInstance(modal).close();
  114. }, 1500);
  115. }
  116. });
  117. M.Modal.init($('.loader'), {
  118. dismissible: false,
  119. startingTop: '30%',
  120. endingTop: '35%'
  121. });
  122. M.Modal.init($('.conf'), {
  123. dismissible: true,
  124. startingTop: '30%',
  125. endingTop: '35%'
  126. });
  127. // Date picker
  128. M.Datepicker.init($('.datepicker'), {
  129. format: 'dd/mm/yyyy',
  130. firstDay: 1,
  131. yearRange: [
  132. new Date(Date.now()).getFullYear() - 100,
  133. new Date(Date.now()).getFullYear()
  134. ],
  135. i18n: {
  136. cancel: 'Annuler',
  137. clear: 'Supprimer',
  138. months: [
  139. 'Janvier',
  140. 'Fevrier',
  141. 'Mars',
  142. 'Avril',
  143. 'Mai',
  144. 'Juin',
  145. 'Juillet',
  146. 'Aout',
  147. 'Septembre',
  148. 'Octobre',
  149. 'Novembre',
  150. 'Decembre'
  151. ],
  152. monthsShort: [
  153. 'Janv',
  154. 'Fevr',
  155. 'Mars',
  156. 'Avr',
  157. 'Mai',
  158. 'Juin',
  159. 'Juil',
  160. 'Aout',
  161. 'Sept',
  162. 'Oct',
  163. 'Nov',
  164. 'Dec'
  165. ],
  166. weekdays: [
  167. 'Dimanche',
  168. 'Lundi',
  169. 'Mardi',
  170. 'Mercredi',
  171. 'Jeudi',
  172. 'Vendredi',
  173. 'Samedi'
  174. ],
  175. weekdaysShort: [
  176. 'Dim',
  177. 'Lun',
  178. 'Mar',
  179. 'Mer',
  180. 'Jeu',
  181. 'Ven',
  182. 'Sam'
  183. ],
  184. weekdaysAbbrev: ['D','L','M','M','J','V','S']
  185. }
  186. });
  187. // Affichage des erreur struts
  188. $('.field-error').each(function() {
  189. const error = $(this).children('ul li').children('span').html();
  190. const parent = $(this).parent();
  191. parent.children('.helper-text').attr('data-error', error);
  192. parent.children('.validate').addClass('invalid');
  193. });
  194. // Cookie consent
  195. window.cookieconsent.initialise({
  196. palette: {
  197. popup: {
  198. background: "#aa0000",
  199. text: "#ffdddd"
  200. },
  201. button: {
  202. background: "#ff0000"
  203. }
  204. },
  205. position: "bottom-right",
  206. content: {
  207. message: "Ce site web utilise des cookies pour améliorer votre expérience de navigation. En continuant sur ce site vous acceptez leurs utilisations.",
  208. dismiss: "Accepter",
  209. link: "En savoir plus"
  210. }
  211. });
  212. });