Merge branch 'MDL-81766-main-clr02' of https://github.com/ferranrecio/moodle
[moodle.git] / course / format / amd / build / local / content / section.min.js.map
blob5b7699f96e767b899a9c4df4195f08617b162ac9
1 {"version":3,"file":"section.min.js","sources":["../../../src/local/content/section.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 <http://www.gnu.org/licenses/>.\n\n/**\n * Course section format component.\n *\n * @module     core_courseformat/local/content/section\n * @class      core_courseformat/local/content/section\n * @copyright  2021 Ferran Recio <ferran@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Header from 'core_courseformat/local/content/section/header';\nimport DndSection from 'core_courseformat/local/courseeditor/dndsection';\nimport Templates from 'core/templates';\nimport Pending from \"core/pending\";\n\nexport default class extends DndSection {\n\n    /**\n     * Constructor hook.\n     */\n    create() {\n        // Optional component name for debugging.\n        this.name = 'content_section';\n        // Default query selectors.\n        this.selectors = {\n            ACTIONMENU: '.section-actions',\n            SECTION_ITEM: `[data-for='section_title']`,\n            CM: `[data-for=\"cmitem\"]`,\n            SECTIONINFO: `[data-for=\"sectioninfo\"]`,\n            SECTIONBADGES: `[data-region=\"sectionbadges\"]`,\n            SHOWSECTION: `[data-action=\"sectionShow\"]`,\n            HIDESECTION: `[data-action=\"sectionHide\"]`,\n            ACTIONTEXT: `.menu-action-text`,\n            ICON: `.icon`,\n        };\n        // Most classes will be loaded later by DndCmItem.\n        this.classes = {\n            LOCKED: 'editinprogress',\n            HASDESCRIPTION: 'description',\n            HIDE: 'd-none',\n            HIDDEN: 'hidden',\n            CURRENT: 'current',\n        };\n\n        // We need our id to watch specific events.\n        this.id = this.element.dataset.id;\n    }\n\n    /**\n     * Initial state ready method.\n     *\n     * @param {Object} state the initial state\n     */\n    stateReady(state) {\n        this.configState(state);\n        // Drag and drop is only available for components compatible course formats.\n        if (this.reactive.isEditing && this.reactive.supportComponents) {\n            // Section zero and other formats sections may not have a title to drag.\n            const sectionItem = this.getElement(this.selectors.SECTION_ITEM);\n            if (sectionItem) {\n                // Init the inner dragable element.\n                const headerComponent = new Header({\n                    ...this,\n                    element: sectionItem,\n                    fullregion: this.element,\n                });\n                this.configDragDrop(headerComponent);\n            }\n        }\n        this._openSectionIfNecessary();\n    }\n\n    /**\n     * Open the section if the anchored activity is inside.\n     */\n    async _openSectionIfNecessary() {\n        const pageCmInfo = this.reactive.getPageAnchorCmInfo();\n        if (!pageCmInfo || pageCmInfo.sectionid !== this.id) {\n            return;\n        }\n        await this.reactive.dispatch('sectionContentCollapsed', [this.id], false);\n        const pendingOpen = new Pending(`courseformat/section:openSectionIfNecessary`);\n        this.element.scrollIntoView({block: \"center\"});\n        setTimeout(() => {\n            this.reactive.dispatch('setPageItem', 'cm', pageCmInfo.id);\n            pendingOpen.resolve();\n        }, 250);\n    }\n\n    /**\n     * Component watchers.\n     *\n     * @returns {Array} of watchers\n     */\n    getWatchers() {\n        return [\n            {watch: `section[${this.id}]:updated`, handler: this._refreshSection},\n        ];\n    }\n\n    /**\n     * Validate if the drop data can be dropped over the component.\n     *\n     * @param {Object} dropdata the exported drop data.\n     * @returns {boolean}\n     */\n    validateDropData(dropdata) {\n        // If the format uses one section per page sections dropping in the content is ignored.\n        if (dropdata?.type === 'section' && this.reactive.sectionReturn !== null) {\n            return false;\n        }\n        return super.validateDropData(dropdata);\n    }\n\n    /**\n     * Get the last CM element of that section.\n     *\n     * @returns {element|null}\n     */\n    getLastCm() {\n        const cms = this.getElements(this.selectors.CM);\n        // DndUpload may add extra elements so :last-child selector cannot be used.\n        if (!cms || cms.length === 0) {\n            return null;\n        }\n        const lastCm = cms[cms.length - 1];\n        // If it is a delegated section return the last item overall.\n        if (this.section.component !== null) {\n            return lastCm;\n        }\n        // If it is a regular section and the last item overall has a parent cm, return the parent instead.\n        const parentSection = lastCm.parentNode.closest(this.selectors.CM);\n        return parentSection ?? lastCm;\n    }\n\n    /**\n     * Get a fallback element when there is no CM in the section.\n     *\n     * @returns {element|null} the las course module element of the section.\n     */\n    getLastCmFallback() {\n        // The sectioninfo is always present, even when the section is empty.\n        return this.getElement(this.selectors.SECTIONINFO);\n    }\n\n    /**\n     * Update a content section using the state information.\n     *\n     * @param {object} param\n     * @param {Object} param.element details the update details.\n     */\n    _refreshSection({element}) {\n        // Update classes.\n        this.element.classList.toggle(this.classes.DRAGGING, element.dragging ?? false);\n        this.element.classList.toggle(this.classes.LOCKED, element.locked ?? false);\n        this.element.classList.toggle(this.classes.HIDDEN, !element.visible ?? false);\n        this.element.classList.toggle(this.classes.CURRENT, element.current ?? false);\n        this.locked = element.locked;\n        // The description box classes depends on the section state.\n        const sectioninfo = this.getElement(this.selectors.SECTIONINFO);\n        if (sectioninfo) {\n            sectioninfo.classList.toggle(this.classes.HASDESCRIPTION, element.hasrestrictions);\n        }\n        // Update section badges and menus.\n        this._updateBadges(element);\n        this._updateActionsMenu(element);\n    }\n\n    /**\n     * Update a section badges using the state information.\n     *\n     * @param {object} section the section state.\n     */\n    _updateBadges(section) {\n        const current = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='iscurrent']`);\n        current?.classList.toggle(this.classes.HIDE, !section.current);\n\n        const hiddenFromStudents = this.getElement(`${this.selectors.SECTIONBADGES} [data-type='hiddenfromstudents']`);\n        hiddenFromStudents?.classList.toggle(this.classes.HIDE, section.visible);\n    }\n\n    /**\n     * Update a section action menus.\n     *\n     * @param {object} section the section state.\n     */\n    async _updateActionsMenu(section) {\n        let selector;\n        let newAction;\n        if (section.visible) {\n            selector = this.selectors.SHOWSECTION;\n            newAction = 'sectionHide';\n        } else {\n            selector = this.selectors.HIDESECTION;\n            newAction = 'sectionShow';\n        }\n        // Find the affected action.\n        const affectedAction = this._getActionMenu(selector);\n        if (!affectedAction) {\n            return;\n        }\n        // Change action.\n        affectedAction.dataset.action = newAction;\n        // Change text.\n        const actionText = affectedAction.querySelector(this.selectors.ACTIONTEXT);\n        if (affectedAction.dataset?.swapname && actionText) {\n            const oldText = actionText?.innerText;\n            actionText.innerText = affectedAction.dataset.swapname;\n            affectedAction.dataset.swapname = oldText;\n        }\n        // Change icon.\n        const icon = affectedAction.querySelector(this.selectors.ICON);\n        if (affectedAction.dataset?.swapicon && icon) {\n            const newIcon = affectedAction.dataset.swapicon;\n            affectedAction.dataset.swapicon = affectedAction.dataset.icon;\n            affectedAction.dataset.icon = newIcon;\n            if (newIcon) {\n                const pixHtml = await Templates.renderPix(newIcon, 'core');\n                Templates.replaceNode(icon, pixHtml, '');\n            }\n        }\n    }\n\n    /**\n     * Get the action menu element from the selector.\n     *\n     * @param {string} selector The selector to find the action menu.\n     * @returns The action menu element.\n     */\n    _getActionMenu(selector) {\n        return document.querySelector(`${this.selectors.ACTIONMENU}[data-sectionid='${this.id}'] ${selector}`);\n    }\n}\n"],"names":["DndSection","create","name","selectors","ACTIONMENU","SECTION_ITEM","CM","SECTIONINFO","SECTIONBADGES","SHOWSECTION","HIDESECTION","ACTIONTEXT","ICON","classes","LOCKED","HASDESCRIPTION","HIDE","HIDDEN","CURRENT","id","this","element","dataset","stateReady","state","configState","reactive","isEditing","supportComponents","sectionItem","getElement","headerComponent","Header","fullregion","configDragDrop","_openSectionIfNecessary","pageCmInfo","getPageAnchorCmInfo","sectionid","dispatch","pendingOpen","Pending","scrollIntoView","block","setTimeout","resolve","getWatchers","watch","handler","_refreshSection","validateDropData","dropdata","type","sectionReturn","super","getLastCm","cms","getElements","length","lastCm","section","component","parentSection","parentNode","closest","getLastCmFallback","classList","toggle","DRAGGING","dragging","locked","visible","current","sectioninfo","hasrestrictions","_updateBadges","_updateActionsMenu","hiddenFromStudents","selector","newAction","affectedAction","_getActionMenu","action","actionText","querySelector","swapname","oldText","innerText","icon","swapicon","newIcon","pixHtml","Templates","renderPix","replaceNode","document"],"mappings":";;;;;;;;4RA6B6BA,oBAKzBC,cAESC,KAAO,uBAEPC,UAAY,CACbC,WAAY,mBACZC,0CACAC,yBACAC,uCACAC,8CACAC,0CACAC,0CACAC,+BACAC,mBAGCC,QAAU,CACXC,OAAQ,iBACRC,eAAgB,cAChBC,KAAM,SACNC,OAAQ,SACRC,QAAS,gBAIRC,GAAKC,KAAKC,QAAQC,QAAQH,GAQnCI,WAAWC,eACFC,YAAYD,OAEbJ,KAAKM,SAASC,WAAaP,KAAKM,SAASE,kBAAmB,OAEtDC,YAAcT,KAAKU,WAAWV,KAAKjB,UAAUE,iBAC/CwB,YAAa,OAEPE,gBAAkB,IAAIC,gBAAO,IAC5BZ,KACHC,QAASQ,YACTI,WAAYb,KAAKC,eAEhBa,eAAeH,uBAGvBI,gEAOCC,WAAahB,KAAKM,SAASW,0BAC5BD,YAAcA,WAAWE,YAAclB,KAAKD,gBAG3CC,KAAKM,SAASa,SAAS,0BAA2B,CAACnB,KAAKD,KAAK,SAC7DqB,YAAc,IAAIC,qEACnBpB,QAAQqB,eAAe,CAACC,MAAO,WACpCC,YAAW,UACFlB,SAASa,SAAS,cAAe,KAAMH,WAAWjB,IACvDqB,YAAYK,YACb,KAQPC,oBACW,CACH,CAACC,wBAAkB3B,KAAKD,gBAAe6B,QAAS5B,KAAK6B,kBAU7DC,iBAAiBC,iBAEU,aAAnBA,MAAAA,gBAAAA,SAAUC,OAAsD,OAAhChC,KAAKM,SAAS2B,gBAG3CC,MAAMJ,iBAAiBC,UAQlCI,kBACUC,IAAMpC,KAAKqC,YAAYrC,KAAKjB,UAAUG,QAEvCkD,KAAsB,IAAfA,IAAIE,cACL,WAELC,OAASH,IAAIA,IAAIE,OAAS,MAED,OAA3BtC,KAAKwC,QAAQC,iBACNF,aAGLG,cAAgBH,OAAOI,WAAW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