script.js 4.8 KB

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