layout.js 18 KB

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