app.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import domReady from '@roots/sage/client/dom-ready';
  2. /**
  3. * Application entrypoint
  4. */
  5. domReady(async () => {
  6. // pour régler tous les pb au refresh quand la page est déjà scrollée
  7. window.scrollTo(0, 0);
  8. // CLEANER LE TABLEAU
  9. let minHeadTh = document.querySelector("#main table thead tr th:first-of-type");
  10. minHeadTh.style.width = `${100 / 8}%`;
  11. let otherHeadThs = document.querySelectorAll("#main table thead tr th:not(:first-of-type)");
  12. for (let otherHeadTh of otherHeadThs) {
  13. otherHeadTh.style.width = `${100 / 8 * 1.75}%`;
  14. }
  15. let minTableTds = document.querySelectorAll("#main table tbody tr td:first-of-type");
  16. for (let minTableTd of minTableTds) {
  17. if (minTableTd.parentNode.childElementCount != 1) {
  18. minTableTd.style.width = `${100 / 8}%`;
  19. }
  20. }
  21. let otherTableTds = document.querySelectorAll("#main table tbody tr td:not(:first-of-type)");
  22. for (let otherTableTd of otherTableTds) {
  23. otherTableTd.style.width = `${100 / 8 * 1.75}%`;
  24. }
  25. // HEADER AU SCROLL
  26. let tableHead = document.querySelector("thead tr");
  27. let tHeadHeight = tableHead.offsetHeight;
  28. tableHead.style.maxHeight = `${tHeadHeight}px`;
  29. let headerHeight = document.querySelector("header").offsetHeight;
  30. window.onscroll = () => {
  31. tableHead.style.minHeight = `${headerHeight + 72}px`;
  32. let scroll = window.scrollY;
  33. let headerNewHeight = tHeadHeight - scroll;
  34. if (headerNewHeight > 72) {
  35. tableHead.style.height = `${headerHeight + headerNewHeight}px`;
  36. } else {
  37. tableHead.style.height = tableHead.style.minHeight;
  38. }
  39. }
  40. // italiques -> A FAIRE SUR LE CSV AVANT D'IMPORTER CA RALENTIT DE OUF LE CHARGEMENT
  41. /* let tbody = document.querySelector('body main table tbody');
  42. let tbodyContent = tbody.innerHTML;
  43. let asteriskCounter = 0;
  44. for (let i = 0; i < tbodyContent.length; i++) {
  45. if (tbodyContent[i] == "*") {
  46. if (asteriskCounter % 2 == 0) {
  47. tbodyContent = tbodyContent.substring(0, i) + '<span class="font-caslon italic">' + tbodyContent.substring(i + 1);
  48. } else {
  49. tbodyContent = tbodyContent.substring(0, i) + '</span>' + tbodyContent.substring(i + 1);
  50. }
  51. asteriskCounter++;
  52. }
  53. }
  54. tbody.innerHTML = tbodyContent;
  55. */
  56. // titres de parties dans le tableau
  57. let mainpartTitless = document.querySelectorAll('.isMainPart');
  58. for (let mainpartTitles of mainpartTitless) {
  59. if (mainpartTitles.nextElementSibling.classList.contains('isSubPart')) {
  60. mainpartTitles.firstElementChild.firstElementChild.style.height = "1.5rem";
  61. mainpartTitles.firstElementChild.style.marginBottom = "-0.25rem";
  62. mainpartTitles.style.borderBottom = "0";
  63. mainpartTitles.style.paddingBottom = "0";
  64. mainpartTitles.nextElementSibling.style.paddingTop = "0";
  65. }
  66. }
  67. // couleurs titres
  68. let colors = [
  69. {'red' : 'cf0118'},
  70. {'blue1': '0101c4'},
  71. {'blue2': '01049e'},
  72. {'blue3': '010678'},
  73. {'blue4': '010952'},
  74. {'blue5': '00113c'},
  75. {'yellow1': 'ade719'},
  76. {'yellow2': '8cc700'},
  77. {'yellow3': '74af00'},
  78. {'yellow4': '5c9900'},
  79. {'yellow5': '377600'},
  80. {'pink1': 'cf0118'},
  81. {'pink2': 'a10418'}
  82. ];
  83. let titles = document.querySelectorAll('.isMainPart, .isSubPart');
  84. let partTitles = [];
  85. for (let title of titles) {
  86. if (!title.nextElementSibling.classList.contains('isSubPart')) {
  87. partTitles.push(title);
  88. }
  89. }
  90. for (let i = 0; i < partTitles.length; i++) {
  91. if (partTitles[i].previousElementSibling?.classList.contains('isMainPart')) {
  92. partTitles[i].previousElementSibling.firstElementChild.firstElementChild.style.backgroundColor = "#" + Object.values(colors[i])[0];
  93. }
  94. partTitles[i].firstElementChild.firstElementChild.style.backgroundColor = "#" + Object.values(colors[i])[0];
  95. }
  96. // set parts rectangles
  97. function convertToSeconds(timeStr) {
  98. let [hourStr, minStr] = timeStr.split('h');
  99. if (!minStr) {
  100. minStr = hourStr;
  101. hourStr = '0';
  102. }
  103. minStr = minStr.replace('’', ':');
  104. minStr = minStr.replace("'", ':');
  105. const [min, sec] = minStr.split(':');
  106. return parseInt(hourStr) * 3600 + parseInt(min) * 60 + parseInt(sec);
  107. }
  108. let partTimesNodes = document.querySelectorAll('body #app main tbody tr:not(.isContentPart) + tr:not(.isSubPart) td:first-of-type');
  109. let partTimes = Array.from(partTimesNodes);
  110. for (let i = 0; i < partTimes.length; i++) {
  111. partTimes[i] = convertToSeconds(partTimes[i].innerText);
  112. }
  113. let lastTime = document.querySelectorAll('body #app main tbody tr:last-of-type td:first-of-type');
  114. partTimes.push(convertToSeconds(lastTime[0].innerText));
  115. let partDurations = [];
  116. for (let i = 0; i < partTimes.length - 1; i++) {
  117. partDurations.push(partTimes[i + 1] - partTimes[i]);
  118. }
  119. let totalTime = convertToSeconds(lastTime[0].innerText);
  120. let tlContainer, tlContainerHeight, bodyHeight;
  121. let partBlockHeights = [];
  122. let activePartIndex = 0;
  123. function getAllHeights() {
  124. partBlockHeights = [];
  125. tlContainer = document.querySelector('#timeline_container');
  126. let header = document.querySelector('header'), footer = document.querySelector('footer');
  127. tlContainer.style.height = `calc(100vh - ${header.offsetHeight}px - ${footer.offsetHeight}px - 60px)`;
  128. tlContainer.style.top = `${document.querySelector('header').offsetHeight + 30}px`;
  129. tlContainerHeight = tlContainer.offsetHeight;
  130. bodyHeight = document.querySelector('body').offsetHeight;
  131. for (let partDuration of partDurations) {
  132. partBlockHeights.push(partDuration / totalTime * tlContainerHeight);
  133. }
  134. }
  135. getAllHeights();
  136. function drawFriseRects() {
  137. for (let i = 0; i < partBlockHeights.length; i++) {
  138. let partDiv = document.createElement('div');
  139. partDiv.style.width = "22px";
  140. partDiv.style.height = partBlockHeights[i] + "px";
  141. partDiv.style.backgroundColor = "#" + Object.values(colors[i])[0];
  142. partDiv.style.borderBottom = "solid 1px #010d19";
  143. partDiv.style.borderTop = "solid 1px #010d19";
  144. partDiv.style.transition = "width 0.3s ease-out";
  145. partDiv.style.cursor = "pointer";
  146. tlContainer.prepend(partDiv);
  147. tlContainer.children[0].addEventListener("mouseenter", function() {
  148. let el = tlContainer.children[tlContainer.children.length - 1 - i];
  149. if (Array.from(el.parentNode.children).length - Array.from(el.parentNode.children).indexOf(el) - 1 != activePartIndex) {
  150. el.style.width = "32px";
  151. toggleTitleHover(i, 'show');
  152. }
  153. });
  154. tlContainer.children[0].addEventListener("mouseleave", function() {
  155. let el = tlContainer.children[tlContainer.children.length - 1 - i];
  156. if (Array.from(el.parentNode.children).length - Array.from(el.parentNode.children).indexOf(el) - 1 != activePartIndex) {
  157. el.style.width = "22px";
  158. }
  159. toggleTitleHover(i, 'hide');
  160. });
  161. tlContainer.children[0].addEventListener("click", function() {
  162. titresFrise[i].el.scrollIntoView({ behavior: 'smooth', block: 'center' });
  163. });
  164. }
  165. }
  166. drawFriseRects();
  167. window.addEventListener('resize', () => {
  168. while(tlContainer.lastChild) {
  169. tlContainer.removeChild(tlContainer.lastChild);
  170. }
  171. getAllHeights();
  172. drawFriseRects();
  173. });
  174. // titres parties frise
  175. let titreFriseEl = document.createElement('div');
  176. let elements = Array.from(document.querySelectorAll('.isMainPart, .isSubPart'));
  177. let mainWithoutSubAfter = [];
  178. let mainWithSubAfter = [];
  179. let subWithoutMainBefore = [];
  180. let lastMain = null;
  181. for (let i = 0; i < elements.length; i++) {
  182. let current = elements[i];
  183. let next = elements[i + 1];
  184. if (current.classList.contains('isMainPart')) {
  185. lastMain = current;
  186. if (next && next.classList.contains('isSubPart')) {
  187. mainWithSubAfter.push({ index: i, main: current.innerText, sub: next.innerText, el: elements[i] });
  188. } else {
  189. mainWithoutSubAfter.push({ index: i, main: current.innerText, sub: "", el: elements[i] });
  190. }
  191. }
  192. if (current.classList.contains('isSubPart')) {
  193. let prevMain = lastMain && lastMain.classList.contains('isMainPart') ? lastMain.innerText : "";
  194. subWithoutMainBefore.push({ index: i, main: prevMain, sub: current.innerText, el: elements[i] });
  195. }
  196. }
  197. let titresFrise = [...mainWithoutSubAfter, ...mainWithSubAfter, ...subWithoutMainBefore];
  198. titresFrise.sort((a, b) => a.index - b.index);
  199. for (let i = titresFrise.length - 1; i > 0; i--) {
  200. let current = titresFrise[i];
  201. let next = titresFrise[i - 1];
  202. if (current.main === next.main && current.sub === next.sub) {
  203. titresFrise.splice(i, 1);
  204. }
  205. }
  206. titreFriseEl.setAttribute('id', 'titres_frise');
  207. titreFriseEl.innerHTML = `
  208. <p class="uppercase">${getCurrentTime(titresFrise[0].el)}</p>
  209. <p>${titresFrise[0].main}</p>
  210. <p class="font-authentic-60">${titresFrise[0].sub}</p>
  211. `;
  212. titreFriseEl.style.fontSize = '0.9em';
  213. titreFriseEl.style.zIndex = '0';
  214. titreFriseEl.style.position = 'fixed';
  215. titreFriseEl.style.left = '60px';
  216. titreFriseEl.style.width = '10vw';
  217. titreFriseEl.style.lineHeight = '1.2';
  218. titreFriseEl.style.marginTop = '-8px';
  219. titreFriseEl.style.top = `${document.querySelector('#timeline_container').getBoundingClientRect().top}px`;
  220. let main = document.querySelector('#main');
  221. main.prepend(titreFriseEl);
  222. // création et togle des titres au survol des div de la timeline
  223. function drawFixedTitles() {
  224. let tlContainer = document.querySelector('#timeline_container');
  225. let fixedTitlesContainer = document.createElement('div');
  226. fixedTitlesContainer.setAttribute('id', 'fixedTitlesContainer');
  227. main.prepend(fixedTitlesContainer);
  228. for (let index = 0; index < titresFrise.length; index++) {
  229. let titreFixedEl = document.createElement('div');
  230. titreFixedEl.style.position = 'fixed';
  231. titreFixedEl.style.zIndex = 1;
  232. titreFixedEl.style.display = 'none';
  233. titreFixedEl.style.opacity = '0';
  234. titreFixedEl.style.transition = 'opacity 0.3s ease-out';
  235. titreFixedEl.style.width = '10vw';
  236. titreFixedEl.style.top = `${tlContainer.children[index].getBoundingClientRect().top - 8}px`;
  237. titreFixedEl.style.left = '60px';
  238. titreFixedEl.style.fontSize = '0.9em';
  239. titreFixedEl.style.lineHeight = '1.2';
  240. let titreFixedElContent = document.createElement('p');
  241. titreFixedElContent.innerHTML = `
  242. <p>${titresFrise[titresFrise.length - index - 1].main}</p>
  243. <p class="font-authentic-60">${titresFrise[titresFrise.length - index - 1].sub}</p>
  244. `;
  245. titreFixedElContent.style.backgroundColor = '#010d19';
  246. titreFixedEl.append(titreFixedElContent);
  247. let topGradient = document.createElement('div');
  248. topGradient.style.height = '10px';
  249. topGradient.style.width = '100%';
  250. topGradient.style.background = 'linear-gradient(to bottom, transparent, #010d19)';
  251. titreFixedEl.prepend(topGradient);
  252. let bottomGradient = document.createElement('div');
  253. bottomGradient.style.height = '10px';
  254. bottomGradient.style.width = '100%';
  255. bottomGradient.style.background = 'linear-gradient(to top, transparent, #010d19)';
  256. titreFixedEl.append(bottomGradient);
  257. fixedTitlesContainer.prepend(titreFixedEl);
  258. }
  259. }
  260. drawFixedTitles();
  261. function toggleTitleHover(elIndex, state) {
  262. let fixedTitlesContainer = document.querySelector('#fixedTitlesContainer');
  263. let el = fixedTitlesContainer.children[elIndex];
  264. if (state === 'show') {
  265. el.style.display = 'block';
  266. setTimeout(() => {
  267. el.style.opacity = '1';
  268. }, 1);
  269. } else if (state === 'hide') {
  270. el.style.opacity = '0';
  271. setTimeout(() => {
  272. el.style.display = 'none';
  273. }, 300);
  274. }
  275. }
  276. // make the cursor move on scroll
  277. let cursor = document.querySelector('#cursor');
  278. cursor.style.top = `${document.querySelector('#timeline_container').getBoundingClientRect().top}px`;
  279. cursor.style.cursor = 'grab';
  280. let titres = document.querySelector('#titres_frise');
  281. titres.style.cursor = 'grab';
  282. let partRects = document.querySelectorAll('#timeline_container div');
  283. partRects = Array.from(partRects);
  284. partRects = partRects.reverse();
  285. partRects[0].style.width = '32px';
  286. let grabbing = false;
  287. document.addEventListener("scroll", (event) => {
  288. if (!grabbing) {
  289. displayCurrentPartTitle(getCurrentPartFromScroll());
  290. moveCursorFromScroll(getCurrentPartFromScroll());
  291. }
  292. });
  293. function makeElementDraggable(element, relatedEl) {
  294. let offsetY;
  295. element.addEventListener('mousedown', (e) => {
  296. e.preventDefault();
  297. grabbing = true;
  298. offsetY = e.clientY - +element.style.top.slice(0, -2);
  299. element.style.cursor = 'grabbing';
  300. });
  301. document.addEventListener('mousemove', (e) => {
  302. if (grabbing) {
  303. if (e.clientY < tlContainerHeight + tlContainer.offsetTop && e.clientY > tlContainer.offsetTop) {
  304. const y = e.clientY - offsetY;
  305. element.style.top = `${y}px`;
  306. relatedEl.style.top = `${y}px`;
  307. if (!isNaN(y)) displayCurrentPartTitle(getCurrentPartFromCursor(y));
  308. } else if (e.clientY <= tlContainer.offsetTop) {
  309. element.style.top = `${tlContainer.offsetTop}px`;
  310. relatedEl.style.top = `${tlContainer.offsetTop}px`;
  311. } else if (e.clientY >= tlContainerHeight + tlContainer.offsetTop) {
  312. element.style.top = `${tlContainerHeight + tlContainer.offsetTop}px`;
  313. relatedEl.style.top = `${tlContainerHeight + tlContainer.offsetTop}px`;
  314. }
  315. }
  316. });
  317. document.addEventListener('mouseup', () => {
  318. grabbing = false;
  319. element.style.cursor = 'grab';
  320. });
  321. }
  322. makeElementDraggable(cursor, titres);
  323. makeElementDraggable(titres, cursor);
  324. /* let cursorPrevTranslateAmount = +cursor.style.transform.slice(0, -3).slice(11);
  325. // make the cursor draggable
  326. let prevMousePos = 0;
  327. cursor.addEventListener("mousedown", (event) => {
  328. if (!grabbing) {
  329. event.preventDefault();
  330. grabbing = true;
  331. prevMousePos = event.clientY;
  332. }
  333. });
  334. window.addEventListener("mousemove", (event) => {
  335. if (grabbing) {
  336. event.preventDefault();
  337. let newPos = cursorPrevTranslateAmount + (event.clientY - prevMousePos);
  338. if (event.clientY < tlContainerHeight + tlContainer.offsetTop && event.clientY > tlContainer.offsetTop) {
  339. cursor.style.transform = `translateY(${newPos}px)`;
  340. window.scrollBy(0, (event.clientY - prevMousePos) * (bodyHeight / tlContainerHeight));
  341. cursorPrevTranslateAmount = +cursor.style.transform.slice(0, -3).slice(11);
  342. } else if (event.clientY <= tlContainer.offsetTop) {
  343. cursor.style.transform = "translateY(0px)";
  344. window.scroll(0, 0);
  345. cursorPrevTranslateAmount = 0;
  346. } else if (event.clientY >= tlContainerHeight + tlContainer.offsetTop) {
  347. cursor.style.transform = `translateY(${tlContainerHeight}px)`;
  348. window.scroll(0, bodyHeight);
  349. cursorPrevTranslateAmount = +cursor.style.transform.slice(0, -3).slice(11);
  350. }
  351. prevMousePos = event.clientY;
  352. }
  353. });
  354. window.addEventListener("mouseup", (event) => {
  355. if (grabbing) {
  356. grabbing = false;
  357. }
  358. });
  359. */
  360. // get heights of parts dans le tableau et dans la timeline
  361. function getSteps() {
  362. let tlElSteps = [];
  363. for (let i = 0; i < partRects.length; i++) {
  364. tlElSteps.push(partRects[i].offsetTop);
  365. }
  366. let titles = document.querySelectorAll('.isMainPart, .isSubPart');
  367. let scrollSteps = [];
  368. for (let title of titles) {
  369. let nextLine = title.nextElementSibling;
  370. if (!nextLine?.classList.contains('isSubPart')) {
  371. scrollSteps.push(title.offsetTop);
  372. }
  373. }
  374. return({
  375. 'tlElSteps':tlElSteps,
  376. 'scrollSteps':scrollSteps
  377. });
  378. }
  379. function moveCursorFromScroll(currentPartIndex) {
  380. let currentScroll = window.scrollY;
  381. let cursorTopValue = 0;
  382. let tlPartHeight, tlPartBottom;
  383. for (let j = 0; j < partRects.length; j++) {
  384. tlPartHeight = partRects[j].getBoundingClientRect().height
  385. tlPartBottom = partRects[j].getBoundingClientRect().top - tlContainer.getBoundingClientRect().top;
  386. cursorTopValue = tlPartBottom;
  387. // get the amount of the current scrollpart scrolled
  388. if (j === currentPartIndex) break;
  389. }
  390. let currentScrollPartBottom = getSteps().scrollSteps[currentPartIndex - 1] ? getSteps().scrollSteps[currentPartIndex - 1] : 0;
  391. let currentScrollPartHeight;
  392. if (getSteps().scrollSteps[currentPartIndex + 1]) {
  393. currentScrollPartHeight = getSteps().scrollSteps[currentPartIndex + 1] - currentScrollPartBottom;
  394. } else {
  395. currentScrollPartHeight = document.querySelector('body').scrollHeight - currentScrollPartBottom;
  396. }
  397. let currentScrollSincePartBottom = currentScroll - currentScrollPartBottom;
  398. let scrollPartProportion = currentScrollSincePartBottom / currentScrollPartHeight;
  399. cursorTopValue = cursorTopValue + tlPartHeight * (tlPartHeight / tlPartHeight * (scrollPartProportion - 0.3));
  400. if (cursorTopValue > 0) {
  401. cursor.style.transform = `translateY(${cursorTopValue}px)`;
  402. titres.style.transform = `translateY(${cursorTopValue}px)`;
  403. } else {
  404. cursor.style.transform = `translateY(0px)`;
  405. titres.style.transform = `translateY(0px)`;
  406. }
  407. }
  408. let prevPartIndex = 0;
  409. function displayCurrentPartTitle(currentPartIndex) {
  410. titres.firstElementChild.innerText = getCurrentTime(titresFrise[currentPartIndex]?.el);
  411. let mainEl = titres.children[1];
  412. let subEl = titres.lastElementChild;
  413. if (mainEl.innerText != titresFrise[currentPartIndex]?.main || subEl.innerText != titresFrise[currentPartIndex]?.sub) {
  414. mainEl.innerText = titresFrise[currentPartIndex]?.main;
  415. subEl.innerText = titresFrise[currentPartIndex]?.sub;
  416. partRects[prevPartIndex].style.width = '22px';
  417. partRects[currentPartIndex].style.width = '32px';
  418. }
  419. activePartIndex = currentPartIndex;
  420. prevPartIndex = currentPartIndex;
  421. }
  422. function getCurrentPartFromScroll() {
  423. let currentScroll = window.scrollY;
  424. for (let i = 0; i < getSteps().scrollSteps.length; i++) {
  425. if (
  426. (currentScroll + window.innerHeight / 2 >= getSteps().scrollSteps[i]
  427. && currentScroll + window.innerHeight / 2 <= getSteps().scrollSteps[i + 1]) ||
  428. (currentScroll + window.innerHeight / 2 >= getSteps().scrollSteps[i]
  429. && i === getSteps().scrollSteps.length - 1)
  430. ) {
  431. return(i);
  432. }
  433. }
  434. }
  435. function getCurrentPartFromCursor(cursorTop) {
  436. for (let i = 0; i < getSteps().tlElSteps.length; i++) {
  437. if (cursorTop - tlContainer.offsetTop >= getSteps().tlElSteps[i] && cursorTop - tlContainer.offsetTop < getSteps().tlElSteps[i + 1]) {
  438. return(i);
  439. } else if (cursorTop - tlContainer.offsetTop > getSteps().tlElSteps[getSteps().tlElSteps.length - 1]) {
  440. return(getSteps().tlElSteps.length - 1);
  441. }
  442. }
  443. }
  444. function getCurrentTime(titleEl) {
  445. let nextRow = titleEl?.nextElementSibling;
  446. while(nextRow) {
  447. if (nextRow.offsetTop > window.scrollY &&
  448. nextRow.classList.contains('isContentPart')) {
  449. return(nextRow.firstElementChild.innerText);
  450. }
  451. nextRow = nextRow.nextElementSibling;
  452. }
  453. }
  454. });
  455. /**
  456. * @see {@link https://webpack.js.org/api/hot-module-replacement/}
  457. */
  458. if (import.meta.webpackHot) import.meta.webpackHot.accept(console.error);