Quellcode durchsuchen

Update destroy and init method for BaseHover, correct the tech problem, and implement handleClick method

Corentin NELHOMME vor 1 Jahr
Ursprung
Commit
4b3011c108
1 geänderte Dateien mit 122 neuen und 91 gelöschten Zeilen
  1. 122 91
      index.js

+ 122 - 91
index.js

@@ -14,9 +14,10 @@
  * - 2025/06/03 - V1.4.1 - C.NELHOMME - Added videoEditor, posterEditor, refactor upload function, implements Kserver
  * - 2025/06/04 - V1.4.2 - 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()
- * - 2025/06/11 - V1.5.1 - C.NELHOMME - Implement setCreateError to show error in modal, added page smartButton for selecting id of the currznt page 
+ * - 2025/06/11 - V1.5.1 - C.NELHOMME - Implement setCreateError to show error in modal, added page smartButton for selecting id of the currznt page
  * - 2025/06/12 - V.1.5.2 - C.NELHOMME (Stable test with Hugo) - Fix multiple request to mistral now one by one, when translate/delete is open all the tinymce is now closed, remove poster from video
  * - 2025/06/13 - V.1.5.3 - C.NELHOMME & E.Podvin - Fix TinyMCE Enter key bug (text disappearing) and merge v1.4.0 → v1.5.3
+ * - 2025/06/17 - V.1.5.4 - C.NELHOMME - Update destroy and init method for BaseHover, correct the tech problem, and implement handleClick method
  * @copyright Intersel 2022-2025
  *
  *
@@ -112,6 +113,10 @@ class Modal {
         return this.modal;
     }
 
