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 * A javascript module to handle summary modal.
19 * @module core_calendar/summary_modal
20 * @package core_calendar
21 * @copyright 2017 Simey Lameze <simey@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 define(['jquery', 'core/str', 'core/notification', 'core/custom_interaction_events', 'core/modal',
25 'core/modal_registry', 'core/modal_factory', 'core/modal_events', 'core_calendar/repository',
26 'core_calendar/events'],
27 function($, Str, Notification, CustomEvents, Modal, ModalRegistry, ModalFactory, ModalEvents, CalendarRepository,
30 var registered = false;
32 ROOT: "[data-region='summary-modal-container']",
33 EDIT_BUTTON: '[data-action="edit"]',
34 DELETE_BUTTON: '[data-action="delete"]',
38 * Constructor for the Modal.
40 * @param {object} root The root jQuery element for the modal
42 var ModalEventSummary = function(root) {
43 Modal.call(this, root);
46 ModalEventSummary.TYPE = 'core_calendar-event_summary';
47 ModalEventSummary.prototype = Object.create(Modal.prototype);
48 ModalEventSummary.prototype.constructor = ModalEventSummary;
51 * Get the edit button element from the footer. The button is cached
52 * as it's not expected to change.
54 * @method getEditButton
55 * @return {object} button element
57 ModalEventSummary.prototype.getEditButton = function() {
58 if (typeof this.editButton == 'undefined') {
59 this.editButton = this.getFooter().find(SELECTORS.EDIT_BUTTON);
62 return this.editButton;
66 * Get the delete button element from the footer. The button is cached
67 * as it's not expected to change.
69 * @method getDeleteButton
70 * @return {object} button element
72 ModalEventSummary.prototype.getDeleteButton = function() {
73 if (typeof this.deleteButton == 'undefined') {
74 this.deleteButton = this.getFooter().find(SELECTORS.DELETE_BUTTON);
77 return this.deleteButton;
81 * Get the id for the event being shown in this modal. This value is
82 * not cached because it will change depending on which event is
88 ModalEventSummary.prototype.getEventId = function() {
89 return this.getBody().find(SELECTORS.ROOT).attr('data-event-id');
93 * Get the url for the event being shown in this modal.
98 ModalEventSummary.prototype.getEditUrl = function() {
99 return this.getBody().find(SELECTORS.ROOT).attr('data-edit-url');
103 * Is this an action event.
105 * @method getEventUrl
108 ModalEventSummary.prototype.isActionEvent = function() {
109 return (this.getBody().find(SELECTORS.ROOT).attr('data-action-event') == 'true');
113 * Set up all of the event handling for the modal.
115 * @method registerEventListeners
117 ModalEventSummary.prototype.registerEventListeners = function() {
118 // Apply parent event listeners.
119 Modal.prototype.registerEventListeners.call(this);
121 // We have to wait for the modal to finish rendering in order to ensure that
122 // the data-event-title property is available to use as the modal title.
123 this.getRoot().on(ModalEvents.bodyRendered, function() {
124 var eventTitle = this.getBody().find(SELECTORS.ROOT).attr('data-event-title');
125 prepareDeleteAction(this, eventTitle);
128 CustomEvents.define(this.getEditButton(), [
129 CustomEvents.events.activate
132 this.getEditButton().on(CustomEvents.events.activate, function(e, data) {
134 if (this.isActionEvent()) {
135 // Action events cannot be edited on the event form and must be redirected to the module UI.
136 $('body').trigger(CalendarEvents.editActionEvent, [this.getEditUrl()]);
138 // When the edit button is clicked we fire an event for the calendar UI to handle.
139 // We don't care how the UI chooses to handle it.
140 $('body').trigger(CalendarEvents.editEvent, [this.getEventId()]);
143 // There is nothing else for us to do so let's hide.
146 // We've handled this event so no need to propagate it.
149 data.originalEvent.preventDefault();
150 data.originalEvent.stopPropagation();
155 * Prepares the action for the summary modal's delete action.
157 * @param {ModalEventSummary} summaryModal The summary modal instance.
158 * @param {string} eventTitle The event title.
160 function prepareDeleteAction(summaryModal, eventTitle) {
161 var deleteStrings = [
164 component: 'calendar'
167 key: 'confirmeventdelete',
168 component: 'calendar',
172 var eventId = summaryModal.getEventId();
173 var stringsPromise = Str.get_strings(deleteStrings);
174 var deletePromise = ModalFactory.create(
176 type: ModalFactory.types.SAVE_CANCEL
178 summaryModal.getDeleteButton()
181 $.when(stringsPromise, deletePromise).then(function(strings, deleteModal) {
182 deleteModal.setTitle(strings[0]);
183 deleteModal.setBody(strings[1]);
184 deleteModal.setSaveButtonText(strings[0]);
185 deleteModal.getRoot().on(ModalEvents.save, function() {
186 CalendarRepository.deleteEvent(eventId).then(function() {
187 $('body').trigger(CalendarEvents.deleted, [eventId]);
190 }).catch(Notification.exception);
193 }).fail(Notification.exception);
196 // Automatically register with the modal registry the first time this module is imported so that you can create modals
197 // of this type using the modal factory.
199 ModalRegistry.register(ModalEventSummary.TYPE, ModalEventSummary, 'core_calendar/event_summary_modal');
203 return ModalEventSummary;