{"version":3,"file":"coursecards.min.js","sources":["https:\/\/leerportaal.rutgers.nl\/blocks\/coursecards\/amd\/src\/coursecards.js"],"sourcesContent":["\/\/ This file is part of Moodle - http:\/\/moodle.org\/\n\/\/\n\/\/ Moodle is free software: you can redistribute it and\/or modify\n\/\/ it under the terms of the GNU General Public License as published by\n\/\/ the Free Software Foundation, either version 3 of the License, or\n\/\/ (at your option) any later version.\n\/\/\n\/\/ Moodle is distributed in the hope that it will be useful,\n\/\/ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\/\/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\/\/ GNU General Public License for more details.\n\/\/\n\/\/ You should have received a copy of the GNU General Public License\n\/\/ along with Moodle. If not, see .\n\n\/**\n * Coursecards javascript\n *\n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\n * @module block_coursecards\/coursecards\n * @copyright 05\/07\/2023 LdesignMedia.nl - Luuk Verhoeven\n * @author Vincent Cornelis\n **\/\n\nimport Log from 'core\/log';\n\nconst SELECTORS = {\n 'block': '.block_coursecards',\n 'category': '.coursecard-category',\n 'coursecardsHolderClass': 'coursecards-holder',\n 'coursecardsHolder': '.coursecards-holder',\n 'coursecard': '.coursecard',\n 'visiblecoursecards': 'visible',\n 'hiddencoursecards': 'hidden',\n 'categorychevron': '.cat-chevron',\n 'myCoursesElement': '.coursecard-category.mycourses',\n 'searchForm': '.block_coursecards-search-form',\n 'openAll': '.btn.coursecards-open',\n 'collapseAll': '.btn.coursecards-collapse',\n};\n\n\/**\n * Event listeners.\n *\/\nconst registerEventListeners = () => {\n\n \/\/ Load correct height for smooth transitions of coursecards.\n window.addEventListener('load', calculateHolderHeights);\n window.addEventListener('orientationchange', calculateHolderHeights);\n\n \/\/ Short timeout on resize to reduce load.\n let resizeTimeout;\n window.addEventListener('resize', function() {\n clearTimeout(resizeTimeout);\n resizeTimeout = setTimeout(function() {\n calculateHolderHeights();\n }, 100);\n });\n\n \/\/ Listen for clicks on categories.\n document.addEventListener('click', (event) => {\n const category = event.target.closest(SELECTORS.category);\n if (category) {\n toggleExpandCollapse(category);\n }\n\n const openAll = event.target.closest(SELECTORS.openAll);\n if (openAll) {\n openOrCollapseAll('open');\n }\n\n const collapseAll = event.target.closest(SELECTORS.collapseAll);\n if (collapseAll) {\n openOrCollapseAll('collapse');\n }\n\n });\n\n \/\/ Listen for submission of search form.\n const searchForm = document.querySelector(SELECTORS.searchForm);\n\n searchForm.addEventListener('submit', (event) => {\n event.preventDefault();\n searchCourses(new FormData(searchForm));\n });\n\n};\n\n\/**\n * Calculates the correct open size.\n *\/\nconst calculateHolderHeights = () => {\n const coursecardsHolders = document.querySelectorAll(SELECTORS.coursecardsHolder);\n\n if (!coursecardsHolders) {\n return;\n }\n\n coursecardsHolders.forEach((coursecardHolder) => {\n\n if (coursecardHolder.classList.contains(SELECTORS.visiblecoursecards)) {\n coursecardHolder.style.height = '';\n coursecardHolder.style.height = coursecardHolder.scrollHeight + 'px';\n }\n\n if (coursecardHolder.classList.contains(SELECTORS.hiddencoursecards)) {\n coursecardHolder.style.height = 0 + 'px';\n }\n\n });\n};\n\n\/**\n * Expand or collapse categories.\n *\n * @param {object} category\n * @param {string} forcedState\n *\/\nconst toggleExpandCollapse = (category, forcedState = '') => {\n\n \/\/ Toggle chevron.\n const chevron = category.querySelector(SELECTORS.categorychevron);\n\n switch (forcedState) {\n case 'open':\n openChevron(chevron);\n break;\n case 'collapse':\n closeChevron(chevron);\n break;\n default:\n if (chevron.classList.contains('chevron-open')) {\n closeChevron(chevron);\n } else if (chevron.classList.contains('chevron-closed')) {\n openChevron(chevron);\n }\n }\n\n \/\/ Toggle coursecards.\n const coursecardsHolder = category.nextElementSibling;\n\n if (coursecardsHolder && !coursecardsHolder.classList.contains(SELECTORS.coursecardsHolderClass)) {\n return;\n }\n\n switch (forcedState) {\n case 'open':\n showHolder(coursecardsHolder);\n break;\n case 'collapse':\n hideHolder(coursecardsHolder);\n break;\n default:\n if (coursecardsHolder.classList.contains('visible')) {\n hideHolder(coursecardsHolder);\n } else if (coursecardsHolder.classList.contains('hidden')) {\n showHolder(coursecardsHolder);\n }\n }\n};\n\n\/**\n * Hide a holder.\n *\n * @param {object} holder\n *\/\nconst hideHolder = (holder) => {\n holder.classList.remove('visible');\n holder.classList.add('hidden');\n holder.style.height = 0 + 'px';\n};\n\n\/**\n * Show a holder.\n *\n * @param {object} holder\n *\/\nconst showHolder = (holder) => {\n holder.classList.remove('hidden');\n holder.classList.add('visible');\n holder.style.height = holder.scrollHeight + 'px';\n};\n\n\/**\n * Open a chevron.\n *\n * @param {object} chevron\n *\/\nconst closeChevron = (chevron) => {\n chevron.classList.remove('chevron-open');\n chevron.parentNode.classList.remove('chevron-open');\n chevron.classList.add('chevron-closed');\n chevron.parentNode.classList.add('chevron-closed');\n};\n\n\/**\n * Close a chevron.\n *\n * @param {object} chevron\n *\/\nconst openChevron = (chevron) => {\n chevron.classList.remove('chevron-closed');\n chevron.parentNode.classList.remove('chevron-closed');\n chevron.classList.add('chevron-open');\n chevron.parentNode.classList.add('chevron-open');\n};\n\n\/**\n * Search through the courses.\n *\n * @param {object} formData\n *\/\nconst searchCourses = (formData) => {\n\n \/\/ Allow multiple words to be searched.\n const search = formData.get('search').toLowerCase().split(' ');\n const courseCards = document.querySelectorAll(SELECTORS.coursecard);\n\n courseCards.forEach((card) => {\n const searchableData = card.dataset.searchable;\n\n const hasMatch = search.every(word => searchableData.includes(word));\n\n if (!hasMatch) {\n card.style.display = 'none';\n } else {\n card.style.display = '';\n }\n });\n\n \/\/ Calculate the height again.\n calculateHolderHeights();\n\n};\n\n\/**\n * Open or collapse al categories at once.\n *\n * @param {string} state\n *\/\nconst openOrCollapseAll = (state) => {\n\n const categories = document.querySelectorAll(SELECTORS.category);\n\n categories.forEach((category) => {\n toggleExpandCollapse(category, state);\n });\n\n};\n\n\/**\n * Initialise\n *\/\nexport const init = () => {\n\n const blockCoursecards = document.querySelector(SELECTORS.block);\n\n if (!blockCoursecards) {\n return;\n }\n\n Log.log('Load coursecard JS v1.0');\n registerEventListeners();\n\n};\n"],"names":["SELECTORS","calculateHolderHeights","coursecardsHolders","document","querySelectorAll","forEach","coursecardHolder","classList","contains","style","height","scrollHeight","toggleExpandCollapse","category","forcedState","chevron","querySelector","openChevron","closeChevron","coursecardsHolder","nextElementSibling","showHolder","hideHolder","holder","remove","add","parentNode","searchCourses","formData","search","get","toLowerCase","split","card","searchableData","dataset","searchable","hasMatch","every","word","includes","display","openOrCollapseAll","state","log","resizeTimeout","window","addEventListener","clearTimeout","setTimeout","event","target","closest","searchForm","preventDefault","FormData","registerEventListeners"],"mappings":";;;;;;;;;2IA2BMA,gBACO,qBADPA,mBAEU,uBAFVA,iCAGwB,qBAHxBA,4BAImB,sBAJnBA,qBAKY,cALZA,6BAMoB,UANpBA,4BAOmB,SAPnBA,0BAQiB,eARjBA,qBAUY,iCAVZA,kBAWS,wBAXTA,sBAYa,4BAqDbC,uBAAyB,WACrBC,mBAAqBC,SAASC,iBAAiBJ,6BAEhDE,oBAILA,mBAAmBG,SAASC,mBAEpBA,iBAAiBC,UAAUC,SAASR,gCACpCM,iBAAiBG,MAAMC,OAAS,GAChCJ,iBAAiBG,MAAMC,OAASJ,iBAAiBK,aAAe,MAGhEL,iBAAiBC,UAAUC,SAASR,+BACpCM,iBAAiBG,MAAMC,OAAS,WAYtCE,qBAAuB,SAACC,cAAUC,mEAAc,SAG5CC,QAAUF,SAASG,cAAchB,kCAE\/Bc,iBACC,OACDG,YAAYF,mBAEX,WACDG,aAAaH,uBAGTA,QAAQR,UAAUC,SAAS,gBAC3BU,aAAaH,SACNA,QAAQR,UAAUC,SAAS,mBAClCS,YAAYF,eAKlBI,kBAAoBN,SAASO,uBAE\/BD,mBAAsBA,kBAAkBZ,UAAUC,SAASR,yCAIvDc,iBACC,OACDO,WAAWF,6BAEV,WACDG,WAAWH,iCAGPA,kBAAkBZ,UAAUC,SAAS,WACrCc,WAAWH,mBACJA,kB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