Merge branch 'MDL-59538-master' of git://github.com/junpataleta/moodle
[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/**
17 * A javascript module to calendar events.
18 *
19 * @module core_calendar/calendar
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
23 */
24define(['jquery', 'core/ajax', 'core/str', 'core/templates', 'core/notification', 'core/custom_interaction_events',
a43ec9eb
JP
25 'core/modal_factory', 'core_calendar/summary_modal', 'core/modal_events', 'core_calendar/calendar_repository'],
26 function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, ModalEvents, CalendarRepository) {
3b0738f0
SL
27
28 var SELECTORS = {
29 ROOT: "[data-region='calendar']",
30 EVENT_LINK: "[data-action='view-event']",
31 };
32
3b0738f0
SL
33 /**
34 * Get the event type lang string.
35 *
36 * @param {String} eventType The event type.
3d67e83f 37 * @return {promise} The lang string promise.
3b0738f0
SL
38 */
39 var getEventType = function(eventType) {
40 var lang = 'type' + eventType;
41 return Str.get_string(lang, 'core_calendar').then(function(langStr) {
42 return langStr;
3d67e83f
SL
43 });
44 };
45
46 /**
47 * Get the event source.
48 *
49 * @param {Object} subscription The event subscription object.
50 * @return {promise} The lang string promise.
51 */
52 var getEventSource = function(subscription) {
53 return Str.get_string('subsource', 'core_calendar', subscription).then(function(langStr) {
54 if (subscription.url) {
55 return '<a href="' + subscription.url + '">' + langStr + '</a>';
56 }
57 return langStr;
58 });
3b0738f0
SL
59 };
60
61 /**
62 * Render the event summary modal.
63 *
64 * @param {Number} eventId The calendar event id.
3b0738f0
SL
65 */
66 var renderEventSummaryModal = function(eventId) {
3d67e83f 67 // Calendar repository promise.
a43ec9eb
JP
68 CalendarRepository.getEventById(eventId).then(function(getEventResponse) {
69 if (!getEventResponse.event) {
70 throw new Error('Error encountered while trying to fetch calendar event with ID: ' + eventId);
71 }
72 var eventData = getEventResponse.event;
73 var eventTypePromise = getEventType(eventData.eventtype);
74
75 // If the calendar event has event source, get the source's language string/link.
76 if (eventData.displayeventsource) {
77 eventData.subscription = JSON.parse(eventData.subscription);
78 var eventSourceParams = {
79 url: eventData.subscription.url,
80 name: eventData.subscription.name
81 };
82 var eventSourcePromise = getEventSource(eventSourceParams);
83
84 // Return event data with event type and event source info.
85 return $.when(eventTypePromise, eventSourcePromise).then(function(eventType, eventSource) {
86 eventData.eventtype = eventType;
87 eventData.source = eventSource;
88 return eventData;
89 });
3b0738f0 90 }
a43ec9eb
JP
91
92 // Return event data with event type info.
93 return eventTypePromise.then(function(eventType) {
94 eventData.eventtype = eventType;
95 return eventData;
3b0738f0 96 });
3d67e83f 97
a43ec9eb
JP
98 }).then(function(eventData) {
99 // Build the modal parameters from the event data.
100 var modalParams = {
101 title: eventData.name,
102 type: SummaryModal.TYPE,
103 body: Templates.render('core_calendar/event_summary_body', eventData)
104 };
105 if (!eventData.caneditevent) {
106 modalParams.footer = '';
3d67e83f 107 }
a43ec9eb
JP
108 // Create the modal.
109 return ModalFactory.create(modalParams);
110
111 }).done(function(modal) {
112 // Handle hidden event.
113 modal.getRoot().on(ModalEvents.hidden, function() {
114 // Destroy when hidden.
115 modal.destroy();
3b0738f0 116 });
a43ec9eb
JP
117
118 // Finally, render the modal!
119 modal.show();
120
121 }).fail(Notification.exception);
3b0738f0
SL
122 };
123
124 /**
125 * Register event listeners for the module.
3b0738f0 126 */
a43ec9eb
JP
127 var registerEventListeners = function() {
128 // Bind click events to event links.
129 $(SELECTORS.EVENT_LINK).click(function(e) {
130 e.preventDefault();
131 var eventId = $(this).attr('data-event-id');
132 renderEventSummaryModal(eventId);
3b0738f0
SL
133 });
134 };
135
136 return {
137 init: function() {
a43ec9eb 138 registerEventListeners();
3b0738f0
SL
139 }
140 };
141 });