weekly release 3.4dev
[moodle.git] / calendar / amd / src / view_manager.js
CommitLineData
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
24define(['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);
45 changeMonth(link.attr('href'), link.data('time'), courseId);
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 */
58 var refreshMonthContent = function(time, courseid) {
b31a1695
SL
59 var root = $(SELECTORS.ROOT);
60
61 startLoading(root);
62
516e7444
SL
63 return CalendarRepository.getCalendarMonthData(time, courseid)
64 .then(function(context) {
65 return Templates.render('core_calendar/month_detailed', context);
66 })
67 .then(function(html, js) {
2c65e0ce 68 return Templates.replaceNode(SELECTORS.CALENDAR_MONTH_WRAPPER, html, js);
516e7444 69 })
b31a1695
SL
70 .always(function() {
71 return stopLoading(root);
72 })
516e7444
SL
73 .fail(Notification.exception);
74 };
75
695c5726
AN
76 /**
77 * Handle changes to the current calendar view.
78 *
a4af4c96 79 * @param {String} url The calendar url to be shown
695c5726
AN
80 * @param {Number} time The calendar time to be shown
81 * @param {Number} courseid The id of the course whose events are shown
516e7444 82 * @return {promise}
695c5726
AN
83 */
84 var changeMonth = function(url, time, courseid) {
516e7444
SL
85 return refreshMonthContent(time, courseid)
86 .then(function() {
87 window.history.pushState({}, '', url);
47b55dad 88 return arguments;
516e7444
SL
89 })
90 .then(function() {
91 $('body').trigger(CalendarEvents.monthChanged, [time, courseid]);
47b55dad 92 return arguments;
516e7444
SL
93 });
94 };
95
96 /**
97 * Reload the current month view data.
98 *
afa8c3da
SL
99 * @param {object} root The container element.
100 * @param {Number} courseId The course id.
516e7444
SL
101 * @return {promise}
102 */
afa8c3da
SL
103 var reloadCurrentMonth = function(root, courseId) {
104 var time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time');
516e7444 105
afa8c3da
SL
106 if (!courseId) {
107 courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
108 }
109 return refreshMonthContent(time, courseId);
695c5726
AN
110 };
111
b31a1695
SL
112 /**
113 * Set the element state to loading.
114 *
115 * @param {object} root The container element
116 * @method startLoading
117 */
118 var startLoading = function(root) {
119 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
120
121 loadingIconContainer.removeClass('hidden');
122 };
123
124 /**
125 * Remove the loading state from the element.
126 *
127 * @param {object} root The container element
128 * @method stopLoading
129 */
130 var stopLoading = function(root) {
131 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
132
133 loadingIconContainer.addClass('hidden');
134 };
135
695c5726
AN
136 return {
137 init: function() {
138 registerEventListeners(SELECTORS.ROOT);
516e7444
SL
139 },
140 reloadCurrentMonth: reloadCurrentMonth,
141 changeMonth: changeMonth,
142 refreshMonthContent: refreshMonthContent
695c5726
AN
143 };
144 });