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);
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 })
c8b6e9ab
AN
68 .then(function() {
69 $('body').trigger(CalendarEvents.viewUpdated);
70 return;
71 })
b31a1695
SL
72 .always(function() {
73 return stopLoading(root);
74 })
516e7444
SL
75 .fail(Notification.exception);
76 };
77
695c5726
AN
78 /**
79 * Handle changes to the current calendar view.
80 *
a4af4c96 81 * @param {String} url The calendar url to be shown
695c5726
AN
82 * @param {Number} time The calendar time to be shown
83 * @param {Number} courseid The id of the course whose events are shown
516e7444 84 * @return {promise}
695c5726 85 */
6397ec54
AN
86 var changeMonth = function(root, url, time, courseid) {
87 return refreshMonthContent(root, time, courseid)
516e7444 88 .then(function() {
41fa6a24
AN
89 if (url.length && url !== '#') {
90 window.history.pushState({}, '', url);
91 }
47b55dad 92 return arguments;
516e7444
SL
93 })
94 .then(function() {
95 $('body').trigger(CalendarEvents.monthChanged, [time, courseid]);
47b55dad 96 return arguments;
516e7444
SL
97 });
98 };
99
100 /**
101 * Reload the current month view data.
102 *
afa8c3da
SL
103 * @param {object} root The container element.
104 * @param {Number} courseId The course id.
516e7444
SL
105 * @return {promise}
106 */
afa8c3da
SL
107 var reloadCurrentMonth = function(root, courseId) {
108 var time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time');
516e7444 109
afa8c3da
SL
110 if (!courseId) {
111 courseId = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid');
112 }
6397ec54 113 return refreshMonthContent(root, time, courseId);
695c5726
AN
114 };
115
b31a1695
SL
116 /**
117 * Set the element state to loading.
118 *
119 * @param {object} root The container element
120 * @method startLoading
121 */
122 var startLoading = function(root) {
123 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
124
125 loadingIconContainer.removeClass('hidden');
126 };
127
128 /**
129 * Remove the loading state from the element.
130 *
131 * @param {object} root The container element
132 * @method stopLoading
133 */
134 var stopLoading = function(root) {
135 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
136
137 loadingIconContainer.addClass('hidden');
138 };
139
695c5726 140 return {
6397ec54
AN
141 init: function(root) {
142 registerEventListeners(root);
516e7444
SL
143 },
144 reloadCurrentMonth: reloadCurrentMonth,
145 changeMonth: changeMonth,
146 refreshMonthContent: refreshMonthContent
695c5726
AN
147 };
148 });