{"version":3,"file":"collapsesections.min.js","sources":["https:\/\/demo.gchlol.sth.health.qld.gov.au\/lib\/form\/amd\/src\/collapsesections.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 * Collapse or expand all form sections on clicking the expand all \/ collapse al link.\n *\n * @module core_form\/collapsesections\n * @copyright 2021 Bas Brands\n * @license http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n * @since 4.0\n *\/\n\nimport $ from 'jquery';\nimport Pending from 'core\/pending';\n\nconst SELECTORS = {\n FORM: '.mform',\n FORMHEADER: '.fheader',\n FORMCONTAINER: 'fieldset > .fcontainer',\n};\n\nconst CLASSES = {\n SHOW: 'show',\n COLLAPSED: 'collapsed',\n HIDDEN: 'd-none'\n};\n\n\/**\n * Initialises the form section collapse \/ expand action.\n *\n * @param {string} collapsesections the collapse\/expand link id.\n *\/\nexport const init = collapsesections => {\n \/\/ All jQuery in this code can be replaced when MDL-71979 is integrated (move to Bootstrap 5).\n const pendingPromise = new Pending('core_form\/collapsesections');\n const collapsemenu = document.querySelector(collapsesections);\n\n const formParent = collapsemenu.closest(SELECTORS.FORM);\n const formContainers = formParent.querySelectorAll(SELECTORS.FORMCONTAINER);\n\n collapsemenu.addEventListener('keydown', e => {\n if (e.key === 'Enter' || e.key === ' ') {\n e.preventDefault();\n collapsemenu.click();\n }\n });\n\n \/\/ Override default collapse class if all visible containers are expanded on page load\n let formcontainercount = 0;\n let expandedcount = 0;\n formContainers.forEach(container => {\n const parentFieldset = container.parentElement;\n if (!parentFieldset.classList.contains(CLASSES.HIDDEN)) {\n formcontainercount++;\n }\n if (container.classList.contains(CLASSES.SHOW)) {\n expandedcount++;\n }\n });\n\n if (formcontainercount === expandedcount) {\n collapsemenu.classList.remove(CLASSES.COLLAPSED);\n collapsemenu.setAttribute('aria-expanded', true);\n }\n\n \/\/ When the collapse menu is toggled, update each form container to match.\n collapsemenu.addEventListener('click', () => {\n let action = 'hide';\n if (collapsemenu.classList.contains(CLASSES.COLLAPSED)) {\n action = 'show';\n }\n\n if (formContainers.length) {\n const pendingPromiseToggle = new Pending('core_form\/collapsesections:toggle-' + action);\n formContainers.forEach((container, index, array) => {\n $(container).collapse(action);\n if (index === array.length - 1) {\n pendingPromiseToggle.resolve();\n }\n });\n }\n });\n\n \/\/ Ensure collapse menu button adds aria-controls attribute referring to each collapsible element.\n const collapseElements = formParent.querySelectorAll(SELECTORS.FORMHEADER);\n const collapseElementIds = [...collapseElements].map((element, index) => {\n element.id = element.id || `collapseElement-${index}`;\n return element.id;\n });\n collapsemenu.setAttribute('aria-controls', collapseElementIds.join(' '));\n\n \/\/ When any form container is toggled, re-calculate collapse menu state.\n $(SELECTORS.FORMCONTAINER).on('hidden.bs.collapse', () => {\n const allCollapsed = [...formContainers].every(container => !container.classList.contains(CLASSES.SHOW));\n if (allCollapsed) {\n collapsemenu.classList.add(CLASSES.COLLAPSED);\n collapsemenu.setAttribute('aria-expanded', false);\n }\n });\n $(SELECTORS.FORMCONTAINER).on('shown.bs.collapse', () => {\n const allExpanded = [...formContainers].every(container => container.classList.contains(CLASSES.SHOW));\n if (allExpanded) {\n collapsemenu.classList.remove(CLASSES.COLLAPSED);\n collapsemenu.setAttribute('aria-expanded', true);\n }\n });\n pendingPromise.resolve();\n};\n"],"names":["SELECTORS","CLASSES","collapsesections","pendingPromise","Pending","collapsemenu","document","querySelector","formParent","closest","formContainers","querySelectorAll","addEventListener","e","key","preventDefault","click","formcontainercount","expandedcount","forEach","container","parentElement","classList","contains","remove","setAttribute","action","length","pendingPromiseToggle","index","array","collapse","resolve","collapseElementIds","map","element","id","join","on","every","add"],"mappings":";;;;;;;;0KA2BMA,eACI,SADJA,qBAEU,WAFVA,wBAGa,yBAGbC,aACI,OADJA,kBAES,YAFTA,eAGM,uBAQQC,yBAEVC,eAAiB,IAAIC,iBAAQ,8BAC7BC,aAAeC,SAASC,cAAcL,kBAEtCM,WAAaH,aAAaI,QAAQT,gBAClCU,eAAiBF,WAAWG,iBAAiBX,yBAEnDK,aAAaO,iBAAiB,WAAWC,IACvB,UAAVA,EAAEC,KAA6B,MAAVD,EAAEC,MACvBD,EAAEE,iBACFV,aAAaW,gBAKjBC,mBAAqB,EACrBC,cAAgB,EACpBR,eAAeS,SAAQC,YACIA,UAAUC,cACbC,UAAUC,SAAStB,iBACnCgB,qBAEAG,UAAUE,UAAUC,SAAStB,eAC7BiB,mBAIJD,qBAAuBC,gBACvBb,aAAaiB,UAAUE,OAAOvB,mBAC9BI,aAAaoB,aAAa,iBAAiB,IAI\/CpB,aAAaO,iBAAiB,SAAS,SAC\/Bc,OAAS,UACTrB,aAAaiB,UAAUC,SAAStB,qBAChCyB,OAAS,QAGThB,eAAeiB,OAAQ,OACjBC,qBAAuB,IAAIxB,iBAAQ,qCAAuCsB,QAChFhB,eAAeS,SAAQ,CAACC,UAAWS,MAAOC,6BACpCV,WAAWW,SAASL,QAClBG,QAAUC,MAAMH,OAAS,GACzBC,qBAAqBI,uBAQ\/BC,mBAAqB,IADFzB,WAAWG,iBAAiBX,uBACJkC,KAAI,CAACC,QAASN,SAC3DM,QAAQC,GAAKD,QAAQC,8BAAyBP,OACvCM,QAAQC,MAEnB\/B,aAAaoB,aAAa,gBAAiBQ,mBAAmBI,KAAK,0BAGjErC,yBAAyBsC,GAAG,sBAAsB,KAC3B,IAAI5B,gBAAgB6B,OAAMnB,YAAcA,UAAUE,UAAUC,SAAStB,kBAEtFI,aAAaiB,UAAUkB,IAAIvC,mBAC3BI,aAAaoB,aAAa,iBAAiB,2BAGjDzB,yBAAyBsC,GAAG,qBAAqB,KAC3B,IAAI5B,gBAAgB6B,OAAMnB,WAAaA,UAAUE,UAAUC,SAAStB,kBAEpFI,aAAaiB,UAAUE,OAAOvB,mBAC9BI,aAAaoB,aAAa,iBAAiB,OAGnDtB,eAAe6B"}