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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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
  77. listItem += item[this.editableTypes[i]["name"]] + '</span>';
  78. }
  79. listItem += "</div></li>";
  80. return listItem;
  81. }
  82. getCategoryCheckboxes(articleId) {
  83. let checkboxes = "<div id='checkboxesContainer' style='display: flex'>";
  84. let categories = [];
  85. if (articleId !== undefined)
  86. categories = this.getCategoriesOfArticle(articleId);
  87. for (let i = 0; i < this.referredTable.length; i++) {
  88. let id = this.referredTable[i]['id'];
  89. let cat = this.getCategoryOfId(id);
  90. let checked = categories.indexOf(id) !== -1 ? 'checked' : '';
  91. checkboxes +=
  92. '<div class="custom-control custom-checkbox" style="margin-right: 20px">\n' +
  93. '<input type="checkbox" class="custom-control-input" id="checkbox_' + id + '"' + checked + ' >\n' +
  94. '<label class="custom-control-label" for="checkbox_' + id + '">' +
  95. '<i class="mdi mdi-' + cat["icon"] + '" style="margin-right: 5px"></i>' +
  96. '</label>\n' +
  97. '</div>';
  98. }
  99. checkboxes += '</div>';
  100. return checkboxes;
  101. }
  102. getFormData(defaultValues) {
  103. let formData = '<form action="" class="categoryForm"><div class="form-group">';
  104. for (let i = 0; i < this.editableTypes.length; i++) {
  105. let inputId = this.editableTypes[i]['name'] + 'Input';
  106. let inputType = this.editableTypes[i]['type'] === 'number' ? 'number' : 'text';
  107. let value = defaultValues[this.editableTypes[i]['name']] !== undefined ? defaultValues[this.editableTypes[i]['name']] : '';
  108. let icon = '';
  109. formData += '<label for="' + inputId + '">' + this.editableTypes[i]['description'] + ' :</label>';
  110. if (this.editableTypes[i]['type'] !== 'checkboxes') {
  111. if (this.editableTypes[i]['name'] === 'icon')
  112. formData += "<i class='mdi mdi-" + value + "' style='margin-left: 5px'></i>";
  113. formData += '<input id="' + inputId + '" type="' + inputType + '" placeholder="Entrez une valeur" class="form-control" value="' + value + '" required />';
  114. } else {
  115. formData += this.getCategoryCheckboxes(defaultValues['id']);
  116. }
  117. }
  118. formData += "</div></form>";
  119. return formData;
  120. }
  121. getFormValues(form) {
  122. let values = [];
  123. for (let i = 0; i < this.editableTypes.length; i++) {
  124. values.push(form.find('#' + this.editableTypes[i]['name'] + 'Input').val())
  125. }
  126. return values;
  127. }
  128. isFormValid(values) {
  129. let isValid = true;
  130. for (let i = 0; i < values.length; i++) {
  131. if (values[i] === '') {
  132. isValid = false;
  133. break;
  134. }
  135. }
  136. return isValid;
  137. }
  138. getCheckedCategories(content) {
  139. let checkedList = [];
  140. let checkboxes = content.find('#checkboxesContainer').children();
  141. for (let i = 0; i < checkboxes.length; i++) {
  142. let input = $(checkboxes[i]).find('input')[0];
  143. let id = $(input).attr('id').replace('checkbox_', '');
  144. if (input.checked)
  145. checkedList.push(id);
  146. }
  147. return checkedList;
  148. }
  149. insertFormDataIntoObject(values, object) {
  150. for (let i = 0; i < values.length; i++) {
  151. object[this.editableTypes[i]['name']] = values[i];
  152. }
  153. return object;
  154. }
  155. updateListItem(values, index) {
  156. for (let i = 0; i < values.length; i++) {
  157. if (this.editableTypes[i]['type'] === 'icon')
  158. this.displayedData[index].find(".mdi")[0].className = "mdi mdi-" + values[i];
  159. else
  160. this.displayedData[index].find(".list-" + this.editableTypes[i]['name']).text(values[i]);
  161. }
  162. }
  163. updateListItemCategories(categories, index) {
  164. if (index === -1)
  165. index = this.displayedData.length -1;
  166. let iconContainer = this.displayedData[index].find(".list-category");
  167. for (let i = 0; i < categories.length; i++) {
  168. iconContainer.html(this.getCategoryIcons(this.currentData[index]['id']));
  169. }
  170. }
  171. updateAssociationList(articleId, newCategories) {
  172. let newTable = [];
  173. for (let i = 0; i < this.associationTable.length; i++) {
  174. if (this.associationTable[i]['article_id'] !== articleId)
  175. newTable.push(this.associationTable[i]);
  176. }
  177. for (let i = 0; i < newCategories.length; i++) {
  178. newTable.push({article_id: articleId, category_id: newCategories[i]});
  179. }
  180. console.log(newTable);
  181. this.associationTable = newTable;
  182. }
  183. showEditPopup(index) {
  184. let defaultValues = {};
  185. let title = "Créer une nouvelle entrée";
  186. if (index !== -1) {
  187. defaultValues = this.currentData[index];
  188. title = "Modifier l'entrée";
  189. }
  190. let formData = this.getFormData(defaultValues);
  191. let thisInstance = this;
  192. $.confirm({
  193. title: title,
  194. content: formData,
  195. buttons: {
  196. formSubmit: {
  197. text: 'Sauvegarder',
  198. btnClass: 'btn-blue',
  199. action: async function () {
  200. let values = thisInstance.getFormValues(this.$content);
  201. if (!thisInstance.isFormValid(values)) {
  202. $.alert('Merci de rentrer toutes les valeurs');
  203. return false;
  204. }
  205. let itemToSave = {};
  206. if (index !== -1) {
  207. itemToSave = thisInstance.currentData[index];
  208. }
  209. itemToSave = thisInstance.insertFormDataIntoObject(values, itemToSave);
  210. if (index !== -1) {
  211. thisInstance.currentData[index] = itemToSave;
  212. thisInstance.updateListItem(values, index);
  213. }
  214. let id = await AjaxManager.save(itemToSave, index === -1, thisInstance.type);
  215. if (id >= 0 && index === -1) {
  216. itemToSave["id"] = id;
  217. thisInstance.currentData[thisInstance.displayedData.length] = itemToSave;
  218. thisInstance.createNewListEntry(itemToSave);
  219. } else if (id === -1) {
  220. $.alert("Erreur serveur !");
  221. }
  222. if (id > 0 && thisInstance.type === 'article') {
  223. let categories = thisInstance.getCheckedCategories(this.$content);
  224. let data = {
  225. id: id,
  226. categories: categories
  227. };
  228. let result = await AjaxManager.save(data, true, 'article_categories');
  229. if (result) {
  230. thisInstance.updateAssociationList(id, categories);
  231. thisInstance.updateListItemCategories(categories, index);
  232. }
  233. }
  234. }
  235. },
  236. deleteButton: {
  237. text: 'Supprimer',
  238. btnClass: 'btn-red',
  239. isHidden: index === -1,
  240. action: function () {
  241. thisInstance.showDeleteConfirmation(index);
  242. }
  243. },
  244. cancelButton: {
  245. text: 'Annuler',
  246. },
  247. },
  248. onContentReady: function () {
  249. // bind to events
  250. let jc = this;
  251. this.$content.find('form').on('submit', function (e) {
  252. // if the user submits the form by pressing enter in the field.
  253. e.preventDefault();
  254. jc.$$formSubmit.trigger('click'); // reference the button and click it
  255. });
  256. }
  257. });
  258. }
  259. showDeleteConfirmation(index) {
  260. let thisInstance = this;
  261. $.confirm({
  262. title: "Confirmation",
  263. content: "Êtes vous sûr de vouloir supprimer cette entrée ?",
  264. buttons: {
  265. deleteButton: {
  266. text: 'Confirmer',
  267. btnClass: 'btn-red',
  268. action: async function () {
  269. let id = thisInstance.currentData[index]['id'];
  270. let status = await AjaxManager.delete(id, thisInstance.type);
  271. if (status === 0)
  272. thisInstance.deleteListEntry(id);
  273. else
  274. $.alert("Erreur serveur !");
  275. }
  276. },
  277. cancelButton: {
  278. text: 'Annuler',
  279. },
  280. }
  281. });
  282. }
  283. }