layout.js 18 KB

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