index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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. });
  153. }
  154. //désactiver les liens href sur sections
  155. function linkEnabled(enableLink) {
  156. if (enableLink) {
  157. $(".siteeditor")
  158. .parents("[data-hrefSave]")
  159. .each(function () {
  160. $(this).attr("href", $(this).attr("data-hrefSave"));
  161. $(this).removeAttr("data-hrefSave");
  162. });
  163. } else {
  164. $(".siteeditor")
  165. .parents("[href]")
  166. .each(function () {
  167. $(this).attr("data-hrefSave", $(this).attr("href"));
  168. $(this).removeAttr("href");
  169. });
  170. }
  171. }
  172. function preventClickEnabled(enableClick) {
  173. let pointerEventsNoneClasses = document.getElementsByClassName(
  174. enableClick ? "pointer-events-none" : "edit-pointer-event"
  175. );
  176. if (pointerEventsNoneClasses.length > 0) {
  177. if (enableClick) {
  178. Array.from(
  179. document.getElementsByClassName("pointer-events-none")
  180. ).forEach((e) => {
  181. e.classList.remove("pointer-events-none");
  182. e.classList.add("edit-pointer-event");
  183. });
  184. } else {
  185. Array.from(
  186. document.getElementsByClassName("edit-pointer-event")
  187. ).forEach((e) => {
  188. e.classList.add("pointer-events-none");
  189. e.classList.remove("edit-pointer-event");
  190. });
  191. }
  192. }
  193. }
  194. // toggle from view to edit mode
  195. function edit() {
  196. $("#edit").click(function () {
  197. TinyMCE();
  198. $(this).css("visibility", "hidden");
  199. $("#undo, #redo, #sidebarToggle, #view, #manager").css("visibility", "");
  200. /*$('section').each(function(){
  201. if ( $(this).attr("data-info") === "" ){
  202. var pos1 = this.getAttribute("class").indexOf("_", this.getAttribute("class").indexOf("_") + 1)+1;
  203. var pos2 = this.getAttribute("class").indexOf(" ") ;
  204. var currentId = this.getAttribute("class").substring(pos1, pos2);
  205. $(this).attr("data-info","managerButton_"+currentId);
  206. }
  207. });*/
  208. $('[data-resourceid!=""][data-resourceid]').attr(
  209. "data-managerbutton",
  210. "true"
  211. );
  212. onClickManager(true);
  213. preventClickEnabled(true);
  214. sidebar(true);
  215. linkEnabled(false);
  216. });
  217. }
  218. // to set data-data after clicking undo button
  219. function undo() {
  220. $("#undo").click(function () {
  221. $.doTimeout("TimeOut", 500, function () {
  222. $(".siteeditor").each(function () {
  223. var field = $(this).attr("field");
  224. if (
  225. $(this).attr("data-data") !=
  226. '{"' + field + '":"' + this.innerHTML + '","syncsite":1}'
  227. ) {
  228. dataData(this);
  229. blapyTools.ajaxSimpleAction(this);
  230. }
  231. });
  232. });
  233. });
  234. }
  235. // to set data-data after clicking redo button
  236. function redo() {
  237. $("#redo").click(function () {
  238. $.doTimeout("TimeOut", 500, function () {
  239. $(".siteeditor").each(function () {
  240. var field = $(this).attr("field");
  241. if (
  242. $(this).attr("data-data") !=
  243. '{"' + field + '":"' + this.innerHTML + '","syncsite":1}'
  244. ) {
  245. dataData(this);
  246. blapyTools.ajaxSimpleAction(this);
  247. }
  248. });
  249. });
  250. });
  251. }
  252. // onHover on content you can edit
  253. function onHover() {
  254. $(".siteeditor").hover(
  255. function () {
  256. //debugger;
  257. if (
  258. $(this).attr("contenteditable") ==
  259. "true" /*&& !($(this).is(":focus")) */
  260. ) {
  261. $(this)[0].style.setProperty(
  262. "background-color",
  263. "rgb(0, 0, 255, 0.1)",
  264. "important"
  265. );
  266. }
  267. },
  268. function () {
  269. if ($(this).attr("contenteditable") == "true") {
  270. $(this).css("background-color", "");
  271. }
  272. }
  273. );
  274. }
  275. // onHover on resource you can edit
  276. function onHoverManager() {
  277. $("section").hover(
  278. function () {
  279. if (
  280. this.getAttribute("data-managerbutton") &&
  281. this.getAttribute("data-managerbutton") == "true"
  282. ) {
  283. $(this).addClass("selected");
  284. }
  285. },
  286. function () {
  287. if (
  288. this.getAttribute("data-managerbutton") &&
  289. this.getAttribute("data-managerbutton") == "true" &&
  290. !this.getAttribute("status")
  291. ) {
  292. $(this).removeClass("selected");
  293. }
  294. }
  295. );
  296. }
  297. // display a manager button when clicking on a resource which redirected on the resource clicked manager
  298. function onClickManager(mode) {
  299. //debugger;
  300. if (mode) {
  301. $("#body").on("click.siteeditor", function (event) {
  302. event.stopPropagation();
  303. $('[status="selected"]').removeClass("selected").removeAttr("status");
  304. $("#manager").css("visibility", "hidden").removeAttr("onclick");
  305. });
  306. $("section").on("click.siteeditor", function (event) {
  307. //debugger;
  308. event.stopPropagation();
  309. $('[status="selected"]').removeClass("selected").removeAttr("status");
  310. $("#manager").css("visibility", "hidden").attr("onclick", "");
  311. if (
  312. this.getAttribute("data-managerbutton") &&
  313. this.getAttribute("data-managerbutton") == "true"
  314. ) {
  315. $(this).addClass("selected");
  316. $(this).attr("status", "selected");
  317. var currentId = this.getAttribute("data-resourceid");
  318. $("#manager").attr(
  319. "onclick",
  320. "window.open('/manager/?a=resource/update&id=" + currentId + "');"
  321. );
  322. $("#manager").css("visibility", "visible");
  323. }
  324. });
  325. } else {
  326. $("#body").off("click.siteeditor");
  327. $("section").off("click.siteeditor");
  328. }
  329. }
  330. //toggle between sidebar and nosidebar
  331. function sidebarDisplay(boolean) {
  332. if (boolean) {
  333. $("#noResourceSelected").css("display", "none");
  334. $("#resourceSelected").css("display", "");
  335. } else {
  336. $("#noResourceSelected").css("display", "");
  337. $("#resourceSelected").css("display", "none");
  338. }
  339. }
  340. //When sidebar is open, display the info of the resource selected or "selectionner une ressource" if no resource selected
  341. function sidebar(mode) {
  342. if (mode) {
  343. $("#body").on("click.sidebar", function () {
  344. $("#resourceName").remove();
  345. if ($("#wrapper").attr("class") !== "no-sidebar") {
  346. sidebarDisplay(false);
  347. }
  348. });
  349. $("section").on("click.sidebar", function (e) {
  350. //debugger;
  351. e.stopPropagation();
  352. $("#resourceName").remove();
  353. if (
  354. $("#wrapper").attr("class") !== "no-sidebar" &&
  355. this.getAttribute("data-resourceid")
  356. ) {
  357. getTitle("#resourceSelected");
  358. getNbColumn();
  359. getWidth();
  360. getPadding();
  361. sidebarDisplay(true);
  362. } else {
  363. sidebarDisplay(false);
  364. }
  365. });
  366. } else {
  367. $("#body").off("click.sidebar");
  368. $("section").off("click.sidebar");
  369. }
  370. }
  371. //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
  372. function sidebarToggle() {
  373. $("#sidebarToggle").on("click", function () {
  374. if ($("#wrapper").attr("class") == "no-sidebar") {
  375. var compt = 0;
  376. $("#resourceName").remove();
  377. $("section").each(function () {
  378. if ($(this).attr("status") == "selected") {
  379. compt += 1;
  380. }
  381. });
  382. if (compt > 0) {
  383. getNbColumn();
  384. getWidth();
  385. getPadding();
  386. getTitle("#resourceSelected");
  387. sidebarDisplay(true);
  388. } else {
  389. sidebarDisplay(false);
  390. }
  391. }
  392. $("#wrapper").toggleClass("no-sidebar");
  393. });
  394. }
  395. // REST : get method to know how many column the resource already occupied
  396. function getNbColumn() {
  397. hrefRestNse("#column-select", "nbcolumn");
  398. blapyTools.ajaxSimpleAction($("#column-select"), function (data) {
  399. var bootstrap = data.data.nb;
  400. var nbColumn;
  401. if (bootstrap === null) {
  402. nbColumn = 12;
  403. } else {
  404. pos3 = bootstrap.lastIndexOf("-");
  405. nbColumn = bootstrap.substr(pos3 + 1);
  406. }
  407. $("#column-select").prop("value", nbColumn);
  408. });
  409. }
  410. //REST : Method PUT when a new number of column is selected in sidebar
  411. // Change instantly the number of column on page
  412. function putNbColumn() {
  413. $("#column-select").change(function () {
  414. var valueSelected = $("#column-select").val();
  415. var bootstrap;
  416. //version input number
  417. if (valueSelected < 1) {
  418. valueSelected = 1;
  419. $("#column-select").attr("value", valueSelected);
  420. bootstrap = "col-sm-1";
  421. } else if (valueSelected >= 12) {
  422. valueSelected = 12;
  423. $("#column-select").attr("value", valueSelected);
  424. bootstrap = null;
  425. } else {
  426. $("#column-select").attr("value", valueSelected);
  427. bootstrap = "col-sm-" + valueSelected;
  428. }
  429. //envoyer le nombre de colonne au manager version number
  430. hrefRestNse('[for="column-select"]', "nbcolumn");
  431. if (bootstrap === null) {
  432. $('[for="column-select"]').attr(
  433. "data-data",
  434. '{"tv_nbBootstrapColumn":' + bootstrap + ',"syncsite":1}'
  435. );
  436. } else {
  437. $('[for="column-select"]').attr(
  438. "data-data",
  439. '{"tv_nbBootstrapColumn":"' + bootstrap + '","syncsite":1}'
  440. );
  441. }
  442. blapyTools.ajaxSimpleAction($('[for="column-select"]'));
  443. //dynamique sur la page
  444. var pos3 = $('[status="selected"]').attr("class").indexOf("col-sm");
  445. var pos4 = $('[status="selected"]')
  446. .attr("class")
  447. .indexOf(" ", pos3 + 1);
  448. var currentBootstrap = $('[status="selected"]')
  449. .attr("class")
  450. .substr(pos3, pos4 - pos3);
  451. if (currentBootstrap.startsWith("col-sm")) {
  452. $('[status="selected"]')
  453. .removeClass(currentBootstrap)
  454. .removeClass("selected");
  455. } else {
  456. $('[status="selected"]').removeClass("selected");
  457. }
  458. $('[status="selected"]').addClass(bootstrap).addClass("selected");
  459. if (bootstrap === null) {
  460. $('[status="selected"]').css("clear", "");
  461. } else {
  462. $('[status="selected"]').css("clear", "none");
  463. }
  464. });
  465. }
  466. //REST : get method to get the managers width in the sidebar
  467. function getWidth() {
  468. hrefRestNse("#width", "width");
  469. blapyTools.ajaxSimpleAction($("#width"), function (data) {
  470. var percentage = data.data.width;
  471. //console.log(percentage);
  472. var width;
  473. if (percentage === null) {
  474. width = 100;
  475. } else {
  476. pos3 = percentage.indexOf("%");
  477. width = percentage.substr(0, pos3);
  478. //console.log(width);
  479. }
  480. $("#width").prop("value", width);
  481. });
  482. }
  483. //REST : Method PUT when a new number of column is selected in sidebar
  484. // Change instantly the number of column on page
  485. function putWidth() {
  486. $("#width").change(function () {
  487. var valueSelected = $("#width").val();
  488. var percentage;
  489. //version input
  490. if (valueSelected < 10) {
  491. valueSelected = 10;
  492. $("#width").prop("value", valueSelected);
  493. percentage = "10%";
  494. } else if (valueSelected > 100) {
  495. valueSelected = 100;
  496. $("#width").prop("value", valueSelected);
  497. percentage = "100%";
  498. } else {
  499. $("#width").prop("value", valueSelected);
  500. percentage = valueSelected + "%";
  501. }
  502. //envoyer le nombre de colonne au manager version number
  503. hrefRestNse('[for="width"]', "width");
  504. if (percentage === "100%") {
  505. $('[for="width"]').attr(
  506. "data-data",
  507. '{"tv_sectionWidth":' + null + ',"syncsite":1}'
  508. );
  509. } else {
  510. $('[for="width"]').attr(
  511. "data-data",
  512. '{"tv_sectionWidth":"' + percentage + '","syncsite":1}'
  513. );
  514. }
  515. blapyTools.ajaxSimpleAction($('[for="width"]'));
  516. //dynamique sur la page
  517. $('[status="selected"]').find("div:first").css("width", percentage);
  518. });
  519. }
  520. //REST : get method to get the manager padding in the sidebar
  521. function getPadding() {
  522. hrefRestNse("#paddingTop", "padding");
  523. blapyTools.ajaxSimpleAction($("#paddingTop"), function (data) {
  524. var css = data.data.CSS;
  525. //console.log(css);
  526. var posTop1;
  527. var posTop2;
  528. var posTop3;
  529. var paddingTop;
  530. if (css !== null && css.includes("padding-top")) {
  531. css = css.replace(/ /g, "");
  532. posTop1 = css.indexOf("padding-top");
  533. posTop2 = css.indexOf(":", posTop1 + 1);
  534. posTop3 = css.indexOf(";", posTop1 + 1);
  535. paddingTop = css.substr(posTop2 + 1, posTop3 - posTop2 - 1);
  536. //console.log(paddingTop);
  537. //$('#paddingTop').prop('value',paddingTop);
  538. var paddingTop2;
  539. if (paddingTop.includes("px")) {
  540. $("#paddingTopUnit").prop("value", "px");
  541. posTop4 = paddingTop.indexOf("px");
  542. paddingTop2 = paddingTop.substr(0, posTop4);
  543. //console.log(paddingTop2);
  544. } else if (paddingTop.includes("%")) {
  545. $("#paddingTopUnit").prop("value", "%");
  546. posTop4 = paddingTop.indexOf("%");
  547. paddingTop2 = paddingTop.substr(0, posTop4);
  548. }
  549. $("#paddingTop").prop("value", paddingTop2);
  550. //console.log(paddingTop2);
  551. } else {
  552. $("#paddingTopUnit").prop("value", "px");
  553. $("#paddingTop").prop("value", "");
  554. }
  555. });
  556. hrefRestNse("#paddingBottom", "padding");
  557. blapyTools.ajaxSimpleAction($("#paddingBottom"), function (data) {
  558. var css = data.data.CSS;
  559. //console.log(css);
  560. var posBot1;
  561. var posBot2;
  562. var posBot3;
  563. var paddingBottom;
  564. if (css !== null && css.includes("padding-bottom")) {
  565. css = css.replace(/ /g, "");
  566. posBot1 = css.indexOf("padding-bottom");
  567. posBot2 = css.indexOf(":", posBot1 + 1);
  568. posBot3 = css.indexOf(";", posBot1 + 1);
  569. paddingBottom = css.substr(posBot2 + 1, posBot3 - posBot2 - 1);
  570. var paddingBottom2;
  571. if (paddingBottom.includes("px")) {
  572. posBot4 = paddingBottom.indexOf("px");
  573. paddingBottom2 = paddingBottom.substr(0, posBot4);
  574. $("#paddingBottomUnit").prop("value", "px");
  575. //console.log(paddingTop2);
  576. } else if (paddingBottom.includes("%")) {
  577. posBot4 = paddingBottom.indexOf("%");
  578. paddingBottom2 = paddingBottom.substr(0, posBot4);
  579. $("#paddingBottomUnit").prop("value", "%");
  580. }
  581. $("#paddingBottom").prop("value", paddingBottom2);
  582. } else {
  583. $("#paddingBottomUnit").prop("value", "px");
  584. $("#paddingBottom").prop("value", "");
  585. }
  586. });
  587. }
  588. // REST : put method to put the sidebar value in the manager
  589. function putPadding() {
  590. $(
  591. "#paddingTop, #paddingBottom, #paddingTopUnit, #paddingBottomUnit"
  592. ).change(function () {
  593. let paddingTop = parseInt($("#paddingTop").val());
  594. if (isNaN(paddingTop)) paddingTop = 0;
  595. let unitTop = $("#paddingTopUnit").val();
  596. let paddingBottom = parseInt($("#paddingBottom").val());
  597. if (isNaN(paddingBottom)) paddingBottom = 0;
  598. let unitBottom = $("#paddingBottomUnit").val();
  599. if (paddingTop > 999) {
  600. paddingTop = 999;
  601. $("#paddingTop").prop("value", paddingTop);
  602. } else if (paddingTop < 0) {
  603. paddingTop = 0;
  604. $("#paddingTop").prop("value", paddingTop);
  605. }
  606. if (paddingBottom > 999) {
  607. paddingBottom = 999;
  608. $("#paddingBottom").prop("value", paddingBottom);
  609. } else if (paddingBottom < 0) {
  610. paddingBottom = 0;
  611. $("#paddingBottom").prop("value", paddingBottom);
  612. }
  613. //envoyer le nombre de colonne au manager version number
  614. hrefRestNse('[for="padding"]', "padding");
  615. $('[for="padding"]').attr(
  616. "data-data",
  617. '{"tv_sectionCSS":"padding-top:' +
  618. paddingTop +
  619. unitTop +
  620. ";padding-bottom:" +
  621. paddingBottom +
  622. unitBottom +
  623. ';","syncsite":1}'
  624. );
  625. if (paddingTop === 0 && paddingBottom === 0) {
  626. $('[for="padding"]').attr(
  627. "data-data",
  628. '{"tv_sectionCSS":"","syncsite":1}'
  629. );
  630. } else if (paddingTop === 0) {
  631. $('[for="padding"]').attr(
  632. "data-data",
  633. '{"tv_sectionCSS":"padding-bottom:' +
  634. paddingBottom +
  635. unitBottom +
  636. ';","syncsite":1}'
  637. );
  638. }
  639. blapyTools.ajaxSimpleAction($('[for="padding"]'));
  640. //dynamique sur la page
  641. if (
  642. (paddingTop === 0 || paddingTop === "") &&
  643. (paddingBottom === 0 || paddingBottom === "")
  644. ) {
  645. $('[status="selected"]').css("padding-top", "");
  646. $('[status="selected"]').css("padding-bottom", "");
  647. } else if (paddingTop === 0 || paddingTop === "") {
  648. $('[status="selected"]').css("padding-top", "");
  649. $('[status="selected"]').css(
  650. "padding-bottom",
  651. paddingBottom + unitBottom
  652. );
  653. } else if (paddingBottom === 0 || paddingBottom === "") {
  654. $('[status="selected"]').css("padding-top", paddingTop + unitTop);
  655. $('[status="selected"]').css("padding-bottom", "");
  656. } else {
  657. $('[status="selected"]').css("padding-top", paddingTop + unitTop);
  658. $('[status="selected"]').css(
  659. "padding-bottom",
  660. paddingBottom + unitBottom
  661. );
  662. }
  663. });
  664. }
  665. let cropper = null;
  666. let pendingFile = null;
  667. let aspectRatio;
  668. function nseImageEditor() {
  669. const uppy = new Uppy({
  670. restrictions: {
  671. maxNumberOfFiles: 1,
  672. allowedFileTypes: ['.jpg', '.jpeg', '.png', '.gif', '.webp']
  673. },
  674. });
  675. uppy.use(Dashboard, {
  676. inline: true,
  677. target: "#uppy",
  678. showAddFilesButton: false,
  679. });
  680. uppy.on("file-added", (file) => {
  681. if (file.meta.isCropped) {
  682. return;
  683. }
  684. pendingFile = file;
  685. openCropper(file);
  686. return false;
  687. });
  688. uppy.on("complete", (file) => {
  689. showNseModal(false);
  690. uppy.cancelAll();
  691. console.log(file);
  692. current.element.src = "http://localhost/php/uploads/"+file.successful[0].meta.name;
  693. });
  694. //Initialiser l'endpoint pour l'upload des fichiers
  695. uppy.use(XHRUpload, { endpoint: "http://localhost/php/index.php" });
  696. const customButton = document.createElement("button");
  697. customButton.innerText = "Edit";
  698. customButton.style.position = "absolute";
  699. customButton.style.display = "none";
  700. customButton.id = "editImageBtn";
  701. document.body.append(customButton);
  702. customButton.onclick = () => showNseModal(true);
  703. const modal = document.getElementById("modalNseBackground");
  704. modal.addEventListener("click", (e) => {
  705. if (e.target === modal) {
  706. showNseModal(false);
  707. closedModal();
  708. }
  709. });
  710. document.querySelectorAll(".imageeditor").forEach((el) => {
  711. el.addEventListener("click", (e) => {
  712. el.style.border = "5px solid pink";
  713. if (el.naturalHeight === 0) {
  714. current.aspectRatio = 1;
  715. } else {
  716. current.aspectRatio = el.naturalWidth / el.naturalHeight;
  717. }
  718. current.element = el;
  719. customButton.style.top = e.clientY + 10 + "px";
  720. customButton.style.left = e.clientX + 10 + "px";
  721. customButton.style.display = "block";
  722. });
  723. });
  724. document.getElementById("cropper-cancel").addEventListener("click", () => {
  725. document.getElementById("cropper-modal").classList.remove("active");
  726. if (cropper) {
  727. cropper.destroy();
  728. cropper = null;
  729. }
  730. pendingFile = null;
  731. });
  732. document.getElementById("cropper-confirm").addEventListener("click", () => {
  733. if (!cropper || !pendingFile) return;
  734. cropper.getCroppedCanvas().toBlob((blob) => {
  735. uppy.removeFile(pendingFile.id);
  736. const ts = new Date()
  737. .toISOString()
  738. .replace(/[-:.]/g, '')
  739. .replace('Z', '');
  740. uppy.addFile({
  741. name: ts+'.png',
  742. type: pendingFile.type,
  743. data: blob,
  744. meta: {
  745. isCropped: true,
  746. },
  747. });
  748. // Nettoyage
  749. document.getElementById("cropper-modal").classList.remove("active");
  750. cropper.destroy();
  751. cropper = null;
  752. pendingFile = null;
  753. }, pendingFile.type);
  754. });
  755. }
  756. function openCropper(file) {
  757. const reader = new FileReader();
  758. reader.onload = (e) => {
  759. const cropperImage = document.getElementById("cropper-image");
  760. cropperImage.src = e.target.result;
  761. document.getElementById("cropper-modal").classList.add("active");
  762. if (cropper) {
  763. cropper.destroy();
  764. }
  765. cropper = new Cropper(cropperImage, {
  766. viewMode: 1,
  767. aspectRatio: current.aspectRatio,
  768. autoCropArea: 1,
  769. });
  770. };
  771. reader.readAsDataURL(file.data);
  772. }
  773. function showNseModal(shouldShow) {
  774. const modal = document.getElementById("modalNseBackground");
  775. modal.style.display = shouldShow ? "flex" : "none";
  776. }
  777. // to set data-data for every element when opening the page
  778. $(".siteeditor").each(function () {
  779. dataData(this);
  780. });
  781. //window.addEventListener('DOMContentLoaded', newsiteeditor, false);
  782. view();
  783. edit();
  784. onHover();
  785. saveContent();
  786. undo();
  787. redo();
  788. onHoverManager();
  789. //onClickManager();
  790. //sidebar();
  791. sidebarToggle();
  792. putNbColumn();
  793. putWidth();
  794. putPadding();
  795. nseImageEditor();
  796. //$('#body').off('click');
  797. //$('section').off('click');
  798. // define the public API
  799. var API = {};
  800. API.version = version;
  801. // API.add = add;
  802. // API.get_array = get_array;
  803. return API;
  804. })();