From 67774a9567337a25ccaa4b9517d793b8b277d49e Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 14 Apr 2020 14:32:09 +0800 Subject: [PATCH] MDL-68409 js: Convert cancel modals to ES6 --- lib/amd/build/modal_cancel.min.js | 2 +- lib/amd/build/modal_cancel.min.js.map | 2 +- lib/amd/build/modal_save_cancel.min.js | 2 +- lib/amd/build/modal_save_cancel.min.js.map | 2 +- lib/amd/src/modal_cancel.js | 61 ++++------------ lib/amd/src/modal_save_cancel.js | 83 +++++++--------------- 6 files changed, 46 insertions(+), 106 deletions(-) diff --git a/lib/amd/build/modal_cancel.min.js b/lib/amd/build/modal_cancel.min.js index c172543552d1a..90366c2fb4eb2 100644 --- a/lib/amd/build/modal_cancel.min.js +++ b/lib/amd/build/modal_cancel.min.js @@ -1,2 +1,2 @@ -define ("core/modal_cancel",["jquery","core/notification","core/custom_interaction_events","core/modal","core/modal_events"],function(a,b,c,d,f){var g={CANCEL_BUTTON:"[data-action=\"cancel\"]"},h=function(a){d.call(this,a);if(!this.getFooter().find(g.CANCEL_BUTTON).length){b.exception({message:"No cancel button found"})}};h.prototype=Object.create(d.prototype);h.prototype.constructor=h;h.prototype.setFooter=function(){b.exception({message:"Can not change the footer of a cancel modal"})};h.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this);this.getModal().on(c.events.activate,g.CANCEL_BUTTON,function(b,c){var d=a.Event(f.cancel);this.getRoot().trigger(d,this);if(!d.isDefaultPrevented()){this.hide();c.originalEvent.preventDefault()}}.bind(this))};return h}); +define ("core/modal_cancel",["exports","core/modal"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);function c(a){if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){c=function(a){return typeof a}}else{c=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return c(a)}function d(a,b){if(!(a instanceof b)){throw new TypeError("Cannot call a class as a function")}}function e(a,b){for(var c=0,d;c.\n\n/**\n * Contain the logic for the cancel modal.\n *\n * @module core/modal_cancel\n * @class modal_cancel\n * @package core\n * @copyright 2016 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'],\n function($, Notification, CustomEvents, Modal, ModalEvents) {\n\n var SELECTORS = {\n CANCEL_BUTTON: '[data-action=\"cancel\"]',\n };\n\n /**\n * Constructor for the Modal.\n *\n * @param {object} root The root jQuery element for the modal\n */\n var ModalCancel = function(root) {\n Modal.call(this, root);\n\n if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) {\n Notification.exception({message: 'No cancel button found'});\n }\n };\n\n ModalCancel.prototype = Object.create(Modal.prototype);\n ModalCancel.prototype.constructor = ModalCancel;\n\n /**\n * Override parent implementation to prevent changing the footer content.\n */\n ModalCancel.prototype.setFooter = function() {\n Notification.exception({message: 'Can not change the footer of a cancel modal'});\n return;\n };\n\n /**\n * Set up all of the event handling for the modal.\n *\n * @method registerEventListeners\n */\n ModalCancel.prototype.registerEventListeners = function() {\n // Apply parent event listeners.\n Modal.prototype.registerEventListeners.call(this);\n\n this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) {\n var cancelEvent = $.Event(ModalEvents.cancel);\n this.getRoot().trigger(cancelEvent, this);\n\n if (!cancelEvent.isDefaultPrevented()) {\n this.hide();\n data.originalEvent.preventDefault();\n }\n }.bind(this));\n };\n\n return ModalCancel;\n});\n"],"file":"modal_cancel.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/modal_cancel.js"],"names":["root","getFooter","find","getActionSelector","length","Notification","exception","message","registerCloseOnCancel","Modal"],"mappings":"kJAwBA,uD,gjDAGI,WAAYA,CAAZ,CAAkB,iBACd,wBAAMA,CAAN,GAEA,GAAI,CAAC,EAAKC,SAAL,GAAiBC,IAAjB,CAAsB,EAAKC,iBAAL,CAAuB,QAAvB,CAAtB,EAAwDC,MAA7D,CAAqE,CACjEC,YAAY,CAACC,SAAb,CAAuB,CAACC,OAAO,CAAE,wBAAV,CAAvB,CACH,CALa,QAMjB,C,0EAKwB,CAErB,2DAGA,KAAKC,qBAAL,EACH,C,cAlBwBC,S","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 * Contain the logic for the cancel modal.\n *\n * @module core/modal_cancel\n * @class modal_cancel\n * @package core\n * @copyright 2016 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Modal from 'core/modal';\n\nexport default class extends Modal {\n constructor(root) {\n super(root);\n\n if (!this.getFooter().find(this.getActionSelector('cancel')).length) {\n Notification.exception({message: 'No cancel button found'});\n }\n }\n\n /**\n * Register all event listeners.\n */\n registerEventListeners() {\n // Call the parent registration.\n super.registerEventListeners();\n\n // Register to close on cancel.\n this.registerCloseOnCancel();\n }\n}\n"],"file":"modal_cancel.min.js"} \ No newline at end of file diff --git a/lib/amd/build/modal_save_cancel.min.js b/lib/amd/build/modal_save_cancel.min.js index 366a16ff86d0a..5cf92def507d1 100644 --- a/lib/amd/build/modal_save_cancel.min.js +++ b/lib/amd/build/modal_save_cancel.min.js @@ -1,2 +1,2 @@ -define ("core/modal_save_cancel",["jquery","core/notification","core/custom_interaction_events","core/modal","core/modal_events"],function(a,b,c,d,f){var g={SAVE_BUTTON:"[data-action=\"save\"]",CANCEL_BUTTON:"[data-action=\"cancel\"]"},h=function(a){d.call(this,a);if(!this.getFooter().find(g.SAVE_BUTTON).length){b.exception({message:"No save button found"})}if(!this.getFooter().find(g.CANCEL_BUTTON).length){b.exception({message:"No cancel button found"})}};h.prototype=Object.create(d.prototype);h.prototype.constructor=h;h.prototype.setFooter=function(){b.exception({message:"Can not change the footer of a save cancel modal"})};h.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this);this.getModal().on(c.events.activate,g.SAVE_BUTTON,function(b,c){var d=a.Event(f.save);this.getRoot().trigger(d,this);if(!d.isDefaultPrevented()){this.hide();c.originalEvent.preventDefault()}}.bind(this));this.registerCloseOnCancel()};h.prototype.setSaveButtonText=function(a){var b=this.getFooter().find(g.SAVE_BUTTON);this.asyncSet(a,b.text.bind(b))};return h}); +define ("core/modal_save_cancel",["exports","core/modal"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.default=void 0;b=function(a){return a&&a.__esModule?a:{default:a}}(b);function c(a){if("function"==typeof Symbol&&"symbol"==typeof Symbol.iterator){c=function(a){return typeof a}}else{c=function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a}}return c(a)}function d(a,b){if(!(a instanceof b)){throw new TypeError("Cannot call a class as a function")}}function e(a,b){for(var c=0,d;c.\n\n/**\n * Contain the logic for the save/cancel modal.\n *\n * @module core/modal_save_cancel\n * @class modal_save_cancel\n * @package core\n * @copyright 2016 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'],\n function($, Notification, CustomEvents, Modal, ModalEvents) {\n\n var SELECTORS = {\n SAVE_BUTTON: '[data-action=\"save\"]',\n CANCEL_BUTTON: '[data-action=\"cancel\"]',\n };\n\n /**\n * Constructor for the Modal.\n *\n * @param {object} root The root jQuery element for the modal\n */\n var ModalSaveCancel = function(root) {\n Modal.call(this, root);\n\n if (!this.getFooter().find(SELECTORS.SAVE_BUTTON).length) {\n Notification.exception({message: 'No save button found'});\n }\n\n if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) {\n Notification.exception({message: 'No cancel button found'});\n }\n };\n\n ModalSaveCancel.prototype = Object.create(Modal.prototype);\n ModalSaveCancel.prototype.constructor = ModalSaveCancel;\n\n /**\n * Override parent implementation to prevent changing the footer content.\n */\n ModalSaveCancel.prototype.setFooter = function() {\n Notification.exception({message: 'Can not change the footer of a save cancel modal'});\n return;\n };\n\n /**\n * Set up all of the event handling for the modal.\n *\n * @method registerEventListeners\n */\n ModalSaveCancel.prototype.registerEventListeners = function() {\n // Apply parent event listeners.\n Modal.prototype.registerEventListeners.call(this);\n\n this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, function(e, data) {\n var saveEvent = $.Event(ModalEvents.save);\n this.getRoot().trigger(saveEvent, this);\n\n if (!saveEvent.isDefaultPrevented()) {\n this.hide();\n data.originalEvent.preventDefault();\n }\n }.bind(this));\n\n this.registerCloseOnCancel();\n };\n\n /**\n * Allows to overwrite the text of \"Save changes\" button.\n *\n * This method is overloaded to take either a string value for the button title or a jQuery promise that is resolved with\n * text most commonly from a Str.get_string call.\n *\n * @param {(String|object)} value The button text, or a jQuery promise which will resolve it\n */\n ModalSaveCancel.prototype.setSaveButtonText = function(value) {\n var button = this.getFooter().find(SELECTORS.SAVE_BUTTON);\n\n this.asyncSet(value, button.text.bind(button));\n };\n\n return ModalSaveCancel;\n});\n"],"file":"modal_save_cancel.min.js"} \ No newline at end of file +{"version":3,"sources":["../src/modal_save_cancel.js"],"names":["root","getFooter","find","getActionSelector","length","Notification","exception","message","registerCloseOnSave","registerCloseOnCancel","value","setButtonText","Modal"],"mappings":"uJAwBA,uD,gjDAGI,WAAYA,CAAZ,CAAkB,iBACd,wBAAMA,CAAN,GAEA,GAAI,CAAC,EAAKC,SAAL,GAAiBC,IAAjB,CAAsB,EAAKC,iBAAL,CAAuB,MAAvB,CAAtB,EAAsDC,MAA3D,CAAmE,CAC/DC,YAAY,CAACC,SAAb,CAAuB,CAACC,OAAO,CAAE,sBAAV,CAAvB,CACH,CAED,GAAI,CAAC,EAAKN,SAAL,GAAiBC,IAAjB,CAAsB,EAAKC,iBAAL,CAAuB,QAAvB,CAAtB,EAAwDC,MAA7D,CAAqE,CACjEC,YAAY,CAACC,SAAb,CAAuB,CAACC,OAAO,CAAE,wBAAV,CAAvB,CACH,CATa,QAUjB,C,0EAKwB,CAErB,2DAGA,KAAKC,mBAAL,GACA,KAAKC,qBAAL,EACH,C,6CAKW,CACRJ,YAAY,CAACC,SAAb,CAAuB,CAACC,OAAO,CAAE,kDAAV,CAAvB,CAEH,C,4DAQiBG,C,CAAO,CACrB,MAAO,MAAKC,aAAL,CAAmB,MAAnB,CAA2BD,CAA3B,CACV,C,cAzCwBE,S","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 * Contain the logic for the save/cancel modal.\n *\n * @module core/modal_save_cancel\n * @class modal_save_cancel\n * @package core\n * @copyright 2016 Ryan Wyllie \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport Modal from 'core/modal';\n\nexport default class extends Modal {\n constructor(root) {\n super(root);\n\n if (!this.getFooter().find(this.getActionSelector('save')).length) {\n Notification.exception({message: 'No save button found'});\n }\n\n if (!this.getFooter().find(this.getActionSelector('cancel')).length) {\n Notification.exception({message: 'No cancel button found'});\n }\n }\n\n /**\n * Register all event listeners.\n */\n registerEventListeners() {\n // Call the parent registration.\n super.registerEventListeners();\n\n // Register to close on save/cancel.\n this.registerCloseOnSave();\n this.registerCloseOnCancel();\n }\n\n /**\n * Override parent implementation to prevent changing the footer content.\n */\n setFooter() {\n Notification.exception({message: 'Can not change the footer of a save cancel modal'});\n return;\n }\n\n /**\n * Set the title of the save button.\n *\n * @param {String|Promise} value The button text, or a Promise which will resolve it\n * @returns{Promise}\n */\n setSaveButtonText(value) {\n return this.setButtonText('save', value);\n }\n}\n"],"file":"modal_save_cancel.min.js"} \ No newline at end of file diff --git a/lib/amd/src/modal_cancel.js b/lib/amd/src/modal_cancel.js index 2e182c1e2d2d4..8f593f0c8fa86 100644 --- a/lib/amd/src/modal_cancel.js +++ b/lib/amd/src/modal_cancel.js @@ -22,56 +22,25 @@ * @copyright 2016 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'], - function($, Notification, CustomEvents, Modal, ModalEvents) { +import Modal from 'core/modal'; - var SELECTORS = { - CANCEL_BUTTON: '[data-action="cancel"]', - }; +export default class extends Modal { + constructor(root) { + super(root); - /** - * Constructor for the Modal. - * - * @param {object} root The root jQuery element for the modal - */ - var ModalCancel = function(root) { - Modal.call(this, root); - - if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) { + if (!this.getFooter().find(this.getActionSelector('cancel')).length) { Notification.exception({message: 'No cancel button found'}); } - }; - - ModalCancel.prototype = Object.create(Modal.prototype); - ModalCancel.prototype.constructor = ModalCancel; - - /** - * Override parent implementation to prevent changing the footer content. - */ - ModalCancel.prototype.setFooter = function() { - Notification.exception({message: 'Can not change the footer of a cancel modal'}); - return; - }; + } /** - * Set up all of the event handling for the modal. - * - * @method registerEventListeners + * Register all event listeners. */ - ModalCancel.prototype.registerEventListeners = function() { - // Apply parent event listeners. - Modal.prototype.registerEventListeners.call(this); - - this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) { - var cancelEvent = $.Event(ModalEvents.cancel); - this.getRoot().trigger(cancelEvent, this); - - if (!cancelEvent.isDefaultPrevented()) { - this.hide(); - data.originalEvent.preventDefault(); - } - }.bind(this)); - }; - - return ModalCancel; -}); + registerEventListeners() { + // Call the parent registration. + super.registerEventListeners(); + + // Register to close on cancel. + this.registerCloseOnCancel(); + } +} diff --git a/lib/amd/src/modal_save_cancel.js b/lib/amd/src/modal_save_cancel.js index 8b37e865e9795..42416bebfcda5 100644 --- a/lib/amd/src/modal_save_cancel.js +++ b/lib/amd/src/modal_save_cancel.js @@ -22,77 +22,48 @@ * @copyright 2016 Ryan Wyllie * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'], - function($, Notification, CustomEvents, Modal, ModalEvents) { +import Modal from 'core/modal'; - var SELECTORS = { - SAVE_BUTTON: '[data-action="save"]', - CANCEL_BUTTON: '[data-action="cancel"]', - }; +export default class extends Modal { + constructor(root) { + super(root); - /** - * Constructor for the Modal. - * - * @param {object} root The root jQuery element for the modal - */ - var ModalSaveCancel = function(root) { - Modal.call(this, root); - - if (!this.getFooter().find(SELECTORS.SAVE_BUTTON).length) { + if (!this.getFooter().find(this.getActionSelector('save')).length) { Notification.exception({message: 'No save button found'}); } - if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) { + if (!this.getFooter().find(this.getActionSelector('cancel')).length) { Notification.exception({message: 'No cancel button found'}); } - }; + } + + /** + * Register all event listeners. + */ + registerEventListeners() { + // Call the parent registration. + super.registerEventListeners(); - ModalSaveCancel.prototype = Object.create(Modal.prototype); - ModalSaveCancel.prototype.constructor = ModalSaveCancel; + // Register to close on save/cancel. + this.registerCloseOnSave(); + this.registerCloseOnCancel(); + } /** * Override parent implementation to prevent changing the footer content. */ - ModalSaveCancel.prototype.setFooter = function() { + setFooter() { Notification.exception({message: 'Can not change the footer of a save cancel modal'}); return; - }; + } /** - * Set up all of the event handling for the modal. + * Set the title of the save button. * - * @method registerEventListeners + * @param {String|Promise} value The button text, or a Promise which will resolve it + * @returns{Promise} */ - ModalSaveCancel.prototype.registerEventListeners = function() { - // Apply parent event listeners. - Modal.prototype.registerEventListeners.call(this); - - this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, function(e, data) { - var saveEvent = $.Event(ModalEvents.save); - this.getRoot().trigger(saveEvent, this); - - if (!saveEvent.isDefaultPrevented()) { - this.hide(); - data.originalEvent.preventDefault(); - } - }.bind(this)); - - this.registerCloseOnCancel(); - }; - - /** - * Allows to overwrite the text of "Save changes" button. - * - * This method is overloaded to take either a string value for the button title or a jQuery promise that is resolved with - * text most commonly from a Str.get_string call. - * - * @param {(String|object)} value The button text, or a jQuery promise which will resolve it - */ - ModalSaveCancel.prototype.setSaveButtonText = function(value) { - var button = this.getFooter().find(SELECTORS.SAVE_BUTTON); - - this.asyncSet(value, button.text.bind(button)); - }; - - return ModalSaveCancel; -}); + setSaveButtonText(value) { + return this.setButtonText('save', value); + } +}