/** * * newsiteeditor.js - gestion de l'interface de NewSiteEditor * * Modifications: * - 2022/07/12 - V.1.0 0 - B.Bouaphakeo - création * - 2025/04/14 - V1.0.1 - E.Podvin - fix hover when siteeditor is activated with tinymce * - 2025/04/24 - V1.0.2 - C.Nelhomme - Fix prevent click problem and unclickable button + update Tinymce settings * * @copyright Intersel 2022 */ //debugger; import { Uppy, Dashboard, XHRUpload, } from "https://releases.transloadit.com/uppy/v4.13.3/uppy.min.mjs"; let current = {}; let NEWSITEEDITOR = (function () { let version = "1.0.2"; //set data-data for content and title function dataData(who) { var obj = {}; var title; var field = $(who).attr("field"); const sync = { syncsite: 1 }; //if(field === 'content'){ if (field !== "pagetitle" && field !== "longtitle") { if ( who.innerHTML === "" || who.innerHTML.includes('
') ) { obj[field] = who.innerHTML; //$(who).parents('.textcontent:first').hide(); //EPO: Why do we need to do that? } else { obj[field] = who.innerHTML; } } else { if ( who.innerHTML === "" || who.innerHTML.includes('
') ) { title = { longtitle: "--" }; $(who).parents(".equalHeight:first").hide(); } else { title = { longtitle: who.innerHTML }; } } Object.assign(obj, title); Object.assign(obj, sync); return $(who).attr("data-data", JSON.stringify(obj)); } // REST : PUT method to put the content of the website in the manager function saveContent() { $(".siteeditor").blur(function () { dataData(this); blapyTools.ajaxSimpleAction(this); }); } // Settings available for the richtext editor function TinyMCE() { // debugger; $.doTimeout("startTinyMCE", 200, function () { tinymce.init({ selector: ".siteeditor", inline: true, plugins: "link, textcolor, table, preview", menubar: false, toolbar: [ "undo redo | bold italic underline | link unlink | fontselect fontsizeselect", "forecolor backcolor | alignjustify alignleft aligncenter alignright alignfull | numlist bullist outdent indent | table | styleselect", ], fontsize_formats: "8px 10px 12px 14px 18px 24px 26px 34px", //, valid_elements: 'p[style],strong,em,span[style],a[href],ul,ol,li' //, valid_styles: {'*': 'font-size,font-family,color,text-decoration,text-align'} powerpaste_word_import: "clean", powerpaste_html_import: "clean", setup: function (editor) { editor.on("click", function (el) { el.preventDefault(); el.stopImmediatePropagation(); }); editor.on("keydown", function (el) { if ( el.code === "ArrowRight" || el.code === "ArrowLeft" || el.code === "ArrowUp" || el.code === "ArrowDown" ) { el.stopPropagation(); } }); }, }); //end init }); //end dotimeout } //REST : Get method to get the title of the resource in sidebar function getTitle(who) { var transform = $("h2").css("text-transform"); $(who).prepend( '