+	/**
+	 * When background is clicked
+	 * @param {CallableFunction} callback - Function that enable when the background is clicked
+	 */
     onBackgroundClick(callback) {
         this.modal.addEventListener("click", (e) => {
             if (e.target === this.modal) callback();
@@ -172,6 +177,10 @@ class SmartButton {
         this.moveTo(x, y);
     }
 
+	/**
+	 * 
+	 * @returns {HTMLElement}
+	 */
     getElement() {
         return this.button;
     }
@@ -196,16 +205,17 @@ var NEWSITEEDITOR = (function () {
         const sync = { syncsite: 1 };
 
         //if(field === 'content') ...
+        // console.log(who);
 
         if (!editModeOn || dirty == undefined)
-			{
+        {
             $(who).attr("dirty", "0");
-        } 
-		else
-		{
+        }
+        else
+        {
             obj = JSON.parse($(who).attr("data-data"));
             if (obj)
-			{
+            {
                 dirty = (obj[field] == who.innerHTML) ? "0" : "1";
                 $(who).attr("dirty", dirty);
             }
@@ -221,7 +231,7 @@ var NEWSITEEDITOR = (function () {
                 (who.innerHTML.includes('<br data-mce-bogus="1">') && who.textContent === "")
             )
         )
-		{
+        {
             if (editModeOn) who.innerHTML = "&nbsp;&nbsp;&nbsp;";
         }
 
@@ -236,7 +246,7 @@ var NEWSITEEDITOR = (function () {
         $(".siteeditor").blur(function () {
             dataData(this);
             if ($(this).attr("dirty") == "1")
-			{
+            {
                 blapyTools.ajaxSimpleAction(this);
                 $(this).attr("dirty", "0");
             }
@@ -331,11 +341,11 @@ var NEWSITEEDITOR = (function () {
                 if (this.hoverActive) {
                     //On supprime chaque instance de TinyMce sinon il s'active lors des clics
                     tinymce.editors.forEach((ed) => {
-                    try {
-                        ed.remove();
-                    } catch (e) {
-                        console.warn("Erreur suppression éditeur :", ed.id, e);
-                    }
+                        try {
+                            ed.remove();
+                        } catch (e) {
+                            console.warn("Erreur suppression éditeur :", ed.id, e);
+                        }
                     });
                     el.addEventListener("mouseenter", this.addHoverClass);
                     el.addEventListener("mouseleave", this.removeHoverClass);
@@ -366,13 +376,27 @@ var NEWSITEEDITOR = (function () {
 
         }
 
+		onHoverDisabled() {}
+		
+		handleClick(e) {}
+
         init() {
+			this.getElements().forEach((el) => {
+				el.addEventListener("click", this.handleClick);
+			});
         }
 
         destroy() {
             this.getElements().forEach((e) => e.classList.remove(this.hoverClass, this.clickedClass))
             this.smartButton.hide();
+			this.getElements().forEach((el) => {
+  				el.removeEventListener("click", this.handleClick);
+			});
+			if(this.hoverActive) {
+				this.toggleHover();
+			}
         }
+
     }
 
     class newDeleteManager extends BaseHoverManager {
@@ -397,16 +421,22 @@ var NEWSITEEDITOR = (function () {
                 this.deleteID = null;
             };
 
-            if (this.smartButton.getElement()) {
-                this.smartButton.onClick(() => {
-                    this.bgModal.setVisible(true);
-                    this.smartButton.hide();
-                })
+			this.handleClick = (e) => {
+				const el = e.currentTarget;
+				this.storeData(e);
 
-                this.bgModal.onBackgroundClick(() => this.bgModal.setVisible(false));
-                this.bgModal.onCloseButtonClick(".cancelBtn");
-                this.bgModal.onValidate(() => this.removeResource());
-            }
+                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);
+                }
+			}
         }
 
         storeData(e) {
@@ -417,26 +447,21 @@ var NEWSITEEDITOR = (function () {
         }
 
         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);
-                    }
-                });
-            });
+			super.init();
 
             if (this.toggleButton) {
                 this.toggleButton.addEventListener("click", this.toggleHover);
+            }
+
+			if (this.smartButton.getElement()) {
+                this.smartButton.onClick(() => {
+                    this.bgModal.setVisible(true);
+                    this.smartButton.hide();
+            })
+
+            this.bgModal.onBackgroundClick(() => this.bgModal.setVisible(false));
+            this.bgModal.onCloseButtonClick(".cancelBtn");
+            this.bgModal.onValidate(() => this.removeResource());
             }
         }
 
@@ -512,6 +537,37 @@ var NEWSITEEDITOR = (function () {
                 })
             };
 
+			this.handleClick = (e) => {
+				const el = e.currentTarget;
+				e.stopPropagation();
+				if (!this.hoverActive) return;
+
+				this.getElements().forEach((otherEl) => {
+					if (otherEl !== el) {
+					otherEl.classList.remove(this.hoverClass, this.clickedClass);
+					}
+				});
+
+				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;
+
+				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);
+
+				elements.forEach((el) => el.classList.add(this.clickedClass));
+			};
+
+
             if (this.smartButton.getElement()) {
                 this.modal.onBackgroundClick(() => this.modal.setVisible(false));
                 this.modal.onCloseButtonClick();
@@ -525,43 +581,15 @@ var NEWSITEEDITOR = (function () {
             this.pageSmartButton.onClick(() => {
                 this.modal.setVisible(true);
                 document.getElementById(this.switchResourceId).value = document.body.getAttribute('data-pageid');
-				document.getElementById('checkbox_children').checked = true;
-				document.getElementById('checkbox_hidden_children').value = 1;
+                document.getElementById('checkbox_children').checked = true;
+                document.getElementById('checkbox_hidden_children').value = 1;
             })
 
         }
 
         init() {
             this.pageSmartButton[document.body.hasAttribute('data-pageid') ? 'show' : 'hide']();
-            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);
-                        }
-                    })
-
-                    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;
-
-                    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);
-
-                    elements.forEach((el) => el.classList.add(this.clickedClass))
-                })
-            })
+			super.init();
 
             if (this.toggleButton) {
                 this.toggleButton.addEventListener("click", this.toggleHover);
@@ -881,7 +909,7 @@ var NEWSITEEDITOR = (function () {
     function sidebarToggle() {
         $("#sidebarToggle").on("click", function () {
             const isNoSidebar = $("#wrapper").hasClass("no-sidebar");
-            console.log(isNoSidebar);
+            // console.log(isNoSidebar);
 
             if (isNoSidebar) {
                 var compt = 0;
@@ -1255,16 +1283,16 @@ var NEWSITEEDITOR = (function () {
         if (el.naturalHeight === 0) {
             current.aspectRatio = 1;
         } else {
-			if (el.naturalWidth)
-			{
-				current.aspectRatio = el.naturalWidth / el.naturalHeight;
-			}
-			else
-			{
-				const rect = el.getBoundingClientRect();
-				current.aspectRatio = rect.height && (rect.width / rect.height);
-			}
-		}
+            if (el.naturalWidth)
+            {
+                current.aspectRatio = el.naturalWidth / el.naturalHeight;
+            }
+            else
+            {
+                const rect = el.getBoundingClientRect();
+                current.aspectRatio = rect.height && (rect.width / rect.height);
+            }
+        }
 
         current.element = el;
         current.lang = el.getAttribute("data-lang");
@@ -1276,10 +1304,10 @@ var NEWSITEEDITOR = (function () {
         };
         uppyReset(imageUppy);
 
-		if (!el.src) el.src = current.customFields.src;
-		let blob = '';
-		if (el.src)
-			blob = await fetch(el.src).then((res) => res.blob());
+        if (!el.src) el.src = current.customFields.src;
+        let blob = '';
+        if (el.src)
+            blob = await fetch(el.src).then((res) => res.blob());
 
         const ts = new Date().toISOString().replace(/[-:.]/g, "").replace("Z", "");
 
@@ -1463,8 +1491,8 @@ var NEWSITEEDITOR = (function () {
                     }
 
                     if (current.customFields?.tv) {
-						params.append("tvField", current.customFields.tv);
-					}
+                        params.append("tvField", current.customFields.tv);
+                    }
 
                     if (file && !file.meta.fromServer) {
                         try {
@@ -1815,13 +1843,16 @@ var NEWSITEEDITOR = (function () {
                 })
             );
 
+			//Permet de ne pas activer l'erreur dans la console (https://github.com/videojs/video.js/issues/2617)
+			const tech = player.tech(true);
+
             return {
                 id: player.el().id,
                 resourceId: player.el().getAttribute("data-resource-id"),
                 context_key: player.el().getAttribute("data-lang"),
                 poster: player.poster(),
                 title: player.el().title,
-                aria: player.tech().el().getAttribute("aria-label"),
+                aria: tech.getAttribute("aria-label"),
                 src: sources[0]?.src,
             };
         }
@@ -2162,7 +2193,7 @@ var NEWSITEEDITOR = (function () {
                         {types: acceptedMimeTypes.map((type) => {
                                 const parts = type.split('/');
                                 return parts[1] ? ` .${parts[1]}` : null;
-							}).filter(Boolean)}), 'error', 5000);
+                            }).filter(Boolean)}), 'error', 5000);
                     console.warn(`Type de fichier non autorisé : ${fileType}`);
                     tempInput.remove();
                     return;
@@ -2269,11 +2300,11 @@ var NEWSITEEDITOR = (function () {
     }
 
     /**
-     * 
+     *
      * @param {string} requestURL URL to request
      * @param {string} method Type of method "POST, GET, PUT, DELETE"
      * @param {FormData} Data Body of the request
-     * @returns 
+     * @returns
      */
     async function sendRequest(requestURL, method, Data) {
         if (!requestURL.startsWith("http")) {