2
0

newDeleteManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { BaseHoverManager } from './BaseHoverManager.js';
  2. import { Modal } from './Modal.js';
  3. import { SmartButton } from './SmartButton.js';
  4. const baseURL = window.location.origin;
  5. // Configuration du préfixe REST - peut être surchargée via window.NEWSITEEDITOR_CONFIG
  6. const REST_PREFIX = (window.NEWSITEEDITOR_CONFIG && window.NEWSITEEDITOR_CONFIG.restPrefix) || "/rest";
  7. export class newDeleteManager extends BaseHoverManager {
  8. constructor(toggleButtonId = "delete", bgModalId = "modalDelete", deleteButtonId = "btnDelete") {
  9. super("[data-deleteid]", "deletable", "inDelete");
  10. this.deleteID = null;
  11. this.bgModal = new Modal(bgModalId);
  12. this.smartButton = new SmartButton("btnDelete");
  13. this.toggleButton = document.getElementById(toggleButtonId);
  14. this.type = "data-deleteType";
  15. this.storeData = this.storeData.bind(this);
  16. this.removeResource = this.removeResource.bind(this);
  17. this.onHoverDisabled = () => {
  18. this.bgModal.setVisible(false);
  19. this.smartButton.hide();
  20. this.deleteID = null;
  21. };
  22. this.handleClick = (e) => {
  23. const el = e.currentTarget;
  24. this.storeData(e);
  25. if (el.classList.contains(this.hoverClass)) {
  26. this.getElements().forEach((otherEl) => {
  27. if (otherEl !== el) {
  28. otherEl.classList.remove(this.hoverClass, this.clickedClass);
  29. }
  30. });
  31. el.classList.add(this.clickedClass);
  32. this.smartButton.setPosition(e.pageX, e.pageY - 20);
  33. }
  34. }
  35. }
  36. storeData(e) {
  37. const id = e.currentTarget.getAttribute("data-deleteid");
  38. if (id) {
  39. this.deleteID = id;
  40. }
  41. }
  42. init() {
  43. super.init();
  44. if (this.toggleButton) {
  45. this.toggleButton.addEventListener("click", this.toggleHover);
  46. }
  47. if (this.smartButton.getElement()) {
  48. this.smartButton.onClick(() => {
  49. this.bgModal.setVisible(true);
  50. this.smartButton.hide();
  51. })
  52. this.bgModal.onBackgroundClick(() => this.bgModal.setVisible(false));
  53. this.bgModal.onCloseButtonClick(".cancelBtn");
  54. this.bgModal.onValidate(() => this.removeResource());
  55. }
  56. }
  57. getType() {
  58. if (!this.deleteID) return null;
  59. const el = document.querySelector(`[data-deleteid="${this.deleteID}"]`);
  60. return el?.getAttribute(this.type) ?? null;
  61. }
  62. clearElementState(el) {
  63. el.classList.remove(this.hoverClass, this.clickedClass);
  64. }
  65. async removeResource() {
  66. try {
  67. if (!this.getType()) {
  68. console.error("No type define in [data-deleteType]");
  69. this.bgModal.setVisible(false);
  70. return;
  71. }
  72. const response = await fetch(
  73. `${baseURL}${REST_PREFIX}/dashboard/${this.getType()}/${this.deleteID}`,
  74. {
  75. method: "DELETE",
  76. }
  77. );
  78. if (!response.ok) {
  79. throw new Error(
  80. `Erreur ${response.status} : la ressource n'a pas pu être supprimée.`
  81. );
  82. }
  83. const data = await response.json();
  84. if (data.code === "200") {
  85. document
  86. .querySelector('[data-deleteid="' + this.deleteID + '"]')
  87. .remove();
  88. this.bgModal.setVisible(false);
  89. }
  90. return data;
  91. } catch (error) {
  92. console.error("Erreur lors de la suppression :", error);
  93. throw error;
  94. }
  95. }
  96. }