Преглед на файлове

Implement SmartButton and abstract class BaseHoverManager and Refactor newDeleteManager and newTranslateManager and moving her into NewSiteEditor function()

Corentin NELHOMME преди 1 година
родител
ревизия
ac1138af27
променени са 1 файла, в които са добавени 263 реда и са изтрити 131 реда
  1. 263 131
      index.js

+ 263 - 131
index.js

@@ -12,7 +12,7 @@
  * - 2025/06/02 - V1.3.2 - E.Podvin - fix toggle on hover when we leave nse
  * - 2025/06/03 - V1.4.0 - C.NELHOMME - Added videoEditor, posterEditor, refactor upload function, implements Kserver
  * - 2025/06/04 - V1.4.1 - C.NELHOMME - Refactor fntManager using Modal class, add some commentary, renaming fntManager to newDeleteManager, renaming variable startingWith RG_ to normal variable 
- *
+ * - 2025/06/04 - V1.5.0 - C.NELHOMME - Implement SmartButton and abstract class BaseHoverManager and Refactor newDeleteManager and newTranslateManager and moving her into NewSiteEditor function()
  * @copyright Intersel 2022-2025
  *
  */
@@ -101,100 +101,78 @@ class Modal {
 }
 
 /**
- * This class is only for "RenaultGroup" actually
+ * SmartButton is a class that enables the creation of a movable button.
  */
