index.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /**
  2. *
  3. * newsiteeditor.js - gestion de l'interface de NewSiteEditor
  4. *
  5. * Modifications:
  6. * - 2022/07/12 - V.1.0 0 - B.Bouaphakeo - création
  7. * - 2025/04/14 - V1.0.1 - E.Podvin - fix hover when siteeditor is activated with tinymce
  8. * - 2025/04/24 - V1.0.2 - C.Nelhomme - Fix prevent click problem and unclickable button + update Tinymce settings
  9. *
  10. * @copyright Intersel 2022
  11. */
  12. //debugger;
  13. import {
  14. Uppy,
  15. Dashboard,
  16. XHRUpload,
  17. } from "https://releases.transloadit.com/uppy/v4.13.3/uppy.min.mjs";
  18. let current = {};
  19. let NEWSITEEDITOR = (function () {
  20. let version = "1.0.2";
  21. //set data-data for content and title
  22. function dataData(who) {
  23. var obj = {};
  24. var title;
  25. var field = $(who).attr("field");
  26. const sync = { syncsite: 1 };
  27. //if(field === 'content'){
  28. if (field !== "pagetitle" && field !== "longtitle") {
  29. if (
  30. who.innerHTML === "" ||
  31. who.innerHTML.includes('<br data-mce-bogus="1">')
  32. ) {
  33. obj[field] = who.innerHTML;
  34. //$(who).parents('.textcontent:first').hide(); //EPO: Why do we need to do that?
  35. } else {
  36. obj[field] = who.innerHTML;
  37. }
  38. } else {
  39. if (
  40. who.innerHTML === "" ||
  41. who.innerHTML.includes('<br data-mce-bogus="1">')
  42. ) {
  43. title = { longtitle: "--" };
  44. $(who).parents(".equalHeight:first").hide();
  45. } else {
  46. title = { longtitle: who.innerHTML };
  47. }
  48. }
  49. Object.assign(obj, title);
  50. Object.assign(obj, sync);
  51. return $(who).attr("data-data", JSON.stringify(obj));
  52. }
  53. // REST : PUT method to put the content of the website in the manager
  54. function saveContent() {
  55. $(".siteeditor").blur(function () {
  56. dataData(this);
  57. blapyTools.ajaxSimpleAction(this);
  58. });
  59. }
  60. // Settings available for the richtext editor
  61. function TinyMCE() {
  62. // debugger;
  63. $.doTimeout("startTinyMCE", 200, function () {
  64. tinymce.init({
  65. selector: ".siteeditor",
  66. inline: true,
  67. plugins: "link, textcolor, table, preview",
  68. menubar: false,
  69. toolbar: [
  70. "undo redo | bold italic underline | link unlink | fontselect fontsizeselect",
  71. "forecolor backcolor | alignjustify alignleft aligncenter alignright alignfull | numlist bullist outdent indent | table | styleselect",
  72. ],
  73. fontsize_formats: "8px 10px 12px 14px 18px 24px 26px 34px",
  74. //, valid_elements: 'p[style],strong,em,span[style],a[href],ul,ol,li'
  75. //, valid_styles: {'*': 'font-size,font-family,color,text-decoration,text-align'}
  76. powerpaste_word_import: "clean",
  77. powerpaste_html_import: "clean",
  78. setup: function (editor) {
  79. editor.on("click", function (el) {
  80. el.preventDefault();
  81. el.stopImmediatePropagation();
  82. });
  83. editor.on("keydown", function (el) {
  84. if (
  85. el.code === "ArrowRight" ||
  86. el.code === "ArrowLeft" ||
  87. el.code === "ArrowUp" ||
  88. el.code === "ArrowDown"
  89. ) {
  90. el.stopPropagation();
  91. }
  92. });
  93. },
  94. }); //end init
  95. }); //end dotimeout
  96. }
  97. //REST : Get method to get the title of the resource in sidebar
  98. function getTitle(who) {
  99. var transform = $("h2").css("text-transform");
  100. $(who).prepend(
  101. '<p id="resourceName" method="GET" style="text-transform:' +
  102. transform +
  103. ';"></p>'
  104. );
  105. hrefRestNse("#resourceName", "longtitle");
  106. blapyTools.ajaxSimpleAction($("#resourceName"), function (data0) {
  107. var title = data0.data.title;
  108. if (title === "" || title == "--") {
  109. hrefRestNse("#resourceName", "pagetitle");
  110. blapyTools.ajaxSimpleAction($("#resourceName"), function (data1) {
  111. var title1 = data1.data.title;
  112. $("#resourceName").html(title1);
  113. //console.log(title1+' '+3);
  114. });
  115. } else {
  116. $("#resourceName").html(title);
  117. }
  118. });
  119. }
  120. // to put href attribut in 'who' element, with 'what' tv and the element id in the url
  121. function hrefRestNse(who, what) {
  122. /*var pos1 = $('[status="selected"]').attr("class").indexOf("_", $('[status="selected"]').attr("class").indexOf("_") + 1)+1;
  123. var pos2 = $('[status="selected"]').attr("class").indexOf(" ") ;
  124. var currentId = $('[status="selected"]').attr("class").substring(pos1, pos2);*/
  125. var currentId = $('[status="selected"]').attr("data-resourceid");
  126. $(who).attr("href", "/rest/newsiteeditor/" + what + "/" + currentId);
  127. }
  128. // toggle from edit mode to view mode
  129. function view() {
  130. $("#view").click(function () {
  131. //remove attr conteneditable sinon bug avec tinyMCE
  132. tinymce.remove();
  133. tinymce.activeEditor.remove();
  134. $(".mce-item-table").removeClass("mce-item-table");
  135. $("#undo, #redo, #sidebarToggle, #manager, #view").css(
  136. "visibility",
  137. "hidden"
  138. );
  139. $("#wrapper").addClass("no-sidebar");
  140. $("#edit").css("visibility", "");
  141. $('[data-managerButton="true"]')
  142. .removeClass("selected")
  143. .removeAttr("status")
  144. .attr("data-managerbutton", "false");
  145. //Disable all contentEditable added by tinyMce
  146. document.querySelectorAll("[contenteditable=true]").forEach((e) => {
  147. e.removeAttribute("contenteditable");
  148. });
  149. onClickManager(false);
  150. sidebar(false);
  151. linkEnabled(true);
  152. nseImageEditor(false);
  153. });
  154. }
  155. //désactiver les liens href sur sections
  156. function linkEnabled(enableLink) {
  157. if (enableLink) {
  158. $(".siteeditor")
  159. .parents("[data-hrefSave]")
  160. .each(function () {
  161. $(this).attr("href", $(this).attr("data-hrefSave"));
  162. $(this).removeAttr("data-hrefSave");
  163. });
  164. } else {
  165. $(".siteeditor")
  166. .parents("[href]")
  167. .each(function () {
  168. $(this).attr("data-hrefSave", $(this).attr("href"));
  169. $(this).removeAttr("href");
  170. });
  171. }
  172. }
  173. function preventClickEnabled(enableClick) {
  174. let pointerEventsNoneClasses = document.getElementsByClassName(
  175. enableClick ? "pointer-events-none" : "edit-pointer-event"
  176. );
  177. if (pointerEventsNoneClasses.length > 0) {
  178. if (enableClick) {
  179. Array.from(
  180. document.getElementsByClassName("pointer-events-none")
  181. ).forEach((e) => {
  182. e.classList.remove("pointer-events-none");
  183. e.classList.add("edit-pointer-event");
  184. });
  185. } else {
  186. Array.from(
  187. document.getElementsByClassName("edit-pointer-event")
  188. ).forEach((e) => {
  189. e.classList.add("pointer-events-none");
  190. e.classList.remove("edit-pointer-event");
  191. });
  192. }
  193. }
  194. }
  195. // toggle from view to edit mode
  196. function edit() {
  197. $("#edit").click(function () {
  198. TinyMCE();
  199. $(this).css("visibility", "hidden");
  200. $("#undo, #redo, #sidebarToggle, #view, #manager").css("visibility", "");
  201. /*$('section').each(function(){
  202. if ( $(this).attr("data-info") === "" ){
  203. var pos1 = this.getAttribute("class").indexOf("_", this.getAttribute("class").indexOf("_") + 1)+1;
  204. var pos2 = this.getAttribute("class").indexOf(" ") ;
  205. var currentId = this.getAttribute("class").substring(pos1, pos2);
  206. $(this).attr("data-info","managerButton_"+currentId);
  207. }
  208. });*/
  209. $('[data-resourceid!=""][data-resourceid]').attr(
  210. "data-managerbutton",
  211. "true"
  212. );
  213. onClickManager(true);
  214. preventClickEnabled(true);
  215. sidebar(true);
  216. linkEnabled(false);
  217. nseImageEditor(true);
  218. });
  219. }
  220. // to set data-data after clicking undo button
  221. function undo() {
  222. $("#undo").click(function () {
  223. $.doTimeout("TimeOut", 500, function () {
  224. $(".siteeditor").each(function () {
  225. var field = $(this).attr("field");
  226. if (
  227. $(this).attr("data-data") !=
  228. '{"' + field + '":"' + this.innerHTML + '","syncsite":1}'
  229. ) {
  230. dataData(this);
  231. blapyTools.ajaxSimpleAction(this);
  232. }
  233. });
  234. });
  235. });
  236. }
  237. // to set data-data after clicking redo button
  238. function redo() {
  239. $("#redo").click(function () {
  240. $.doTimeout("TimeOut", 500, function () {
  241. $(".siteeditor").each(function () {
  242. var field = $(this).attr("field");
  243. if (
  244. $(this).attr("data-data") !=
  245. '{"' + field + '":"' + this.innerHTML + '","syncsite":1}'
  246. ) {
  247. dataData(this);
  248. blapyTools.ajaxSimpleAction(this);
  249. }
  250. });
  251. });
  252. });
  253. }
  254. // onHover on content you can edit
  255. function onHover() {
  256. $(".siteeditor").hover(
  257. function () {
  258. //debugger;
  259. if (
  260. $(this).attr("contenteditable") ==
  261. "true" /*&& !($(this).is(":focus")) */
  262. ) {
  263. $(this)[0].style.setProperty(
  264. "background-color",
  265. "rgb(0, 0, 255, 0.1)",
  266. "important"
  267. );
  268. }
  269. },
  270. function () {
  271. if ($(this).attr("contenteditable") == "true") {
  272. $(this).css("background-color", "");
  273. }
  274. }
  275. );
  276. }
  277. // onHover on resource you can edit
  278. function onHoverManager() {
  279. $("section").hover(
  280. function () {
  281. if (
  282. this.getAttribute("data-managerbutton") &&
  283. this.getAttribute("data-managerbutton") == "true"
  284. ) {
  285. $(this).addClass("selected");
  286. }
  287. },
  288. function () {
  289. if (
  290. this.getAttribute("data-managerbutton") &&
  291. this.getAttribute("data-managerbutton") == "true" &&
  292. !this.getAttribute("status")
  293. ) {
  294. $(this).removeClass("selected");
  295. }
  296. }
  297. );
  298. }
  299. // display a manager button when clicking on a resource which redirected on the resource clicked manager
  300. function onClickManager(mode) {
  301. //debugger;
  302. if (mode) {
  303. $("#body").on("click.siteeditor", function (event) {
  304. event.stopPropagation();
  305. $('[status="selected"]').removeClass("selected").removeAttr("status");
  306. $("#manager").css("visibility", "hidden").removeAttr("onclick");
  307. });
  308. $("section").on("click.siteeditor", function (event) {
  309. //debugger;
  310. event.stopPropagation();
  311. $('[status="selected"]').removeClass("selected").removeAttr("status");
  312. $("#manager").css("visibility", "hidden").attr("onclick", "");
  313. if (
  314. this.getAttribute("data-managerbutton") &&
  315. this.getAttribute("data-managerbutton") == "true"
  316. ) {
  317. $(this).addClass("selected");
  318. $(this).attr("status", "selected");
  319. var currentId = this.getAttribute("data-resourceid");
  320. $("#manager").attr(
  321. "onclick",
  322. "window.open('/manager/?a=resource/update&id=" + currentId + "');"
  323. );
  324. $("#manager").css("visibility", "visible");
  325. }
  326. });
  327. } else {
  328. $("#body").off("click.siteeditor");
  329. $("section").off("click.siteeditor");
  330. }
  331. }
  332. //toggle between sidebar and nosidebar
  333. function sidebarDisplay(boolean) {
  334. if (boolean) {
  335. $("#noResourceSelected").css("display", "none");
  336. $("#resourceSelected").css("display", "");
  337. } else {
  338. $("#noResourceSelected").css("display", "");
  339. $("#resourceSelected").css("display", "none");
  340. }
  341. }
  342. //When sidebar is open, display the info of the resource selected or "selectionner une ressource" if no resource selected
  343. function sidebar(mode) {
  344. if (mode) {
  345. $("#body").on("click.sidebar", function () {
  346. $("#resourceName").remove();
  347. if ($("#wrapper").attr("class") !== "no-sidebar") {
  348. sidebarDisplay(false);
  349. }
  350. });
  351. $("section").on("click.sidebar", function (e) {
  352. //debugger;
  353. e.stopPropagation();
  354. $("#resourceName").remove();
  355. if (
  356. $("#wrapper").attr("class") !== "no-sidebar" &&
  357. this.getAttribute("data-resourceid")
  358. ) {
  359. getTitle("#resourceSelected");
  360. getNbColumn();
  361. getWidth();
  362. getPadding();
  363. sidebarDisplay(true);
  364. } else {
  365. sidebarDisplay(false);
  366. }
  367. });
  368. } else {
  369. $("#body").off("click.sidebar");
  370. $("section").off("click.sidebar");
  371. }
  372. }
  373. //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
  374. function sidebarToggle() {
  375. $("#sidebarToggle").on("click", function () {
  376. if ($("#wrapper").attr("class") == "no-sidebar") {
  377. var compt = 0;
  378. $("#resourceName").remove();
  379. $("section").each(function () {
  380. if ($(this).attr("status") == "selected") {
  381. compt += 1;
  382. }
  383. });
  384. if (compt > 0) {
  385. getNbColumn();
  386. getWidth();
  387. getPadding();
  388. getTitle("#resourceSelected");
  389. sidebarDisplay(true);
  390. } else {
  391. sidebarDisplay(false);
  392. }
  393. }
  394. $("#wrapper").toggleClass("no-sidebar");
  395. });
  396. }
  397. // REST : get method to know how many column the resource already occupied
  398. function getNbColumn() {
  399. hrefRestNse("#column-select", "nbcolumn");
  400. blapyTools.ajaxSimpleAction($("#column-select"), function (data) {
  401. var bootstrap = data.data.nb;
  402. var nbColumn;
  403. if (bootstrap === null) {
  404. nbColumn = 12;
  405. } else {
  406. pos3 = bootstrap.lastIndexOf("-");
  407. nbColumn = bootstrap.substr(pos3 + 1);
  408. }
  409. $("#column-select").prop("value", nbColumn);
  410. });
  411. }
  412. //REST : Method PUT when a new number of column is selected in sidebar
  413. // Change instantly the number of column on page
  414. function putNbColumn() {
  415. $("#column-select").change(function () {
  416. var valueSelected = $("#column-select").val();
  417. var bootstrap;
  418. //version input number
  419. if (valueSelected < 1) {
  420. valueSelected = 1;
  421. $("#column-select").attr("value", valueSelected);
  422. bootstrap = "col-sm-1";
  423. } else if (valueSelected >= 12) {
  424. valueSelected = 12;
  425. $("#column-select").attr("value", valueSelected);
  426. bootstrap = null;
  427. } else {
  428. $("#column-select").attr("value", valueSelected);
  429. bootstrap = "col-sm-" + valueSelected;
  430. }
  431. //envoyer le nombre de colonne au manager version number
  432. hrefRestNse('[for="column-select"]', "nbcolumn");
  433. if (bootstrap === null) {
  434. $('[for="column-select"]').attr(
  435. "data-data",
  436. '{"tv_nbBootstrapColumn":' + bootstrap + ',"syncsite":1}'
  437. );
  438. } else {
  439. $('[for="column-select"]').attr(
  440. "data-data",
  441. '{"tv_nbBootstrapColumn":"' + bootstrap + '","syncsite":1}'
  442. );
  443. }
  444. blapyTools.ajaxSimpleAction($('[for="column-select"]'));
  445. //dynamique sur la page
  446. var pos3 = $('[status="selected"]').attr("class").indexOf("col-sm");
  447. var pos4 = $('[status="selected"]')
  448. .attr("class")
  449. .indexOf(" ", pos3 + 1);
  450. var currentBootstrap = $('[status="selected"]')
  451. .attr("class")
  452. .substr(pos3, pos4 - pos3);
  453. if (currentBootstrap.startsWith("col-sm")) {
  454. $('[status="selected"]')
  455. .removeClass(currentBootstrap)
  456. .removeClass("selected");
  457. } else {
  458. $('[status="selected"]').removeClass("selected");
  459. }
  460. $('[status="selected"]').addClass(bootstrap).addClass("selected");
  461. if (bootstrap === null) {
  462. $('[status="selected"]').css("clear", "");
  463. } else {
  464. $('[status="selected"]').css("clear", "none");
  465. }
  466. });
  467. }
  468. //REST : get method to get the managers width in the sidebar
  469. function getWidth() {
  470. hrefRestNse("#width", "width");
  471. blapyTools.ajaxSimpleAction($("#width"), function (data) {
  472. var percentage = data.data.width;
  473. //console.log(percentage);
  474. var width;
  475. if (percentage === null) {
  476. width = 100;
  477. } else {
  478. pos3 = percentage.indexOf("%");
  479. width = percentage.substr(0, pos3);
  480. //console.log(width);
  481. }
  482. $("#width").prop("value", width);
  483. });
  484. }
  485. //REST : Method PUT when a new number of column is selected in sidebar
  486. // Change instantly the number of column on page
  487. function putWidth() {
  488. $("#width").change(function () {
  489. var valueSelected = $("#width").val();
  490. var percentage;
  491. //version input
  492. if (valueSelected < 10) {
  493. valueSelected = 10;
  494. $("#width").prop("value", valueSelected);
  495. percentage = "10%";
  496. } else if (valueSelected > 100) {
  497. valueSelected = 100;
  498. $("#width").prop("value", valueSelected);
  499. percentage = "100%";
  500. } else {
  501. $("#width").prop("value", valueSelected);
  502. percentage = valueSelected + "%";
  503. }
  504. //envoyer le nombre de colonne au manager version number
  505. hrefRestNse('[for="width"]', "width");
  506. if (percentage === "100%") {
  507. $('[for="width"]').attr(
  508. "data-data",
  509. '{"tv_sectionWidth":' + null + ',"syncsite":1}'
  510. );
  511. } else {
  512. $('[for="width"]').attr(
  513. "data-data",
  514. '{"tv_sectionWidth":"' + percentage + '","syncsite":1}'
  515. );
  516. }
  517. blapyTools.ajaxSimpleAction($('[for="width"]'));
  518. //dynamique sur la page
  519. $('[status="selected"]').find("div:first").css("width", percentage);
  520. });
  521. }
  522. //REST : get method to get the manager padding in the sidebar
  523. function getPadding() {
  524. hrefRestNse("#paddingTop", "padding");
  525. blapyTools.ajaxSimpleAction($("#paddingTop"), function (data) {
  526. var css = data.data.CSS;
  527. //console.log(css);
  528. var posTop1;
  529. var posTop2;
  530. var posTop3;
  531. var paddingTop;
  532. if (css !== null && css.includes("padding-top")) {
  533. css = css.replace(/ /g, "");
  534. posTop1 = css.indexOf("padding-top");
  535. posTop2 = css.indexOf(":", posTop1 + 1);
  536. posTop3 = css.indexOf(";", posTop1 + 1);
  537. paddingTop = css.substr(posTop2 + 1, posTop3 - posTop2 - 1);
  538. //console.log(paddingTop);
  539. //$('#paddingTop').prop('value',paddingTop);
  540. var paddingTop2;
  541. if (paddingTop.includes("px")) {
  542. $("#paddingTopUnit").prop("value", "px");
  543. posTop4 = paddingTop.indexOf("px");
  544. paddingTop2 = paddingTop.substr(0, posTop4);
  545. //console.log(paddingTop2);
  546. } else if (paddingTop.includes("%")) {
  547. $("#paddingTopUnit").prop("value", "%");
  548. posTop4 = paddingTop.indexOf("%");
  549. paddingTop2 = paddingTop.substr(0, posTop4);
  550. }
  551. $("#paddingTop").prop("value", paddingTop2);
  552. //console.log(paddingTop2);
  553. } else {
  554. $("#paddingTopUnit").prop("value", "px");
  555. $("#paddingTop").prop("value", "");
  556. }
  557. });
  558. hrefRestNse("#paddingBottom", "padding");
  559. blapyTools.ajaxSimpleAction($("#paddingBottom"), function (data) {
  560. var css = data.data.CSS;
  561. //console.log(css);
  562. var posBot1;
  563. var posBot2;
  564. var posBot3;
  565. var paddingBottom;
  566. if (css !== null && css.includes("padding-bottom")) {
  567. css = css.replace(/ /g, "");
  568. posBot1 = css.indexOf("padding-bottom");
  569. posBot2 = css.indexOf(":", posBot1 + 1);
  570. posBot3 = css.indexOf(";", posBot1 + 1);
  571. paddingBottom = css.substr(posBot2 + 1, posBot3 - posBot2 - 1);
  572. var paddingBottom2;
  573. if (paddingBottom.includes("px")) {
  574. posBot4 = paddingBottom.indexOf("px");
  575. paddingBottom2 = paddingBottom.substr(0, posBot4);
  576. $("#paddingBottomUnit").prop("value", "px");
  577. //console.log(paddingTop2);
  578. } else if (paddingBottom.includes("%")) {
  579. posBot4 = paddingBottom.indexOf("%");
  580. paddingBottom2 = paddingBottom.substr(0, posBot4);
  581. $("#paddingBottomUnit").prop("value", "%");
  582. }
  583. $("#paddingBottom").prop("value", paddingBottom2);
  584. } else {
  585. $("#paddingBottomUnit").prop("value", "px");
  586. $("#paddingBottom").prop("value", "");
  587. }
  588. });
  589. }
  590. // REST : put method to put the sidebar value in the manager
  591. function putPadding() {
  592. $(
  593. "#paddingTop, #paddingBottom, #paddingTopUnit, #paddingBottomUnit"
  594. ).change(function () {
  595. let paddingTop = parseInt($("#paddingTop").val());
  596. if (isNaN(paddingTop)) paddingTop = 0;
  597. let unitTop = $("#paddingTopUnit").val();
  598. let paddingBottom = parseInt($("#paddingBottom").val());
  599. if (isNaN(paddingBottom)) paddingBottom = 0;
  600. let unitBottom = $("#paddingBottomUnit").val();
  601. if (paddingTop > 999) {
  602. paddingTop = 999;
  603. $("#paddingTop").prop("value", paddingTop);
  604. } else if (paddingTop < 0) {
  605. paddingTop = 0;
  606. $("#paddingTop").prop("value", paddingTop);
  607. }
  608. if (paddingBottom > 999) {
  609. paddingBottom = 999;
  610. $("#paddingBottom").prop("value", paddingBottom);
  611. } else if (paddingBottom < 0) {
  612. paddingBottom = 0;
  613. $("#paddingBottom").prop("value", paddingBottom);
  614. }
  615. //envoyer le nombre de colonne au manager version number
  616. hrefRestNse('[for="padding"]', "padding");
  617. $('[for="padding"]').attr(
  618. "data-data",
  619. '{"tv_sectionCSS":"padding-top:' +
  620. paddingTop +
  621. unitTop +
  622. ";padding-bottom:" +
  623. paddingBottom +
  624. unitBottom +
  625. ';","syncsite":1}'
  626. );
  627. if (paddingTop === 0 && paddingBottom === 0) {
  628. $('[for="padding"]').attr(
  629. "data-data",
  630. '{"tv_sectionCSS":"","syncsite":1}'
  631. );
  632. } else if (paddingTop === 0) {
  633. $('[for="padding"]').attr(
  634. "data-data",
  635. '{"tv_sectionCSS":"padding-bottom:' +
  636. paddingBottom +
  637. unitBottom +
  638. ';","syncsite":1}'
  639. );
  640. }
  641. blapyTools.ajaxSimpleAction($('[for="padding"]'));
  642. //dynamique sur la page
  643. if (
  644. (paddingTop === 0 || paddingTop === "") &&
  645. (paddingBottom === 0 || paddingBottom === "")
  646. ) {
  647. $('[status="selected"]').css("padding-top", "");
  648. $('[status="selected"]').css("padding-bottom", "");
  649. } else if (paddingTop === 0 || paddingTop === "") {
  650. $('[status="selected"]').css("padding-top", "");
  651. $('[status="selected"]').css(
  652. "padding-bottom",
  653. paddingBottom + unitBottom
  654. );
  655. } else if (paddingBottom === 0 || paddingBottom === "") {
  656. $('[status="selected"]').css("padding-top", paddingTop + unitTop);
  657. $('[status="selected"]').css("padding-bottom", "");
  658. } else {
  659. $('[status="selected"]').css("padding-top", paddingTop + unitTop);
  660. $('[status="selected"]').css(
  661. "padding-bottom",
  662. paddingBottom + unitBottom
  663. );
  664. }
  665. });
  666. }
  667. //nseImageEditor
  668. let cropper = null;
  669. let pendingFile = null;
  670. let aspectRatio;
  671. function nseImageEditor(active) {
  672. if (!active) {
  673. return;
  674. }
  675. const uppy = new Uppy({
  676. restrictions: {
  677. maxNumberOfFiles: 1,
  678. allowedFileTypes: [".jpg", ".jpeg", ".png", ".gif", ".webp"],
  679. },
  680. });
  681. uppy.use(Dashboard, {
  682. inline: true,
  683. target: "#uppy",
  684. showAddFilesButton: false,
  685. });
  686. uppy.on("file-added", (file) => {
  687. if (file.meta.isCropped) {
  688. return;
  689. }
  690. pendingFile = file;
  691. openCropper(file);
  692. return false;
  693. });
  694. uppy.on("complete", (file) => {
  695. showNseModal(false);
  696. uppy.cancelAll();
  697. console.log(file);
  698. current.element.src =
  699. "http://localhost/php/uploads/" + file.successful[0].meta.name;
  700. });
  701. //Initialiser l'endpoint pour l'upload des fichiers
  702. uppy.use(XHRUpload, { endpoint: "http://localhost/php/index.php" });
  703. const customButton = document.createElement("button");
  704. customButton.innerText = "Edit";
  705. customButton.style.position = "absolute";
  706. customButton.style.display = "none";
  707. customButton.id = "editImageBtn";
  708. document.body.append(customButton);
  709. customButton.onclick = () => showNseModal(true);
  710. const modal = document.getElementById("modalNseBackground");
  711. modal.addEventListener("click", (e) => {
  712. if (e.target === modal) {
  713. showNseModal(false);
  714. }
  715. });
  716. document.addEventListener("click", (event) => {
  717. if (!event.target.classList.contains("imageeditor")) {
  718. customButton.style.display = "none";
  719. document.querySelectorAll(".imageeditor").forEach((el) => {
  720. el.classList.remove("selected-img");
  721. });
  722. }
  723. });
  724. document.querySelectorAll(".imageeditor").forEach((el) => {
  725. el.style.cursor = "pointer";
  726. el.addEventListener("click", (e) => {
  727. const prev = document.querySelector(".imageeditor.selected-img");
  728. if (prev) prev.classList.remove("selected-img");
  729. el.classList.add("selected-img");
  730. if (el.naturalHeight === 0) {
  731. current.aspectRatio = 1;
  732. } else {
  733. current.aspectRatio = el.naturalWidth / el.naturalHeight;
  734. }
  735. current.element = el;
  736. customButton.style.top = e.pageY + 10 + "px";
  737. customButton.style.left = e.pageX + 10 + "px";
  738. customButton.style.display = "block";
  739. });
  740. });
  741. document.getElementById("cropper-cancel").addEventListener("click", () => {
  742. document.getElementById("cropper-modal").classList.remove("active");
  743. if (cropper) {
  744. cropper.destroy();
  745. cropper = null;
  746. }
  747. pendingFile = null;
  748. });
  749. document.getElementById("cropper-confirm").addEventListener("click", () => {
  750. if (!cropper || !pendingFile) return;
  751. cropper.getCroppedCanvas().toBlob((blob) => {
  752. uppy.removeFile(pendingFile.id);
  753. const ts = new Date()
  754. .toISOString()
  755. .replace(/[-:.]/g, "")
  756. .replace("Z", "");
  757. uppy.addFile({
  758. name: ts + ".png",
  759. type: pendingFile.type,
  760. data: blob,
  761. meta: {
  762. isCropped: true,
  763. },
  764. });
  765. // Nettoyage
  766. document.getElementById("cropper-modal").classList.remove("active");
  767. cropper.destroy();
  768. cropper = null;
  769. pendingFile = null;
  770. }, pendingFile.type);
  771. });
  772. }
  773. function openCropper(file) {
  774. const reader = new FileReader();
  775. reader.onload = (e) => {
  776. const cropperImage = document.getElementById("cropper-image");
  777. cropperImage.src = e.target.result;
  778. document.getElementById("cropper-modal").classList.add("active");
  779. if (cropper) {
  780. cropper.destroy();
  781. }
  782. cropper = new Cropper(cropperImage, {
  783. viewMode: 1,
  784. aspectRatio: current.aspectRatio,
  785. autoCropArea: 1,
  786. });
  787. };
  788. reader.readAsDataURL(file.data);
  789. }
  790. function showNseModal(shouldShow) {
  791. const modal = document.getElementById("modalNseBackground");
  792. modal.style.display = shouldShow ? "flex" : "none";
  793. }
  794. //Fin du nseImageEditor
  795. //nseBgImageEditor
  796. //nseBgImageEditor
  797. function nseBgImageEditor() {
  798. const elems = document.querySelectorAll(".bgimageeditor");
  799. elems.forEach((elem) => {
  800. // 1) Extraction de l'URL du background
  801. elem.addEventListener(
  802. "click",
  803. (e) => {
  804. elem.style.borderTop = "10px solid red";
  805. elem.style.borderBottom = "10px solid red";
  806. const bg = getComputedStyle(elem).backgroundImage; // ex: 'url("…/votre-image.jpg")'
  807. const urlMatch = bg.match(/url\(["']?(.*?)["']?\)/);
  808. if (!urlMatch) return; // pas d'image → on skip
  809. const imgUrl = urlMatch[1];
  810. // 2) Chargement de l'image pour lire sa taille naturelle
  811. const img = new Image();
  812. let Iw, Ih;
  813. img.onload = () => {
  814. Iw = img.naturalWidth;
  815. Ih = img.naturalHeight;
  816. elem.style.backgroundSize = "cover";
  817. };
  818. img.src = imgUrl;
  819. // 3) Variables de drag
  820. let isDragging = false;
  821. let startX, startY;
  822. let origX = 0,
  823. origY = 0;
  824. //Gestion du zoom
  825. let zoom = 1;
  826. const zoomStep = 0.05;
  827. const minZoom = 0.5;
  828. const maxZoom = 3;
  829. // Récupérer la pos courante (px → nombre)
  830. function getBgPos() {
  831. const [x, y] = getComputedStyle(elem).backgroundPosition.split(" ");
  832. return [parseFloat(x) || 0, parseFloat(y) || 0];
  833. }
  834. function calculateBackgroundSize(zoom, size, elem) {
  835. elem.style.backgroundSize = zoom * size + "px";
  836. console.log(zoom * size);
  837. }
  838. elem.addEventListener("mousedown", (e) => {
  839. e.preventDefault();
  840. isDragging = true;
  841. [origX, origY] = getBgPos();
  842. startX = e.clientX;
  843. startY = e.clientY;
  844. elem.style.cursor = "grabbing";
  845. // on fixe no-repeat pour éviter la répétition
  846. elem.style.backgroundRepeat = "no-repeat";
  847. });
  848. document.addEventListener("mousemove", (e) => {
  849. if (!isDragging) return;
  850. const dx = e.clientX - startX;
  851. const dy = e.clientY - startY;
  852. let nx = origX + dx;
  853. let ny = origY + dy;
  854. // 4) Clamp : une fois l'image chargée
  855. const Cw = elem.clientWidth;
  856. const Ch = elem.clientHeight;
  857. if (Iw && Ih) {
  858. console.log(
  859. `Cadre largeur : ${Cw}, Cadre hauteur : ${Ch}\nImage largeur ${Iw}, Image hauteur ${Ih}`
  860. );
  861. // bornes horizontales
  862. /*
  863. * Ih et Iw : Height et width de l'image d'origine (source)
  864. * Ch et Cw : Height et width du cadre (canvas)
  865. * minX : Taille minimale de la position du background-position sur l'axe horizontal en px.
  866. * maxX : Taille maximale de la position du background-position sur l'axe horizontal en px.
  867. * minY : Taille minimale de la position du background-position sur l'axe vertical en px.
  868. * maxY : Taille minimale de la position du background-position sur l'axe vertical en px.
  869. * nx et ny : Position du background-position manipulé par l'utilisateur.
  870. */
  871. const minX = Math.min(Cw - Iw * zoom, 0);
  872. const maxX = 0;
  873. // bornes verticales
  874. const minY = Math.min(Ch - Ih * zoom, 0);
  875. const maxY = 0;
  876. // clamp X et Y
  877. nx = Math.max(minX, Math.min(maxX, nx));
  878. ny = Math.max(minY, Math.min(maxY, ny));
  879. }
  880. elem.style.backgroundPosition = `${0}px ${ny}px`;
  881. });
  882. document.addEventListener("mouseup", () => {
  883. if (!isDragging) return;
  884. isDragging = false;
  885. elem.style.cursor = "";
  886. });
  887. // éviter la sélection de texte/image native
  888. elem.addEventListener("dragstart", (e) => e.preventDefault());
  889. },
  890. { once: true }
  891. );
  892. });
  893. }
  894. // to set data-data for every element when opening the page
  895. $(".siteeditor").each(function () {
  896. dataData(this);
  897. });
  898. //window.addEventListener('DOMContentLoaded', newsiteeditor, false);
  899. view();
  900. edit();
  901. onHover();
  902. saveContent();
  903. undo();
  904. redo();
  905. onHoverManager();
  906. //onClickManager();
  907. //sidebar();
  908. sidebarToggle();
  909. putNbColumn();
  910. putWidth();
  911. putPadding();
  912. //$('#body').off('click');
  913. //$('section').off('click');
  914. //nseFileEditor
  915. //nseImageEditor(false);
  916. nseBgImageEditor();
  917. // define the public API
  918. var API = {};
  919. API.version = version;
  920. // API.add = add;
  921. // API.get_array = get_array;
  922. return API;
  923. })();