From 13245050e2ff72f2f9de989b1153e64ba60bd4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikel=20Mart=C3=ADn?= Date: Mon, 29 Apr 2024 11:54:54 +0200 Subject: [PATCH] MDL-81745 mod_feedback: Refactor edit amd module --- mod/feedback/amd/build/edit.min.js | 2 +- mod/feedback/amd/build/edit.min.js.map | 2 +- mod/feedback/amd/src/edit.js | 89 +++++++++++++++----------- mod/feedback/classes/complete_form.php | 4 -- mod/feedback/edit.php | 1 + 5 files changed, 53 insertions(+), 45 deletions(-) diff --git a/mod/feedback/amd/build/edit.min.js b/mod/feedback/amd/build/edit.min.js index cd7544e575782..dba76262ca08d 100644 --- a/mod/feedback/amd/build/edit.min.js +++ b/mod/feedback/amd/build/edit.min.js @@ -1,3 +1,3 @@ -define("mod_feedback/edit",["jquery","core/ajax","core/str","core/notification"],(function($,ajax,str,notification){var manager={deleteItem:function(e){e.preventDefault();var targetUrl=$(e.currentTarget).attr("href");str.get_strings([{key:"confirmation",component:"admin"},{key:"confirmdeleteitem",component:"mod_feedback"},{key:"yes",component:"moodle"},{key:"no",component:"moodle"}]).then((function(s){notification.confirm(s[0],s[1],s[2],s[3],(function(){window.location=targetUrl}))})).catch()},setup:function(){$("body").delegate('[data-action="delete"]',"click",manager.deleteItem)}};return{setup:manager.setup}})); +define("mod_feedback/edit",["exports","core/prefetch","core/str","core/notification"],(function(_exports,_prefetch,_str,_notification){var obj;Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_notification=(obj=_notification)&&obj.__esModule?obj:{default:obj};const Selectors_deleteQuestionButton='[data-action="delete"]';let initialized=!1;_exports.init=()=>{initialized||((0,_prefetch.prefetchStrings)("core",["yes","no"]),(0,_prefetch.prefetchStrings)("admin",["confirmation"]),(0,_prefetch.prefetchStrings)("mod_feedback",["confirmdeleteitem"]),document.addEventListener("click",(async event=>{const deleteButton=event.target.closest(Selectors_deleteQuestionButton);if(deleteButton){event.preventDefault();const confirmationStrings=await(0,_str.getStrings)([{key:"confirmation",component:"admin"},{key:"confirmdeleteitem",component:"mod_feedback"},{key:"yes",component:"core"},{key:"no",component:"core"}]);_notification.default.confirm(...confirmationStrings,(()=>{window.location=deleteButton.getAttribute("href")}))}else;})),initialized=!0)}})); //# sourceMappingURL=edit.min.js.map \ No newline at end of file diff --git a/mod/feedback/amd/build/edit.min.js.map b/mod/feedback/amd/build/edit.min.js.map index b391cae147a68..76bb489a9416a 100644 --- a/mod/feedback/amd/build/edit.min.js.map +++ b/mod/feedback/amd/build/edit.min.js.map @@ -1 +1 @@ -{"version":3,"file":"edit.min.js","sources":["../src/edit.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 * Edit items in feedback module\n *\n * @module mod_feedback/edit\n * @copyright 2016 Marina Glancy\n */\ndefine(['jquery', 'core/ajax', 'core/str', 'core/notification'],\nfunction($, ajax, str, notification) {\n var manager = {\n deleteItem: function(e) {\n e.preventDefault();\n var targetUrl = $(e.currentTarget).attr('href');\n\n str.get_strings([\n {\n key: 'confirmation',\n component: 'admin'\n },\n {\n key: 'confirmdeleteitem',\n component: 'mod_feedback'\n },\n {\n key: 'yes',\n component: 'moodle'\n },\n {\n key: 'no',\n component: 'moodle'\n }\n ])\n .then(function(s) {\n notification.confirm(s[0], s[1], s[2], s[3], function() {\n window.location = targetUrl;\n });\n\n return;\n })\n .catch();\n },\n\n setup: function() {\n $('body').delegate('[data-action=\"delete\"]', 'click', manager.deleteItem);\n }\n };\n\n return {\n setup: manager.setup\n };\n});\n"],"names":["define","$","ajax","str","notification","manager","deleteItem","e","preventDefault","targetUrl","currentTarget","attr","get_strings","key","component","then","s","confirm","window","location","catch","setup","delegate"],"mappings":"AAqBAA,2BAAO,CAAC,SAAU,YAAa,WAAY,sBAC3C,SAASC,EAAGC,KAAMC,IAAKC,kBACfC,QAAU,CACVC,WAAY,SAASC,GACjBA,EAAEC,qBACEC,UAAYR,EAAEM,EAAEG,eAAeC,KAAK,QAExCR,IAAIS,YAAY,CACZ,CACIC,IAAY,eACZC,UAAY,SAEhB,CACID,IAAY,oBACZC,UAAY,gBAEhB,CACID,IAAY,MACZC,UAAY,UAEhB,CACID,IAAY,KACZC,UAAY,YAGnBC,MAAK,SAASC,GACXZ,aAAaa,QAAQD,EAAE,GAAIA,EAAE,GAAIA,EAAE,GAAIA,EAAE,IAAI,WACzCE,OAAOC,SAAWV,gBAKzBW,SAGLC,MAAO,WACHpB,EAAE,QAAQqB,SAAS,yBAA0B,QAASjB,QAAQC,oBAI/D,CACHe,MAAOhB,QAAQgB"} \ No newline at end of file +{"version":3,"file":"edit.min.js","sources":["../src/edit.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 * Edit items in feedback module\n *\n * @module mod_feedback/edit\n * @copyright 2016 Marina Glancy\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n\"use strict\";\n\nimport {prefetchStrings} from 'core/prefetch';\nimport {getStrings} from 'core/str';\nimport Notification from 'core/notification';\n\nconst Selectors = {\n deleteQuestionButton: '[data-action=\"delete\"]',\n};\n\nlet initialized = false;\n\n/**\n * Initialise editor and all it's modules\n */\nexport const init = () => {\n // Ensure we only add our listeners once (can be called multiple times).\n if (initialized) {\n return;\n }\n\n prefetchStrings('core', [\n 'yes',\n 'no',\n ]);\n prefetchStrings('admin', [\n 'confirmation',\n ]);\n prefetchStrings('mod_feedback', [\n 'confirmdeleteitem',\n ]);\n\n document.addEventListener('click', async event => {\n\n // Delete question.\n const deleteButton = event.target.closest(Selectors.deleteQuestionButton);\n if (deleteButton) {\n event.preventDefault();\n const confirmationStrings = await getStrings([\n {key: 'confirmation', component: 'admin'},\n {key: 'confirmdeleteitem', component: 'mod_feedback'},\n {key: 'yes', component: 'core'},\n {key: 'no', component: 'core'},\n ]);\n Notification.confirm(...confirmationStrings, () => {\n window.location = deleteButton.getAttribute('href');\n });\n return;\n }\n });\n\n initialized = true;\n};\n"],"names":["Selectors","initialized","document","addEventListener","async","deleteButton","event","target","closest","preventDefault","confirmationStrings","key","component","confirm","window","location","getAttribute"],"mappings":"sSA6BMA,+BACoB,6BAGtBC,aAAc,gBAKE,KAEZA,4CAIY,OAAQ,CACpB,MACA,qCAEY,QAAS,CACrB,+CAEY,eAAgB,CAC5B,sBAGJC,SAASC,iBAAiB,SAASC,MAAAA,cAGzBC,aAAeC,MAAMC,OAAOC,QAAQR,mCACtCK,cACAC,MAAMG,uBACAC,0BAA4B,mBAAW,CACzC,CAACC,IAAK,eAAgBC,UAAW,SACjC,CAACD,IAAK,oBAAqBC,UAAW,gBACtC,CAACD,IAAK,MAAOC,UAAW,QACxB,CAACD,IAAK,KAAMC,UAAW,gCAEdC,WAAWH,qBAAqB,KACzCI,OAAOC,SAAWV,aAAaW,aAAa,oBAMxDf,aAAc"} \ No newline at end of file diff --git a/mod/feedback/amd/src/edit.js b/mod/feedback/amd/src/edit.js index 1a2052d0b17c1..76ff5a872a148 100644 --- a/mod/feedback/amd/src/edit.js +++ b/mod/feedback/amd/src/edit.js @@ -18,48 +18,59 @@ * * @module mod_feedback/edit * @copyright 2016 Marina Glancy + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/ajax', 'core/str', 'core/notification'], -function($, ajax, str, notification) { - var manager = { - deleteItem: function(e) { - e.preventDefault(); - var targetUrl = $(e.currentTarget).attr('href'); - str.get_strings([ - { - key: 'confirmation', - component: 'admin' - }, - { - key: 'confirmdeleteitem', - component: 'mod_feedback' - }, - { - key: 'yes', - component: 'moodle' - }, - { - key: 'no', - component: 'moodle' - } - ]) - .then(function(s) { - notification.confirm(s[0], s[1], s[2], s[3], function() { - window.location = targetUrl; - }); +"use strict"; - return; - }) - .catch(); - }, +import {prefetchStrings} from 'core/prefetch'; +import {getStrings} from 'core/str'; +import Notification from 'core/notification'; - setup: function() { - $('body').delegate('[data-action="delete"]', 'click', manager.deleteItem); +const Selectors = { + deleteQuestionButton: '[data-action="delete"]', +}; + +let initialized = false; + +/** + * Initialise editor and all it's modules + */ +export const init = () => { + // Ensure we only add our listeners once (can be called multiple times). + if (initialized) { + return; + } + + prefetchStrings('core', [ + 'yes', + 'no', + ]); + prefetchStrings('admin', [ + 'confirmation', + ]); + prefetchStrings('mod_feedback', [ + 'confirmdeleteitem', + ]); + + document.addEventListener('click', async event => { + + // Delete question. + const deleteButton = event.target.closest(Selectors.deleteQuestionButton); + if (deleteButton) { + event.preventDefault(); + const confirmationStrings = await getStrings([ + {key: 'confirmation', component: 'admin'}, + {key: 'confirmdeleteitem', component: 'mod_feedback'}, + {key: 'yes', component: 'core'}, + {key: 'no', component: 'core'}, + ]); + Notification.confirm(...confirmationStrings, () => { + window.location = deleteButton.getAttribute('href'); + }); + return; } - }; + }); - return { - setup: manager.setup - }; -}); + initialized = true; +}; diff --git a/mod/feedback/classes/complete_form.php b/mod/feedback/classes/complete_form.php index 5693a3ddd4616..f49ba2cc9b859 100644 --- a/mod/feedback/classes/complete_form.php +++ b/mod/feedback/classes/complete_form.php @@ -580,9 +580,5 @@ public function display() { } $this->_form->display(); - - if ($this->mode == self::MODE_EDIT) { - $PAGE->requires->js_call_amd('mod_feedback/edit', 'setup'); - } } } diff --git a/mod/feedback/edit.php b/mod/feedback/edit.php index 784f60e599909..1f0ddfa99c747 100644 --- a/mod/feedback/edit.php +++ b/mod/feedback/edit.php @@ -86,6 +86,7 @@ 'description' => '' ]); $PAGE->add_body_class('limitedwidth'); +$PAGE->requires->js_call_amd('mod_feedback/edit', 'init'); //Adding the javascript module for the items dragdrop. if (count($feedbackitems) > 1) {