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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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, index) {
  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. formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>';
  112. if (this.editableTypes[i]['type'] === 'image') {
  113. formData += this.getImagePicker(defaultValues['id'], index);
  114. } else if (this.editableTypes[i]['type'] === 'checkboxes') {
  115. formData += this.getCategoryCheckboxes(defaultValues['id']);
  116. } else {
  117. if (this.editableTypes[i]['name'] === 'icon')
  118. formData += "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
  119. formData += '<input name="' + inputName + '" id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
  120. }
  121. }
  122. formData += "</div></form>";
  123. return formData;
  124. }
  125. getImagePicker(id, index) {
  126. let imageSrc = $(this.displayedData[index].find("img")[0]).attr('src');
  127. return (
  128. '<img style="width: 50px; height: 50px" src="' + imageSrc + '" 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. 'onchange="listManager.updatePickerImagePreview(this, \'image_' + id + '\')"' +
  136. '>\n' +
  137. '<label class="custom-file-label" for="customFile">Choose file</label>\n' +
  138. '</div>');
  139. }
  140. updatePickerImagePreview(picker, previewId) {
  141. $("#" + previewId).attr('src', window.URL.createObjectURL(picker.files[0]));
  142. }
  143. getFormValues(form) {
  144. let values = [];
  145. for (let i = 0; i < this.editableTypes.length; i++) {
  146. values.push(form.find('#' + this.editableTypes[i]['name'] + 'Input').val())
  147. }
  148. return values;
  149. }
  150. isFormValid(values) {
  151. let isValid = true;
  152. for (let i = 0; i < values.length; i++) {
  153. if (values[i] === '') {
  154. isValid = false;
  155. break;
  156. }
  157. }
  158. return isValid;
  159. }
  160. getCheckedCategories(content) {
  161. let checkedList = [];
  162. let checkboxes = content.find('#checkboxesContainer').children();
  163. for (let i = 0; i < checkboxes.length; i++) {
  164. let input = $(checkboxes[i]).find('input')[0];
  165. let id = $(input).attr('id').replace('checkbox_', '');
  166. if (input.checked)
  167. checkedList.push(id);
  168. }
  169. return checkedList;
  170. }
  171. insertFormDataIntoObject(values, object) {
  172. for (let i = 0; i < values.length; i++) {
  173. object[this.editableTypes[i]['name']] = values[i];
  174. }
  175. return object;
  176. }
  177. updateListItem(values, index) {
  178. for (let i = 0; i < values.length; i++) {
  179. if (this.editableTypes[i]['type'] === 'icon')
  180. this.displayedData[index].find(".mdi")[0].className = "mdi mdi-" + values[i];
  181. else
  182. this.displayedData[index].find(".list-" + this.editableTypes[i]['name']).text(values[i]);
  183. }
  184. }
  185. updateListItemCategories(categories, index) {
  186. if (index === -1)
  187. index = this.displayedData.length - 1;
  188. let iconContainer = this.displayedData[index].find(".list-category");
  189. for (let i = 0; i < categories.length; i++) {
  190. iconContainer.html(this.getCategoryIcons(this.currentData[index]['id']));
  191. }
  192. }
  193. updateAssociationList(articleId, newCategories) {
  194. let newTable = [];
  195. for (let i = 0; i < this.associationTable.length; i++) {
  196. if (this.associationTable[i]['article_id'] !== articleId)
  197. newTable.push(this.associationTable[i]);
  198. }
  199. for (let i = 0; i < newCategories.length; i++) {
  200. newTable.push({article_id: articleId, category_id: newCategories[i]});
  201. }
  202. console.log(newTable);
  203. this.associationTable = newTable;
  204. }
  205. updateArticleImage(image, index) {
  206. $(this.displayedData[index].find("img")[0]).attr('src', window.URL.createObjectURL(image));
  207. }
  208. showEditPopup(index) {
  209. let defaultValues = {};
  210. let title = "Créer une nouvelle entrée";
  211. if (index !== -1) {
  212. defaultValues = this.currentData[index];
  213. title = "Modifier l'entrée";
  214. }
  215. let formData = this.getFormData(defaultValues, index);
  216. let thisInstance = this;
  217. $.confirm({
  218. title: title,
  219. content: formData,
  220. buttons: {
  221. formSubmit: {
  222. text: 'Sauvegarder',
  223. btnClass: 'btn-blue',
  224. action: async function () {
  225. let image = undefined;
  226. if (this.$content.find('input[type=file]').length > 0)
  227. image = this.$content.find('input[type=file]')[0].files[0];
  228. let values = thisInstance.getFormValues(this.$content);
  229. if (!thisInstance.isFormValid(values)) {
  230. $.alert('Merci de rentrer toutes les valeurs');
  231. return false;
  232. }
  233. let itemToSave = {};
  234. if (index !== -1) {
  235. itemToSave = thisInstance.currentData[index];
  236. }
  237. itemToSave = thisInstance.insertFormDataIntoObject(values, itemToSave);
  238. if (index !== -1) {
  239. thisInstance.currentData[index] = itemToSave;
  240. thisInstance.updateListItem(values, index);
  241. }
  242. let id = await AjaxManager.save(itemToSave, index === -1, thisInstance.type);
  243. if (id >= 0 && index === -1) {
  244. itemToSave["id"] = id;
  245. thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
  246. thisInstance.createNewListEntry(itemToSave);
  247. } else if (id === -1) {
  248. $.alert("Erreur serveur !");
  249. }
  250. if (id > 0 && thisInstance.type === 'article') {
  251. let categories = thisInstance.getCheckedCategories(this.$content);
  252. let data = {
  253. id: id,
  254. categories: categories
  255. };
  256. let result = await AjaxManager.save(data, true, 'article_categories');
  257. if (result) {
  258. thisInstance.updateAssociationList(id, categories);
  259. thisInstance.updateListItemCategories(categories, index);
  260. let imageResult = await AjaxManager.saveArticleImage(image, id);
  261. if (imageResult === 0)
  262. thisInstance.updateArticleImage(image, index);
  263. }
  264. }
  265. }
  266. },
  267. deleteButton: {
  268. text: 'Supprimer',
  269. btnClass: 'btn-red',
  270. isHidden: index === -1,
  271. action: function () {
  272. thisInstance.showDeleteConfirmation(index);
  273. }
  274. },
  275. cancelButton: {
  276. text: 'Annuler',
  277. },
  278. }
  279. });
  280. }
  281. showDeleteConfirmation(index) {
  282. let thisInstance = this;
  283. $.confirm({
  284. title: "Confirmation",
  285. content: "Êtes vous sûr de vouloir supprimer cette entrée ?",
  286. buttons: {
  287. deleteButton: {
  288. text: 'Confirmer',
  289. btnClass: 'btn-red',
  290. action: async function () {
  291. let id = thisInstance.currentData[index]['id'];
  292. let status = await AjaxManager.delete(id, thisInstance.type);
  293. if (status === 0)
  294. thisInstance.deleteListEntry(id);
  295. else
  296. $.alert("Erreur serveur !");
  297. }
  298. },
  299. cancelButton: {
  300. text: 'Annuler',
  301. },
  302. }
  303. });
  304. }
  305. }