' ); hrefRestNse("#resourceName", "longtitle"); blapyTools.ajaxSimpleAction($("#resourceName"), function (data0) { var title = data0.data.title; if (title === "" || title == "--") { hrefRestNse("#resourceName", "pagetitle"); blapyTools.ajaxSimpleAction($("#resourceName"), function (data1) { var title1 = data1.data.title; $("#resourceName").html(title1); //console.log(title1+' '+3); }); } else { $("#resourceName").html(title); } }); } // to put href attribut in 'who' element, with 'what' tv and the element id in the url function hrefRestNse(who, what) { /*var pos1 = $('[status="selected"]').attr("class").indexOf("_", $('[status="selected"]').attr("class").indexOf("_") + 1)+1; var pos2 = $('[status="selected"]').attr("class").indexOf(" ") ; var currentId = $('[status="selected"]').attr("class").substring(pos1, pos2);*/ var currentId = $('[status="selected"]').attr("data-resourceid"); $(who).attr("href", "/rest/newsiteeditor/" + what + "/" + currentId); } // toggle from edit mode to view mode function view() { $("#view").click(function () { //remove attr conteneditable sinon bug avec tinyMCE tinymce.remove(); tinymce.activeEditor.remove(); $(".mce-item-table").removeClass("mce-item-table"); $("#undo, #redo, #sidebarToggle, #manager, #view").css( "visibility", "hidden" ); $("#wrapper").addClass("no-sidebar"); $("#edit").css("visibility", ""); $('[data-managerButton="true"]') .removeClass("selected") .removeAttr("status") .attr("data-managerbutton", "false"); //Disable all contentEditable added by tinyMce document.querySelectorAll("[contenteditable=true]").forEach((e) => { e.removeAttribute("contenteditable"); }); onClickManager(false); sidebar(false); linkEnabled(true); }); } //désactiver les liens href sur sections function linkEnabled(enableLink) { if (enableLink) { $(".siteeditor") .parents("[data-hrefSave]") .each(function () { $(this).attr("href", $(this).attr("data-hrefSave")); $(this).removeAttr("data-hrefSave"); }); } else { $(".siteeditor") .parents("[href]") .each(function () { $(this).attr("data-hrefSave", $(this).attr("href")); $(this).removeAttr("href"); }); } } function preventClickEnabled(enableClick) { let pointerEventsNoneClasses = document.getElementsByClassName( enableClick ? "pointer-events-none" : "edit-pointer-event" ); if (pointerEventsNoneClasses.length > 0) { if (enableClick) { Array.from( document.getElementsByClassName("pointer-events-none") ).forEach((e) => { e.classList.remove("pointer-events-none"); e.classList.add("edit-pointer-event"); }); } else { Array.from( document.getElementsByClassName("edit-pointer-event") ).forEach((e) => { e.classList.add("pointer-events-none"); e.classList.remove("edit-pointer-event"); }); } } } // toggle from view to edit mode function edit() { $("#edit").click(function () { TinyMCE(); $(this).css("visibility", "hidden"); $("#undo, #redo, #sidebarToggle, #view, #manager").css("visibility", ""); /*$('section').each(function(){ if ( $(this).attr("data-info") === "" ){ var pos1 = this.getAttribute("class").indexOf("_", this.getAttribute("class").indexOf("_") + 1)+1; var pos2 = this.getAttribute("class").indexOf(" ") ; var currentId = this.getAttribute("class").substring(pos1, pos2); $(this).attr("data-info","managerButton_"+currentId); } });*/ $('[data-resourceid!=""][data-resourceid]').attr( "data-managerbutton", "true" ); onClickManager(true); preventClickEnabled(true); sidebar(true); linkEnabled(false); }); } // to set data-data after clicking undo button function undo() { $("#undo").click(function () { $.doTimeout("TimeOut", 500, function () { $(".siteeditor").each(function () { var field = $(this).attr("field"); if ( $(this).attr("data-data") != '{"' + field + '":"' + this.innerHTML + '","syncsite":1}' ) { dataData(this); blapyTools.ajaxSimpleAction(this); } }); }); }); } // to set data-data after clicking redo button function redo() { $("#redo").click(function () { $.doTimeout("TimeOut", 500, function () { $(".siteeditor").each(function () { var field = $(this).attr("field"); if ( $(this).attr("data-data") != '{"' + field + '":"' + this.innerHTML + '","syncsite":1}' ) { dataData(this); blapyTools.ajaxSimpleAction(this); } }); }); }); } // onHover on content you can edit function onHover() { $(".siteeditor").hover( function () { //debugger; if ( $(this).attr("contenteditable") == "true" /*&& !($(this).is(":focus")) */ ) { $(this)[0].style.setProperty( "background-color", "rgb(0, 0, 255, 0.1)", "important" ); } }, function () { if ($(this).attr("contenteditable") == "true") { $(this).css("background-color", ""); } } ); } // onHover on resource you can edit function onHoverManager() { $("section").hover( function () { if ( this.getAttribute("data-managerbutton") && this.getAttribute("data-managerbutton") == "true" ) { $(this).addClass("selected"); } }, function () { if ( this.getAttribute("data-managerbutton") && this.getAttribute("data-managerbutton") == "true" && !this.getAttribute("status") ) { $(this).removeClass("selected"); } } ); } // display a manager button when clicking on a resource which redirected on the resource clicked manager function onClickManager(mode) { //debugger; if (mode) { $("#body").on("click.siteeditor", function (event) { event.stopPropagation(); $('[status="selected"]').removeClass("selected").removeAttr("status"); $("#manager").css("visibility", "hidden").removeAttr("onclick"); }); $("section").on("click.siteeditor", function (event) { //debugger; event.stopPropagation(); $('[status="selected"]').removeClass("selected").removeAttr("status"); $("#manager").css("visibility", "hidden").attr("onclick", ""); if ( this.getAttribute("data-managerbutton") && this.getAttribute("data-managerbutton") == "true" ) { $(this).addClass("selected"); $(this).attr("status", "selected"); var currentId = this.getAttribute("data-resourceid"); $("#manager").attr( "onclick", "window.open('/manager/?a=resource/update&id=" + currentId + "');" ); $("#manager").css("visibility", "visible"); } }); } else { $("#body").off("click.siteeditor"); $("section").off("click.siteeditor"); } } //toggle between sidebar and nosidebar function sidebarDisplay(boolean) { if (boolean) { $("#noResourceSelected").css("display", "none"); $("#resourceSelected").css("display", ""); } else { $("#noResourceSelected").css("display", ""); $("#resourceSelected").css("display", "none"); } } //When sidebar is open, display the info of the resource selected or "selectionner une ressource" if no resource selected function sidebar(mode) { if (mode) { $("#body").on("click.sidebar", function () { $("#resourceName").remove(); if ($("#wrapper").attr("class") !== "no-sidebar") { sidebarDisplay(false); } }); $("section").on("click.sidebar", function (e) { //debugger; e.stopPropagation(); $("#resourceName").remove(); if ( $("#wrapper").attr("class") !== "no-sidebar" && this.getAttribute("data-resourceid") ) { getTitle("#resourceSelected"); getNbColumn(); getWidth(); getPadding(); sidebarDisplay(true); } else { sidebarDisplay(false); } }); } else { $("#body").off("click.sidebar"); $("section").off("click.sidebar"); } } //Open the sidebar when clicking on the parameter button with the information of the current selected resource or "selectionner une resssource" if no selected resource function sidebarToggle() { $("#sidebarToggle").on("click", function () { if ($("#wrapper").attr("class") == "no-sidebar") { var compt = 0; $("#resourceName").remove(); $("section").each(function () { if ($(this).attr("status") == "selected") { compt += 1; } }); if (compt > 0) { getNbColumn(); getWidth(); getPadding(); getTitle("#resourceSelected"); sidebarDisplay(true); } else { sidebarDisplay(false); } } $("#wrapper").toggleClass("no-sidebar"); }); } // REST : get method to know how many column the resource already occupied function getNbColumn() { hrefRestNse("#column-select", "nbcolumn"); blapyTools.ajaxSimpleAction($("#column-select"), function (data) { var bootstrap = data.data.nb; var nbColumn; if (bootstrap === null) { nbColumn = 12; } else { pos3 = bootstrap.lastIndexOf("-"); nbColumn = bootstrap.substr(pos3 + 1); } $("#column-select").prop("value", nbColumn); }); } //REST : Method PUT when a new number of column is selected in sidebar // Change instantly the number of column on page function putNbColumn() { $("#column-select").change(function () { var valueSelected = $("#column-select").val(); var bootstrap; //version input number if (valueSelected < 1) { valueSelected = 1; $("#column-select").attr("value", valueSelected); bootstrap = "col-sm-1"; } else if (valueSelected >= 12) { valueSelected = 12; $("#column-select").attr("value", valueSelected); bootstrap = null; } else { $("#column-select").attr("value", valueSelected); bootstrap = "col-sm-" + valueSelected; } //envoyer le nombre de colonne au manager version number hrefRestNse('[for="column-select"]', "nbcolumn"); if (bootstrap === null) { $('[for="column-select"]').attr( "data-data", '{"tv_nbBootstrapColumn":' + bootstrap + ',"syncsite":1}' ); } else { $('[for="column-select"]').attr( "data-data", '{"tv_nbBootstrapColumn":"' + bootstrap + '","syncsite":1}' ); } blapyTools.ajaxSimpleAction($('[for="column-select"]')); //dynamique sur la page var pos3 = $('[status="selected"]').attr("class").indexOf("col-sm"); var pos4 = $('[status="selected"]') .attr("class") .indexOf(" ", pos3 + 1); var currentBootstrap = $('[status="selected"]') .attr("class") .substr(pos3, pos4 - pos3); if (currentBootstrap.startsWith("col-sm")) { $('[status="selected"]') .removeClass(currentBootstrap) .removeClass("selected"); } else { $('[status="selected"]').removeClass("selected"); } $('[status="selected"]').addClass(bootstrap).addClass("selected"); if (bootstrap === null) { $('[status="selected"]').css("clear", ""); } else { $('[status="selected"]').css("clear", "none"); } }); } //REST : get method to get the managers width in the sidebar function getWidth() { hrefRestNse("#width", "width"); blapyTools.ajaxSimpleAction($("#width"), function (data) { var percentage = data.data.width; //console.log(percentage); var width; if (percentage === null) { width = 100; } else { pos3 = percentage.indexOf("%"); width = percentage.substr(0, pos3); //console.log(width); } $("#width").prop("value", width); }); } //REST : Method PUT when a new number of column is selected in sidebar // Change instantly the number of column on page function putWidth() { $("#width").change(function () { var valueSelected = $("#width").val(); var percentage; //version input if (valueSelected < 10) { valueSelected = 10; $("#width").prop("value", valueSelected); percentage = "10%"; } else if (valueSelected > 100) { valueSelected = 100; $("#width").prop("value", valueSelected); percentage = "100%"; } else { $("#width").prop("value", valueSelected); percentage = valueSelected + "%"; } //envoyer le nombre de colonne au manager version number hrefRestNse('[for="width"]', "width"); if (percentage === "100%") { $('[for="width"]').attr( "data-data", '{"tv_sectionWidth":' + null + ',"syncsite":1}' ); } else { $('[for="width"]').attr( "data-data", '{"tv_sectionWidth":"' + percentage + '","syncsite":1}' ); } blapyTools.ajaxSimpleAction($('[for="width"]')); //dynamique sur la page $('[status="selected"]').find("div:first").css("width", percentage); }); } //REST : get method to get the manager padding in the sidebar function getPadding() { hrefRestNse("#paddingTop", "padding"); blapyTools.ajaxSimpleAction($("#paddingTop"), function (data) { var css = data.data.CSS; //console.log(css); var posTop1; var posTop2; var posTop3; var paddingTop; if (css !== null && css.includes("padding-top")) { css = css.replace(/ /g, ""); posTop1 = css.indexOf("padding-top"); posTop2 = css.indexOf(":", posTop1 + 1); posTop3 = css.indexOf(";", posTop1 + 1); paddingTop = css.substr(posTop2 + 1, posTop3 - posTop2 - 1); //console.log(paddingTop); //$('#paddingTop').prop('value',paddingTop); var paddingTop2; if (paddingTop.includes("px")) { $("#paddingTopUnit").prop("value", "px"); posTop4 = paddingTop.indexOf("px"); paddingTop2 = paddingTop.substr(0, posTop4); //console.log(paddingTop2); } else if (paddingTop.includes("%")) { $("#paddingTopUnit").prop("value", "%"); posTop4 = paddingTop.indexOf("%"); paddingTop2 = paddingTop.substr(0, posTop4); } $("#paddingTop").prop("value", paddingTop2); //console.log(paddingTop2); } else { $("#paddingTopUnit").prop("value", "px"); $("#paddingTop").prop("value", ""); } }); hrefRestNse("#paddingBottom", "padding"); blapyTools.ajaxSimpleAction($("#paddingBottom"), function (data) { var css = data.data.CSS; //console.log(css); var posBot1; var posBot2; var posBot3; var paddingBottom; if (css !== null && css.includes("padding-bottom")) { css = css.replace(/ /g, ""); posBot1 = css.indexOf("padding-bottom"); posBot2 = css.indexOf(":", posBot1 + 1); posBot3 = css.indexOf(";", posBot1 + 1); paddingBottom = css.substr(posBot2 + 1, posBot3 - posBot2 - 1); var paddingBottom2; if (paddingBottom.includes("px")) { posBot4 = paddingBottom.indexOf("px"); paddingBottom2 = paddingBottom.substr(0, posBot4); $("#paddingBottomUnit").prop("value", "px"); //console.log(paddingTop2); } else if (paddingBottom.includes("%")) { posBot4 = paddingBottom.indexOf("%"); paddingBottom2 = paddingBottom.substr(0, posBot4); $("#paddingBottomUnit").prop("value", "%"); } $("#paddingBottom").prop("value", paddingBottom2); } else { $("#paddingBottomUnit").prop("value", "px"); $("#paddingBottom").prop("value", ""); } }); } // REST : put method to put the sidebar value in the manager function putPadding() { $( "#paddingTop, #paddingBottom, #paddingTopUnit, #paddingBottomUnit" ).change(function () { let paddingTop = parseInt($("#paddingTop").val()); if (isNaN(paddingTop)) paddingTop = 0; let unitTop = $("#paddingTopUnit").val(); let paddingBottom = parseInt($("#paddingBottom").val()); if (isNaN(paddingBottom)) paddingBottom = 0; let unitBottom = $("#paddingBottomUnit").val(); if (paddingTop > 999) { paddingTop = 999; $("#paddingTop").prop("value", paddingTop); } else if (paddingTop < 0) { paddingTop = 0; $("#paddingTop").prop("value", paddingTop); } if (paddingBottom > 999) { paddingBottom = 999; $("#paddingBottom").prop("value", paddingBottom); } else if (paddingBottom < 0) { paddingBottom = 0; $("#paddingBottom").prop("value", paddingBottom); } //envoyer le nombre de colonne au manager version number hrefRestNse('[for="padding"]', "padding"); $('[for="padding"]').attr( "data-data", '{"tv_sectionCSS":"padding-top:' + paddingTop + unitTop + ";padding-bottom:" + paddingBottom + unitBottom + ';","syncsite":1}' ); if (paddingTop === 0 && paddingBottom === 0) { $('[for="padding"]').attr( "data-data", '{"tv_sectionCSS":"","syncsite":1}' ); } else if (paddingTop === 0) { $('[for="padding"]').attr( "data-data", '{"tv_sectionCSS":"padding-bottom:' + paddingBottom + unitBottom + ';","syncsite":1}' ); } blapyTools.ajaxSimpleAction($('[for="padding"]')); //dynamique sur la page if ( (paddingTop === 0 || paddingTop === "") && (paddingBottom === 0 || paddingBottom === "") ) { $('[status="selected"]').css("padding-top", ""); $('[status="selected"]').css("padding-bottom", ""); } else if (paddingTop === 0 || paddingTop === "") { $('[status="selected"]').css("padding-top", ""); $('[status="selected"]').css( "padding-bottom", paddingBottom + unitBottom ); } else if (paddingBottom === 0 || paddingBottom === "") { $('[status="selected"]').css("padding-top", paddingTop + unitTop); $('[status="selected"]').css("padding-bottom", ""); } else { $('[status="selected"]').css("padding-top", paddingTop + unitTop); $('[status="selected"]').css( "padding-bottom", paddingBottom + unitBottom ); } }); } let cropper = null; let pendingFile = null; let aspectRatio; function nseImageEditor() { const uppy = new Uppy({ restrictions: { maxNumberOfFiles: 1, allowedFileTypes: ['.jpg', '.jpeg', '.png', '.gif', '.webp'] }, }); uppy.use(Dashboard, { inline: true, target: "#uppy", showAddFilesButton: false, }); uppy.on("file-added", (file) => { if (file.meta.isCropped) { return; } pendingFile = file; openCropper(file); return false; }); uppy.on("complete", (file) => { showNseModal(false); uppy.cancelAll(); console.log(file); current.element.src = "http://localhost/php/uploads/"+file.successful[0].meta.name; }); //Initialiser l'endpoint pour l'upload des fichiers uppy.use(XHRUpload, { endpoint: "http://localhost/php/index.php" }); const customButton = document.createElement("button"); customButton.innerText = "Edit"; customButton.style.position = "absolute"; customButton.style.display = "none"; customButton.id = "editImageBtn"; document.body.append(customButton); customButton.onclick = () => showNseModal(true); const modal = document.getElementById("modalNseBackground"); modal.addEventListener("click", (e) => { if (e.target === modal) { showNseModal(false); closedModal(); } }); document.querySelectorAll(".imageeditor").forEach((el) => { el.addEventListener("click", (e) => { el.style.border = "5px solid pink"; if (el.naturalHeight === 0) { current.aspectRatio = 1; } else { current.aspectRatio = el.naturalWidth / el.naturalHeight; } current.element = el; customButton.style.top = e.clientY + 10 + "px"; customButton.style.left = e.clientX + 10 + "px"; customButton.style.display = "block"; }); }); document.getElementById("cropper-cancel").addEventListener("click", () => { document.getElementById("cropper-modal").classList.remove("active"); if (cropper) { cropper.destroy(); cropper = null; } pendingFile = null; }); document.getElementById("cropper-confirm").addEventListener("click", () => { if (!cropper || !pendingFile) return; cropper.getCroppedCanvas().toBlob((blob) => { uppy.removeFile(pendingFile.id); const ts = new Date() .toISOString() .replace(/[-:.]/g, '') .replace('Z', ''); uppy.addFile({ name: ts+'.png', type: pendingFile.type, data: blob, meta: { isCropped: true, }, }); // Nettoyage document.getElementById("cropper-modal").classList.remove("active"); cropper.destroy(); cropper = null; pendingFile = null; }, pendingFile.type); }); } function openCropper(file) { const reader = new FileReader(); reader.onload = (e) => { const cropperImage = document.getElementById("cropper-image"); cropperImage.src = e.target.result; document.getElementById("cropper-modal").classList.add("active"); if (cropper) { cropper.destroy(); } cropper = new Cropper(cropperImage, { viewMode: 1, aspectRatio: current.aspectRatio, autoCropArea: 1, }); }; reader.readAsDataURL(file.data); } function showNseModal(shouldShow) { const modal = document.getElementById("modalNseBackground"); modal.style.display = shouldShow ? "flex" : "none"; } // to set data-data for every element when opening the page $(".siteeditor").each(function () { dataData(this); }); //window.addEventListener('DOMContentLoaded', newsiteeditor, false); view(); edit(); onHover(); saveContent(); undo(); redo(); onHoverManager(); //onClickManager(); //sidebar(); sidebarToggle(); putNbColumn(); putWidth(); putPadding(); nseImageEditor(); //$('#body').off('click'); //$('section').off('click'); // define the public API var API = {}; API.version = version; // API.add = add; // API.get_array = get_array; return API; })();