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) { /*