Ver código fonte

Refactor fntManager using Modal class, add some commentary

Corentin NELHOMME 1 ano atrás
pai
commit
0b3447d57e
1 arquivos alterados com 45 adições e 82 exclusões
  1. 45 82
      index.js

+ 45 - 82
index.js

@@ -4,19 +4,21 @@
  *
  * Modifications:
  * - 2022/07/12 - V.1.0 0 - B.Bouaphakeo - création
- * - 20250414 - V1.0.1 - E.Podvin - fix hover when siteeditor is activated with tinymce
+ * - 2025/04/14 - V1.0.1 - E.Podvin - fix hover when siteeditor is activated with tinymce
  * - 2025/05/06 - V.1.1.0 H.Malezet & C.Nelhomme - Added newFileEditor to edit image and video.
  * - 2025/05/09 - V.1.2.0 C.Nelhomme - Added compressorJS and add request to mistralAI
  * - 2025/05/31 - V1.3.0 - E.Podvin - add redirections to dashboard and help pages
  * - 2025/06/02 - V1.3.1 - E.Podvin - call newsiteeditor/image POST
  * - 2025/06/02 - V1.3.2 - E.Podvin - fix toggle on hover when we leave nse
- * - 2025/06/06 - V1.4.0 - C.NELHOMME - Added videoEditor
+ * - 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
  *
  * @copyright Intersel 2022-2025
  *
  */
 //debugger;
 
+//Configuration
 const { Uppy: UppyConstructor, Dashboard, ImageEditor } = window.Uppy;
 const availableLang = Uppy.locales;
 
@@ -43,7 +45,7 @@ class Modal {
 	 */
 	constructor(modalId) {
 		this.modal = document.getElementById(modalId);
-		this.isOpen = false;
+		this.isOpen = this.modal ? this.modal.classList.contains("open") : false;
 	}
 
 	open() {
@@ -96,31 +98,41 @@ class Modal {
 	}
 }
 
-//Only for RG /!\
+/**
+ * This class is only for "RenaultGroup" actually
+ */
 class fntManager {
 	constructor(
 		toggleButtonId = "delete",
-		bgModalId = "modalDeleteTemplate",
+		bgModalId = "modalDelete",
 		deleteButtonId = "btnDelete"
 	) {
 		this.RG_deleteID = null;
-		this.bgModal = document.getElementById(bgModalId);
+		this.bgModal = new Modal(bgModalId);
 		this.RG_hoverActive = false;
 		this.selector = "[data-deleteid]";
 		this.type = "data-deleteType";
 		this.toggleButton = document.getElementById(toggleButtonId);
 		this.deleteButton = document.getElementById(deleteButtonId);
 
-		this.id = this.generateUUID();
-
 		this.storeData = this.storeData.bind(this);
 		this.toggleHover = this.toggleHover.bind(this);
-		this.openModal = this.openModal.bind(this);
-		this.closeModal = this.closeModal.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.visibility = "hidden";
+			});
+    		this.bgModal.onBackgroundClick(() => this.bgModal.close());
+    		this.bgModal.onCloseButtonClick(".cancelBtn");
+			this.bgModal.onValidate(() => this.removeResource())
+        }
+
 	}
 
 	init() {
@@ -138,8 +150,6 @@ class fntManager {
 					el.classList.add("inDelete");
 
 					this.updateButtonPosition(e.pageX, e.pageY - 20);
-
-					this.deleteButton.onclick = this.openModal;
 				}
 			});
 		});
@@ -148,13 +158,14 @@ class fntManager {
 			this.toggleButton.addEventListener("click", this.toggleHover);
 		}
 	}
-
+	
 	destroy() {
 		if (!this.RG_hoverActive) this.RG_hoverActive = true;
 		this.toggleHover();
 
 		if (this.toggleButton) {
 			this.toggleButton.removeEventListener("click", this.toggleHover);
+			this.bgModal.close();
 		}
 	}
 
