MDL-59392 calendar: Stop polluting history with useless URLs
[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 })
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 84 .then(function() {
41fa6a24
AN
85 if (url.length && url !== '#') {
86 window.history.pushState({}, '', url);
87 }
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 }
6397ec54 109 return refreshMonthContent(root, 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 136 return {
6397ec54
AN
137 init: function(root) {
138 registerEventListeners(root);
516e7444
SL
139 },
140 reloadCurrentMonth: reloadCurrentMonth,
141 changeMonth: changeMonth,
142 refreshMonthContent: refreshMonthContent
695c5726
AN
143 };
144 });