script.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. $(document).ready( () => {
  2. lazyLoading()
  3. displayPictograms()
  4. getDeviceSize()
  5. hideMobileNavOnResize()
  6. hoverDesktopNavItems()
  7. if (currentPage.text().trim() == 'Accueil') {
  8. hideFooter()
  9. toggleIndexContent()
  10. } else if (currentPage.text().trim() == 'Projets' && $('main').children(":first").attr('id') != 'reader') {
  11. hideFooter()
  12. loadProjetIndex()
  13. } else if ($('main').children(":first").attr('id') == 'reader') {
  14. hideFooter()
  15. displayReader()
  16. }
  17. initBarba()
  18. })
  19. // GLOBAL VARIABLES
  20. let isMobileNavOpen = false,
  21. isDesktopDevice = false,
  22. currentPage,
  23. filterObj = {
  24. publiqueCardsDisplayed : true,
  25. socialeCardsDisplayed : true,
  26. culturelleCardsDisplayed : true
  27. },
  28. projectTitleIsScrolling = false,
  29. titleScrollTimer
  30. // MAIN FUNCTIONS
  31. function initBarba() {
  32. barba.init({
  33. prefetchIgnore: true,
  34. transitions: [{
  35. name: 'opacity-transition',
  36. leave(data) {
  37. return gsap.to(data.current.container, {
  38. opacity: 0
  39. })
  40. },
  41. enter(data) {
  42. return gsap.from(data.next.container, {
  43. opacity: 0
  44. })
  45. }
  46. }]
  47. })
  48. barba.hooks.beforeLeave((data) => {
  49. if (isMobileNavOpen) { toggleMobileNav() }
  50. if ($(data.current.container).attr('id') == 'projets-index') { resetFilters() }
  51. if ($(data.current.container).attr('id') == 'text-content' ) { hideFooter() }
  52. })
  53. barba.hooks.afterLeave((data) => {
  54. if ($(data.current.container).attr('id') == 'reader') {
  55. if ($(data.next.container).attr('id') != 'reader') {
  56. leaveReader()
  57. $('#nav-container h1').empty().append(currentPage.text())
  58. }
  59. }
  60. })
  61. barba.hooks.beforeEnter((data) => {
  62. lazyLoading()
  63. displayPictograms()
  64. $('html, body').scrollTop(0)
  65. if ($(data.next.container).attr('id') == 'index-content') {
  66. toggleIndexContent()
  67. } else if ($(data.next.container).attr('id') == 'projets-index') {
  68. loadProjetIndex()
  69. filterWhenEntering()
  70. } else if ($(data.next.container).attr('id') == 'text-content')
  71. { if (isDesktopDevice) { displayFooter() } }
  72. })
  73. barba.hooks.after((data) => {
  74. let pageTitleComparator, navIndex
  75. switch ($('main').children(":first").attr('id')) {
  76. case 'index-content':
  77. pageTitleComparator = 'Accueil'
  78. navIndex = 0
  79. break
  80. case 'text-content':
  81. // dirty hack to get what content it is
  82. if ($('main').children(":first").children(":first").text().substring(3).replace(/ .*/,'') == "équipe") {
  83. pageTitleComparator = 'Le collectif'
  84. navIndex = 1;
  85. } else {
  86. pageTitleComparator = 'Logiciels Libres'
  87. navIndex = 3;
  88. }
  89. break
  90. case 'projets-index':
  91. case 'reader':
  92. pageTitleComparator = 'Projets'
  93. navIndex = 2
  94. break
  95. }
  96. if (pageTitleComparator != currentPage.text().trim()) {
  97. changeMenuSelected($('header nav ul li').eq(navIndex))
  98. }
  99. if ($(data.next.container).attr('id') == 'reader') {
  100. displayReader()
  101. }
  102. })
  103. }
  104. function lazyLoading() {
  105. $('.lazy').Lazy({
  106. scrollDirection: 'vertical',
  107. effect: 'fadeIn',
  108. effectTime: 400,
  109. threshold: 100,
  110. afterLoad: function(element) {
  111. $(element).removeClass('loader')
  112. if ($(element).parent().is('figure')) {
  113. $(element).parent().addClass('loaded')
  114. } else {
  115. $(element).parentsUntil('figure').parent().addClass('loaded')
  116. }
  117. }
  118. })
  119. }
  120. // UI FUNCTIONS
  121. // general content
  122. function displayPictograms() {
  123. // To be able to change the pictograms color through scss variables
  124. addLinkPictogram()
  125. $('.picto').each((i, el) => {
  126. let source = $(el).data('src')
  127. $(el).css({
  128. '-webkit-mask': 'url(' + source + ') 0% 0% / contain no-repeat',
  129. 'mask': 'url(' + source + ') 0% 0% / contain no-repeat',
  130. })
  131. })
  132. }
  133. function addLinkPictogram() {
  134. $('main a[target="_blank"]').each( (i, e) => {
  135. $(e).append('<div class="picto picto-url" data-src="/user/themes/figureslibres-v2/images/pictos/arrow-diagonal.svg" aria-hidden="true"></div>')
  136. })
  137. // hardcoded url because cant use twig in js
  138. }
  139. function getDeviceSize() {
  140. function setDeviceSize() {
  141. if (window.matchMedia('(min-width: 996px)').matches) {
  142. isDesktopDevice = true
  143. } else {
  144. isDesktopDevice = false
  145. }
  146. }
  147. setDeviceSize()
  148. let timer
  149. $(window).on('resize', () => {
  150. clearTimeout(timer)
  151. timer = setTimeout(setDeviceSize(), 100)
  152. refreshUiOnResize()
  153. })
  154. }
  155. function refreshUiOnResize() {
  156. hideFooterOnResize()
  157. hideMobileNavOnResize()
  158. checkTitleAutoScroll()
  159. }
  160. function triggerLazyLoading() {
  161. setTimeout(() => {
  162. $(document).scrollTop($(document).scrollTop() + 1)
  163. setTimeout(() => {
  164. $(document).scrollTop($(document).scrollTop() - 1)
  165. }, 100)
  166. }, 300)
  167. }
  168. function titleScrollTop() {
  169. if ($('html, body').scrollTop() > 0) {
  170. $('html, body').animate({scrollTop: 0}, 1200)
  171. if (currentPage.text().trim() == 'Accueil') {
  172. toggleIndexContent(true)
  173. }
  174. }
  175. }
  176. // set navigation
  177. function toggleMobileNav() {
  178. if (!isMobileNavOpen) {
  179. $('nav').addClass('mobile-nav-open')
  180. $('footer').fadeIn()
  181. isMobileNavOpen = true
  182. } else {
  183. $('nav').removeClass('mobile-nav-open')
  184. isMobileNavOpen = false
  185. if (window.matchMedia('(min-width: 576px)').matches && (currentPage.text().trim() == 'Figures Libres' || currentPage.text().trim() == 'Logiciels Libres')) {
  186. return
  187. } else {
  188. $('footer').fadeOut()
  189. }
  190. }
  191. }
  192. function hideMobileNavOnResize() {
  193. if (!isDesktopDevice) {
  194. if (isMobileNavOpen) {
  195. toggleMobileNav()
  196. }
  197. $('nav').css('display', 'none')
  198. setTimeout( () => {
  199. $('nav').css('display', 'flex')
  200. }, 200)
  201. }
  202. }
  203. function hoverDesktopNavItems() {
  204. currentPage = $('.selected')
  205. $('nav li').hover(() => {
  206. if (isDesktopDevice) {
  207. currentPage.removeClass('selected')
  208. }
  209. }, () => {
  210. if (isDesktopDevice) {
  211. currentPage.addClass('selected')
  212. }
  213. })
  214. }
  215. function changeMenuSelected(page) {
  216. $('.selected').removeClass('selected')
  217. currentPage = $(page)
  218. currentPage.addClass('selected')
  219. changeMobileTitle()
  220. }
  221. function changeMobileTitle() {
  222. $('#nav-container h1').empty().append(currentPage.text())
  223. }
  224. // index interaction
  225. function toggleIndexContent(closeEverything) {
  226. let isTextOpen = false,
  227. isCommanditairesOpen = false,
  228. isExtraitOpen = false,
  229. quelExtrait
  230. if (closeEverything) {
  231. closeEverything()
  232. }
  233. $('#second-p-index, #commanditaires-grid, #extrait-publique, #extrait-social, #extrait-culturelle').children().fadeOut()
  234. $('#arrowIndex').click( () => {
  235. if (!isTextOpen && !isExtraitOpen) {
  236. if (isDesktopDevice) {
  237. displayContent($('#second-p-index'), '70vh')
  238. } else {
  239. displayContent($('#second-p-index'), '90vh')
  240. }
  241. turnArrow('down')
  242. isTextOpen = true
  243. } else {
  244. if (isExtraitOpen) {
  245. hideContent($('#extrait-' + quelExtrait))
  246. $('#' + quelExtrait).css('text-decoration', 'none')
  247. isExtraitOpen = false
  248. turnArrow('top')
  249. }
  250. if (isTextOpen) {
  251. hideContent($('#second-p-index'))
  252. turnArrow('top')
  253. isTextOpen = false
  254. }
  255. if (isCommanditairesOpen) {
  256. hideContent($('#commanditaires-grid'))
  257. $('#commanditaires').css('text-decoration', 'none')
  258. isCommanditairesOpen = false
  259. }
  260. }
  261. })
  262. $('#index-content .animateText').click( function(event) {
  263. target = $(event.target).attr('id')
  264. if (target == 'publique' || target == 'sociale' || target == 'culturelle') {
  265. toggleExtrait(target)
  266. return false
  267. } else if (target == 'commanditaires') {
  268. toggleCommanditaires()
  269. return false
  270. } else {
  271. switch (target) {
  272. case 'figureslibres':
  273. changeMenuSelected($('nav li a[href$=collectif]').parent())
  274. break
  275. case 'logicielslibres':
  276. changeMenuSelected($('nav li a[href$=logiciels-libres]').parent())
  277. break
  278. case 'projets':
  279. changeMenuSelected($('nav li a[href$=projets]').parent())
  280. break
  281. }
  282. }
  283. })
  284. $('#extrait-projets .projets-link-filter').click(() => {
  285. changeMenuSelected($('nav li a[href$=projets]').parent())
  286. })
  287. function toggleExtrait(category) {
  288. if(!isExtraitOpen) {
  289. if (isTextOpen) {
  290. hideContent($('#second-p-index'))
  291. isTextOpen = false
  292. }
  293. if (isCommanditairesOpen) {
  294. hideContent($('#commanditaires-grid'))
  295. $('#commanditaires').css('text-decoration', 'none')
  296. isCommanditairesOpen = false
  297. }
  298. displayContent($('#extrait-' + category), '80vh')
  299. turnArrow('down')
  300. $('#' + category).css({'text-decoration': 'underline', 'text-decoration-thickness': '2px', 'text-underline-offset': '8px'})
  301. isExtraitOpen = true
  302. quelExtrait = category
  303. } else if (isExtraitOpen && quelExtrait != category) {
  304. hideContent($('#extrait-' + quelExtrait))
  305. $('#' + category).css({'text-decoration': 'underline', 'text-decoration-thickness': '2px', 'text-underline-offset': '8px'})
  306. $('#' + quelExtrait).css('text-decoration', 'none')
  307. setTimeout(() => {
  308. displayContent($('#extrait-' + category), '80vh')
  309. quelExtrait = category
  310. }, 1000)
  311. } else if (isExtraitOpen && quelExtrait == category) {
  312. hideContent($('#extrait-' + category))
  313. turnArrow('top')
  314. $('#' + category).css('text-decoration', 'none')
  315. isExtraitOpen = false
  316. }
  317. }
  318. function toggleCommanditaires() {
  319. if (!isCommanditairesOpen) {
  320. displayContent($('#commanditaires-grid'), '150vh')
  321. $('#commanditaires').css({'text-decoration': 'underline', 'text-decoration-thickness': '2px', 'text-underline-offset': '8px'})
  322. isCommanditairesOpen = true
  323. } else {
  324. hideContent($('#commanditaires-grid'))
  325. $('#commanditaires').css('text-decoration', 'none')
  326. isCommanditairesOpen = false
  327. }
  328. }
  329. function closeEverything() {
  330. if (isTextOpen) {
  331. hideContent($('#second-p-index'))
  332. isTextOpen = false
  333. }
  334. if (isExtraitOpen) {
  335. hideContent($('#extrait-' + quelExtrait))
  336. $('#' + quelExtrait).css('text-decoration', 'none')
  337. isExtraitOpen = false
  338. }
  339. if (isCommanditairesOpen) {
  340. hideContent($('#commanditaires-grid'))
  341. $('#commanditaires').css('text-decoration', 'none')
  342. isCommanditairesOpen = false
  343. }
  344. turnArrow('top')
  345. }
  346. }
  347. function turnArrow(direction) {
  348. if (direction == 'down') {
  349. $('#arrow-container').css({
  350. 'transform': 'rotate(180deg)',
  351. 'margin-bottom': '10vh'
  352. })
  353. $('#arrow-container .picto').removeAttr('id')
  354. } else if (direction == 'top') {
  355. $('#arrow-container').css({
  356. 'transform': 'rotate(0deg)',
  357. 'margin-bottom': '0vh'
  358. })
  359. $('#arrow-container .picto').attr('id', 'arrowIndex')
  360. }
  361. }
  362. function displayContent(content, maxHeight) {
  363. content.children().fadeIn()
  364. setTimeout( () => {
  365. content.css({
  366. 'max-height': maxHeight,
  367. 'opacity': '1'
  368. })
  369. if (isDesktopDevice) {
  370. content.css('margin-top', '3vh')
  371. }
  372. }, 200)
  373. setTimeout( () => {
  374. $('html, body').animate({
  375. scrollTop: $(content).position().top
  376. }, 1200, 'swing')
  377. }, 400)
  378. }
  379. function hideContent(content) {
  380. content.css({
  381. 'max-height': '0vh',
  382. 'opacity': '0',
  383. 'margin-top': '0vh'
  384. })
  385. setTimeout(() => {
  386. content.children().fadeOut()
  387. }, 1200)
  388. }
  389. // display project grid
  390. function toggleCategory(category) {
  391. let chevron = $(category).find('.chevron-category')
  392. if (chevron.hasClass('close')) {
  393. $(chevron.removeClass('close'))
  394. $(category).next().slideToggle('slow')
  395. $('.card-displayed figure').each(function() {
  396. if ($(this).css('transform') != 'none') {
  397. $(this).css('transform', 'none')
  398. }
  399. })
  400. } else {
  401. $(chevron.addClass('close'))
  402. $(category).next().slideToggle('slow')
  403. triggerLazyLoading()
  404. }
  405. }
  406. function filterCards(utilite) {
  407. let filterIsOn
  408. function checkFilter() {
  409. switch (utilite) {
  410. case 'publique':
  411. filterIsOn = filterObj.publiqueCardsDisplayed ? true : false
  412. if (filterObj.publiqueCardsDisplayed) {
  413. filterObj.publiqueCardsDisplayed = false
  414. } else {
  415. filterObj.publiqueCardsDisplayed = true
  416. }
  417. break
  418. case 'sociale':
  419. filterIsOn = filterObj.socialeCardsDisplayed ? true : false
  420. if (filterObj.socialeCardsDisplayed) {
  421. filterObj.socialeCardsDisplayed = false
  422. } else {
  423. filterObj.socialeCardsDisplayed = true
  424. }
  425. break
  426. case 'culturelle':
  427. filterIsOn = filterObj.culturelleCardsDisplayed ? true : false
  428. if (filterObj.culturelleCardsDisplayed) {
  429. filterObj.culturelleCardsDisplayed = false
  430. } else {
  431. filterObj.culturelleCardsDisplayed = true
  432. }
  433. break
  434. }
  435. }
  436. if (Object.values(filterObj).every(Boolean) || Object.values(filterObj).every(e => e == false)) { // si tous les filtres sont actifs (état de base)
  437. $('.projets-grid div').each(function () { // hide all cards
  438. if($(this).hasClass(utilite + '-card')) { // except those from the filtered category
  439. $(this).removeClass('card-hidden').addClass('card-displayed')
  440. }
  441. else {
  442. $(this).removeClass('card-displayed').addClass('card-hidden')
  443. }
  444. })
  445. $('#' + utilite + '-filter').css('opacity', 1)
  446. Object.keys(filterObj).forEach(v => filterObj[v] = false)
  447. checkFilter()
  448. }
  449. else if (Object.values(filterObj).filter(Boolean).length == 1) {
  450. // check if the filter is on or off
  451. checkFilter()
  452. if (filterIsOn) {
  453. $('#' + utilite + '-filter').css('opacity', 0.4)
  454. $('.projets-grid div').each(function () { // display all cards
  455. $(this).removeClass('card-hidden').addClass('card-displayed')
  456. })
  457. } else {
  458. $('#' + utilite + '-filter').css('opacity', 1)
  459. $('.' + utilite + '-card').each(function () { // only display cards from selected category
  460. $(this).removeClass('card-hidden').addClass('card-displayed')
  461. })
  462. }
  463. }
  464. else if (Object.values(filterObj).filter(Boolean).length == 2) {
  465. checkFilter()
  466. if (filterIsOn) {
  467. $('#' + utilite + '-filter').css('opacity', 0.4)
  468. $('.' + utilite + '-card').each(function () { // only display cards from selected category
  469. $(this).removeClass('card-displayed').addClass('card-hidden')
  470. })
  471. } else {
  472. $('.filter-button').each(function() {
  473. $(this).css('opacity', 0.4)
  474. })
  475. $('.projets-grid div').each(function () { // hide all cards
  476. $(this).removeClass('card-hidden').addClass('card-displayed')
  477. })
  478. }
  479. }
  480. triggerLazyLoading()
  481. checkIfCategoryEmpty() // to hide empty category
  482. function checkIfCategoryEmpty() {
  483. $('.projets-grid').each(function() {
  484. let hiddenCount = 0
  485. $(this).children().each(function() {
  486. if ($(this).hasClass('card-hidden')) { hiddenCount++ }
  487. })
  488. if (hiddenCount == $(this).children().length) { $(this).prev().fadeOut() }
  489. else { $(this).prev().fadeIn() }
  490. })
  491. }
  492. }
  493. function loadProjetIndex() {
  494. animateProjectGrid()
  495. isProjetsIndexLoaded = true
  496. }
  497. function animateProjectGrid() {
  498. const grid = document.querySelector(".projets-grid")
  499. animateCSSGrid.wrapGrid(grid, {duration : 600})
  500. }
  501. function resetFilters() {
  502. Object.keys(filterObj).forEach(v => filterObj[v] = false)
  503. }
  504. function filterWhenEntering() {
  505. let searchParam = new URLSearchParams(window.location.search)
  506. if (searchParam.has('filter')) {
  507. switch (searchParam.get('filter')) {
  508. case 'publique':
  509. filterCards('publique')
  510. break
  511. case 'sociale':
  512. filterCards('sociale')
  513. break
  514. case 'culturelle':
  515. filterCards('culturelle')
  516. break
  517. }
  518. }
  519. }
  520. // display reader
  521. function displayReader() {
  522. const swiper = new Swiper('.swiper', {
  523. // Optional parameters
  524. loop: true,
  525. // Navigation arrows
  526. navigation: {
  527. nextEl: '.swiper-button-next',
  528. prevEl: '.swiper-button-prev',
  529. },
  530. });
  531. $('#cover-image img, .project-image').click( function(event) {
  532. const totalImg = $('img')
  533. let indexImg = 0
  534. for (let img of totalImg) {
  535. if (event.target === img) { break }
  536. indexImg++
  537. }
  538. swiper.slideToLoop(indexImg - 1)
  539. $('body').css('overflow-y', 'hidden');
  540. swiper.el.style.display = "block";
  541. setTimeout(() => {
  542. swiper.el.style.opacity = "1";
  543. }, 10);
  544. })
  545. $('#close-carousel').click( () => {
  546. $('body').css('overflow-y', 'auto');
  547. swiper.el.style.opacity = "0";
  548. setTimeout(() => {
  549. swiper.el.style.display = "none";
  550. }, 300);
  551. })
  552. $('header').fadeOut()
  553. projectTitleIsScrolling = false
  554. checkTitleAutoScroll()
  555. let isProjectHeaderDisplayed = false
  556. $(window).scroll( () => {
  557. if ($(window).scrollTop() > $('#main-project-title').offset().top && !isProjectHeaderDisplayed) {
  558. $('#header-project').css('top', '0vh')
  559. if(isDesktopDevice) {
  560. $('#close-project').addClass('scrolled-cross')
  561. }
  562. isProjectHeaderDisplayed = true
  563. } else if ($(window).scrollTop() < $('#main-project-title').offset().top && isProjectHeaderDisplayed) {
  564. $('#header-project').css('top', '-9vh')
  565. if (isDesktopDevice) {
  566. $('#close-project').removeClass('scrolled-cross')
  567. }
  568. isProjectHeaderDisplayed = false
  569. }
  570. })
  571. }
  572. function leaveReader() {
  573. $(window).off('scroll')
  574. $('header').fadeIn()
  575. if (!$('nav li a[href$=projets]').parent().hasClass('selected')) {
  576. changeNavSelected('projets')
  577. }
  578. disableTitleAutoScroll()
  579. }
  580. function titleAutoScroll() {
  581. if (!projectTitleIsScrolling) {
  582. $('#header-project').append('<div id="gradient-long-title"></div>')
  583. projectTitleIsScrolling = true
  584. }
  585. $('#header-project h2 span').addClass('scrollText')
  586. $('#header-project h2 span').css({
  587. 'transition': 'transform 7s linear',
  588. 'transform': 'translate(-' + parseInt($('#header-project h2 span').width() / 2 + 8) + 'px, 0)'
  589. })
  590. titleScrollTimer = setTimeout( () => {
  591. $('#header-project h2 span').css({'transition': 'none', 'transform': 'translate(0, 0)'})
  592. titleAutoScroll()
  593. }, 7000)
  594. }
  595. function disableTitleAutoScroll() {
  596. $('#gradient-long-title').remove()
  597. $('#header-project h2 span').removeClass('scrollText').css({'transition': 'none', 'transform': 'translate(0, 0)'})
  598. projectTitleIsScrolling = false
  599. }
  600. function checkTitleAutoScroll() {
  601. if (titleScrollTimer != null) { clearTimeout(titleScrollTimer) }
  602. if ($('#header-project h2 span').width() > $('#header-project h2').width() && !projectTitleIsScrolling) {
  603. titleAutoScroll()
  604. } else if ($('#header-project h2 span').width() / 2 < $('#header-project h2').width() && projectTitleIsScrolling) {
  605. $('#header-project h2 span').removeClass('scrollText')
  606. disableTitleAutoScroll()
  607. }
  608. }
  609. // show and hide footer
  610. function displayFooter() {
  611. $('footer').fadeIn()
  612. }
  613. function hideFooter() {
  614. $('footer').fadeOut()
  615. }
  616. function hideFooterOnResize() {
  617. if ($('footer').css('display', 'flex') &&
  618. (window.matchMedia('(max-width: 576px)').matches ||
  619. $('.selected').text().trim() == 'Accueil' ||
  620. $('.selected').text().trim() == 'Projets')) {
  621. $('footer').css('display', 'none')
  622. }
  623. }