MDL-68409 js: Add helper to register close event on save/cancel
authorAndrew Nicols <andrew@nicols.co.uk>
Tue, 14 Apr 2020 00:53:35 +0000 (08:53 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 4 May 2020 07:07:48 +0000 (15:07 +0800)
12 files changed:
lib/amd/build/local/modal/alert.min.js [new file with mode: 0644]
lib/amd/build/local/modal/alert.min.js.map [new file with mode: 0644]
lib/amd/build/modal.min.js
lib/amd/build/modal.min.js.map
lib/amd/build/modal_factory.min.js
lib/amd/build/modal_factory.min.js.map
lib/amd/build/modal_save_cancel.min.js
lib/amd/build/modal_save_cancel.min.js.map
lib/amd/src/local/modal/alert.js [new file with mode: 0644]
lib/amd/src/modal.js
lib/amd/src/modal_factory.js
lib/amd/src/modal_save_cancel.js

diff --git a/lib/amd/build/local/modal/alert.min.js b/lib/amd/build/local/modal/alert.min.js
new file mode 100644 (file)
index 0000000..8344163
Binary files /dev/null and b/lib/amd/build/local/modal/alert.min.js differ
diff --git a/lib/amd/build/local/modal/alert.min.js.map b/lib/amd/build/local/modal/alert.min.js.map
new file mode 100644 (file)
index 0000000..5d15d67
Binary files /dev/null and b/lib/amd/build/local/modal/alert.min.js.map differ
index 3269e6e..6825b53 100644 (file)
Binary files a/lib/amd/build/modal.min.js and b/lib/amd/build/modal.min.js differ
index 5b24706..49f3b81 100644 (file)
Binary files a/lib/amd/build/modal.min.js.map and b/lib/amd/build/modal.min.js.map differ
index 2fa4d54..212386f 100644 (file)
Binary files a/lib/amd/build/modal_factory.min.js and b/lib/amd/build/modal_factory.min.js differ
index ff93873..8116c26 100644 (file)
Binary files a/lib/amd/build/modal_factory.min.js.map and b/lib/amd/build/modal_factory.min.js.map differ
index 2a02302..366a16f 100644 (file)
Binary files a/lib/amd/build/modal_save_cancel.min.js and b/lib/amd/build/modal_save_cancel.min.js differ
index c2f3a26..cbe4dbf 100644 (file)
Binary files a/lib/amd/build/modal_save_cancel.min.js.map and b/lib/amd/build/modal_save_cancel.min.js.map differ
diff --git a/lib/amd/src/local/modal/alert.js b/lib/amd/src/local/modal/alert.js
new file mode 100644 (file)
index 0000000..da90eef
--- /dev/null
@@ -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 <andrew@nicols.co.uk>
+ * @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();
+    }
+}
index aeefaed..97d113a 100644 (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;
 });
index 2b304bc..65c8854 100644 (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;
             });
 
index f62751c..8b37e86 100644 (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();
     };
 
     /**