@@ -176,7 +187,7 @@ class fntManager {
 	getElements() {
 		return document.querySelectorAll(this.selector);
 	}
-
+	
 	toggleHover() {
 		this.RG_hoverActive = !this.RG_hoverActive;
 
@@ -207,55 +218,6 @@ class fntManager {
 		e.currentTarget.classList.remove("deletable");
 	}
 
-	openModal() {
-		this.removeModal();
-
-		const clone = this.bgModal.firstElementChild.cloneNode(true);
-		clone.id = this.id;
-		clone.addEventListener("click", this.closeModal);
-
-		const cancelBtn = clone.querySelector(".cancelBtn");
-		const deleteBtn = clone.querySelector(".deleteBtn");
-
-		cancelBtn.addEventListener("click", (e) => {
-			e.stopPropagation();
-			this.removeModal();
-		});
-
-		deleteBtn.addEventListener("click", this.removeResource);
-
-		document.body.style.overflow = "hidden";
-		this.deleteButton.style.visibility = "hidden";
-
-		document.body.appendChild(clone);
-	}
-
-	removeModal() {
-		const el = document.getElementById(this.id);
-		if (el) {
-			document.body.style.overflow = "auto";
-			el.remove();
-		}
-	}
-
-	closeModal(e) {
-		const modalContainer = document.getElementById(this.id);
-		if (e.target === modalContainer) {
-			this.removeModal();
-		}
-	}
-
-	generateUUID() {
-		return (
-			crypto.randomUUID?.() ||
-			"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
-				const r = (Math.random() * 16) | 0;
-				const v = c === "x" ? r : (r & 0x3) | 0x8;
-				return v.toString(16);
-			})
-		);
-	}
-
 	getType() {
 		return document
 			.querySelector('[data-deleteid="' + this.RG_deleteID + '"]')
@@ -266,7 +228,7 @@ class fntManager {
 		try {
 			if (!this.getType()) {
 				console.error("No type define in [data-deleteType]");
-				this.removeModal();
+				this.bgModal.close();
 				return;
 			}
 
@@ -284,12 +246,11 @@ class fntManager {
 			}
 
 			const data = await response.json();
-			console.log(data);
 			if (data.code === "200") {
 				document
 					.querySelector('[data-deleteid="' + this.RG_deleteID + '"]')
 					.remove();
-				this.removeModal();
+				this.bgModal.close();
 			}
 			return data;
 		} catch (error) {
@@ -320,7 +281,7 @@ const fnt = new fntManager();
 const translate = new translateToContext();
 
 var NEWSITEEDITOR = (function () {
-	let version = "1.4.0";
+	let version = "1.4.1";
 
 	//set data-data for content and title
 	function dataData(who) {
@@ -501,7 +462,7 @@ var NEWSITEEDITOR = (function () {
 			$(".siteeditor")
 				.parents("[href]")
 				.each(function () {
-					console.log($(this));
+					//console.log($(this));
 					$(this).attr("data-hrefSave", $(this).attr("href"));
 					$(this).removeAttr("href");
 				});
@@ -1126,8 +1087,8 @@ var NEWSITEEDITOR = (function () {
 		});
 
 		function initializeUppy() {
-			console.log(Uppy.locales);
-			console.log(current);
+			//console.log(Uppy.locales);
+			//console.log(current);
 			const uppy = new UppyConstructor({
 				locale: availableLang[lang],
 				restrictions: {
@@ -1163,7 +1124,7 @@ var NEWSITEEDITOR = (function () {
 			});
 
 			uppy.on("file-added", (file) => {
-				console.log(file.type);
+				//console.log(file.type);
 				if (file.meta.isCropped) return;
 				pendingFile = file;
 				openCropper(file);
@@ -1206,7 +1167,7 @@ var NEWSITEEDITOR = (function () {
 						width: 1000,
 
 						async success(result) {
-							console.log(result.size / (1024 * 1024) + " mo");
+							//console.log(result.size / (1024 * 1024) + " mo");
 
 							const base64 = await blobToBase64(result);
 
@@ -1259,8 +1220,6 @@ var NEWSITEEDITOR = (function () {
 					const [file] = imageUppy.getFiles();
 					const params = new URLSearchParams();
 
-					console.log(current);
-
 					params.append("urlvalue", current.dataRessourceId);
 					params.append("alt", document.getElementById("altText").value);
 
@@ -1384,8 +1343,13 @@ var NEWSITEEDITOR = (function () {
 				});
 		}
 
+		/**
+		 * 
+		 * @param {Blob} file - File you want to upload
+		 * @returns {Response} - Response from the server with the image location
+		 */
 		async function uploadFile(file) {
-			console.log(current);
+			//console.log(current);
 			const formData = new FormData();
 			formData.append("file", file.data, file.name);
 			formData.append("inputFileFieldName", "file");
@@ -1521,7 +1485,7 @@ var NEWSITEEDITOR = (function () {
 						hideModal();
 					}
 				});
-				console.log(info);
+				//console.log(info);
 				posterPreview.src = info?.poster;
 				videoPreview.src = info.src;
 			});
@@ -1754,7 +1718,7 @@ var NEWSITEEDITOR = (function () {
 			});
 
 			uppy.on("file-added", (file) => {
-				console.log(file);
+				//console.log(file);
 				if (file.meta.fromServer) {
 					const objectURL = URL.createObjectURL(file.data);
 					posterPreview.src = objectURL;
@@ -1925,7 +1889,7 @@ var NEWSITEEDITOR = (function () {
 
 		if (!isVideoEditorSetup) {
 			setupEventListeners();
-			console.log("setup");
+			//console.log("setup");
 			isVideoEditorSetup = true;
 		}
 		document.getElementById(buttonId).setAttribute("data-movable", true);
@@ -1949,7 +1913,7 @@ var NEWSITEEDITOR = (function () {
 			.split(",")
 			.map((type) => type.trim().toLowerCase());
 
-			console.log(acceptedMimeTypes);
+			//console.log(acceptedMimeTypes);
 
 		tempInput.addEventListener("change", (e) => {
 			const file = e.target.files[0];
@@ -1965,6 +1929,7 @@ var NEWSITEEDITOR = (function () {
 				});
 
 				if (!isAccepted) {
+					//Get the error from uppy and inform the user the valid fileTypes.
 					uppyInstance.info(uppyInstance.i18n('youCanOnlyUploadFileTypes',
 					{types: acceptedMimeTypes.map((type) => {
   						const parts = type.split('/');
@@ -1994,7 +1959,6 @@ var NEWSITEEDITOR = (function () {
 
 	async function openFileBrowser(
 		containerId,
-		apiLink,
 		acceptedFileType,
 		fileDirectory
 	) {
@@ -2005,7 +1969,7 @@ var NEWSITEEDITOR = (function () {
 				acceptedTypeFiles: acceptedFileType,
 				lang: document.documentElement.lang.toLowerCase(),
 				onValidate(file) {
-					console.log(file);
+					//console.log(file);
 					resolve(file);
 					explorer.destroy();
 				},
@@ -2126,7 +2090,6 @@ var NEWSITEEDITOR = (function () {
 		try {
 			const selectedFile = await openFileBrowser(
 				fileBrowserId,
-				baseURL + "/rest/contexts/list/get.json",
 				acceptedTypes,
 				fileType
 			);