layout.js 18 KB

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