1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * A javascript module to handler calendar view changes.
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
24 define(['jquery', 'core/templates', 'core/notification', 'core_calendar/repository', 'core_calendar/events'],
25 function($, Templates, Notification, CalendarRepository, CalendarEvents) {
28 ROOT: "[data-region='calendar']",
29 CALENDAR_NAV_LINK: "span.calendarwrapper .arrow_link",
30 CALENDAR_MONTH_WRAPPER: ".calendarwrapper",
34 * Register event listeners for the module.
36 * @param {object} root The root element.
38 var registerEventListeners = function(root) {
41 root.on('click', SELECTORS.CALENDAR_NAV_LINK, function(e) {
42 var courseId = $(root).find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
43 var link = $(e.currentTarget);
44 changeMonth(link.attr('href'), link.data('time'), courseId);
51 * Refresh the month content.
53 * @param {Number} time The calendar time to be shown
54 * @param {Number} courseid The id of the course whose events are shown
57 var refreshMonthContent = function(time, courseid) {
58 return CalendarRepository.getCalendarMonthData(time, courseid)
59 .then(function(context) {
60 return Templates.render('core_calendar/month_detailed', context);
62 .then(function(html, js) {
63 return Templates.replaceNodeContents(SELECTORS.CALENDAR_MONTH_WRAPPER, html, js);
65 .fail(Notification.exception);
69 * Handle changes to the current calendar view.
71 * @param {String} url The calendar url to be shown
72 * @param {Number} time The calendar time to be shown
73 * @param {Number} courseid The id of the course whose events are shown
76 var changeMonth = function(url, time, courseid) {
77 return refreshMonthContent(time, courseid)
79 window.history.pushState({}, '', url);
83 $('body').trigger(CalendarEvents.monthChanged, [time, courseid]);
89 * Reload the current month view data.
93 var reloadCurrentMonth = function() {
94 var root = $(SELECTORS.ROOT),
95 courseid = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'),
96 time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time');
98 return refreshMonthContent(time, courseid);
103 registerEventListeners(SELECTORS.ROOT);
105 reloadCurrentMonth: reloadCurrentMonth,
106 changeMonth: changeMonth,
107 refreshMonthContent: refreshMonthContent