MDL-59538 core_calendar: add event source to summary template
[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',
25 'core/modal_factory', 'core_calendar/summary_modal', 'core_calendar/calendar_repository'],
26 function($, Ajax, Str, Templates, Notification, CustomEvents, ModalFactory, SummaryModal, CalendarRepository) {
27
28 var SELECTORS = {
29 ROOT: "[data-region='calendar']",
30 EVENT_LINK: "[data-action='view-event']",
31 };
32
33 var modalPromise = null;
34
35 /**
36 * Get the event type lang string.
37 *
38 * @param {String} eventType The event type.
39 * @return {String} The lang string of the event type.
40 */
41 var getEventType = function(eventType) {
42 var lang = 'type' + eventType;
43 return Str.get_string(lang, 'core_calendar').then(function(langStr) {
44 return langStr;
45 }).fail(Notification.exception);
46 };
47
48 /**
49 * Render the event summary modal.
50 *
51 * @param {Number} eventId The calendar event id.
52 * @return {promise} The summary modal promise.
53 */
54 var renderEventSummaryModal = function(eventId) {
55
56 var promise = CalendarRepository.getEventById(eventId);
57
58 return promise.then(function(result) {
59 if (!result.event) {
60 promise.fail(Notification.exception);
61 } else {
62 return result.event;
63 }
64 }).then(function(eventdata) {
65 return getEventType(eventdata.eventtype).then(function(langStr) {
66 eventdata.eventtype = langStr;
67 return eventdata;
68 });
69 }).then(function(eventdata) {
70 return modalPromise.done(function(modal) {
71 modal.setTitle(eventdata.name);
d4a87aa3 72 modal.setBody(Templates.render('core_calendar/event_summary_body', eventdata));
3b0738f0
SL
73 // Hide edit and delete buttons if I don't have permission.
74 if (eventdata.caneditevent == false) {
75 modal.setFooter('');
76 }
77
78 modal.show();
79 });
80 });
81 };
82
83 /**
84 * Register event listeners for the module.
85 *
86 * @param {object} root The root element.
87 */
88 var registerEventListeners = function(root) {
89 root = $(root);
90
91 var loading = false;
92 root.on('click', SELECTORS.EVENT_LINK, function(e) {
93 if (!loading) {
94 loading = true;
95 e.preventDefault();
96
97 var eventElement = $(e.target).closest(SELECTORS.EVENT_LINK);
98 var eventId = eventElement.attr('data-event-id');
99
100 renderEventSummaryModal(eventId).done(function() {
101 loading = false;
102 });
103 }
104 });
105 };
106
107 return {
108 init: function() {
109 modalPromise = ModalFactory.create({
110 type: SummaryModal.TYPE
111 });
112
113 registerEventListeners(SELECTORS.ROOT);
114 }
115 };
116 });