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
 * Manage the timeline dates view for the timeline block.
18
 *
19
 * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 
23
define(
24
[
25
    'jquery',
26
    'block_timeline/event_list',
27
    'core/pubsub',
28
    'core/paged_content_events'
29
],
30
function(
31
    $,
32
    EventList,
33
    PubSub,
34
    PagedContentEvents
35
) {
36
 
37
    var SELECTORS = {
38
        EVENT_LIST_CONTAINER: '[data-region="event-list-container"]',
39
        NO_COURSES_EMPTY_MESSAGE: '[data-region="no-courses-empty-message"]',
40
    };
41
 
42
    /**
43
     * Setup the listeners for the timeline block
44
     *
45
     * @param {string} root view dates container
46
     * @param {string} namespace The namespace for the paged content
47
     */
48
    var registerEventListeners = function(root, namespace) {
49
        var event = namespace + PagedContentEvents.SET_ITEMS_PER_PAGE_LIMIT;
50
        PubSub.subscribe(event, function(limit) {
51
            $(root).data('limit', limit);
52
        });
53
    };
54
 
55
    /**
56
     * Initialise the event list and being loading the events.
57
     *
58
     * @param {object} root The root element for the timeline dates view.
59
     */
60
    var load = function(root) {
61
 
62
        if (!root.find(SELECTORS.NO_COURSES_EMPTY_MESSAGE).length) {
63
            var eventListContainer = root.find(SELECTORS.EVENT_LIST_CONTAINER);
64
            var namespace = $(eventListContainer).attr('id') + "user_block_timeline" + Math.random();
65
            registerEventListeners(root, namespace);
66
 
67
            var config = {
68
                persistentLimitKey: "block_timeline_user_limit_preference",
69
                eventNamespace: namespace
70
            };
71
            EventList.init(eventListContainer, config);
72
        }
73
    };
74
 
75
    /**
76
     * Initialise the timeline dates view. Begin loading the events
77
     * if this view is active.
78
     *
79
     * @param {object} root The root element for the timeline courses view.
80
     */
81
    var init = function(root) {
82
        root = $(root);
83
 
84
        // Only need to handle events loading if the user is actively enrolled in a course and this view is active.
85
        if (root.hasClass('active') && !root.find(SELECTORS.NO_COURSES_EMPTY_MESSAGE).length) {
86
            load(root);
87
            root.attr('data-seen', true);
88
        }
89
    };
90
 
91
    /**
92
     * Reset the view back to it's initial state. If this view is active then
93
     * beging loading the events.
94
     *
95
     * @param {object} root The root element for the timeline courses view.
96
     */
97
    var reset = function(root) {
98
        root.removeAttr('data-seen');
99
        if (root.hasClass('active')) {
100
            load(root);
101
            root.attr('data-seen', true);
102
        }
103
    };
104
 
105
    /**
106
     * Load the events if this is the first time the view is displayed.
107
     *
108
     * @param {object} root The root element for the timeline courses view.
109
     */
110
    var shown = function(root) {
111
        if (!root.attr('data-seen')) {
112
            load(root);
113
            root.attr('data-seen', true);
114
        }
115
    };
116
 
117
    return {
118
        init: init,
119
        reset: reset,
120
        shown: shown
121
    };
122
});