layout.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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.previousElementSibling.classList.contains('citation') ||
  59. bottomVignette.previousElementSibling.classList.contains('free') ||
  60. bottomVignette.previousElementSibling.classList.contains('temps')) {
  61. bottomVignette.style.marginTop = `${(pageBottom - textBottom) - vignetteSize}px`;
  62. }
  63. bottomVignette.style.transform = 'translateY(12px)';
  64. } else {
  65. let vignetteBottom = bottomVignette.getBoundingClientRect().bottom;
  66. bottomVignette.style.transform = `translateY(${pageBottom - vignetteBottom}px)`;
  67. }
  68. }
  69. // éléments justifiés pas nécessaires
  70. let justifiedSplitEl = document.querySelectorAll("[data-align-last-split-element='justify']");
  71. for (let i = 0; i < justifiedSplitEl.length; i++) {
  72. for (let j = 0; j < justifiedSplitEl[i].childNodes.length; j++) {
  73. let elStyle = justifiedSplitEl[i].childNodes[j].style;
  74. if (elStyle != undefined) {
  75. elStyle.textAlignLast = "left";
  76. }
  77. }
  78. }
  79. // images fullspread
  80. let fullspreadEl = document.querySelectorAll('.imgfullspreadleft, .imgfullspreadright, .imgfullspreadright_bleedtop, .imgfullspreadright_bleed, .doublepage_bigleft, .doublepage_bigright');
  81. for (let i = 0; i < fullspreadEl.length; i++) {
  82. let imgSrc = fullspreadEl[i].firstElementChild.getAttribute('src');
  83. let nextPage = fullspreadEl[i].closest('.pagedjs_page').nextElementSibling.querySelector('.pagedjs_page_content');
  84. let imgOverflowEl = document.createElement('div');
  85. if (fullspreadEl[i].classList.contains('imgfullspreadleft')) {
  86. imgOverflowEl.setAttribute('class', 'imgfullspreadleft-right');
  87. } else if (fullspreadEl[i].classList.contains('imgfullspreadright')) {
  88. imgOverflowEl.setAttribute('class', 'imgfullspreadright-right');
  89. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')) {
  90. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleedtop-right');
  91. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  92. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleed-right');
  93. } else if (fullspreadEl[i].classList.contains('doublepage_bigleft')) {
  94. imgOverflowEl.setAttribute('class', 'overflow_bigimgleft');
  95. } else if (fullspreadEl[i].classList.contains('doublepage_bigright')) {
  96. imgOverflowEl.setAttribute('class', 'overflow_bigimgright');
  97. }
  98. let imgOverflow = document.createElement('img');
  99. imgOverflow.src = imgSrc;
  100. imgOverflowEl.append(imgOverflow);
  101. nextPage.append(imgOverflowEl);
  102. if (fullspreadEl[i].classList.contains('imgfullspreadright')
  103. || fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')
  104. || fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  105. let imgMargin = imgOverflowEl.getBoundingClientRect().right - fullspreadEl[i].firstElementChild.getBoundingClientRect().right;
  106. fullspreadEl[i].firstElementChild.style.marginLeft = imgMargin + 'px';
  107. }
  108. if (fullspreadEl[i].classList.contains('doublepage_bigleft') || fullspreadEl[i].classList.contains('doublepage_bigright')) {
  109. let smallImgSrc = fullspreadEl[i].lastElementChild.getAttribute('src');
  110. fullspreadEl[i].lastElementChild.remove();
  111. let smallImg = document.createElement('img');
  112. smallImg.setAttribute('src', smallImgSrc);
  113. let smallImgEl = document.createElement('div');
  114. smallImgEl.classList.add('dp_sm_img');
  115. smallImgEl.append(smallImg);
  116. if (fullspreadEl[i].classList.contains('doublepage_bigleft')) {
  117. nextPage.append(smallImgEl);
  118. } else {
  119. fullspreadEl[i].parentElement.append(smallImgEl);
  120. }
  121. }
  122. }
  123. // fullpage img page gauche
  124. let fullPageImg = document.getElementsByClassName('fullpageimage');
  125. for (let i = 0; i < fullPageImg.length; i++) {
  126. if (fullPageImg[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  127. .classList.contains('pagedjs_left_page')) {
  128. fullPageImg[i].style.marginLeft = '-22mm';
  129. }
  130. }
  131. // encarts
  132. let encarts = document.querySelectorAll('.latour, .lampe, .latour_nohead, .lampe_nohead');
  133. for (let encart of encarts) {
  134. /* if (encart.previousElementSibling != null) {
  135. encart.style.marginTop = '10mm';
  136. }
  137. if (encart.nextElementSibling != null) {
  138. encart.style.marginBottom = '10mm';
  139. }
  140. */
  141. if (encart.hasAttribute('data-split-to')) {
  142. let plainColor = document.createElement('div');
  143. plainColor.setAttribute('class', 'encart-split');
  144. let plainHeight = encart.closest('.pagedjs_sheet').getBoundingClientRect().bottom - encart.getBoundingClientRect().bottom;
  145. if (encart.closest('.pagedjs_page').classList.contains('pagedjs_right_page')) {
  146. plainColor.style.width = "149mm";
  147. } else {
  148. plainColor.style.width = "129mm";
  149. }
  150. plainColor.style.height = `${plainHeight}px`;
  151. encart.parentNode.append(plainColor);
  152. }
  153. if (encart.hasAttribute('data-split-from')) {
  154. encart.style.marginTop = "0mm";
  155. let plainColor = document.createElement('div');
  156. plainColor.setAttribute('class', 'encart-split');
  157. if (encart.closest('.pagedjs_page').classList.contains('pagedjs_right_page')) {
  158. plainColor.style.width = "149mm";
  159. } else {
  160. plainColor.style.width = "129mm";
  161. }
  162. plainColor.style.height = "22mm";
  163. plainColor.style.top = "-22mm";
  164. encart.parentNode.prepend(plainColor);
  165. }
  166. }
  167. // TITRES ATELIERS
  168. let h3s = document.querySelectorAll('h3');
  169. for (let h3 of h3s) {
  170. if (h3.nextSibling?.firstChild?.tagName === "IMG") {
  171. let coverPhoto;
  172. let photoContainer = h3.nextElementSibling;
  173. if (h3.nextSibling?.children.length === 2) {
  174. coverPhoto = h3.nextElementSibling.children[1];
  175. let coverDessin = document.createElement('img');
  176. coverDessin.setAttribute('src', photoContainer.firstElementChild.getAttribute('src'));
  177. h3.prepend(coverDessin);
  178. coverDessin.style.width = '100%';
  179. coverDessin.style.height = 'auto';
  180. photoContainer.firstElementChild.remove();
  181. } else {
  182. coverPhoto = h3.nextElementSibling.firstChild;
  183. }
  184. photoContainer.style.position = 'absolute';
  185. photoContainer.style.width = '53mm';
  186. photoContainer.style.height = '220mm';
  187. photoContainer.style.overflow = 'hidden';
  188. photoContainer.style.top = "-22mm";
  189. photoContainer.style.left = "96mm";
  190. photoContainer.style.margin = '0';
  191. coverPhoto.style.width = 'auto';
  192. coverPhoto.style.height = '100%';
  193. let nextImg = document.createElement('img');
  194. nextImg.setAttribute('src', coverPhoto.getAttribute('src'));
  195. let nextContainer = document.createElement('div');
  196. nextContainer.style.position = "absolute";
  197. nextContainer.style.top = "-22mm";
  198. nextContainer.style.left = "-8mm";
  199. nextContainer.style.height = "220mm";
  200. nextContainer.style.width = "224mm";
  201. nextContainer.style.overflow = "hidden";
  202. nextContainer.style.marginLeft = "-53mm";
  203. nextImg.style.height = "100%";
  204. nextImg.style.width = "auto";
  205. nextContainer.append(nextImg);
  206. h3.closest('.pagedjs_page').nextElementSibling?.querySelector('.pagedjs_page_content').append(nextContainer);
  207. }
  208. h3.style.position = 'absolute';
  209. h3.style.top = `${(h3.closest('.pagedjs_page_content').offsetHeight - h3.offsetHeight) / 3}px`;
  210. h3.closest('.pagedjs_pagebox').querySelector('.pagedjs_margin-left').innerHTML = '';
  211. h3.closest('.pagedjs_sheet').classList.add('atelier_cover_page');
  212. }
  213. // TITRES PARTIES
  214. let h2s = document.querySelectorAll('h2');
  215. for (let i = 0; i < h2s.length; i++) {
  216. if (i != 0) {
  217. let parentSheet = h2s[i].closest('.pagedjs_sheet');
  218. parentSheet.querySelector('.pagedjs_margin-left').innerHTML = '';
  219. parentSheet.classList.add('partie_cover_page');
  220. parentSheet.parentElement.nextElementSibling?.firstElementChild.classList.add('partie_cover_page_right');
  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. }
  240. }
  241. // afficher correctement les éléments en marge
  242. let topLeftBoxes = document.querySelectorAll('.pagedjs_margin-left-top');
  243. for (let topLeftBox of topLeftBoxes) {
  244. let contentDiv = topLeftBox.firstElementChild;
  245. let textToPut = document.createElement('p');
  246. let atelierGras;
  247. let currentPage = contentDiv.closest('.pagedjs_page');
  248. if (currentPage.id != "page-1") {
  249. let previousPage = currentPage.previousElementSibling;
  250. while (previousPage) {
  251. if (previousPage.firstElementChild.classList.contains('atelier_cover_page')) {
  252. textToPut.innerText = previousPage.querySelector('h3').innerText;
  253. let words = textToPut.innerText.split(' ');
  254. let num = parseInt(words[0], 10);
  255. if (!isNaN(num)) {
  256. atelierGras = document.createElement('span');
  257. atelierGras.style.fontWeight = "bold";
  258. atelierGras.innerText = `Atelier ${num} `;
  259. textToPut.innerText = textToPut.innerText.substring(1);
  260. }
  261. break;
  262. }
  263. previousPage = previousPage.previousElementSibling;
  264. }
  265. }
  266. if (atelierGras) {
  267. textToPut.prepend(atelierGras);
  268. }
  269. contentDiv.append(textToPut);
  270. contentDiv.style.marginTop = contentDiv.offsetWidth / 2 - contentDiv.offsetHeight / 2 + "px";
  271. }
  272. let bottomLeftBoxes = document.querySelectorAll('.pagedjs_margin-left-bottom');
  273. for (let bottomLeftBox of bottomLeftBoxes) {
  274. let contentDiv = bottomLeftBox.firstElementChild;
  275. let textToPut = document.createElement('p');
  276. let partieGras;
  277. let currentPage = contentDiv.closest('.pagedjs_page');
  278. if (currentPage.id != "page-1") {
  279. let previousPage = currentPage.previousElementSibling;
  280. while (previousPage) {
  281. if (previousPage.firstElementChild.classList.contains('partie_cover_page')) {
  282. textToPut.innerText = previousPage.querySelector('h2').innerText;
  283. partieGras = document.createElement('span');
  284. partieGras.style.fontWeight = "bold";
  285. partieGras.innerText = previousPage.querySelector('.partie_count').innerHTML.replace("<br>", " ") + " ";
  286. break;
  287. }
  288. previousPage = previousPage.previousElementSibling;
  289. }
  290. }
  291. if (!textToPut.innerText) {
  292. textToPut.innerText = "Introduction";
  293. }
  294. if (partieGras) {
  295. textToPut.prepend(partieGras);
  296. }
  297. contentDiv.append(textToPut);
  298. contentDiv.style.marginBottom = contentDiv.offsetWidth / 2 - contentDiv.offsetHeight / 2 + "px";
  299. }
  300. // SET COLORS
  301. let colors = [
  302. '#5595a1',
  303. '#5f9796',
  304. '#65998d',
  305. '#6f9b80',
  306. '#799e75',
  307. '#83a16b',
  308. '#8ba460',
  309. '#95a358',
  310. '#9ca151',
  311. '#a59d4a',
  312. '#af9944',
  313. '#b8963d',
  314. '#c19238',
  315. '#cb8e31'
  316. ]
  317. let sprays = [
  318. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page001.png',
  319. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page002.png',
  320. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page003.png',
  321. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page004.png',
  322. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page005.png',
  323. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page006.png',
  324. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page007.png',
  325. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page008.png',
  326. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page009.png',
  327. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page010.png',
  328. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page011.png',
  329. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page012.png',
  330. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page013.png',
  331. '/user/themes/carnet-atterrissage/assets/sprays/spary_couleurs-page014.png'
  332. ]
  333. // set all colors
  334. let allPages = document.querySelectorAll('.pagedjs_page');
  335. let atelierColor = '';
  336. let bgColor = 'rgb(245, 245, 245)';
  337. let atelierIndex = 0;
  338. for (let page of allPages) {
  339. if (page.firstElementChild.classList.contains('atelier_cover_page')) {
  340. atelierColor = colors[atelierIndex];
  341. if (atelierColor == undefined) {
  342. atelierColor = colors[colors.length];
  343. }
  344. atelierIndex++;
  345. }
  346. if (page.firstElementChild.classList.contains('partie_cover_page')) {
  347. atelierIndex = 0;
  348. }
  349. let pageCounter = page.querySelector('.pagedjs_margin-left-middle')
  350. if (pageCounter) {
  351. pageCounter.style.color = atelierColor;
  352. }
  353. let citations = page.querySelectorAll('.citation');
  354. for (let citation of citations) {
  355. // dessiner et colorer les guillemets
  356. if (citation.innerText.charAt(0) === '“') {
  357. citation.innerText = citation.innerText.substring(1);
  358. }
  359. if (citation.innerText.charAt(citation.innerText.length - 1) === '”') {
  360. citation.innerText = citation.innerText.substring(0, citation.innerText.length - 1);
  361. }
  362. let guillemetOuvrant = document.createElement('div');
  363. guillemetOuvrant.innerText = '“';
  364. guillemetOuvrant.style.color = atelierColor;
  365. guillemetOuvrant.classList.add('guillement_ouvrant');
  366. citation.prepend(guillemetOuvrant);
  367. let guillemetFermant = document.createElement('div');
  368. guillemetFermant.innerText = '”';
  369. guillemetFermant.style.color = atelierColor;
  370. guillemetFermant.classList.add('guillement_fermant');
  371. citation.append(guillemetFermant);
  372. }
  373. let atelierTitle = page.querySelector('h3');
  374. if (atelierTitle) {
  375. atelierTitle.style.color = atelierColor;
  376. }
  377. let bibliographie = page.querySelector('.bibliographie');
  378. if (bibliographie) {
  379. bibliographie.style.color = atelierColor;
  380. // set bg et sprays
  381. let bibliographiePage = bibliographie.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
  382. bibliographiePage.style.backgroundColor = bgColor;
  383. let nextPage = bibliographiePage.parentElement?.nextElementSibling?.firstElementChild;
  384. if (!bibliographie.closest('.pagedjs_page').previousElementSibling.querySelector('.bibliographie')) {
  385. if (nextPage) {
  386. nextPage.style.backgroundColor = bgColor;
  387. }
  388. let sprayImg = document.createElement('img');
  389. sprayImg.setAttribute('src', sprays[atelierIndex]);
  390. sprayImg.style.height = "242mm";
  391. sprayImg.style.width = "60mm";
  392. sprayImg.style.position = "absolute";
  393. sprayImg.style.top = "-22mm";
  394. sprayImg.style.right = "-20mm";
  395. sprayImg.style.mixBlendMode = "multiply";
  396. bibliographiePage.parentElement.nextElementSibling?.querySelector('.bibliographie').insertAdjacentElement("afterend", sprayImg);
  397. }
  398. }
  399. let h4 = page.querySelector('h4');
  400. if (h4 && h4?.nextElementSibling?.classList.contains('bibliographie')) {
  401. h4.style.color = atelierColor;
  402. }
  403. let serpentins = document.querySelectorAll('img[alt="serpentin"]');
  404. for (let serpentin of serpentins) {
  405. let page = serpentin.closest('.pagedjs_page');
  406. let nextPage = page.nextElementSibling;
  407. page.style.backgroundColor = "rgb(245, 245, 245)";
  408. nextPage.style.backgroundColor = "rgb(245, 245, 245)";
  409. }
  410. let links = page.querySelectorAll('a');
  411. for (let link of links) {
  412. link.style.color = atelierColor;
  413. }
  414. if (atelierColor) {
  415. let lightColor = `${atelierColor}1a`;
  416. let encarts = page.querySelectorAll('.lampe, .latour, .lampe_nohead, .latour_nohead, .encart-split');
  417. for (let encart of encarts) {
  418. encart.style.backgroundColor = lightColor;
  419. }
  420. }
  421. }
  422. // all links open in a new tab
  423. let links = document.querySelectorAll('a');
  424. for (let link of links) {
  425. link.setAttribute('target', '_blank');
  426. }
  427. // loading fini
  428. document.querySelector('#chargement').remove();
  429. }
  430. }
  431. Paged.registerHandlers(setMarginTexts);