Proyectos de Subversion Moodle

Rev

Rev 1 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

{"version":3,"file":"modal_quiz_question_bank.min.js","sources":["../src/modal_quiz_question_bank.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 * Contain the logic for the question bank modal.\n *\n * @module     mod_quiz/modal_quiz_question_bank\n * @copyright  2018 Ryan Wyllie <ryan@moodle.com>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Modal from './add_question_modal';\nimport * as Fragment from 'core/fragment';\nimport * as FormChangeChecker from 'core_form/changechecker';\nimport * as ModalEvents from 'core/modal_events';\nimport * as Notification from 'core/notification';\n\nconst SELECTORS = {\n    ADD_TO_QUIZ_CONTAINER: 'td.addtoquizaction',\n    ANCHOR: 'a[href]',\n    PREVIEW_CONTAINER: 'td.previewquestionaction',\n    ADD_QUESTIONS_FORM: 'form#questionsubmit',\n    SORTERS: '.sorters',\n    SWITCH_TO_OTHER_BANK: 'button[data-action=\"switch-question-bank\"]',\n    NEW_BANKMOD_ID: 'data-newmodid',\n    BANK_SEARCH: '#searchbanks',\n    GO_BACK_BUTTON: 'button[data-action=\"go-back\"]',\n    ADD_ON_PAGE_FORM_ELEMENT: 'input[name=\"addonpage\"]',\n    CMID_FORM_ELEMENT: 'form#questionsubmit input[name=\"cmid\"]',\n};\n\nexport default class ModalQuizQuestionBank extends Modal {\n    static TYPE = 'mod_quiz-quiz-question-bank';\n\n    /**\n     * Create the question bank modal.\n     *\n     * @param {Number} contextId Current module context id.\n     * @param {Number} bankCmId Current question bank course module id.\n     * @param {Number} quizCmId Current quiz course module id.\n     */\n    static init(contextId, bankCmId, quizCmId) {\n        const selector = '.menu [data-action=\"questionbank\"]';\n        document.addEventListener('click', (e) => {\n            const trigger = e.target.closest(selector);\n            if (!trigger) {\n                return;\n            }\n            e.preventDefault();\n\n            ModalQuizQuestionBank.create({\n                contextId,\n                quizCmId,\n                bankCmId,\n                title: trigger.dataset.header,\n                addOnPage: trigger.dataset.addonpage,\n                templateContext: {\n                    hidden: true,\n                },\n                large: true,\n            });\n        });\n    }\n\n    /**\n     * Override the parent show function.\n     *\n     * Reload the body contents when the modal is shown. The current\n     * window URL is used to inform the new content that should be\n     * displayed.\n     *\n     * @method show\n     * @return {void}\n     */\n    show() {\n        this.reloadBodyContent(window.location.search);\n        return super.show(this);\n    }\n\n    /**\n     * Replaces the current body contents with a new version of the question\n     * bank.\n     *\n     * The contents of the question bank are generated using the provided\n     * query string.\n     *\n     * @method reloadBodyContent\n     * @param {string} querystring URL encoded string.\n     */\n    reloadBodyContent(querystring) {\n        // Load the question bank fragment to be displayed in the modal and hide the 'go back' button.\n        this.hideFooter();\n        this.setTitle(this.originalTitle);\n        this.setBody(Fragment.loadFragment(\n            'mod_quiz',\n            'quiz_question_bank',\n            this.getContextId(),\n            {\n                querystring,\n                quizcmid: this.quizCmId,\n                bankcmid: this.bankCmId,\n            }\n        ));\n    }\n\n    /**\n     * Update the URL of the anchor element that the user clicked on to make\n     * sure that the question is added to the correct page.\n     *\n     * @method handleAddToQuizEvent\n     * @param {event} e A JavaScript event\n     * @param {object} anchorElement The anchor element that was triggered\n     */\n    handleAddToQuizEvent(e, anchorElement) {\n        // If the user clicks the plus icon to add the question to the page\n        // directly then we need to intercept the click in order to adjust the\n        // href and include the correct add on page id and cmid before the page is\n        // redirected.\n        const href = new URL(anchorElement.getAttribute('href'));\n        href.searchParams.set('addonpage', this.getAddOnPageId());\n        href.searchParams.set('cmid', this.quizCmId);\n        anchorElement.setAttribute('href', href);\n    }\n\n    /**\n     * Set up all of the event handling for the modal.\n     *\n     * @method registerEventListeners\n     */\n    registerEventListeners() {\n        // Apply parent event listeners.\n        super.registerEventListeners(this);\n\n        this.getModal().on('submit', SELECTORS.ADD_QUESTIONS_FORM, (e) => {\n            // If the user clicks on the \"Add selected questions to the quiz\" button to add some questions to the page\n            // then we need to intercept the submit in order to include the correct \"add on page id\"\n            // and the quizmod id before the form is submitted.\n            const formElement = e.currentTarget;\n            document.querySelector(SELECTORS.ADD_ON_PAGE_FORM_ELEMENT).setAttribute('value', this.getAddOnPageId());\n\n            // We also need to set the form cmid & action as the quiz modid as this could be coming from a module that isn't a quiz.\n            document.querySelector(SELECTORS.CMID_FORM_ELEMENT).setAttribute('value', this.quizCmId);\n            const actionUrl = new URL(formElement.getAttribute('action'));\n            actionUrl.searchParams.set('cmid', this.quizCmId);\n            formElement.setAttribute('action', actionUrl.toString());\n        });\n\n        this.getModal().on('click', SELECTORS.SWITCH_TO_OTHER_BANK, () => {\n            this.handleSwitchBankContentReload(SELECTORS.BANK_SEARCH)\n                .then(function(ModalQuizQuestionBank) {\n                        document.querySelector(SELECTORS.BANK_SEARCH)?.addEventListener('change', (e) => {\n                            const bankCmId = e.currentTarget.value;\n                            if (bankCmId > 0) {\n                                ModalQuizQuestionBank.bankCmId = bankCmId;\n                                ModalQuizQuestionBank.reloadBodyContent(window.location.search);\n                            }\n                        });\n                        document.querySelector(SELECTORS.GO_BACK_BUTTON).addEventListener('click', (e) => {\n                            ModalQuizQuestionBank.bankCmId = e.currentTarget.value;\n                            ModalQuizQuestionBank.reloadBodyContent(window.location.search);\n                        });\n                    }\n                )\n                .catch(Notification.exception);\n        });\n\n        this.getModal().on('click', SELECTORS.ANCHOR, (e) => {\n            const anchorElement = e.currentTarget;\n\n            // If the anchor element was the add to quiz link.\n            if (anchorElement.closest(SELECTORS.ADD_TO_QUIZ_CONTAINER)) {\n                this.handleAddToQuizEvent(e, anchorElement);\n                return;\n            }\n\n            // If the anchor element was a preview question link.\n            if (anchorElement.closest(SELECTORS.PREVIEW_CONTAINER)) {\n                return;\n            }\n\n            // Sorting links have their own handler.\n            if (anchorElement.closest(SELECTORS.SORTERS)) {\n                return;\n            }\n\n            if (anchorElement.closest('a[' + SELECTORS.NEW_BANKMOD_ID + ']')) {\n                this.bankCmId = anchorElement.getAttribute(SELECTORS.NEW_BANKMOD_ID);\n\n                // We need to clear the filter as we are about to reload the content.\n                const url = new URL(location.href);\n                url.searchParams.delete('filter');\n                history.pushState({}, '', url);\n            }\n\n            // Anything else means reload the pop-up contents.\n            e.preventDefault();\n            this.reloadBodyContent(anchorElement.search);\n        });\n\n        // Disable the form change checker when the body is rendered.\n        this.getRoot().on(ModalEvents.bodyRendered, () => {\n            // Make sure the form change checker is disabled otherwise it'll stop the user from navigating away from the\n            // page once the modal is hidden.\n            FormChangeChecker.disableAllChecks();\n        });\n    }\n}\n\nModalQuizQuestionBank.registerModalType();\n"],"names":["SELECTORS","ModalQuizQuestionBank","Modal","contextId","bankCmId","quizCmId","document","addEventListener","e","trigger","target","closest","preventDefault","create","title","dataset","header","addOnPage","addonpage","templateContext","hidden","large","show","reloadBodyContent","window","location","search","super","this","querystring","hideFooter","setTitle","originalTitle","setBody","Fragment","loadFragment","getContextId","quizcmid","bankcmid","handleAddToQuizEvent","anchorElement","href","URL","getAttribute","searchParams","set","getAddOnPageId","setAttribute","registerEventListeners","getModal","on","formElement","currentTarget","querySelector","actionUrl","toString","handleSwitchBankContentReload","then","value","catch","Notification","exception","url","delete","history","pushState","getRoot","ModalEvents","bodyRendered","FormChangeChecker","disableAllChecks","registerModalType"],"mappings":"6iDA6BMA,gCACqB,qBADrBA,iBAEM,UAFNA,4BAGiB,2BAHjBA,6BAIkB,sBAJlBA,kBAKO,WALPA,+BAMoB,6CANpBA,yBAOc,gBAPdA,sBAQW,eARXA,yBASc,gCATdA,mCAUwB,0BAVxBA,4BAWiB,+CAGFC,8BAA8BC,wCA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