1 // This file is part of Moodle - http://moodle.org/
3 // Moodle is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // Moodle is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 * Contain the logic for the save/cancel modal.
19 * @module core/modal_save_cancel
20 * @class modal_save_cancel
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'],
26 function($, Notification, CustomEvents, Modal, ModalEvents) {
29 SAVE_BUTTON: '[data-action="save"]',
30 CANCEL_BUTTON: '[data-action="cancel"]',
34 * Constructor for the Modal.
36 * @param {object} root The root jQuery element for the modal
38 var ModalSaveCancel = function(root) {
39 Modal.call(this, root);
41 if (!this.getFooter().find(SELECTORS.SAVE_BUTTON).length) {
42 Notification.exception({message: 'No save button found'});
45 if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) {
46 Notification.exception({message: 'No cancel button found'});
50 ModalSaveCancel.prototype = Object.create(Modal.prototype);
51 ModalSaveCancel.prototype.constructor = ModalSaveCancel;
54 * Override parent implementation to prevent changing the footer content.
56 ModalSaveCancel.prototype.setFooter = function() {
57 Notification.exception({message: 'Can not change the footer of a save cancel modal'});
62 * Set up all of the event handling for the modal.
64 * @method registerEventListeners
66 ModalSaveCancel.prototype.registerEventListeners = function() {
67 // Apply parent event listeners.
68 Modal.prototype.registerEventListeners.call(this);
70 this.getModal().on(CustomEvents.events.activate, SELECTORS.SAVE_BUTTON, function(e, data) {
71 var saveEvent = $.Event(ModalEvents.save);
72 this.getRoot().trigger(saveEvent, this);
74 if (!saveEvent.isDefaultPrevented()) {
76 data.originalEvent.preventDefault();
80 this.getModal().on(CustomEvents.events.activate, SELECTORS.CANCEL_BUTTON, function(e, data) {
81 var cancelEvent = $.Event(ModalEvents.cancel);
82 this.getRoot().trigger(cancelEvent, this);
84 if (!cancelEvent.isDefaultPrevented()) {
86 data.originalEvent.preventDefault();
92 * Allows to overwrite the text of "Save changes" button.
94 * @param {String} text
96 ModalSaveCancel.prototype.setSaveButtonText = function(text) {
97 this.getFooter().find(SELECTORS.SAVE_BUTTON).text(text);
100 return ModalSaveCancel;