Commit | Line | Data |
---|---|---|
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 | */ | |
24 | define(['jquery', 'core/templates', 'core/notification', 'core_calendar/repository'], | |
25 | function($, Templates, Notification, CalendarRepository) { | |
26 | ||
27 | var SELECTORS = { | |
28 | ROOT: "[data-region='calendar']", | |
29 | CALENDAR_NAV_LINK: "span.calendarwrapper .arrow_link", | |
30 | CALENDAR_MONTH_WRAPPER: ".calendarwrapper", | |
31 | }; | |
32 | ||
33 | /** | |
34 | * Register event listeners for the module. | |
35 | * | |
36 | * @param {object} root The root element. | |
37 | */ | |
38 | var registerEventListeners = function(root) { | |
39 | root = $(root); | |
40 | ||
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); | |
45 | ||
46 | e.preventDefault(); | |
47 | }); | |
48 | }; | |
49 | ||
50 | ||
51 | /** | |
52 | * Handle changes to the current calendar view. | |
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 | */ | |
57 | var changeMonth = function(url, time, courseid) { | |
58 | CalendarRepository.getCalendarMonthData(time, courseid) | |
59 | .then(function(context) { | |
60 | // TODO Fetch the page title from somewhere..? | |
61 | window.history.pushState({}, 'Some new title', url); | |
62 | ||
63 | return Templates.render('core_calendar/month_detailed', context); | |
64 | }) | |
65 | .then(function(html, js) { | |
66 | $(SELECTORS.CALENDAR_MONTH_WRAPPER).replaceWith(html); | |
67 | ||
68 | return Templates.runTemplateJS(js); | |
69 | }) | |
70 | .then(function() { | |
71 | // TODO Fire an event to say the month changed. | |
72 | }) | |
73 | .fail(Notification.exception); | |
74 | }; | |
75 | ||
76 | return { | |
77 | init: function() { | |
78 | registerEventListeners(SELECTORS.ROOT); | |
79 | } | |
80 | }; | |
81 | }); |