-class newDeleteManager {
-	constructor(
-		toggleButtonId = "delete",
-		bgModalId = "modalDelete",
-		deleteButtonId = "btnDelete"
-	) {
-		this.deleteID = null;
-		this.bgModal = new Modal(bgModalId);
-		this.hoverActive = false;
-		this.selector = "[data-deleteid]";
-		this.type = "data-deleteType";
-		this.toggleButton = document.getElementById(toggleButtonId);
-		this.deleteButton = document.getElementById(deleteButtonId);
-
-		this.storeData = this.storeData.bind(this);
-		this.toggleHover = this.toggleHover.bind(this);
-		this.removeResource = this.removeResource.bind(this);
-		this.getType = this.getType.bind(this);
-		this.addHoverClass = this.addHoverClass.bind(this);
-		this.removeHoverClass = this.removeHoverClass.bind(this);
-
-		if (this.deleteButton)
-        {
-    		this.deleteButton.addEventListener("click", () => {
-				this.bgModal.open();
-				this.deleteButton.style.display = "none";
-			});
-    		this.bgModal.onBackgroundClick(() => this.bgModal.close());
-    		this.bgModal.onCloseButtonClick(".cancelBtn");
-			this.bgModal.onValidate(() => this.removeResource())
-        }
-
+class SmartButton {
+		constructor(buttonId) {
+		this.button = document.getElementById(buttonId);
+		if (!this.button) throw new Error("SmartButton not found");
+		this.isVisible = false;
 	}
 
-	init() {
-		this.getElements().forEach((el) => {
-			el.addEventListener("click", (e) => {
-				this.storeData(e);
+	moveTo(x, y) {
+		Object.assign(this.button.style, {
+			left: `${x}px`,
+			top: `${y}px`,
+			position: "absolute",
+		});
+		this.show();
+	}
 
-				if (el.classList.contains("deletable")) {
-					this.getElements().forEach((otherEl) => {
-						if (otherEl !== el) {
-							otherEl.classList.remove("deletable", "inDelete");
-						}
-					});
+	show() {
+		this.button.style.display = "block";
+		this.isVisible = true;
+	}
 
-					el.classList.add("inDelete");
+	hide() {
+		this.button.style.display = "hidden";
+		this.isVisible = false;
+	}
 
-					this.updateButtonPosition(e.pageX, e.pageY - 20);
-				}
-			});
-		});
+	setVisible(visible) {
+		visible ? this.show() : this.hide();
+	}
 
-		if (this.toggleButton) {
-			this.toggleButton.addEventListener("click", this.toggleHover);
-		}
+	setPosition(x, y) {
+		this.moveTo(x, y);
 	}
-	
-	destroy() {
-		if (!this.hoverActive) this.hoverActive = true;
-		this.toggleHover();
 
-		if (this.toggleButton) {
-			this.toggleButton.removeEventListener("click", this.toggleHover);
-			this.bgModal.close();
-		}
+	getElement() {
+		return this.button;
 	}
 
-	updateButtonPosition(x, y) {
-		Object.assign(this.deleteButton.style, {
-			display: "",
-			left: x + "px",
-			top: y + "px",
+	onClick(callback) {
+		this.button.addEventListener("click", () => {
+			callback();
 		});
 	}
+}
 
-	storeData(e) {
-		const id = e.currentTarget.getAttribute("data-deleteid");
-		if (id) {
-			this.deleteID = id;
-		}
+/**
+ * BaseHoverManager is an abstract class that enable to create extends manager using hover
+ */
+class BaseHoverManager {
+		constructor(selector, hoverClass = "hovered", clickedClass = "clicked") {
+		this.selector = selector;
+		this.hoverClass = hoverClass;
+		this.clickedClass = clickedClass;
+		this.hoverActive = false;
+
+		this.addHoverClass = this.addHoverClass.bind(this);
+		this.removeHoverClass = this.removeHoverClass.bind(this);
+		this.toggleHover = this.toggleHover.bind(this);
 	}
 
 	getElements() {
 		return document.querySelectorAll(this.selector);
 	}
-	
+
 	toggleHover() {
 		this.hoverActive = !this.hoverActive;
 
 		this.getElements().forEach((el) => {
 			if (this.hoverActive) {
+				tinymce?.remove?.();
 				el.addEventListener("mouseenter", this.addHoverClass);
 				el.addEventListener("mouseleave", this.removeHoverClass);
 			} else {
@@ -202,86 +180,36 @@ class newDeleteManager {
 				el.removeEventListener("mouseleave", this.removeHoverClass);
 			}
 
-			el.classList.remove("deletable", "inDelete");
+			this.clearElementState(el);
 		});
 
 		if (!this.hoverActive) {
-			this.removeModal();
-			this.deleteButton.style.display = "none";
-			this.deleteID = null;
+			this.onHoverDisabled?.();
 		}
 	}
 
+	
 	addHoverClass(e) {
-		e.currentTarget.classList.add("deletable");
+		e.currentTarget.classList.add(this.hoverClass);
 	}
 
 	removeHoverClass(e) {
-		e.currentTarget.classList.remove("deletable");
+		e.currentTarget.classList.remove(this.hoverClass);
 	}
 
-	getType() {
-		return document
-			.querySelector('[data-deleteid="' + this.deleteID + '"]')
-			.getAttribute(this.type);
+	clearElementState(el) {
+	
 	}
 
-	async removeResource() {
-		try {
-			if (!this.getType()) {
-				console.error("No type define in [data-deleteType]");
-				this.bgModal.close();
-				return;
-			}
-
-			const response = await fetch(
-				`${baseURL}/rest/dashboard/${this.getType()}/${this.deleteID}`,
-				{
-					method: "DELETE",
-				}
-			);
-
-			if (!response.ok) {
-				throw new Error(
-					`Erreur ${response.status} : la ressource n'a pas pu être supprimée.`
-				);
-			}
+	init() {
 
-			const data = await response.json();
-			if (data.code === "200") {
-				document
-					.querySelector('[data-deleteid="' + this.deleteID + '"]')
-					.remove();
-				this.bgModal.close();
-			}
-			return data;
-		} catch (error) {
-			console.error("Erreur lors de la suppression :", error);
-			throw error;
-		}
 	}
-}
 
-class translateToContext {
-	constructor(
-		openModalButtonId = "translate",
-		bgModalId = "modalTranslateTemplate"
-	) {
-		const modal = new Modal(bgModalId);
-		const button = document.getElementById(openModalButtonId);
-
-        if (button)
-        {
-    		button.addEventListener("click", () => modal.open());
-    		modal.onBackgroundClick(() => modal.close());
-    		modal.onCloseButtonClick();
-        }
+	destroy() {
+
 	}
 }
 
-const fnt = new newDeleteManager();
-const translate = new translateToContext();
-
 var NEWSITEEDITOR = (function () {
 	let version = "1.4.1";
 
@@ -383,6 +311,209 @@ var NEWSITEEDITOR = (function () {
 		});
 	}
 
+	class newDeleteManager extends BaseHoverManager {
+		constructor(toggleButtonId = "delete", bgModalId = "modalDelete", deleteButtonId = "btnDelete") {
+			super("[data-deleteid]", "deletable", "inDelete");
+
+			this.deleteID = null;
+
+			this.bgModal = new Modal(bgModalId);
+			this.smartButton = new SmartButton("btnDelete");
+
+			this.toggleButton = document.getElementById(toggleButtonId);
+			this.deleteButton = document.getElementById(deleteButtonId);
+
+			this.type = "data-deleteType";
+
+			this.storeData = this.storeData.bind(this);
+			this.removeResource = this.removeResource.bind(this);
+
+			this.onHoverDisabled = () => {
+				this.bgModal.close();
+				this.deleteButton.style.display = "none";
+				this.deleteID = null;
+			};
+
+			if (this.deleteButton) {
+				this.deleteButton.addEventListener("click", () => {
+					this.bgModal.open();
+					this.deleteButton.style.display = "none";
+				});
+				this.bgModal.onBackgroundClick(() => this.bgModal.close());
+				this.bgModal.onCloseButtonClick(".cancelBtn");
+				this.bgModal.onValidate(() => this.removeResource());
+			}
+		}
+
+		storeData(e) {
+			const id = e.currentTarget.getAttribute("data-deleteid");
+			if (id) {
+				this.deleteID = id;
+			}
+		}
+
+		init() {
+			this.getElements().forEach((el) => {
+				el.addEventListener("click", (e) => {
+					this.storeData(e);
+
+					if (el.classList.contains(this.hoverClass)) {
+						this.getElements().forEach((otherEl) => {
+							if (otherEl !== el) {
+								otherEl.classList.remove(this.hoverClass, this.clickedClass);
+							}
+						});
+
+						el.classList.add(this.clickedClass);
+
+						this.smartButton.setPosition(e.pageX, e.pageY - 20);
+					}
+				});
+			});
+
+			if (this.toggleButton) {
+				this.toggleButton.addEventListener("click", this.toggleHover);
+			}
+		}
+
+		destroy() {
+			this.getElements().forEach((e) => e.classList.remove(this.hoverClass, this.clickedClass))
+			this.smartButton.hide();
+		}
+
+		getType() {
+			return document
+			.querySelector('[data-deleteid="' + this.deleteID + '"]')
+			.getAttribute(this.type);
+		}
+
+		clearElementState(el) {
+			el.classList.remove(this.hoverClass, this.clickedClass);
+		}
+
+		async removeResource() {
+			try {
+				if (!this.getType()) {
+					console.error("No type define in [data-deleteType]");
+					this.bgModal.close();
+					return;
+				}
+
+				const response = await fetch(
+					`${baseURL}/rest/dashboard/${this.getType()}/${this.deleteID}`,
+					{
+						method: "DELETE",
+					}
+				);
+
+				if (!response.ok) {
+					throw new Error(
+						`Erreur ${response.status} : la ressource n'a pas pu être supprimée.`
+					);
+				}
+
+				const data = await response.json();
+				if (data.code === "200") {
+					document
+						.querySelector('[data-deleteid="' + this.deleteID + '"]')
+						.remove();
+					this.bgModal.close();
+				}
+				return data;
+			} catch (error) {
+				console.error("Erreur lors de la suppression :", error);
+				throw error;
+			}
+		}
+	}
+
+	class newTranslateManager extends BaseHoverManager {
+		constructor(openModalButtonId = "btnTranslate", bgModalId = "modalTranslateTemplate", toggleButtonId = "translate") {
+
+			super("[data-resourceid]", "clicked", "veryclicked");
+
+			this.resourceId = null;
+
+			this.modal = new Modal(bgModalId);
+			this.smartButton = new SmartButton(openModalButtonId);
+			this.toggleButton = document.getElementById(toggleButtonId);
+
+			this.getSelector = this.getSelector.bind(this);
+			this.getElementWithSameId = this.getElementWithSameId.bind(this);
+
+			this.onHoverDisabled = () => {
+				this.modal.close();
+				this.smartButton.hide();
+				this.resourceId = null;
+			};
+
+			if (this.smartButton) {
+				this.modal.onBackgroundClick(() => this.modal.close());
+				this.modal.onCloseButtonClick();
+				this.smartButton.onClick(() => this.modal.open());
+			}
+
+		}
+
+		init() {
+			this.getElements().forEach((el) => {
+				el.addEventListener("click", (e) => {
+					e.stopPropagation();
+					if (!this.hoverActive) return;
+					this.getElements().forEach((otherEl) => {
+						if (otherEl !== el) {
+							otherEl.classList.remove(this.hoverClass, this.clickedClass);
+						}
+					})
+
+					const rect = el.getBoundingClientRect();
+					const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
+					const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
+
+					const button = this.smartButton.getElement();
+					const buttonWidth = button.offsetWidth;
+					const buttonHeight = button.offsetHeight;
+
+					const x = rect.left + scrollLeft + rect.width / 2 - buttonWidth / 2;
+					const y = rect.top + scrollTop - buttonHeight - 8;
+
+					this.smartButton.setPosition(x, y);
+					this.resourceId = this.getSelector(el);
+						
+					let elements = this.getElementWithSameId(this.getSelector(el)).forEach((el) => el.classList.add(this.clickedClass));
+					console.log(elements[0])
+				})
+			})
+
+			if (this.toggleButton) {
+					this.toggleButton.addEventListener("click", this.toggleHover);
+				}
+			}
+
+			getSelector(e) {
+				return e.getAttribute(this.selector.replace(/\[|\]/g, ""))
+			}
+
+			addHoverClass(e) {
+				this.getElements().forEach((el) => el.classList.remove("clicked"));
+				this.getElementWithSameId(this.getSelector(e.target)).forEach((el) => el.classList.add("clicked"));
+			}
+
+			removeHoverClass(e) {
+				e.currentTarget.classList.remove("clicked");
+			}
+
+			getElementWithSameId(id) {
+				return Array.from(document.querySelectorAll(this.selector)).filter(
+					(el) => el.getAttribute("data-resourceid") === id
+				);
+			}
+
+	}
+
+	const fnt = new newDeleteManager();
+	const translate = new newTranslateManager();
+
 	//REST : Get method to get the title of the resource in sidebar
 	function getTitle(who) {
 		var transform = $("h2").css("text-transform");
@@ -500,6 +631,7 @@ var NEWSITEEDITOR = (function () {
 			nseVideoEditor(true);
 			nseImageEditor(true);
 			fnt.init();
+			translate.init();
 		});
 	}