MDL-59538 core_calendar: add behat step back
[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.
3d67e83f 39 * @return {promise} The lang string promise.
3b0738f0
SL
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;
3d67e83f
SL
45 });
46 };
47
48 /**
49 * Get the event source.
50 *
51 * @param {Object} subscription The event subscription object.
52 * @return {promise} The lang string promise.
53 */
54 var getEventSource = function(subscription) {
55 return Str.get_string('subsource', 'core_calendar', subscription).then(function(langStr) {
56 if (subscription.url) {
57 return '<a href="' + subscription.url + '">' + langStr + '</a>';
58 }
59 return langStr;
60 });
3b0738f0
SL
61 };
62
63 /**
64 * Render the event summary modal.
65 *
66 * @param {Number} eventId The calendar event id.
67 * @return {promise} The summary modal promise.
68 */
69 var renderEventSummaryModal = function(eventId) {
70
3b0738f0 71
3d67e83f
SL
72 // Calendar repository promise.
73 var repositoryPromise = CalendarRepository.getEventById(eventId);
74 return repositoryPromise.then(function(result) {
3b0738f0 75 if (!result.event) {
3d67e83f 76 repositoryPromise.fail(Notification.exception);
3b0738f0
SL
77 } else {
78 return result.event;
79 }
80 }).then(function(eventdata) {
3d67e83f
SL
81 // Event type promise.
82 var eventTypePromise = getEventType(eventdata.eventtype);
83 return eventTypePromise.then(function(langStr) {
84 if(!langStr) {
85 eventTypePromise.fail(Notification.exception);
86 } else {
87 eventdata.eventtype = langStr;
88 return eventdata;
89 }
3b0738f0
SL
90 });
91 }).then(function(eventdata) {
3d67e83f
SL
92 // If the calendar event has event source, get the language string or the link.
93 if (eventdata.displayeventsource == true) {
94 eventdata.subscription = JSON.parse(eventdata.subscription);
95 var eventpromise = getEventSource({url: eventdata.subscription.url, name: eventdata.subscription.name});
96 if (eventpromise) {
97 $.Deferred().resolve();
98 eventpromise.done(function(source) {
99 eventdata.source = source;
100 });
101 } else {
102 eventpromise.fail(Notification.exception);
103 }
104
105 }
3b0738f0
SL
106 return modalPromise.done(function(modal) {
107 modal.setTitle(eventdata.name);
d4a87aa3 108 modal.setBody(Templates.render('core_calendar/event_summary_body', eventdata));
3b0738f0
SL
109 // Hide edit and delete buttons if I don't have permission.
110 if (eventdata.caneditevent == false) {
111 modal.setFooter('');
112 }
113
114 modal.show();
115 });
116 });
117 };
118
119 /**
120 * Register event listeners for the module.
121 *
122 * @param {object} root The root element.
123 */
124 var registerEventListeners = function(root) {
125 root = $(root);
126
127 var loading = false;
128 root.on('click', SELECTORS.EVENT_LINK, function(e) {
129 if (!loading) {
130 loading = true;
131 e.preventDefault();
132
133 var eventElement = $(e.target).closest(SELECTORS.EVENT_LINK);
134 var eventId = eventElement.attr('data-event-id');
135
136 renderEventSummaryModal(eventId).done(function() {
137 loading = false;
3d67e83f 138 }).fail(Notification.exception);
3b0738f0
SL
139 }
140 });
141 };
142
143 return {
144 init: function() {
145 modalPromise = ModalFactory.create({
146 type: SummaryModal.TYPE
147 });
148
149 registerEventListeners(SELECTORS.ROOT);
150 }
151 };
152 });