Commit | Line | Data |
---|---|---|
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 | */ | |
24 | define(['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); | |
72 | modal.setBody(Templates.render('core_calendar/event_summary_body', eventdata)); | |
73 | ||
74 | // Hide edit and delete buttons if I don't have permission. | |
75 | if (eventdata.caneditevent == false) { | |
76 | modal.setFooter(''); | |
77 | } | |
78 | ||
79 | modal.show(); | |
80 | }); | |
81 | }); | |
82 | }; | |
83 | ||
84 | /** | |
85 | * Register event listeners for the module. | |
86 | * | |
87 | * @param {object} root The root element. | |
88 | */ | |
89 | var registerEventListeners = function(root) { | |
90 | root = $(root); | |
91 | ||
92 | var loading = false; | |
93 | root.on('click', SELECTORS.EVENT_LINK, function(e) { | |
94 | if (!loading) { | |
95 | loading = true; | |
96 | e.preventDefault(); | |
97 | ||
98 | var eventElement = $(e.target).closest(SELECTORS.EVENT_LINK); | |
99 | var eventId = eventElement.attr('data-event-id'); | |
100 | ||
101 | renderEventSummaryModal(eventId).done(function() { | |
102 | loading = false; | |
103 | }); | |
104 | } | |
105 | }); | |
106 | }; | |
107 | ||
108 | return { | |
109 | init: function() { | |
110 | modalPromise = ModalFactory.create({ | |
111 | type: SummaryModal.TYPE | |
112 | }); | |
113 | ||
114 | registerEventListeners(SELECTORS.ROOT); | |
115 | } | |
116 | }; | |
117 | }); |