platypuce.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. function checkPromoId(promoid)
  2. {
  3. return promoid.match(/A[1-5]/) != null;
  4. }
  5. function editRow() {
  6. var row = $(this).parent().parent();
  7. switch ($("title").html()) {
  8. case "Rentrée ISEN Brest - Documents - Administration":
  9. var rang = $(row.children()[0]);
  10. var promo = $(row.children()[1]);
  11. var rangValeur = rang.html();
  12. var promoValeur = $(promo[0]).attr("class");
  13. var promos = $("#promo").children();
  14. var str = '<select class="tempPromos">';
  15. var selected = "";
  16. rang.html('<input type="number" value="' + rangValeur + '" size="2" style="width:35px" />');
  17. $(promo[0]).removeClass(promoValeur);
  18. for (var i = 0; i < promos.length; i++) {
  19. if (promos[i].value === promoValeur) {
  20. selected = "selected";
  21. }
  22. else {
  23. selected = "";
  24. }
  25. str += '<option value="' + promos[i].value + '" ' + selected + '>' + promos[i].text + '</option>';
  26. }
  27. str += "</select>";
  28. promo.html(str);
  29. $(this).addClass("fa-check");
  30. $(this).removeClass("fa-pencil");
  31. $(this).off("click");
  32. $(this).on("click", function () {
  33. rangValeur = $(rang.children()[0]).val();
  34. rang.html(rangValeur);
  35. promoValeur = promo.children().val();
  36. promo.addClass(promoValeur);
  37. var promoName = "";
  38. for (var i = 0; i < promos.length; i++) {
  39. if (promos[i].value === promoValeur) {
  40. promoName = promos[i].text;
  41. }
  42. }
  43. promo.html(promoName);
  44. $(this).addClass("fa-pencil");
  45. $(this).removeClass("fa-check");
  46. $.ajax({
  47. method: "PUT",
  48. url: "document/" + row[0].id,
  49. data: "rang=" + rangValeur + "&promo=" + promoValeur,
  50. processData: false,
  51. contentType: false
  52. });
  53. $(this).off("click");
  54. $(this).on("click", editRow);
  55. });
  56. break;
  57. case "Rentrée ISEN Brest - Promotions - Administration":
  58. var promoName = $(row.children()[1]);
  59. var name = $(promoName[0]).text();
  60. $(promoName).html(
  61. '<input type="text" value="'+name+'" style="width: 400px" />'
  62. );
  63. $(this).addClass("fa-check");
  64. $(this).removeClass("fa-pencil");
  65. $(this).off("click");
  66. $(this).on("click", function () {
  67. name = $(promoName.children()[0]).val();
  68. $(promoName).html(name);
  69. $(this).addClass("fa-pencil");
  70. $(this).removeClass("fa-check");
  71. $.ajax({
  72. method: "PUT",
  73. url: "promo/" + row[0].id,
  74. data: "libelle=" + name,
  75. processData: false,
  76. contentType: false
  77. });
  78. $(this).off("click");
  79. $(this).on("click", editRow);
  80. });
  81. break;
  82. }
  83. }
  84. $('.fa-pencil').on("click", editRow);
  85. $('.fa-trash-o').on("click", function () {
  86. var row = $(this).parent().parent();
  87. var url = "";
  88. switch($("title").html())
  89. {
  90. case "Documents":
  91. url = "document/"+row[0].id;
  92. break;
  93. case "Promotions":
  94. url = "promo/"+row[0].id;
  95. }
  96. $.ajax({
  97. method: "DELETE",
  98. url: url,
  99. processData: false,
  100. contentType: false
  101. });
  102. row.remove();
  103. });
  104. $('#addForm').submit(function(e) {
  105. e.preventDefault();
  106. var data = new FormData();
  107. switch ($("title").html())
  108. {
  109. case "Documents":
  110. $("#helpBlock2").remove();
  111. $(".has-error").removeClass("has-error");
  112. if (!$("#rang").val().length)
  113. {
  114. var block = $("#rang").parent();
  115. block.addClass("has-error");
  116. block.html(block.html()+'<span id="helpBlock2" class="help-block">Vous devez spécifier un rang.</span>');
  117. }
  118. else if (!$("#libelle").val().length)
  119. {
  120. var block = $("#libelle").parent();
  121. block.addClass("has-error");
  122. block.html(block.html()+'<span id="helpBlock2" class="help-block">Vous devez spécifier un libellé.</span>');
  123. }
  124. else if (!$("#file").val().length)
  125. {
  126. var block = $("#file").parent();
  127. block.addClass("has-error");
  128. block.html(block.html()+'<span id="helpBlock2" class="help-block">Vous devez sélectionner un fichier.</span>');
  129. }
  130. else {
  131. data.append("document", $("#file")[0].files[0]);
  132. data.append("promo", $("#promo").val());
  133. data.append("rang", $("#rang").val());
  134. data.append("libelle", $("#libelle").val());
  135. $.ajax({
  136. method: "POST",
  137. url: "document",
  138. data: data,
  139. dataType: 'json',
  140. processData: false,
  141. contentType: false,
  142. complete: function (result) {
  143. result = result.responseJSON;
  144. var promoName = "";
  145. var promoValeur = $("#promo").val();
  146. for (var i = 0; i < $("#promo").children().length; i++) {
  147. if ($("#promo").children()[i].value === promoValeur) {
  148. promoName = $("#promo").children()[i].text;
  149. }
  150. }
  151. $("table").append('<tr id="' + result.id + '">' +
  152. '<td>' + $("#rang").val() + '</td>' +
  153. '<td>' + promoName + '</td>' +
  154. '<td>' + $("#libelle").val() + '</td>' +
  155. '<td>' + result.path + '</td>' +
  156. '<td><i class="fa fa-pencil" style="cursor:pointer"></i>&nbsp;&nbsp;&nbsp;<i class="fa fa-trash-o" style="cursor:pointer"></i></td>' +
  157. '</tr>');
  158. $($("#alert")[0]).html('<div class="alert alert-success alert-dismissible" role="alert" style="text-align:center; display: none">Le document a été ajouté !<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
  159. $($("button.btn")[0]).click();
  160. $($(".alert")[0]).fadeIn();
  161. // Reset the listeners
  162. $('.fa-pencil').on("click", editRow);
  163. $('.fa-trash-o').on("click", function () {
  164. var row = $(this).parent().parent();
  165. var url = "";
  166. switch ($("title").html()) {
  167. case "Documents":
  168. url = "document/" + row[0].id;
  169. break;
  170. case "Promotions":
  171. url = "promo/" + row[0].id;
  172. }
  173. $.ajax({
  174. method: "DELETE",
  175. url: url,
  176. processData: false,
  177. contentType: false
  178. });
  179. row.remove();
  180. });
  181. }
  182. });
  183. }
  184. break;
  185. case "Promotions":
  186. $("#helpBlock2").remove();
  187. $(".has-error").removeClass("has-error");
  188. var id = $("#id").val().toUpperCase();
  189. if(checkPromoId(id))
  190. {
  191. if (!$("#libelle").val().length)
  192. {
  193. var block = $("#libelle").parent();
  194. block.addClass("has-error");
  195. block.html(block.html()+'<span id="helpBlock2" class="help-block">Vous devez spécifier un libellé.</span>');
  196. }
  197. else {
  198. data.append("id", id);
  199. data.append("libelle", $("#libelle").val());
  200. $.ajax({
  201. method: "POST",
  202. url: "promo",
  203. data: data,
  204. processData: false,
  205. contentType: false
  206. });
  207. $("table").append('<tr>' +
  208. '<td>' + id + '</td>' +
  209. '<td>' + $("#libelle").val() + '</td>' +
  210. '<td><i class="fa fa-pencil" style="cursor:pointer"></i>&nbsp;&nbsp;&nbsp;<i class="fa fa-trash-o" style="cursor:pointer"></i></td>' +
  211. '</tr>');
  212. $($("#alert")[0]).html('<div class="alert alert-success alert-dismissible" role="alert" style="text-align:center; display: none">La promotion a été ajoutée !<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
  213. $($("button.btn")[0]).click();
  214. $($(".alert")[0]).fadeIn();
  215. // Reset the listeners
  216. $('.fa-pencil').on("click", editRow);
  217. $('.fa-trash-o').on("click", function () {
  218. var row = $(this).parent().parent();
  219. var url = "";
  220. switch ($("title").html()) {
  221. case "Documents":
  222. url = "document/" + row[0].id;
  223. break;
  224. case "Promotions":
  225. url = "promo/" + row[0].id;
  226. }
  227. $.ajax({
  228. method: "DELETE",
  229. url: url,
  230. processData: false,
  231. contentType: false
  232. });
  233. row.remove();
  234. });
  235. }
  236. }
  237. else
  238. {
  239. var block = $("#id").parent();
  240. block.addClass("has-error");
  241. block.html(block.html()+'<span id="helpBlock2" class="help-block">Votre identifiant doit contenir "A" suivi de l\'année correspondant à la promotion.</span>')
  242. }
  243. break;
  244. }
  245. });