Moodle release 3.3rc2
[moodle.git] / lib / amd / src / modal_save_cancel.js
CommitLineData
2bcef559
RW
1// This file is part of Moodle - http://moodle.org/
2//
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.
7//
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.
12//
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/>.
15
16/**
17 * Contain the logic for the save/cancel modal.
18 *
19 * @module core/modal_save_cancel
20 * @class modal_save_cancel
21 * @package core
22 * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/modal', 'core/modal_events'],
26 function($, Notification, CustomEvents, Modal, ModalEvents) {
27
28 var SELECTORS = {
29 SAVE_BUTTON: '[data-action="save"]',
30 CANCEL_BUTTON: '[data-action="cancel"]',
31 };
32
33 /**
34 * Constructor for the Modal.
35 *
36 * @param {object} root The root jQuery element for the modal
37 */
38 var ModalSaveCancel = function(root) {
39 Modal.call(this, root);
40
41 if (!this.getFooter().find(SELECTORS.SAVE_BUTTON).length) {
42 Notification.exception({message: 'No save button found'});
43 }
44
45 if (!this.getFooter().find(SELECTORS.CANCEL_BUTTON).length) {
46 Notification.exception({message: 'No cancel button found'});
47 }
48 };
49
50 ModalSaveCancel.prototype = Object.create(Modal.prototype);
51 ModalSaveCancel.prototype.constructor = ModalSaveCancel;
52
53 /**
54 * Override parent implementation to prevent changing the footer content.
55 */
56 ModalSaveCancel.prototype.setFooter = function() {
57 Notification.exception({message: 'Can not change the footer of a save cancel modal'});
58 return;
59 };
60
61 /**
62 * Set up all of the event handling for the modal.
63 *
64 * @method registerEventListeners
65 */
66 ModalSaveCancel.prototype.registerEventListeners = function() {
67 // Apply parent event listeners.
68 Modal.prototype.registerEventListeners.call(this);
69
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);
73
74 if (!saveEvent.isDefaultPrevented()) {
75 this.hide();
76 data.originalEvent.preventDefault();
77 }
78 }.bind(this));
79
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);
83
84 if (!cancelEvent.isDefaultPrevented()) {
85 this.hide();
86 data.originalEvent.preventDefault();
87 }
88 }.bind(this));
89 };
90
91 return ModalSaveCancel;
92});