MDL-59382 calendar: fix course id for site event
[moodle.git] / calendar / amd / src / calendar.js
CommitLineData
3b0738f0
SL
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/**
aa091225
RW
17 * This module is the highest level module for the calendar. It is
18 * responsible for initialising all of the components required for
19 * the calendar to run. It also coordinates the interaction between
20 * components by listening for and responding to different events
21 * triggered within the calendar UI.
3b0738f0
SL
22 *
23 * @module core_calendar/calendar
24 * @package core_calendar
25 * @copyright 2017 Simey Lameze <simey@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
aa091225
RW
28define([
29 'jquery',
30 'core/ajax',
31 'core/str',
32 'core/templates',
33 'core/notification',
34 'core/custom_interaction_events',
35 'core/modal_events',
36 'core/modal_factory',
37 'core_calendar/modal_event_form',
38 'core_calendar/summary_modal',
39 'core_calendar/repository',
40 'core_calendar/events'
41 ],
42 function(
43 $,
44 Ajax,
45 Str,
46 Templates,
47 Notification,
48 CustomEvents,
49 ModalEvents,
50 ModalFactory,
51 ModalEventForm,
52 SummaryModal,
53 CalendarRepository,
54 CalendarEvents
55 ) {
56
57 var SELECTORS = {
58 ROOT: "[data-region='calendar']",
59 EVENT_LINK: "[data-action='view-event']",
60 NEW_EVENT_BUTTON: "[data-action='new-event-button']"
61 };
62
63 /**
64 * Get the event type lang string.
65 *
66 * @param {String} eventType The event type.
67 * @return {promise} The lang string promise.
68 */
69 var getEventType = function(eventType) {
70 var lang = 'type' + eventType;
71 return Str.get_string(lang, 'core_calendar').then(function(langStr) {
72 return langStr;
73 });
74 };
75
76 /**
77 * Get the event source.
78 *
79 * @param {Object} subscription The event subscription object.
80 * @return {promise} The lang string promise.
81 */
82 var getEventSource = function(subscription) {
83 return Str.get_string('subsource', 'core_calendar', subscription).then(function(langStr) {
84 if (subscription.url) {
85 return '<a href="' + subscription.url + '">' + langStr + '</a>';
86 }
87 return langStr;
88 });
89 };
a43ec9eb 90
aa091225
RW
91 /**
92 * Render the event summary modal.
93 *
94 * @param {Number} eventId The calendar event id.
95 */
96 var renderEventSummaryModal = function(eventId) {
97 // Calendar repository promise.
98 CalendarRepository.getEventById(eventId).then(function(getEventResponse) {
99 if (!getEventResponse.event) {
100 throw new Error('Error encountered while trying to fetch calendar event with ID: ' + eventId);
101 }
102 var eventData = getEventResponse.event;
103 var eventTypePromise = getEventType(eventData.eventtype);
104
105 // If the calendar event has event source, get the source's language string/link.
106 if (eventData.displayeventsource) {
107 eventData.subscription = JSON.parse(eventData.subscription);
108 var eventSourceParams = {
109 url: eventData.subscription.url,
110 name: eventData.subscription.name
111 };
112 var eventSourcePromise = getEventSource(eventSourceParams);
113
114 // Return event data with event type and event source info.
115 return $.when(eventTypePromise, eventSourcePromise).then(function(eventType, eventSource) {
a43ec9eb 116 eventData.eventtype = eventType;
aa091225 117 eventData.source = eventSource;
a43ec9eb 118 return eventData;
3b0738f0 119 });
aa091225 120 }
3d67e83f 121
aa091225
RW
122 // Return event data with event type info.
123 return eventTypePromise.then(function(eventType) {
124 eventData.eventtype = eventType;
125 return eventData;
126 });
127
128 }).then(function(eventData) {
129 // Build the modal parameters from the event data.
130 var modalParams = {
131 title: eventData.name,
132 type: SummaryModal.TYPE,
133 body: Templates.render('core_calendar/event_summary_body', eventData)
134 };
135 if (!eventData.caneditevent) {
136 modalParams.footer = '';
137 }
138 // Create the modal.
139 return ModalFactory.create(modalParams);
140
141 }).done(function(modal) {
142 // Handle hidden event.
143 modal.getRoot().on(ModalEvents.hidden, function() {
144 // Destroy when hidden.
145 modal.destroy();
146 });
147
148 // Finally, render the modal!
149 modal.show();
150
151 }).fail(Notification.exception);
152 };
153
154 /**
155 * Create the event form modal for creating new events and
156 * editing existing events.
157 *
158 * @method registerEventFormModal
159 * @param {object} root The calendar root element
160 * @return {object} The create modal promise
161 */
162 var registerEventFormModal = function(root) {
163 var newEventButton = root.find(SELECTORS.NEW_EVENT_BUTTON);
164 var contextId = newEventButton.attr('data-context-id');
165
166 return ModalFactory.create(
167 {
168 type: ModalEventForm.TYPE,
169 large: true,
170 templateContext: {
171 contextid: contextId
3d67e83f 172 }
aa091225
RW
173 },
174 newEventButton
175 );
176 };
a43ec9eb 177
aa091225
RW
178 /**
179 * Listen to and handle any calendar events fired by the calendar UI.
180 *
181 * @method registerCalendarEventListeners
182 * @param {object} root The calendar root element
183 * @param {object} eventFormModalPromise A promise reolved with the event form modal
184 */
185 var registerCalendarEventListeners = function(root, eventFormModalPromise) {
186 var body = $('body');
187
188 // TODO: Replace these with actual logic to update
189 // the UI without having to force a page reload.
190 body.on(CalendarEvents.created, function() { window.location.reload(); });
191 body.on(CalendarEvents.deleted, function() { window.location.reload(); });
192 body.on(CalendarEvents.updated, function() { window.location.reload(); });
a43ec9eb 193
aa091225
RW
194 eventFormModalPromise.then(function(modal) {
195 // When something within the calendar tells us the user wants
196 // to edit an event then show the event form modal.
197 body.on(CalendarEvents.editEvent, function(e, eventId) {
198 modal.setEventId(eventId);
199 modal.show();
3b0738f0 200 });
3b0738f0 201
aa091225
RW
202 return;
203 });
204 };
205
206 /**
207 * Register event listeners for the module.
208 */
209 var registerEventListeners = function() {
210 var root = $(SELECTORS.ROOT);
211
212 // Bind click events to event links.
213 $(SELECTORS.EVENT_LINK).click(function(e) {
214 e.preventDefault();
215 var eventId = $(this).attr('data-event-id');
216 renderEventSummaryModal(eventId);
217 });
218
219 var eventFormPromise = registerEventFormModal(root);
220 registerCalendarEventListeners(root, eventFormPromise);
221 };
222
223 return {
224 init: function() {
225 registerEventListeners();
226 }
227 };
228});