script.js 5.7 KB

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