script.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Tooltip
  94. $('.tooltipped').tooltip();
  95. // Modal
  96. M.Modal.init($('.popup'), {
  97. dismissible: false,
  98. startingTop: '30%',
  99. endingTop: '35%',
  100. onOpenEnd: (modal) => {
  101. setTimeout(() => {
  102. M.Modal.getInstance(modal).close();
  103. }, 3000);
  104. }
  105. });
  106. M.Modal.init($('.loader'), {
  107. dismissible: false,
  108. startingTop: '30%',
  109. endingTop: '35%'
  110. });
  111. // Date picker
  112. M.Datepicker.init($('.datepicker'), {
  113. format: 'dd/mm/yyyy',
  114. firstDay: 1,
  115. yearRange: [
  116. new Date(Date.now()).getFullYear() - 100,
  117. new Date(Date.now()).getFullYear()
  118. ],
  119. i18n: {
  120. cancel: 'Annuler',
  121. clear: 'Supprimer',
  122. months: [
  123. 'Janvier',
  124. 'Fevrier',
  125. 'Mars',
  126. 'Avril',
  127. 'Mai',
  128. 'Juin',
  129. 'Juillet',
  130. 'Aout',
  131. 'Septembre',
  132. 'Octobre',
  133. 'Novembre',
  134. 'Decembre'
  135. ],
  136. monthsShort: [
  137. 'Janv',
  138. 'Fevr',
  139. 'Mars',
  140. 'Avr',
  141. 'Mai',
  142. 'Juin',
  143. 'Juil',
  144. 'Aout',
  145. 'Sept',
  146. 'Oct',
  147. 'Nov',
  148. 'Dec'
  149. ],
  150. weekdays: [
  151. 'Dimanche',
  152. 'Lundi',
  153. 'Mardi',
  154. 'Mercredi',
  155. 'Jeudi',
  156. 'Vendredi',
  157. 'Samedi'
  158. ],
  159. weekdaysShort: [
  160. 'Dim',
  161. 'Lun',
  162. 'Mar',
  163. 'Mer',
  164. 'Jeu',
  165. 'Ven',
  166. 'Sam'
  167. ],
  168. weekdaysAbbrev: ['D','L','M','M','J','V','S']
  169. }
  170. });
  171. // Affichage des erreur struts
  172. $('.field-error').each(function() {
  173. const error = $(this).children('ul li').children('span').html();
  174. const parent = $(this).parent();
  175. parent.children('.helper-text').attr('data-error', error);
  176. parent.children('.validate').addClass('invalid');
  177. });
  178. });