layout.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. // fonction s'éxecute après le rendu de pagedJS
  2. // cf https://pagedjs.org/documentation/10-handlers-hooks-and-custom-javascript/
  3. class setMarginTexts extends Paged.Handler {
  4. constructor(chunker, polisher, caller) {
  5. super(chunker, polisher, caller);
  6. }
  7. afterParsed(parsed) {
  8. // indication du chargement
  9. let chargement = document.createElement('div');
  10. chargement.setAttribute('id', 'chargement');
  11. chargement.innerHTML = 'chargement';
  12. chargement.style.color = "orange";
  13. chargement.style.position = "fixed";
  14. chargement.style.fontWeight = "bold";
  15. chargement.style.zIndex = "9999";
  16. document.querySelector('body').prepend(chargement);
  17. }
  18. afterPreview(pages) {
  19. // donner class à la dernière page
  20. let lastElement = $('.pagedjs_page').last();
  21. lastElement.addClass('last-page');
  22. // cleaner pour que les paragraphes tombent sur la marge haute
  23. for (let i = 0; i < labeurs.length; i++) {
  24. if (labeurs[i] == labeurs[i].parentNode?.firstElementChild
  25. && !labeurs[i].firstElementChild?.hasAttribute("data-split-from")) {
  26. if (labeurs[i].firstElementChild) {
  27. labeurs[i].firstElementChild.style.marginTop = "0px";
  28. }
  29. }
  30. }
  31. // pareil pour les titres temps
  32. for (let i = 0; i < moments.length; i++) {
  33. if (moments[i].previousElementSibling?.tagName === "P"
  34. && moments[i].previousElementSibling == moments[i].parentNode.firstElementChild) {
  35. moments[i].parentNode.removeChild(moments[i].parentNode.firstElementChild);
  36. }
  37. }
  38. // enlever les paragraphes vides
  39. let paragraphes = document.querySelectorAll("p");
  40. for (let i = 0; i < paragraphes.length; i++) {
  41. if (paragraphes[i].innerHTML == "") {
  42. paragraphes[i].remove();
  43. }
  44. }
  45. // images collées en bas
  46. let bottomImgs = document.querySelectorAll('.bottomimg, .tripleimgs_bottom, .tripleimgs2_bottom');
  47. for (let bottomImg of bottomImgs) {
  48. let pageBottom = bottomImg.closest('.pagedjs_area').getBoundingClientRect().bottom;
  49. bottomImg.style.transform = 'translateY(' + (pageBottom - bottomImg.lastElementChild.getBoundingClientRect().bottom) + 'px)';
  50. }
  51. let bottomVignettes = document.querySelectorAll('.imgsmall_bottom'); // pour les images en bas dans les pages avec du texte
  52. for (let bottomVignette of bottomVignettes) {
  53. let pageBottom = bottomVignette.closest('.pagedjs_area').getBoundingClientRect().bottom;
  54. let vignetteSize = bottomVignette.getBoundingClientRect().height;
  55. if (bottomVignette.previousElementSibling) {
  56. let textBottom = bottomVignette.previousElementSibling.getBoundingClientRect().bottom;
  57. if (bottomVignette.previousElementSibling.classList.contains('labeur')) {
  58. bottomVignette.style.marginTop = `${(pageBottom - textBottom) - vignetteSize}px`;
  59. }
  60. bottomVignette.style.transform = 'translateY(12px)';
  61. } else {
  62. let vignetteBottom = bottomVignette.getBoundingClientRect().bottom;
  63. bottomVignette.style.transform = `translateY(${pageBottom - vignetteBottom}px)`;
  64. }
  65. }
  66. // éléments justifiés pas nécessaires
  67. let justifiedSplitEl = document.querySelectorAll("[data-align-last-split-element='justify']");
  68. for (let i = 0; i < justifiedSplitEl.length; i++) {
  69. for (let j = 0; j < justifiedSplitEl[i].childNodes.length; j++) {
  70. let elStyle = justifiedSplitEl[i].childNodes[j].style;
  71. if (elStyle != undefined) {
  72. elStyle.textAlignLast = "left";
  73. }
  74. }
  75. }
  76. // images fullspread
  77. let fullspreadEl = document.querySelectorAll('.imgfullspreadleft, .imgfullspreadright, .imgfullspreadright_bleedtop, .imgfullspreadright_bleed, .doublepage_bigleft, .doublepage_bigright');
  78. for (let i = 0; i < fullspreadEl.length; i++) {
  79. let imgSrc = fullspreadEl[i].firstElementChild.getAttribute('src');
  80. let nextPage = fullspreadEl[i].closest('.pagedjs_page').nextElementSibling.querySelector('.pagedjs_page_content');
  81. let imgOverflowEl = document.createElement('div');
  82. if (fullspreadEl[i].classList.contains('imgfullspreadleft')) {
  83. imgOverflowEl.setAttribute('class', 'imgfullspreadleft-right');
  84. } else if (fullspreadEl[i].classList.contains('imgfullspreadright')) {
  85. imgOverflowEl.setAttribute('class', 'imgfullspreadright-right');
  86. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')) {
  87. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleedtop-right');
  88. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  89. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleed-right');
  90. } else if (fullspreadEl[i].classList.contains('doublepage_bigleft')) {
  91. imgOverflowEl.setAttribute('class', 'overflow_bigimgleft');
  92. } else if (fullspreadEl[i].classList.contains('doublepage_bigright')) {
  93. imgOverflowEl.setAttribute('class', 'overflow_bigimgright');
  94. }
  95. let imgOverflow = document.createElement('img');
  96. imgOverflow.src = imgSrc;
  97. imgOverflowEl.append(imgOverflow);
  98. nextPage.append(imgOverflowEl);
  99. if (fullspreadEl[i].classList.contains('imgfullspreadright')
  100. || fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')
  101. || fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  102. let imgMargin = imgOverflowEl.getBoundingClientRect().right - fullspreadEl[i].firstElementChild.getBoundingClientRect().right;
  103. fullspreadEl[i].firstElementChild.style.marginLeft = imgMargin + 'px';
  104. }
  105. if (fullspreadEl[i].classList.contains('doublepage_bigleft') || fullspreadEl[i].classList.contains('doublepage_bigright')) {
  106. let smallImgSrc = fullspreadEl[i].lastElementChild.getAttribute('src');
  107. fullspreadEl[i].lastElementChild.remove();
  108. let smallImg = document.createElement('img');
  109. smallImg.setAttribute('src', smallImgSrc);
  110. let smallImgEl = document.createElement('div');
  111. smallImgEl.classList.add('dp_sm_img');
  112. smallImgEl.append(smallImg);
  113. if (fullspreadEl[i].classList.contains('doublepage_bigleft')) {
  114. nextPage.append(smallImgEl);
  115. } else {
  116. fullspreadEl[i].parentElement.append(smallImgEl);
  117. }
  118. }
  119. }
  120. // fullpage img page gauche
  121. let fullPageImg = document.getElementsByClassName('fullpageimage');
  122. for (let i = 0; i < fullPageImg.length; i++) {
  123. if (fullPageImg[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  124. .classList.contains('pagedjs_left_page')) {
  125. fullPageImg[i].style.marginLeft = '-22mm';
  126. }
  127. }
  128. // encarts
  129. let encarts = document.querySelectorAll('.latour, .lampe, .latour_nohead, .lampe_nohead');
  130. for (let encart of encarts) {
  131. if (encart.previousElementSibling != null) {
  132. encart.style.marginTop = '10mm';
  133. }
  134. if (encart.nextElementSibling != null) {
  135. encart.style.marginBottom = '10mm';
  136. }
  137. if (encart.hasAttribute('data-split-to')) {
  138. let plainColor = document.createElement('div');
  139. plainColor.setAttribute('class', 'encart-split');
  140. let plainHeight = encart.closest('.pagedjs_sheet').getBoundingClientRect().bottom - encart.getBoundingClientRect().bottom;
  141. if (encart.closest('.pagedjs_page').classList.contains('pagedjs_right_page')) {
  142. plainColor.style.width = "149mm";
  143. } else {
  144. plainColor.style.width = "129mm";
  145. }
  146. plainColor.style.height = `${plainHeight}px`;
  147. encart.parentNode.append(plainColor);
  148. }
  149. if (encart.hasAttribute('data-split-from')) {
  150. let plainColor = document.createElement('div');
  151. plainColor.setAttribute('class', 'encart-split');
  152. if (encart.closest('.pagedjs_page').classList.contains('pagedjs_right_page')) {
  153. plainColor.style.width = "149mm";
  154. } else {
  155. plainColor.style.width = "129mm";
  156. }
  157. plainColor.style.height = "22mm";
  158. plainColor.style.top = "-22mm";
  159. encart.parentNode.prepend(plainColor);
  160. }
  161. }
  162. // TITRES ATELIERS
  163. let h3s = document.querySelectorAll('h3');
  164. for (let h3 of h3s) {
  165. if (h3.nextSibling?.firstChild.tagName === "IMG") {
  166. let coverPhoto;
  167. let photoContainer = h3.nextElementSibling;
  168. if (h3.nextSibling?.children.length === 2) {
  169. coverPhoto = h3.nextElementSibling.children[1];
  170. let coverDessin = document.createElement('img');
  171. coverDessin.setAttribute('src', photoContainer.firstElementChild.getAttribute('src'));
  172. h3.prepend(coverDessin);
  173. coverDessin.style.width = '100%';
  174. coverDessin.style.height = 'auto';
  175. photoContainer.firstElementChild.remove();
  176. } else {
  177. coverPhoto = h3.nextElementSibling.firstChild;
  178. }
  179. photoContainer.style.position = 'absolute';
  180. photoContainer.style.width = '53mm';
  181. photoContainer.style.height = '220mm';
  182. photoContainer.style.overflow = 'hidden';
  183. photoContainer.style.top = "-22mm";
  184. photoContainer.style.left = "96mm";
  185. photoContainer.style.margin = '0';
  186. coverPhoto.style.width = 'auto';
  187. coverPhoto.style.height = '100%';
  188. let nextImg = document.createElement('img');
  189. nextImg.setAttribute('src', coverPhoto.getAttribute('src'));
  190. let nextContainer = document.createElement('div');
  191. nextContainer.style.position = "absolute";
  192. nextContainer.style.top = "-22mm";
  193. nextContainer.style.left = "-8mm";
  194. nextContainer.style.height = "220mm";
  195. nextContainer.style.width = "224mm";
  196. nextContainer.style.overflow = "hidden";
  197. nextContainer.style.marginLeft = "-53mm";
  198. nextImg.style.height = "100%";
  199. nextImg.style.width = "auto";
  200. nextContainer.append(nextImg);
  201. h3.closest('.pagedjs_page').nextElementSibling?.querySelector('.pagedjs_page_content').append(nextContainer);
  202. }
  203. h3.style.position = 'absolute';
  204. h3.style.top = `${(h3.closest('.pagedjs_page_content').offsetHeight - h3.offsetHeight) / 3}px`;
  205. h3.closest('.pagedjs_pagebox').querySelector('.pagedjs_margin-left').innerHTML = '';
  206. h3.closest('.pagedjs_sheet').classList.add('atelier_cover_page');
  207. }
  208. // TITRES PARTIES
  209. let h2s = document.querySelectorAll('h2');
  210. for (let i = 0; i < h2s.length; i++) {
  211. if (i != 0) {
  212. let parentSheet = h2s[i].closest('.pagedjs_sheet');
  213. parentSheet.querySelector('.pagedjs_margin-left').innerHTML = '';
  214. parentSheet.classList.add('partie_cover_page');
  215. parentSheet.parentElement.nextElementSibling?.firstElementChild.classList.add('partie_cover_page_right');
  216. let partieCount = document.createElement('div');
  217. partieCount.classList.add('partie_count');
  218. let romanCount;
  219. switch (i) {
  220. case 1: romanCount = 'I'; break;
  221. case 2: romanCount = 'II'; break;
  222. case 3: romanCount = 'III'; break;
  223. case 4: romanCount = 'IV'; break;
  224. case 5: romanCount = 'V'; break;
  225. case 6: romanCount = 'VI'; break;
  226. case 7: romanCount = 'VII'; break;
  227. case 8: romanCount = 'VIII'; break;
  228. case 9: romanCount = 'IX'; break;
  229. case 10: romanCount = 'X'; break;
  230. }
  231. partieCount.innerHTML = 'Partie<br>' + romanCount;
  232. h2s[i].parentElement.prepend(partieCount);
  233. h2s[i].nextElementSibling?.remove();
  234. }
  235. }
  236. // afficher correctement les éléments en marge
  237. let topLeftBoxes = document.querySelectorAll('.pagedjs_margin-left-top');
  238. for (let topLeftBox of topLeftBoxes) {
  239. let contentDiv = topLeftBox.firstElementChild;
  240. let textToPut = document.createElement('p');
  241. let atelierGras;
  242. let currentPage = contentDiv.closest('.pagedjs_page');
  243. if (currentPage.id != "page-1") {
  244. let previousPage = currentPage.previousElementSibling;
  245. while (previousPage) {
  246. if (previousPage.firstElementChild.classList.contains('atelier_cover_page')) {
  247. textToPut.innerText = previousPage.querySelector('h3').innerText;
  248. let words = textToPut.innerText.split(' ');
  249. let num = parseInt(words[0], 10);
  250. if (!isNaN(num)) {
  251. atelierGras = document.createElement('span');
  252. atelierGras.style.fontWeight = "bold";
  253. atelierGras.innerText = `Atelier ${num} `;
  254. textToPut.innerText = textToPut.innerText.substring(1);
  255. }
  256. break;
  257. }
  258. previousPage = previousPage.previousElementSibling;
  259. }
  260. }
  261. if (atelierGras) {
  262. textToPut.prepend(atelierGras);
  263. }
  264. contentDiv.append(textToPut);
  265. contentDiv.style.marginTop = contentDiv.offsetWidth / 2 - contentDiv.offsetHeight / 2 + "px";
  266. }
  267. let bottomLeftBoxes = document.querySelectorAll('.pagedjs_margin-left-bottom');
  268. for (let bottomLeftBox of bottomLeftBoxes) {
  269. let contentDiv = bottomLeftBox.firstElementChild;
  270. let textToPut = document.createElement('p');
  271. let partieGras;
  272. let currentPage = contentDiv.closest('.pagedjs_page');
  273. if (currentPage.id != "page-1") {
  274. let previousPage = currentPage.previousElementSibling;
  275. while (previousPage) {
  276. if (previousPage.firstElementChild.classList.contains('partie_cover_page')) {
  277. textToPut.innerText = previousPage.querySelector('h2').innerText;
  278. partieGras = document.createElement('span');
  279. partieGras.style.fontWeight = "bold";
  280. partieGras.innerText = previousPage.querySelector('.partie_count').innerHTML.replace("<br>", " ") + " ";
  281. break;
  282. }
  283. previousPage = previousPage.previousElementSibling;
  284. }
  285. }
  286. if (!textToPut.innerText) {
  287. textToPut.innerText = "Introduction";
  288. }
  289. if (partieGras) {
  290. textToPut.prepend(partieGras);
  291. }
  292. contentDiv.append(textToPut);
  293. contentDiv.style.marginBottom = contentDiv.offsetWidth / 2 - contentDiv.offsetHeight / 2 + "px";
  294. }
  295. // SET COLORS
  296. let colors = [
  297. '#5595a1',
  298. '#5f9796',
  299. '#65998d',
  300. '#6f9b80',
  301. '#799e75',
  302. '#83a16b',
  303. '#8ba460',
  304. '#95a358',
  305. '#9ca151',
  306. '#a59d4a',
  307. '#af9944',
  308. '#b8963d',
  309. '#c19238',
  310. '#cb8e31'
  311. ]
  312. // set all colors
  313. let allPages = document.querySelectorAll('.pagedjs_page');
  314. let atelierColor = '';
  315. let bgColor = 'rgb(245, 245, 245)';
  316. let atelierIndex = 0;
  317. for (let page of allPages) {
  318. if (page.firstElementChild.classList.contains('atelier_cover_page')) {
  319. atelierColor = colors[atelierIndex];
  320. if (atelierColor == undefined) {
  321. atelierColor = colors[colors.length];
  322. }
  323. atelierIndex++;
  324. }
  325. if (page.firstElementChild.classList.contains('partie_cover_page')) {
  326. atelierIndex = 0;
  327. }
  328. let pageCounter = page.querySelector('.pagedjs_margin-left-middle')
  329. if (pageCounter) {
  330. pageCounter.style.color = atelierColor;
  331. }
  332. let atelierTitle = page.querySelector('h3');
  333. if (atelierTitle) {
  334. atelierTitle.style.color = atelierColor;
  335. }
  336. let bibliographie = page.querySelector('.bibliographie');
  337. if (bibliographie) {
  338. bibliographie.style.color = atelierColor;
  339. // set bg et sprays
  340. let bibliographiePage = bibliographie.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
  341. bibliographiePage.style.backgroundColor = bgColor;
  342. let nextPage = bibliographiePage.parentElement?.nextElementSibling?.firstElementChild;
  343. if (nextPage) {
  344. nextPage.style.backgroundColor = bgColor;
  345. }
  346. let sprayImg = document.createElement('img');
  347. sprayImg.setAttribute('src', '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page001.png');
  348. sprayImg.style.height = "242mm";
  349. sprayImg.style.width = "60mm";
  350. sprayImg.style.position = "absolute";
  351. sprayImg.style.top = "-22mm";
  352. sprayImg.style.right = "-20mm";
  353. sprayImg.style.mixBlendMode = "multiply";
  354. bibliographiePage.parentElement.nextElementSibling?.querySelector('.pagedjs_page_content').prepend(sprayImg);
  355. }
  356. let h4 = page.querySelector('h4');
  357. if (h4 && h4?.nextElementSibling?.classList.contains('bibliographie')) {
  358. h4.style.color = atelierColor;
  359. }
  360. let links = page.querySelectorAll('a');
  361. for (let link of links) {
  362. link.style.color = atelierColor;
  363. }
  364. if (atelierColor) {
  365. let lightColor = `${atelierColor}1a`;
  366. let encarts = page.querySelectorAll('.lampe, .latour, .lampe_nohead, .latour_nohead, .encart-split');
  367. for (let encart of encarts) {
  368. encart.style.backgroundColor = lightColor;
  369. }
  370. }
  371. }
  372. // all links open in a new tab
  373. let links = document.querySelectorAll('a');
  374. for (let link of links) {
  375. link.setAttribute('target', '_blank');
  376. }
  377. // loading fini
  378. document.querySelector('#chargement').remove();
  379. }
  380. }
  381. Paged.registerHandlers(setMarginTexts);