script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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($('.modal'), {
  77. dismissible: false,
  78. startingTop: '20%',
  79. endingTop: '25%',
  80. onOpenEnd: (modal) => {
  81. setTimeout(() => {
  82. M.Modal.getInstance(modal).close();
  83. }, 3000);
  84. }
  85. });
  86. // Date picker
  87. M.Datepicker.init($('.datepicker'), {
  88. format: 'dd/mm/yyyy',
  89. firstDay: 1,
  90. yearRange: [
  91. new Date(Date.now()).getFullYear() - 100,
  92. new Date(Date.now()).getFullYear()
  93. ],
  94. i18n: {
  95. cancel: 'Annuler',
  96. clear: 'Supprimer',
  97. months: [
  98. 'Janvier',
  99. 'Fevrier',
  100. 'Mars',
  101. 'Avril',
  102. 'Mai',
  103. 'Juin',
  104. 'Juillet',
  105. 'Aout',
  106. 'Septembre',
  107. 'Octobre',
  108. 'Novembre',
  109. 'Decembre'
  110. ],
  111. monthsShort: [
  112. 'Janv',
  113. 'Fevr',
  114. 'Mars',
  115. 'Avr',
  116. 'Mai',
  117. 'Juin',
  118. 'Juil',
  119. 'Aout',
  120. 'Sept',
  121. 'Oct',
  122. 'Nov',
  123. 'Dec'
  124. ],
  125. weekdays: [
  126. 'Dimanche',
  127. 'Lundi',
  128. 'Mardi',
  129. 'Mercredi',
  130. 'Jeudi',
  131. 'Vendredi',
  132. 'Samedi'
  133. ],
  134. weekdaysShort: [
  135. 'Dim',
  136. 'Lun',
  137. 'Mar',
  138. 'Mer',
  139. 'Jeu',
  140. 'Ven',
  141. 'Sam'
  142. ],
  143. weekdaysAbbrev: ['D','L','M','M','J','V','S']
  144. }
  145. });
  146. });