Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * This module is the highest level module for the calendar. It is
18
 * responsible for initialising all of the components required for
19
 * the calendar to run. It also coordinates the interaction between
20
 * components by listening for and responding to different events
21
 * triggered within the calendar UI.
22
 *
23
 * @module     core_calendar/calendar_mini
24
 * @copyright  2017 Andrew Nicols <andrew@nicols.co.uk>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
define([
28
    'jquery',
29
    'core_calendar/selectors',
30
    'core_calendar/events',
31
    'core_calendar/view_manager',
32
],
33
function(
34
    $,
35
    CalendarSelectors,
36
    CalendarEvents,
37
    CalendarViewManager
38
) {
39
 
40
    /**
41
     * Listen to and handle any calendar events fired by the calendar UI.
42
     *
43
     * @method registerCalendarEventListeners
44
     * @param {object} root The calendar root element
45
     */
46
    var registerCalendarEventListeners = function(root) {
47
        var body = $('body');
48
        var namespace = '.' + root.attr('id');
49
 
50
        body.on(CalendarEvents.created + namespace, root, reloadMonth);
51
        body.on(CalendarEvents.deleted + namespace, root, reloadMonth);
52
        body.on(CalendarEvents.updated + namespace, root, reloadMonth);
53
        body.on(CalendarEvents.eventMoved + namespace, root, reloadMonth);
54
    };
55
 
56
    /**
57
     * Reload the month view in this month.
58
     *
59
     * @param {EventFacade} e
60
     */
61
    var reloadMonth = function(e) {
62
        var root = e.data;
63
        var body = $('body');
64
        var namespace = '.' + root.attr('id');
65
 
66
        if (root.is(':visible')) {
67
            CalendarViewManager.reloadCurrentMonth(root);
68
        } else {
69
            // The root has been removed.
70
            // Remove all events in the namespace.
71
            body.off(CalendarEvents.created + namespace);
72
            body.off(CalendarEvents.deleted + namespace);
73
            body.off(CalendarEvents.updated + namespace);
74
            body.off(CalendarEvents.eventMoved + namespace);
75
        }
76
    };
77
 
78
    var registerEventListeners = function(root) {
79
        $('body').on(CalendarEvents.filterChanged, function(e, data) {
80
            var daysWithEvent = root.find(CalendarSelectors.eventType[data.type]);
81
 
82
            daysWithEvent.toggleClass('calendar_event_' + data.type, !data.hidden);
83
        });
84
 
85
        var namespace = '.' + root.attr('id');
86
        $('body').on('change' + namespace, CalendarSelectors.elements.courseSelector, function() {
87
            if (root.is(':visible')) {
88
                var selectElement = $(this);
89
                var courseId = selectElement.val();
90
                var categoryId = null;
91
 
92
                CalendarViewManager.reloadCurrentMonth(root, courseId, categoryId);
93
            } else {
94
                $('body').off('change' + namespace);
95
            }
96
        });
97
 
98
    };
99
 
100
    return {
101
        init: function(root, loadOnInit) {
102
            root = $(root);
103
 
104
            CalendarViewManager.init(root);
105
            registerEventListeners(root);
106
            registerCalendarEventListeners(root);
107
 
108
            if (loadOnInit) {
109
                // The calendar hasn't yet loaded it's events so we
110
                // should load them as soon as we've initialised.
111
                CalendarViewManager.reloadCurrentMonth(root);
112
            }
113
 
114
        }
115
    };
116
});