script.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // Création du tableau
  24. let tab = `<table class="${tableClass}">`;
  25. let first = true;
  26. let keys = [];
  27. dataPage.forEach(elt => {
  28. if(first) {
  29. tab += '<thead><tr>';
  30. let lastKey;
  31. for (const key in elt) {
  32. // Garde la derniere clef pour la fin
  33. if (last != key) {
  34. keys.push(key);
  35. tab += `<th>${key.capitalize()}</th>`;
  36. } else {
  37. lastKey = key;
  38. }
  39. }
  40. // Si il y a une derniere clef on l'ajoute
  41. if (lastKey) {
  42. keys.push(lastKey);
  43. tab += `<th>${lastKey.capitalize()}</th>`;
  44. }
  45. tab += '</tr></thead><tbody>';
  46. first = false;
  47. }
  48. tab += '<tr>';
  49. keys.forEach(key => {
  50. // Garde la valeur de la derniere clef pour la fin
  51. if (last != key) {
  52. tab += `<td style="width: ${Math.round((1/keys.length)*100)}%;">${elt[key]}</td>`;
  53. }
  54. });
  55. // Ajoute la valeur de la derniere clef
  56. if (last && keys.indexOf(last) !== -1) {
  57. tab += `<td style="width: ${Math.round((1/keys.length)*100)}%;">${elt[last]}</td>`;
  58. }
  59. tab += '</tr>';
  60. });
  61. tab += '</tbody></table>';
  62. // Creation de la pagination
  63. const nbPage = Math.ceil(total / perPage);
  64. let pagination = `<ul class="pagination" data-active="${pageNum}">`;
  65. if(pageNum == 1) {
  66. pagination += '<li class="disabled"><span><i class="material-icons">chevron_left</i></span></li>';
  67. } else {
  68. pagination += `<li class="waves-effect"><span class="pagination-${id}-prev" data-page="${pageNum - 1}"><i class="material-icons">chevron_left</i></span></li>`;
  69. }
  70. for(let i = 1; i <= nbPage; i++) {
  71. if(pageNum === i) {
  72. pagination += `<li class="active"><span>${i}</span></li>`;
  73. } else {
  74. pagination += `<li class="waves-effect"><span class="pagination-${id}-number" data-page="${i}">${i}</span></li>`;
  75. }
  76. }
  77. if(pageNum == nbPage) {
  78. pagination += '<li class="disabled"><span><i class="material-icons">chevron_right</i></span></li>';
  79. } else {
  80. pagination += `<li class="waves-effect"><span class="pagination-${id}-next" data-page="${pageNum + 1}"><i class="material-icons">chevron_right</i></span></li>`;
  81. }
  82. pagination += '</ul>';
  83. // Affichage
  84. tabElt.html(tab);
  85. paginationElt.html(pagination);
  86. }
  87. /* --- Initialisation modules --- */
  88. $(document).ready(function(){
  89. // Chargement menu mobile
  90. $('.sidenav').sidenav();
  91. // Select
  92. $('select').formSelect();
  93. // Modal
  94. M.Modal.init($('.popup'), {
  95. dismissible: false,
  96. startingTop: '30%',
  97. endingTop: '35%',
  98. onOpenEnd: (modal) => {
  99. setTimeout(() => {
  100. M.Modal.getInstance(modal).close();
  101. }, 3000);
  102. }
  103. });
  104. M.Modal.init($('.loader'), {
  105. dismissible: false,
  106. startingTop: '30%',
  107. endingTop: '35%'
  108. });
  109. // Date picker
  110. M.Datepicker.init($('.datepicker'), {
  111. format: 'dd/mm/yyyy',
  112. firstDay: 1,
  113. yearRange: [
  114. new Date(Date.now()).getFullYear() - 100,
  115. new Date(Date.now()).getFullYear()
  116. ],
  117. i18n: {
  118. cancel: 'Annuler',
  119. clear: 'Supprimer',
  120. months: [
  121. 'Janvier',
  122. 'Fevrier',
  123. 'Mars',
  124. 'Avril',
  125. 'Mai',
  126. 'Juin',
  127. 'Juillet',
  128. 'Aout',
  129. 'Septembre',
  130. 'Octobre',
  131. 'Novembre',
  132. 'Decembre'
  133. ],
  134. monthsShort: [
  135. 'Janv',
  136. 'Fevr',
  137. 'Mars',
  138. 'Avr',
  139. 'Mai',
  140. 'Juin',
  141. 'Juil',
  142. 'Aout',
  143. 'Sept',
  144. 'Oct',
  145. 'Nov',
  146. 'Dec'
  147. ],
  148. weekdays: [
  149. 'Dimanche',
  150. 'Lundi',
  151. 'Mardi',
  152. 'Mercredi',
  153. 'Jeudi',
  154. 'Vendredi',
  155. 'Samedi'
  156. ],
  157. weekdaysShort: [
  158. 'Dim',
  159. 'Lun',
  160. 'Mar',
  161. 'Mer',
  162. 'Jeu',
  163. 'Ven',
  164. 'Sam'
  165. ],
  166. weekdaysAbbrev: ['D','L','M','M','J','V','S']
  167. }
  168. });
  169. // Affichage des erreur struts
  170. $('.field-error').each(function() {
  171. const error = $(this).children('ul li').children('span').html();
  172. const parent = $(this).parent();
  173. parent.children('.helper-text').attr('data-error', error);
  174. parent.children('.validate').addClass('invalid');
  175. });
  176. });