From 975ed6ee5df2a00e408b4be1e317de40b783f61a Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Sat, 5 Sep 2020 23:14:47 +0200 Subject: [PATCH] feat(Home): add sort by name or random button fix(Header): fix href div box for home link --- assets/development/scripts/home.js | 120 ++++++++++++++++------ assets/development/styles/home.css | 49 ++++++++- assets/production/scripts/home.js | 2 +- assets/production/styles/home.css | 2 +- assets/production/styles/organization.css | 2 +- views/base.twig | 2 +- views/home.twig | 18 +++- 7 files changed, 159 insertions(+), 36 deletions(-) diff --git a/assets/development/scripts/home.js b/assets/development/scripts/home.js index 01422b5..a224815 100644 --- a/assets/development/scripts/home.js +++ b/assets/development/scripts/home.js @@ -1,3 +1,14 @@ +/** + * Utils + */ +function createEl(className = false, elName = "div") { + let el = document.createElement(elName); + if (className != false) { + el.className = className; + } + return el; +} + /** * Nav management */ @@ -9,9 +20,10 @@ let navEnabler = document.getElementById('nav-enabler'); let navEnablerText = document.getElementById('nav-enabler-text'); let navEnablerIcon = document.getElementById('nav-enabler-icon'); let navContent = document.getElementById('nav-content'); +let navAll = document.getElementById('nav-all') let mosaic = document.getElementById('mosaic'); -let mosaicHeader = document.getElementById('mosaic-header'); +let mosaicCount = document.getElementById('mosaic-count'); let tags = [] let navEnablerExists = false @@ -36,17 +48,65 @@ navEnabler.onclick = async () => { navOpened = !navOpened } -function createEl(className = false, elName = "div") { - let el = document.createElement(elName); - if (className != false) { - el.className = className; + +/** + * Content rendering + */ + +/** + * Shuffle organization + */ +function shuffle(array) { + var currentIndex = array.length, temporaryValue, randomIndex + while (0 !== currentIndex) { + randomIndex = Math.floor(Math.random() * currentIndex) + currentIndex -= 1 + + temporaryValue = array[currentIndex] + array[currentIndex] = array[randomIndex] + array[randomIndex] = temporaryValue } - return el; + + return array } /** - * Render + * Filter & Sort actions */ +const randomBtn = document.getElementById('random-btn') +const sortBtn = document.getElementById('sort-btn') + +// by default we render the cards with the random behaviour +let sort = false +organizations = shuffle(organizations) + +// the user want to sort randomly +randomBtn.onclick = () => { + if (sort) { + organizations = shuffle(organizations) + enableTag(currentTag) + sort = false + randomBtn.classList.add('enabled') + sortBtn.classList.remove('enabled') + } +} + +// the user want to sort by name +sortBtn.onclick = () => { + if (!sort) { + // sort by name + organizations = organizations.sort((a, b) => { + var textA = a.name.toUpperCase() + var textB = b.name.toUpperCase() + return (textA < textB) ? -1 : (textA > textB) ? 1 : 0; + }) + enableTag(currentTag) + sort = true + sortBtn.classList.add('enabled') + randomBtn.classList.remove('enabled') + } +} + function renderNavItem(tag) { /*