Commit | Line | Data |
---|---|---|
695c5726 AN |
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 handler calendar view changes. | |
18 | * | |
19 | * @module core_calendar/view_manager | |
20 | * @package core_calendar | |
21 | * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
a4af4c96 SL |
24 | define(['jquery', 'core/templates', 'core/notification', 'core_calendar/repository', 'core_calendar/events'], |
25 | function($, Templates, Notification, CalendarRepository, CalendarEvents) { | |
695c5726 AN |
26 | |
27 | var SELECTORS = { | |
28 | ROOT: "[data-region='calendar']", | |
a06b3a27 | 29 | CALENDAR_NAV_LINK: ".calendarwrapper .arrow_link", |
695c5726 | 30 | CALENDAR_MONTH_WRAPPER: ".calendarwrapper", |
b31a1695 | 31 | LOADING_ICON_CONTAINER: '[data-region="overlay-icon-container"]' |
695c5726 AN |
32 | }; |
33 | ||
34 | /** | |
35 | * Register event listeners for the module. | |
36 | * | |
37 | * @param {object} root The root element. | |
38 | */ | |
39 | var registerEventListeners = function(root) { | |
40 | root = $(root); | |
41 | ||
836aa3f6 | 42 | root.on('click', SELECTORS.CALENDAR_NAV_LINK, function(e) { |
695c5726 AN |
43 | var courseId = $(root).find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'); |
44 | var link = $(e.currentTarget); | |
6397ec54 | 45 | changeMonth(root, link.attr('href'), link.data('time'), courseId); |
695c5726 AN |
46 | |
47 | e.preventDefault(); | |
48 | }); | |
49 | }; | |
50 | ||
516e7444 SL |
51 | /** |
52 | * Refresh the month content. | |
53 | * | |
54 | * @param {Number} time The calendar time to be shown | |
55 | * @param {Number} courseid The id of the course whose events are shown | |
56 | * @return {promise} | |
57 | */ | |
6397ec54 | 58 | var refreshMonthContent = function(root, time, courseid) { |
b31a1695 SL |
59 | startLoading(root); |
60 | ||
516e7444 SL |
61 | return CalendarRepository.getCalendarMonthData(time, courseid) |
62 | .then(function(context) { | |
2281a835 | 63 | return Templates.render(root.attr('data-template'), context); |
516e7444 SL |
64 | }) |
65 | .then(function(html, js) { | |
6397ec54 | 66 | return Templates.replaceNode(root.find(SELECTORS.CALENDAR_MONTH_WRAPPER), html, js); |
516e7444 | 67 | }) |
b31a1695 SL |
68 | .always(function() { |
69 | return stopLoading(root); | |
70 | }) | |
516e7444 SL |
71 | .fail(Notification.exception); |
72 | }; | |
73 | ||
695c5726 AN |
74 | /** |
75 | * Handle changes to the current calendar view. | |
76 | * | |
a4af4c96 | 77 | * @param {String} url The calendar url to be shown |
695c5726 AN |
78 | * @param {Number} time The calendar time to be shown |
79 | * @param {Number} courseid The id of the course whose events are shown | |
516e7444 | 80 | * @return {promise} |
695c5726 | 81 | */ |
6397ec54 AN |
82 | var changeMonth = function(root, url, time, courseid) { |
83 | return refreshMonthContent(root, time, courseid) | |
516e7444 SL |
84 | .then(function() { |
85 | window.history.pushState({}, '', url); | |
47b55dad | 86 | return arguments; |
516e7444 SL |
87 | }) |
88 | .then(function() { | |
89 | $('body').trigger(CalendarEvents.monthChanged, [time, courseid]); | |
47b55dad | 90 | return arguments; |
516e7444 SL |
91 | }); |
92 | }; | |
93 | ||
94 | /** | |
95 | * Reload the current month view data. | |
96 | * | |
afa8c3da SL |
97 | * @param {object} root The container element. |
98 | * @param {Number} courseId The course id. | |
516e7444 SL |
99 | * @return {promise} |
100 | */ | |
afa8c3da SL |
101 | var reloadCurrentMonth = function(root, courseId) { |
102 | var time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time'); | |
516e7444 | 103 | |
afa8c3da SL |
104 | if (!courseId) { |
105 | courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'); | |
106 | } | |
6397ec54 | 107 | return refreshMonthContent(root, time, courseId); |
695c5726 AN |
108 | }; |
109 | ||
b31a1695 SL |
110 | /** |
111 | * Set the element state to loading. | |
112 | * | |
113 | * @param {object} root The container element | |
114 | * @method startLoading | |
115 | */ | |
116 | var startLoading = function(root) { | |
117 | var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER); | |
118 | ||
119 | loadingIconContainer.removeClass('hidden'); | |
120 | }; | |
121 | ||
122 | /** | |
123 | * Remove the loading state from the element. | |
124 | * | |
125 | * @param {object} root The container element | |
126 | * @method stopLoading | |
127 | */ | |
128 | var stopLoading = function(root) { | |
129 | var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER); | |
130 | ||
131 | loadingIconContainer.addClass('hidden'); | |
132 | }; | |
133 | ||
695c5726 | 134 | return { |
6397ec54 AN |
135 | init: function(root) { |
136 | registerEventListeners(root); | |
516e7444 SL |
137 | }, |
138 | reloadCurrentMonth: reloadCurrentMonth, | |
139 | changeMonth: changeMonth, | |
140 | refreshMonthContent: refreshMonthContent | |
695c5726 AN |
141 | }; |
142 | }); |