script.js 5.6 KB

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