{"version":3,"file":"content.min.js","sources":["https:\/\/www.alsg.org\/home\/course\/format\/topcoll\/amd\/src\/local\/content.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 * Collapsed Topics Course index main component.\n *\n * @module     format_topcoll\/local\/content\n * @class      format_topcoll\/local\/content\n * @copyright  2022 G J Barnard based upon work done by:\n * @copyright  2020 Ferran Recio \n * @license    http:\/\/www.gnu.org\/copyleft\/gpl.html GNU GPL v3 or later\n *\/\n\nimport Component from 'core_courseformat\/local\/content';\nimport {getCurrentCourseEditor} from 'core_courseformat\/courseeditor';\n\nexport default class TopcollComponent extends Component {\n\n    \/**\n     * Constructor hook.\n     *\n     * @param {Object} descriptor the component descriptor\n     *\/\n    create(descriptor) {\n        super.create(descriptor);\n    }\n\n    \/**\n     * Static method to create a component instance form the mustahce template.\n     *\n     * @param {string} target the DOM main element or its ID\n     * @param {object} selectors optional css selector overrides\n     * @param {number} sectionReturn the content section return\n     * @return {Component}\n     *\/\n    static init(target, selectors, sectionReturn) {\n        return new TopcollComponent({\n            element: document.getElementById(target),\n            reactive: getCurrentCourseEditor(),\n            selectors,\n            sectionReturn,\n        });\n    }\n\n    \/**\n     * Return the component watchers.\n     *\n     * @returns {Array} of watchers\n     *\/\n    getWatchers() {\n        \/\/ Section return is a global page variable but most formats define it just before start printing\n        \/\/ the course content. This is the reason why we define this page setting here.\n        this.reactive.sectionReturn = this.sectionReturn;\n\n        \/\/ Check if the course format is compatible with reactive components.\n        if (!this.reactive.supportComponents) {\n            return [];\n        }\n        return [\n            \/\/ State changes that require to reload some course modules.\n            {watch: `cm.visible:updated`, handler: this._reloadCm},\n            {watch: `cm.stealth:updated`, handler: this._reloadCm},\n            {watch: `cm.indent:updated`, handler: this._reloadCm},\n            \/\/ Update section number and title.\n            {watch: `section.number:updated`, handler: this._refreshSectionNumber},\n            \/\/ Sections and cm sorting.\n            {watch: `transaction:start`, handler: this._startProcessing},\n            {watch: `course.sectionlist:updated`, handler: this._refreshCourseSectionlist},\n            {watch: `section.cmlist:updated`, handler: this._refreshSectionCmlist},\n            \/\/ Reindex sections and cms.\n            {watch: `state:updated`, handler: this._indexContents},\n            \/\/ State changes thaty require to reload course modules.\n            {watch: `cm.visible:updated`, handler: this._reloadCm},\n            {watch: `cm.sectionid:updated`, handler: this._reloadCm},\n        ];\n    }\n\n    \/**\n     * Refresh the section list.\n     *\n     * @param {Object} param\n     * @param {Object} param.element details the update details.\n     *\/\n    _refreshCourseSectionlist({element}) {\n        \/\/ If we have a section return means we only show a single section so no need to fix order.\n        if (this.reactive.sectionReturn != 0) {\n            return;\n        }\n        const sectionlist = element.sectionlist.slice(1) ?? []; \/\/ Remove section 0 from the list!\n        const listparent = this.getElement(this.selectors.COURSE_SECTIONLIST);\n        \/\/ For now section cannot be created at a frontend level.\n        const createSection = this._createSectionItem.bind(this);\n        if (listparent) {\n            this._fixTopcollSectionOrder(listparent, sectionlist, this.selectors.SECTION, this.dettachedSections, createSection);\n        }\n    }\n\n    \/**\n     * Fix\/reorder the section or cms order.\n     *\n     * @param {Element} container the HTML element to reorder.\n     * @param {Array} neworder an array with the ids order\n     * @param {string} selector the element selector\n     * @param {Object} dettachedelements a list of dettached elements\n     * @param {function} createMethod method to create missing elements\n     *\/\n    async _fixTopcollSectionOrder(container, neworder, selector, dettachedelements, createMethod) {\n        if (!container) {\n            return;\n        }\n\n        \/\/ Empty lists should not be visible.\n        if (!neworder.length) {\n            container.classList.add('hidden');\n            container.innerHTML = '';\n            return;\n        }\n\n        \/\/ Grant the list is visible (in case it was empty).\n        container.classList.remove('hidden');\n\n        \/\/ Move the elements in order at the beginning of the list.\n        neworder.forEach((itemid, index) => {\n            let item = this.getElement(selector, itemid) ?? dettachedelements[itemid] ?? createMethod(container, itemid);\n            if (!item) {\n                \/\/ Missing elements cannot be sorted.\n                return;\n            }\n            let itemno = this.getElement('#tcnoid-'+itemid);\n            if (itemno) {\n                itemno.textContent = index + 1; \/\/ Update the section number in the 'left' part.\n            }\n            \/\/ Get the current elemnt at that position.\n            const currentitem = container.children[index];\n            if (!currentitem) {\n                container.append(item);\n                return;\n            }\n            if (currentitem !== item) {\n                container.insertBefore(item, currentitem);\n            }\n        });\n\n        \/\/ Dndupload add a fake element we need to keep.\n        let dndFakeActivity;\n\n        \/\/ Remove the remaining elements.\n        while (container.children.length > neworder.length) {\n            const lastchild = container.lastChild;\n            if (lastchild?.classList?.contains('dndupload-preview')) {\n                dndFakeActivity = lastchild;\n            } else {\n                dettachedelements[lastchild?.dataset?.id ?? 0] = lastchild;\n            }\n            container.removeChild(lastchild);\n        }\n        \/\/ Restore dndupload fake element.\n        if (dndFakeActivity) {\n            container.append(dndFakeActivity);\n        }\n    }\n}\n"],"names":["TopcollComponent","Component","create","descriptor","target","selectors","sectionReturn","element","document","getElementById","reactive","getWatchers","this","supportComponents","watch","handler","_reloadCm","_refreshSectionNumber","_startProcessing","_refreshCourseSectionlist","_refreshSectionCmlist","_indexContents","sectionlist","slice","listparent","getElement","COURSE_SECTIONLIST","createSection","_createSectionItem","bind","_fixTopcollSectionOrder","SECTION","dettachedSections","container","neworder","selector","dettachedelements","createMethod","length","classList","add","innerHTML","dndFakeActivity","remove","forEach","itemid","index","item","itemno","textContent","currentitem","children","insertBefore","append","lastchild","lastChild","_lastchild$classList","contains","dataset","_lastchild$dataset","id","removeChild"],"mappings":";;;;;;;;;qJA4BqBA,yBAAyBC,iBAO1CC,OAAOC,kBACGD,O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