Proyectos de Subversion Moodle

Rev

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

{"version":3,"file":"templateseditor.min.js","sources":["../src/templateseditor.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 * Javascript module to control the template editor.\n *\n * @module      mod_data/templateseditor\n * @copyright   2021 Mihail Geshoski <mihail@moodle.com>\n * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport {getString} from 'core/str';\nimport {prefetchStrings} from 'core/prefetch';\nimport {relativeUrl} from 'core/url';\nimport {saveCancel} from 'core/notification';\nimport Templates from 'core/templates';\n\nprefetchStrings('admin', ['confirmation']);\nprefetchStrings('mod_data', [\n    'resettemplateconfirmtitle',\n    'enabletemplateeditorcheck',\n    'editorenable'\n]);\nprefetchStrings('core', [\n    'reset',\n]);\n\n/**\n * Template editor constants.\n */\nconst selectors = {\n    toggleTemplateEditor: 'input[name=\"useeditor\"]',\n    resetTemplateAction: '[data-action=\"resettemplate\"]',\n    resetTemplate: 'input[name=\"defaultform\"]',\n    resetAllTemplates: 'input[name=\"resetall\"]',\n    resetAllCheck: 'input[name=\"resetallcheck\"]',\n    editForm: '#edittemplateform',\n};\n\n/**\n * Register event listeners for the module.\n *\n * @param {Number} instanceId The database ID\n * @param {string} mode The template mode\n */\nconst registerEventListeners = (instanceId, mode) => {\n    registerResetButton(mode);\n    registerEditorToggler(instanceId, mode);\n};\n\nconst registerResetButton = (mode) => {\n    const editForm = document.querySelector(selectors.editForm);\n    const resetTemplate = document.querySelector(selectors.resetTemplate);\n    const resetAllTemplates = document.querySelector(selectors.resetAllTemplates);\n    const resetTemplateAction = document.querySelector(selectors.resetTemplateAction);\n\n    if (!resetTemplateAction || !resetTemplate || !editForm) {\n        return;\n    }\n    prefetchStrings('mod_data', [\n        mode\n    ]);\n    resetTemplateAction.addEventListener('click', async(event) => {\n        event.preventDefault();\n        const params = {\n            resetallname: \"resetallcheck\",\n            templatename: await getString(mode, 'mod_data'),\n        };\n        saveCancel(\n            getString('resettemplateconfirmtitle', 'mod_data'),\n            Templates.render('mod_data/template_editor_resetmodal', params),\n            getString('reset', 'core'),\n            () => {\n                resetTemplate.value = \"true\";\n                editForm.submit();\n            },\n            null,\n            {triggerElement: event.target}\n        );\n    });\n\n    // The reset all checkbox is inside a modal so we need to capture at document level.\n    if (!resetAllTemplates) {\n        return;\n    }\n    document.addEventListener('change', (event) => {\n        if (event.target.matches(selectors.resetAllCheck)) {\n            resetAllTemplates.value = (event.target.checked) ? \"true\" : \"\";\n        }\n    });\n};\n\nconst registerEditorToggler = (instanceId, mode) => {\n    const toggleTemplateEditor = document.querySelector(selectors.toggleTemplateEditor);\n\n    if (!toggleTemplateEditor) {\n        return;\n    }\n\n    toggleTemplateEditor.addEventListener('click', async(event) => {\n        event.preventDefault();\n        // Whether the event action attempts to enable or disable the template editor.\n        const enableTemplateEditor = event.target.checked;\n\n        if (enableTemplateEditor) {\n            // Display a confirmation dialog before enabling the template editor.\n            saveCancel(\n                getString('confirmation', 'admin'),\n                getString('enabletemplateeditorcheck', 'mod_data'),\n                getString('editorenable', 'mod_data'),\n                () => {\n                    window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: true});\n                },\n                null,\n                {triggerElement: event.target}\n            );\n        } else {\n            window.location = relativeUrl('/mod/data/templates.php', {d: instanceId, mode: mode, useeditor: false});\n        }\n    });\n};\n\n/**\n * Initialize the module.\n *\n * @param {int} instanceId The database ID\n * @param {string} mode The template mode\n */\nexport const init = (instanceId, mode) => {\n    registerEventListeners(instanceId, mode);\n};\n"],"names":["selectors","registerResetButton","mode","editForm","document","querySelector","resetTemplate","resetAllTemplates","resetTemplateAction","addEventListener","async","event","preventDefault","params","resetallname","templatename","Templates","render","value","submit","triggerElement","target","matches","checked","registerEditorToggler","instanceId","toggleTemplateEditor","window","location","d","useeditor","registerEventListeners"],"mappings":";;;;;;;8KA6BgB,QAAS,CAAC,+CACV,WAAY,CACxB,4BACA,4BACA,+CAEY,OAAQ,CACpB,gBAMEA,+BACoB,0BADpBA,8BAEmB,gCAFnBA,wBAGa,4BAHbA,4BAIiB,yBAJjBA,wBAKa,8BALbA,mBAMQ,oBAcRC,oBAAuBC,aACnBC,SAAWC,SAASC,cAAcL,oBAClCM,cAAgBF,SAASC,cAAcL,yBACvCO,kBAAoBH,SAASC,cAAcL,6BAC3CQ,oBAAsBJ,SAASC,cAAcL,+BAE9CQ,qBAAwBF,eAAkBH,yCAG/B,WAAY,CACxBD,OAEJM,oBAAoBC,iBAAiB,SAASC,MAAAA,QAC1CC,MAAMC,uBACAC,OAAS,CACXC,aAAc,gBACdC,mBAAoB,kBAAUb,KAAM,2CAGpC,kBAAU,4BAA6B,YACvCc,mBAAUC,OAAO,sCAAuCJ,SACxD,kBAAU,QAAS,SACnB,KACIP,cAAcY,MAAQ,OACtBf,SAASgB,WAEb,KACA,CAACC,eAAgBT,MAAMU,YAK1Bd,mBAGLH,SAASK,iBAAiB,UAAWE,QAC7BA,MAAMU,OAAOC,QAAQtB,2BACrBO,kBAAkBW,MAASP,MAAMU,OAAOE,QAAW,OAAS,SAKlEC,sBAAwB,CAACC,WAAYvB,cACjCwB,qBAAuBtB,SAASC,cAAcL,gCAE/C0B,sBAILA,qBAAqBjB,iBAAiB,SAASC,MAAAA,QAC3CC,MAAMC,iBAEuBD,MAAMU,OAAOE,sCAKlC,kBAAU,eAAgB,UAC1B,kBAAU,4BAA6B,aACvC,kBAAU,eAAgB,aAC1B,KACII,OAAOC,UAAW,oBAAY,0BAA2B,CAACC,EAAGJ,WAAYvB,KAAMA,KAAM4B,WAAW,MAEpG,KACA,CAACV,eAAgBT,MAAMU,SAG3BM,OAAOC,UAAW,oBAAY,0BAA2B,CAACC,EAAGJ,WAAYvB,KAAMA,KAAM4B,WAAW,sBAWxF,CAACL,WAAYvB,QAnFF,EAACuB,WAAYvB,QACxCD,oBAAoBC,MACpBsB,sBAAsBC,WAAYvB,OAkFlC6B,CAAuBN,WAAYvB"}