MDL-59753 core_calendar: change course selector to normal select
[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 *
99 * @return {promise}
100 */
b31a1695
SL
101 var reloadCurrentMonth = function(root) {
102 var courseid = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('courseid'),
516e7444
SL
103 time = root.find(SELECTORS.CALENDAR_MONTH_WRAPPER).data('current-time');
104
105 return refreshMonthContent(time, courseid);
695c5726
AN
106 };
107
b31a1695
SL
108 /**
109 * Set the element state to loading.
110 *
111 * @param {object} root The container element
112 * @method startLoading
113 */
114 var startLoading = function(root) {
115 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
116
117 loadingIconContainer.removeClass('hidden');
118 };
119
120 /**
121 * Remove the loading state from the element.
122 *
123 * @param {object} root The container element
124 * @method stopLoading
125 */
126 var stopLoading = function(root) {
127 var loadingIconContainer = root.find(SELECTORS.LOADING_ICON_CONTAINER);
128
129 loadingIconContainer.addClass('hidden');
130 };
131
695c5726
AN
132 return {
133 init: function() {
134 registerEventListeners(SELECTORS.ROOT);
516e7444
SL
135 },
136 reloadCurrentMonth: reloadCurrentMonth,
137 changeMonth: changeMonth,
138 refreshMonthContent: refreshMonthContent
695c5726
AN
139 };
140 });