Forráskód Böngészése

fix smartButton error, moved all the class into NEWSITEEDITOR, remove this.deleteButton and replacing by a smartButton, adding new smartButton to translate the entiere page, adding destroy

Corentin NELHOMME 1 éve
szülő
commit
7428462a1a
1 módosított fájl, 136 hozzáadás és 90 törlés
  1. 136 90
      index.js

+ 136 - 90
index.js

@@ -15,9 +15,16 @@
  * - 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
  *
+ *
  */
 //debugger;
 
+/**
+ * Todo :
+ * - Faire en sorte qu'on puisse traduire tt la page
+ * - Faire en sorte que la poubelle et la traduction puisse pas etre fait au mm moment sinon confliiit
+ */
+
 //Configuration
 const { Uppy: UppyConstructor, Dashboard, ImageEditor } = window.Uppy;
 const availableLang = Uppy.locales;
@@ -38,6 +45,10 @@ function extensionWODot(extension) {
 const managerURL = window.customerManagerUrl; //set in NewSiteEditor plugin
 const managerHelpURL = window.helpManagerUrl; //set in NewSiteEditor plugin
 
+/**
+ * @class Modal
+ * @description Modal is a class that enable to control easily a modal
+ */
 class Modal {
 	/**
 	 *
@@ -101,7 +112,8 @@ class Modal {
 }
 
 /**
- * SmartButton is a class that enables the creation of a movable button.
+ * @class SmartButton
+ * @description SmartButton is a class that enables the creation of a movable button.
  */
 class SmartButton {
 		constructor(buttonId) {
@@ -125,7 +137,7 @@ class SmartButton {
 	}
 
 	hide() {
-		this.button.style.display = "hidden";
+		this.button.style.display = "none";
 		this.isVisible = false;
 	}
 
@@ -148,68 +160,6 @@ class SmartButton {
 	}
 }
 
-/**
- * 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 {
-				el.removeEventListener("mouseenter", this.addHoverClass);
-				el.removeEventListener("mouseleave", this.removeHoverClass);
-			}
-
-			this.clearElementState(el);
-		});
-
-		if (!this.hoverActive) {
-			this.onHoverDisabled?.();
-		}
-	}
-
-	
-	addHoverClass(e) {
-		e.currentTarget.classList.add(this.hoverClass);
-	}
-
-	removeHoverClass(e) {
-		e.currentTarget.classList.remove(this.hoverClass);
-	}
-
-	clearElementState(el) {
-	
-	}
-
-	init() {
-
-	}
-
-	destroy() {
-
-	}
-}
-
 var NEWSITEEDITOR = (function () {
 	let version = "1.4.1";
 
@@ -311,6 +261,91 @@ var NEWSITEEDITOR = (function () {
 		});
 	}
 
+	/**
+	 * @abstract
+	 * @class BaseHoverManager
+	 * An abstract class that provides hover and click state management for a group of DOM elements.
+	 * Extend this class to build custom managers with specific behaviors.
+	 */
+	class BaseHoverManager {
+		    /**
+			 *
+			 * @param {string} selector - CSS selector for targeting DOM elements.
+			 * @param {string} [hoverClass="hovered"] - CSS class applied on hover.
+			 * @param {string} [clickedClass="clicked"] - CSS class applied on click.
+			 */
+			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);
+		}
+
+		/**
+		 * Use the data-newmanager with value attribute to exclude from the selected elements 
+		 */
+		getElements() {
+			return Array.from(document.querySelectorAll(this.selector)).filter(
+				(el) => el.getAttribute("data-newmanager") !== "false"
+			);
+		}
+
+		disableHover() {
+			if (this.hoverActive) {
+				this.hoverActive = false;
+				this.onHoverDisabled();
+			}
+		}
+
+		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 {
+					TinyMCE();
+					el.removeEventListener("mouseenter", this.addHoverClass);
+					el.removeEventListener("mouseleave", this.removeHoverClass);
+				}
+
+				this.clearElementState(el);
+			});
+
+			if (!this.hoverActive) {
+				this.onHoverDisabled?.();
+			}
+		}
+
+		
+		
+		addHoverClass(e) {
+			e.currentTarget.classList.add(this.hoverClass);
+		}
+
+		removeHoverClass(e) {
+			e.currentTarget.classList.remove(this.hoverClass);
+		}
+
+		clearElementState(el) {
+		
+		}
+
+		init() {
+		}
+
+		destroy() {
+				this.getElements().forEach((e) => e.classList.remove(this.hoverClass, this.clickedClass))
+				this.smartButton.hide();
+		}
+	}
+
 	class newDeleteManager extends BaseHoverManager {
 		constructor(toggleButtonId = "delete", bgModalId = "modalDelete", deleteButtonId = "btnDelete") {
 			super("[data-deleteid]", "deletable", "inDelete");
@@ -321,7 +356,6 @@ var NEWSITEEDITOR = (function () {
 			this.smartButton = new SmartButton("btnDelete");
 
 			this.toggleButton = document.getElementById(toggleButtonId);
-			this.deleteButton = document.getElementById(deleteButtonId);
 
 			this.type = "data-deleteType";
 
@@ -330,15 +364,16 @@ var NEWSITEEDITOR = (function () {
 
 			this.onHoverDisabled = () => {
 				this.bgModal.close();
-				this.deleteButton.style.display = "none";
+				this.smartButton.hide();
 				this.deleteID = null;
 			};
 
-			if (this.deleteButton) {
-				this.deleteButton.addEventListener("click", () => {
+			if (this.smartButton.getElement()) {
+				this.smartButton.onClick(() => {
 					this.bgModal.open();
-					this.deleteButton.style.display = "none";
-				});
+					this.smartButton.hide();
+				})
+
 				this.bgModal.onBackgroundClick(() => this.bgModal.close());
 				this.bgModal.onCloseButtonClick(".cancelBtn");
 				this.bgModal.onValidate(() => this.removeResource());
@@ -376,17 +411,13 @@ var NEWSITEEDITOR = (function () {
 			}
 		}
 
-		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);
+			if (!this.deleteID) return null;
+			const el = document.querySelector(`[data-deleteid="${this.deleteID}"]`);
+			return el?.getAttribute(this.type) ?? null;
 		}
 
+
 		clearElementState(el) {
 			el.classList.remove(this.hoverClass, this.clickedClass);
 		}
@@ -436,26 +467,40 @@ var NEWSITEEDITOR = (function () {
 
 			this.modal = new Modal(bgModalId);
 			this.smartButton = new SmartButton(openModalButtonId);
+			this.pageSmartButton = new SmartButton("btnTranslatePage");
 			this.toggleButton = document.getElementById(toggleButtonId);
 
 			this.getSelector = this.getSelector.bind(this);
 			this.getElementWithSameId = this.getElementWithSameId.bind(this);
+			this.switchResourceId = "id_switchResourceId";
 
 			this.onHoverDisabled = () => {
 				this.modal.close();
 				this.smartButton.hide();
 				this.resourceId = null;
+				this.getElements().forEach((element) => {
+					element.classList.remove(this.hoverClass, this.clickedClass);
+				})
 			};
 
-			if (this.smartButton) {
+			if (this.smartButton.getElement()) {
 				this.modal.onBackgroundClick(() => this.modal.close());
 				this.modal.onCloseButtonClick();
-				this.smartButton.onClick(() => this.modal.open());
+				this.smartButton.onClick(() => {
+					this.modal.open();
+					this.smartButton.hide();
+					document.getElementById(this.switchResourceId).value = this.resourceId;
+			});
 			}
 
+			this.pageSmartButton.onClick(() => {
+				this.modal.open();
+			})
+
 		}
 
-		init() {
+			init() {
+			this.pageSmartButton.show();	
 			this.getElements().forEach((el) => {
 				el.addEventListener("click", (e) => {
 					e.stopPropagation();
@@ -465,8 +510,9 @@ var NEWSITEEDITOR = (function () {
 							otherEl.classList.remove(this.hoverClass, this.clickedClass);
 						}
 					})
-
-					const rect = el.getBoundingClientRect();
+						
+					let elements = this.getElementWithSameId(this.getSelector(el));
+					const rect = elements[0].getBoundingClientRect();
 					const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
 					const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
 
@@ -479,9 +525,8 @@ var NEWSITEEDITOR = (function () {
 
 					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])
+
+					elements.forEach((el) => el.classList.add(this.clickedClass))
 				})
 			})
 
@@ -495,17 +540,17 @@ var NEWSITEEDITOR = (function () {
 			}
 
 			addHoverClass(e) {
-				this.getElements().forEach((el) => el.classList.remove("clicked"));
-				this.getElementWithSameId(this.getSelector(e.target)).forEach((el) => el.classList.add("clicked"));
+				this.getElements().forEach((el) => el.classList.remove(this.hoverClass));
+				this.getElementWithSameId(this.getSelector(e.target)).forEach((el) => el.classList.add(this.hoverClass));
 			}
 
 			removeHoverClass(e) {
-				e.currentTarget.classList.remove("clicked");
+				this.getElementWithSameId(this.getSelector(e.target)).forEach((el) => el.classList.remove(this.hoverClass));
 			}
 
 			getElementWithSameId(id) {
 				return Array.from(document.querySelectorAll(this.selector)).filter(
-					(el) => el.getAttribute("data-resourceid") === id
+					(el) => this.getSelector(el) === id
 				);
 			}
 
@@ -579,6 +624,7 @@ var NEWSITEEDITOR = (function () {
 			nseVideoEditor(false);
 			nseImageEditor(false);
 			fnt.destroy();
+			translate.destroy();
 		});
 	}