Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"copy_modal.min.js","sources":["../src/copy_modal.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 * This module provides the course copy modal from the course and\n * category management screen.\n *\n * @module     core_course/copy_modal\n * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>\n * @author     Matt Porritt <mattp@catalyst-au.net>\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since      3.9\n */\n\nimport {get_string as getString} from 'core/str';\nimport Modal from 'core/modal';\nimport * as ajax from 'core/ajax';\nimport * as Fragment from 'core/fragment';\nimport Notification from 'core/notification';\nimport * as Config from 'core/config';\n\nexport default class CopyModal {\n    static init(context) {\n        return new CopyModal(context);\n    }\n\n    constructor(context) {\n        this.contextid = context;\n\n        this.registerEventListeners();\n    }\n\n    registerEventListeners() {\n        document.addEventListener('click', (e) => {\n            const copyAction = e.target.closest('.action-copy');\n            if (!copyAction) {\n                return;\n            }\n            e.preventDefault(); // Stop. Hammer time.\n\n            const url = new URL(copyAction.href);\n            const params = new URLSearchParams(url.search);\n\n            this.fetchCourseData(params.get('id'))\n            .then(([course]) => this.createModal(course))\n            .catch((error) => Notification.exception(error));\n        });\n    }\n\n    fetchCourseData(courseid) {\n        return ajax.call([{\n            methodname: 'core_course_get_courses',\n            args: {\n                options: {\n                    ids: [courseid],\n                },\n            },\n        }])[0];\n    }\n\n    submitBackupRequest(jsonformdata) {\n        return ajax.call([{\n            methodname: 'core_backup_submit_copy_form',\n            args: {\n                jsonformdata,\n            },\n        }])[0];\n    }\n\n    createModal(\n        course,\n        formdata = {},\n    ) {\n        const params = {\n            jsonformdata: JSON.stringify(formdata),\n            courseid: course.id,\n        };\n\n        // Create the Modal.\n        return Modal.create({\n            title: getString('copycoursetitle', 'backup', course.shortname),\n            body: Fragment.loadFragment('course', 'new_base_form', this.contextid, params),\n            large: true,\n            show: true,\n            removeOnClose: true,\n        })\n        .then((modal) => {\n            // Explicitly handle form click events.\n            modal.getRoot().on('click', '#id_submitreturn', (e) => {\n                this.processModalForm(course, modal, e);\n            });\n            modal.getRoot().on('click', '#id_cancel', (e) => {\n                e.preventDefault();\n                modal.destroy();\n            });\n            modal.getRoot().on('click', '#id_submitdisplay', (e) => {\n                e.formredirect = true;\n                this.processModalForm(course, modal, e);\n\n            });\n\n            return modal;\n        });\n    }\n\n    processModalForm(course, modal, e) {\n        e.preventDefault(); // Stop modal from closing.\n\n        // Form data.\n        const copyform = modal.getRoot().find('form').serialize();\n        const formjson = JSON.stringify(copyform);\n\n        // Handle invalid form fields for better UX.\n        const invalid = modal.getRoot()[0].querySelectorAll('[aria-invalid=\"true\"], .error');\n        if (invalid.length) {\n            invalid[0].focus();\n            return;\n        }\n\n        modal.destroy();\n\n        // Submit form via ajax.\n        this.submitBackupRequest(formjson)\n        .then(() => {\n            if (e.formredirect == true) {\n                // We are redirecting to copy progress display.\n                const redirect = `${Config.wwwroot}/backup/copyprogress.php?id=${course.id}`;\n                window.location.assign(redirect);\n            }\n\n            return;\n        })\n        .catch(() => {\n            // Form submission failed server side, redisplay with errors.\n            this.createModal(course, copyform);\n        });\n    }\n}\n"],"names":["CopyModal","context","constructor","contextid","registerEventListeners","document","addEventListener","e","copyAction","target","closest","preventDefault","url","URL","href","params","URLSearchParams","search","fetchCourseData","get","then","_ref","course","this","createModal","catch","error","Notification","exception","courseid","ajax","call","methodname","args","options","ids","submitBackupRequest","jsonformdata","formdata","JSON","stringify","id","Modal","create","title","shortname","body","Fragment","loadFragment","large","show","removeOnClose","modal","getRoot","on","processModalForm","destroy","formredirect","copyform","find","serialize","formjson","invalid","querySelectorAll","length","focus","redirect","Config","wwwroot","window","location","assign"],"mappings":";;;;;;;;;;0SAiCqBA,sBACLC,gBACD,IAAID,UAAUC,SAGzBC,YAAYD,cACHE,UAAYF,aAEZG,yBAGTA,yBACIC,SAASC,iBAAiB,SAAUC,UAC1BC,WAAaD,EAAEE,OAAOC,QAAQ,oBAC/BF,kBAGLD,EAAEI,uBAEIC,IAAM,IAAIC,IAAIL,WAAWM,MACzBC,OAAS,IAAIC,gBAAgBJ,IAAIK,aAElCC,gBAAgBH,OAAOI,IAAI,OAC/BC,MAAKC,WAAEC,oBAAYC,KAAKC,YAAYF,WACpCG,OAAOC,OAAUC,sBAAaC,UAAUF,YAIjDR,gBAAgBW,iBACLC,KAAKC,KAAK,CAAC,CACdC,WAAY,0BACZC,KAAM,CACFC,QAAS,CACLC,IAAK,CAACN,eAGd,GAGRO,oBAAoBC,qBACTP,KAAKC,KAAK,CAAC,CACdC,WAAY,+BACZC,KAAM,CACFI,aAAAA,iBAEJ,GAGRb,YACIF,YACAgB,gEAAW,SAELvB,OAAS,CACXsB,aAAcE,KAAKC,UAAUF,UAC7BT,SAAUP,OAAOmB,WAIdC,eAAMC,OAAO,CAChBC,OAAO,mBAAU,kBAAmB,SAAUtB,OAAOuB,WACrDC,KAAMC,SAASC,aAAa,SAAU,gBAAiBzB,KAAKpB,UAAWY,QACvEkC,OAAO,EACPC,MAAM,EACNC,eAAe,IAElB/B,MAAMgC,QAEHA,MAAMC,UAAUC,GAAG,QAAS,oBAAqB/C,SACxCgD,iBAAiBjC,OAAQ8B,MAAO7C,MAEzC6C,MAAMC,UAAUC,GAAG,QAAS,cAAe/C,IACvCA,EAAEI,iBACFyC,MAAMI,aAEVJ,MAAMC,UAAUC,GAAG,QAAS,qBAAsB/C,IAC9CA,EAAEkD,cAAe,OACZF,iBAAiBjC,OAAQ8B,M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