Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

listManager.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. class ListManager {
  2. currentData = [];
  3. displayedData = [];
  4. editableTypes = [];
  5. type = '';
  6. referredTable = [];
  7. associationTable = [];
  8. constructor(listContainer, type, editableTypes) {
  9. this.listContainer = listContainer;
  10. this.editableTypes = editableTypes;
  11. this.type = type;
  12. if (type === 'article') {
  13. AjaxManager.getAll().then((data) => {
  14. this.currentData = data['articles'];
  15. this.referredTable = data['categories'];
  16. this.associationTable = data['article_categories'];
  17. this.generateList()
  18. });
  19. } else {
  20. AjaxManager.get(type).then((data) => {
  21. this.currentData = data;
  22. this.generateList()
  23. });
  24. }
  25. }
  26. generateList() {
  27. for (let i = 0; i < this.currentData.length; i++) {
  28. this.createNewListEntry(this.currentData[i]);
  29. }
  30. }
  31. createNewListEntry(item) {
  32. let listItem = this.getListItem(item);
  33. this.displayedData.push($(listItem));
  34. const index = this.displayedData.length - 1;
  35. let JQueryItem = this.displayedData[index];
  36. this.listContainer.append(JQueryItem);
  37. JQueryItem.on('click', () => {
  38. this.showEditPopup(index);
  39. });
  40. }
  41. deleteListEntry(id) {
  42. $('#listItem_' + id).remove();
  43. }
  44. getCategoriesOfArticle(articleId) {
  45. let filteredList = [];
  46. for (let i = 0; i < this.associationTable.length; i++) {
  47. if (this.associationTable[i]['article_id'] === articleId)
  48. filteredList.push(this.associationTable[i]['category_id']);
  49. }
  50. return filteredList;
  51. }
  52. getCategoryOfId(categoryId) {
  53. for (let i = 0; i < this.referredTable.length; i++) {
  54. if (this.referredTable[i]['id'] === categoryId) {
  55. return this.referredTable[i];
  56. }
  57. }
  58. return "";
  59. }
  60. getCategoryIcons(articleId) {
  61. let icons = "";
  62. let categories = this.getCategoriesOfArticle(articleId);
  63. for (let i = 0; i < categories.length; i++) {
  64. icons += "<i class='mdi mdi-" + this.getCategoryOfId(categories[i])["icon"] + "' style='margin-right: 5px'></i>";
  65. }
  66. return icons;
  67. }
  68. getListItem(item) {
  69. let listItem = "<li id='listItem_" + item["id"] + "'><div>";
  70. for (let i = 0; i < this.editableTypes.length; i++) {
  71. listItem += "<span class='list-" + this.editableTypes[i]["name"] + "'>";
  72. if (this.editableTypes[i]["type"] === "icon")
  73. listItem += "<i class='mdi mdi-" + item["icon"] + "' style='margin-right: 5px'></i></span>";
  74. else if (this.editableTypes[i]["type"] === 'checkboxes') {
  75. listItem += this.getCategoryIcons(item['id']);
  76. } else if (this.editableTypes[i]["type"] === 'image') {
  77. listItem += '<img src="../uploaded_images/' + item['id'] + '.jpg" /></span>'
  78. } else
  79. listItem += item[this.editableTypes[i]["name"]] + '</span>';
  80. }
  81. listItem += "</div></li>";
  82. return listItem;
  83. }
  84. getCategoryCheckboxes(articleId) {
  85. let checkboxes = "<div id='checkboxesContainer' style='display: flex'>";
  86. let categories = [];
  87. if (articleId !== undefined)
  88. categories = this.getCategoriesOfArticle(articleId);
  89. for (let i = 0; i < this.referredTable.length; i++) {
  90. let id = this.referredTable[i]['id'];
  91. let cat = this.getCategoryOfId(id);
  92. let checked = categories.indexOf(id) !== -1 ? 'checked' : '';
  93. checkboxes +=
  94. '<div class="custom-control custom-checkbox" style="margin-right: 20px">\n' +
  95. '<input type="checkbox" class="custom-control-input" id="checkbox_' + id + '"' + checked + ' >\n' +
  96. '<label class="custom-control-label" for="checkbox_' + id + '">' +
  97. '<i class="mdi mdi-' + cat["icon"] + '" style="margin-right: 5px"></i>' +
  98. '</label>\n' +
  99. '</div>';
  100. }
  101. checkboxes += '</div>';
  102. return checkboxes;
  103. }
  104. getFormData(defaultValues) {
  105. let formData = '<form id="popupForm" name="popupForm" method="post" enctype="multipart/form-data"><div class="form-group">';
  106. for (let i = 0; i < this.editableTypes.length; i++) {
  107. let inputId = this.editableTypes[i]['name'] + 'Input';
  108. let inputName = this.editableTypes[i]['name'];
  109. let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text';
  110. let value = defaultValues[this.editableTypes[i]['name']] !== undefined ? defaultValues[this.editableTypes[i]['name']] : '';
  111. let icon = '';
  112. formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>';
  113. if (this.editableTypes[i]['type'] === 'image') {
  114. formData += this.getImagePicker(defaultValues['id']);
  115. } else if (this.editableTypes[i]['type'] === 'checkboxes') {
  116. formData += this.getCategoryCheckboxes(defaultValues['id']);
  117. } else {
  118. if (this.editableTypes[i]['name'] === 'icon')
  119. formData += "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
  120. formData += '<input name="' + inputName + '" id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
  121. }
  122. }
  123. formData += "</div></form>";
  124. return formData;
  125. }
  126. getImagePicker(id) {
  127. return (
  128. '<img style="width: 50px; height: 50px" src="../uploaded_images/' + id + '.jpg" id="image_' + id + '"/>' +
  129. '<div class="custom-file">\n' +
  130. '<input ' +
  131. 'type="file" ' +
  132. 'class="custom-file-input" ' +
  133. 'id="fileInput"' +
  134. 'accept="image/jpeg"' +
  135. '>\n' +
  136. '<label class="custom-file-label" for="customFile">Choose file</label>\n' +
  137. '</div>');
  138. }
  139. getFormValues(form) {
  140. let values = [];
  141. for (let i = 0; i < this.editableTypes.length; i++) {
  142. values.push(form.find('#' + this.editableTypes[i]['name'] + 'Input').val())
  143. }
  144. return values;
  145. }
  146. isFormValid(values) {
  147. let isValid = true;
  148. for (let i = 0; i < values.length; i++) {
  149. if (values[i] === '') {
  150. isValid = false;
  151. break;
  152. }
  153. }
  154. return isValid;
  155. }
  156. getCheckedCategories(content) {
  157. let checkedList = [];
  158. let checkboxes = content.find('#checkboxesContainer').children();
  159. for (let i = 0; i < checkboxes.length; i++) {
  160. let input = $(checkboxes[i]).find('input')[0];
  161. let id = $(input).attr('id').replace('checkbox_', '');
  162. if (input.checked)
  163. checkedList.push(id);
  164. }
  165. return checkedList;
  166. }
  167. insertFormDataIntoObject(values, object) {
  168. for (let i = 0; i < values.length; i++) {
  169. object[this.editableTypes[i]['name']] = values[i];
  170. }
  171. return object;
  172. }
  173. updateListItem(values, index) {
  174. for (let i = 0; i < values.length; i++) {
  175. if (this.editableTypes[i]['type'] === 'icon')
  176. this.displayedData[index].find(".mdi")[0].className = "mdi mdi-" + values[i];
  177. else
  178. this.displayedData[index].find(".list-" + this.editableTypes[i]['name']).text(values[i]);
  179. }
  180. }
  181. updateListItemCategories(categories, index) {
  182. if (index === -1)
  183. index = this.displayedData.length - 1;
  184. let iconContainer = this.displayedData[index].find(".list-category");
  185. for (let i = 0; i < categories.length; i++) {
  186. iconContainer.html(this.getCategoryIcons(this.currentData[index]['id']));
  187. }
  188. }
  189. updateAssociationList(articleId, newCategories) {
  190. let newTable = [];
  191. for (let i = 0; i < this.associationTable.length; i++) {
  192. if (this.associationTable[i]['article_id'] !== articleId)
  193. newTable.push(this.associationTable[i]);
  194. }
  195. for (let i = 0; i < newCategories.length; i++) {
  196. newTable.push({article_id: articleId, category_id: newCategories[i]});
  197. }
  198. console.log(newTable);
  199. this.associationTable = newTable;
  200. }
  201. updateArticleImage(image, index) {
  202. $(this.displayedData[index].find("img")[0]).attr('src', window.URL.createObjectURL(image));
  203. }
  204. showEditPopup(index) {
  205. let defaultValues = {};
  206. let title = "Créer une nouvelle entrée";
  207. if (index !== -1) {
  208. defaultValues = this.currentData[index];
  209. title = "Modifier l'entrée";
  210. }
  211. let formData = this.getFormData(defaultValues);
  212. let thisInstance = this;
  213. $.confirm({
  214. title: title,
  215. content: formData,
  216. buttons: {
  217. formSubmit: {
  218. text: 'Sauvegarder',
  219. btnClass: 'btn-blue',
  220. action: async function () {
  221. let image = undefined;
  222. if (this.$content.find('input[type=file]').length > 0)
  223. image = this.$content.find('input[type=file]')[0].files[0];
  224. let values = thisInstance.getFormValues(this.$content);
  225. if (!thisInstance.isFormValid(values)) {
  226. $.alert('Merci de rentrer toutes les valeurs');
  227. return false;
  228. }
  229. let itemToSave = {};
  230. if (index !== -1) {
  231. itemToSave = thisInstance.currentData[index];
  232. }
  233. itemToSave = thisInstance.insertFormDataIntoObject(values, itemToSave);
  234. if (index !== -1) {
  235. thisInstance.currentData[index] = itemToSave;
  236. thisInstance.updateListItem(values, index);
  237. }
  238. let id = await AjaxManager.save(itemToSave, index === -1, thisInstance.type);
  239. if (id >= 0 && index === -1) {
  240. itemToSave["id"] = id;
  241. thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
  242. thisInstance.createNewListEntry(itemToSave);
  243. } else if (id === -1) {
  244. $.alert("Erreur serveur !");
  245. }
  246. if (id > 0 && thisInstance.type === 'article') {
  247. let categories = thisInstance.getCheckedCategories(this.$content);
  248. let data = {
  249. id: id,
  250. categories: categories
  251. };
  252. let result = await AjaxManager.save(data, true, 'article_categories');
  253. if (result) {
  254. thisInstance.updateAssociationList(id, categories);
  255. thisInstance.updateListItemCategories(categories, index);
  256. let imageResult = await AjaxManager.saveArticleImage(image, id);
  257. if (imageResult === 0)
  258. thisInstance.updateArticleImage(image, index);
  259. }
  260. }
  261. }
  262. },
  263. deleteButton: {
  264. text: 'Supprimer',
  265. btnClass: 'btn-red',
  266. isHidden: index === -1,
  267. action: function () {
  268. thisInstance.showDeleteConfirmation(index);
  269. }
  270. },
  271. cancelButton: {
  272. text: 'Annuler',
  273. },
  274. }
  275. });
  276. }
  277. showDeleteConfirmation(index) {
  278. let thisInstance = this;
  279. $.confirm({
  280. title: "Confirmation",
  281. content: "Êtes vous sûr de vouloir supprimer cette entrée ?",
  282. buttons: {
  283. deleteButton: {
  284. text: 'Confirmer',
  285. btnClass: 'btn-red',
  286. action: async function () {
  287. let id = thisInstance.currentData[index]['id'];
  288. let status = await AjaxManager.delete(id, thisInstance.type);
  289. if (status === 0)
  290. thisInstance.deleteListEntry(id);
  291. else
  292. $.alert("Erreur serveur !");
  293. }
  294. },
  295. cancelButton: {
  296. text: 'Annuler',
  297. },
  298. }
  299. });
  300. }
  301. }