MDL-68409 js: Add helper to register close event on save/cancel

This commit is contained in:
Andrew Nicols
2020-05-04 15:07:48 +08:00
parent cd391f9922
commit fa6101ba27
12 changed files with 121 additions and 15 deletions
+2
View File
@@ -0,0 +1,2 @@
define ("core/local/modal/alert",["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<b.length;c++){d=b[c];d.enumerable=d.enumerable||!1;d.configurable=!0;if("value"in d)d.writable=!0;Object.defineProperty(a,d.key,d)}}function f(a,b,c){if(b)e(a.prototype,b);if(c)e(a,c);return a}function g(a,b){if(b&&("object"===c(b)||"function"==typeof b)){return b}return h(a)}function h(a){if(void 0===a){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return a}function i(a,b,c){if("undefined"!=typeof Reflect&&Reflect.get){i=Reflect.get}else{i=function(a,b,c){var d=j(a,b);if(!d)return;var e=Object.getOwnPropertyDescriptor(d,b);if(e.get){return e.get.call(c)}return e.value}}return i(a,b,c||a)}function j(a,b){while(!Object.prototype.hasOwnProperty.call(a,b)){a=k(a);if(null===a)break}return a}function k(a){k=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)};return k(a)}function l(a,b){if("function"!=typeof b&&null!==b){throw new TypeError("Super expression must either be null or a function")}a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}});if(b)m(a,b)}function m(a,b){m=Object.setPrototypeOf||function(a,b){a.__proto__=b;return a};return m(a,b)}var n=function(a){l(b,a);function b(){d(this,b);return g(this,k(b).apply(this,arguments))}f(b,[{key:"registerEventListeners",value:function registerEventListeners(){i(k(b.prototype),"registerEventListeners",this).call(this);this.registerCloseOnCancel()}}]);return b}(b.default);a.default=n;return a.default});
//# sourceMappingURL=alert.min.js.map
@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/local/modal/alert.js"],"names":["registerCloseOnCancel","Modal"],"mappings":"uJAyBA,uD,2rDAM6B,CAErB,2DAGA,KAAKA,qBAAL,EACH,C,cAVwBC,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 <http://www.gnu.org/licenses/>.\n\n/**\n * Alert modal.\n *\n * @module core/modal_alert\n * @class modal_alert\n * @package core\n * @copyright 2020 Andrew Nicols <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Modal from 'core/modal';\n\nexport default class extends Modal {\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":"alert.min.js"}
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core/modal_factory",["jquery","core/modal_events","core/modal_registry","core/modal","core/modal_save_cancel","core/modal_cancel","core/templates","core/notification","core/custom_interaction_events","core/pending"],function(a,b,c,d,e,f,g,h,i,j){var k={DEFAULT:"core/modal",SAVE_CANCEL:"core/modal_save_cancel",CANCEL:"core/modal_cancel"},l={DEFAULT:"DEFAULT",SAVE_CANCEL:"SAVE_CANCEL",CANCEL:"CANCEL"};c.register(l.DEFAULT,d,k.DEFAULT);c.register(l.SAVE_CANCEL,e,k.SAVE_CANCEL);c.register(l.CANCEL,f,k.CANCEL);var m=function(c,d,e){var f=null,g="function"==typeof e.preShowCallback,h=function(b,d){var h=new j("core/modal_factory:setUpTrigger:triggeredCallback");f=a(b.currentTarget);c.then(function(a){if(g){e.preShowCallback(f,a)}a.show();return a}).then(h.resolve);d.originalEvent.preventDefault()};if(Array.isArray(d)){var k=d[1];d=d[0];i.define(d,[i.events.activate]);d.on(i.events.activate,k,h)}else{i.define(d,[i.events.activate]);d.on(i.events.activate,h)}c.then(function(a){a.getRoot().on(b.hidden,function(){if(null!==f){f.focus()}});return a})},n=function(b,c){c=a(c);var d=b.module,e=new d(c);return e},o=function(b,c){var d=b.template,e=g.render(d,c).then(function(c){var d=a(c);return n(b,d)}).fail(h.exception);return e};return{create:function create(a,b){var d=a.type||l.DEFAULT,e=a.large?!0:!1,f=null,g={};f=c.get(d);if(!f){h.exception({message:"Unable to find modal of type: "+d})}if("undefined"!=typeof a.templateContext){g=a.templateContext}var i=o(f,g).then(function(b){if("undefined"!=typeof a.title){b.setTitle(a.title)}if("undefined"!=typeof a.body){b.setBody(a.body)}if("undefined"!=typeof a.footer){b.setFooter(a.footer)}if(e){b.setLarge()}return b});if("undefined"!=typeof b){m(i,b,a)}return i},types:l}});
define ("core/modal_factory",["jquery","core/modal_events","core/modal_registry","core/modal","core/modal_save_cancel","core/modal_cancel","core/templates","core/notification","core/custom_interaction_events","core/pending"],function(a,b,c,d,e,f,g,h,i,j){var k={DEFAULT:"core/modal",SAVE_CANCEL:"core/modal_save_cancel",CANCEL:"core/modal_cancel"},l={DEFAULT:"DEFAULT",SAVE_CANCEL:"SAVE_CANCEL",CANCEL:"CANCEL"};c.register(l.DEFAULT,d,k.DEFAULT);c.register(l.SAVE_CANCEL,e,k.SAVE_CANCEL);c.register(l.CANCEL,f,k.CANCEL);var m=function(c,d,e){var f=null,g="function"==typeof e.preShowCallback,h=function(b,d){var h=new j("core/modal_factory:setUpTrigger:triggeredCallback");f=a(b.currentTarget);c.then(function(a){if(g){e.preShowCallback(f,a)}a.show();return a}).then(h.resolve);d.originalEvent.preventDefault()};if(Array.isArray(d)){var k=d[1];d=d[0];i.define(d,[i.events.activate]);d.on(i.events.activate,k,h)}else{i.define(d,[i.events.activate]);d.on(i.events.activate,h)}c.then(function(a){a.getRoot().on(b.hidden,function(){if(null!==f){f.focus()}});return a})},n=function(b,c){c=a(c);var d=b.module,e=new d(c);return e},o=function(b,c){var d=b.template,e=g.render(d,c).then(function(c){var d=a(c);return n(b,d)}).fail(h.exception);return e};return{create:function create(a,b){var d=a.type||l.DEFAULT,e=a.large?!0:!1,f=null,g={};f=c.get(d);if(!f){h.exception({message:"Unable to find modal of type: "+d})}if("undefined"!=typeof a.templateContext){g=a.templateContext}var i=o(f,g).then(function(b){if("undefined"!=typeof a.title){b.setTitle(a.title)}if("undefined"!=typeof a.body){b.setBody(a.body)}if("undefined"!=typeof a.footer){b.setFooter(a.footer)}if(e){b.setLarge()}if("undefined"!=typeof a.removeOnClose){b.setRemoveOnClose(a.removeOnClose)}return b});if("undefined"!=typeof b){m(i,b,a)}return i},types:l}});
//# sourceMappingURL=modal_factory.min.js.map
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -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.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))};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",["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});
//# sourceMappingURL=modal_save_cancel.min.js.map
File diff suppressed because one or more lines are too long
+39
View File
@@ -0,0 +1,39 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Alert modal.
*
* @module core/modal_alert
* @class modal_alert
* @package core
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import Modal from 'core/modal';
export default class extends Modal {
/**
* Register all event listeners.
*/
registerEventListeners() {
// Call the parent registration.
super.registerEventListeners();
// Register to close on cancel.
this.registerCloseOnCancel();
}
}
+67
View File
@@ -699,6 +699,7 @@ define([
* @method destroy
*/
Modal.prototype.destroy = function() {
this.hide();
this.root.remove();
this.root.trigger(ModalEvents.destroyed, this);
};
@@ -802,6 +803,52 @@ define([
}.bind(this));
};
/**
* Register a listener to close the dialogue when the cancel button is pressed.
*
* @method registerCloseOnCancel
*/
Modal.prototype.registerCloseOnCancel = function() {
// Handle the clicking of the Cancel button.
this.getModal().on(CustomEvents.events.activate, this.getActionSelector('cancel'), function(e, data) {
var cancelEvent = $.Event(ModalEvents.cancel);
this.getRoot().trigger(cancelEvent, this);
if (!cancelEvent.isDefaultPrevented()) {
data.originalEvent.preventDefault();
if (this.removeOnClose) {
this.destroy();
} else {
this.hide();
}
}
}.bind(this));
};
/**
* Register a listener to close the dialogue when the save button is pressed.
*
* @method registerCloseOnSave
*/
Modal.prototype.registerCloseOnSave = function() {
// Handle the clicking of the Cancel button.
this.getModal().on(CustomEvents.events.activate, this.getActionSelector('save'), function(e, data) {
var saveEvent = $.Event(ModalEvents.save);
this.getRoot().trigger(saveEvent, this);
if (!saveEvent.isDefaultPrevented()) {
data.originalEvent.preventDefault();
if (this.removeOnClose) {
this.destroy();
} else {
this.hide();
}
}
}.bind(this));
};
/**
* Set or resolve and set the value using the function.
*
@@ -827,5 +874,25 @@ define([
return p;
};
/**
* Get the Selector for an action.
*
* @param {String} action
* @returns {DOMString}
*/
Modal.prototype.getActionSelector = function(action) {
return "[data-action='" + action + "']";
};
/**
* Set the flag to remove the modal from the DOM on close.
*
* @param {Boolean} remove
*/
Modal.prototype.setRemoveOnClose = function(remove) {
this.removeOnClose = remove;
};
return Modal;
});
+5
View File
@@ -189,6 +189,11 @@ define(['jquery', 'core/modal_events', 'core/modal_registry', 'core/modal',
modal.setLarge();
}
if (typeof modalConfig.removeOnClose !== 'undefined') {
// If configured remove the modal when hiding it.
modal.setRemoveOnClose(modalConfig.removeOnClose);
}
return modal;
});
+1 -9
View File
@@ -77,15 +77,7 @@ define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/m
}
}.bind(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));
this.registerCloseOnCancel();
};
/**