MDL-63044 block_timeline: add timeline block
[moodle.git] / blocks / timeline / amd / src / view_dates.js
blobb18d21a3fd3b4df999d5c26307cfdab06c4161b9
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.
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/>.
16 /**
17  * Manage the timeline dates view for the timeline block.
18  *
19  * @package    block_timeline
20  * @copyright  2018 Ryan Wyllie <ryan@moodle.com>
21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 define(
26     'jquery',
27     'core/str',
28     'block_timeline/event_list'
30 function(
31     $,
32     Str,
33     EventList
34 ) {
36     var SELECTORS = {
37         EVENT_LIST_CONTAINER: '[data-region="event-list-container"]',
38     };
40     /**
41      * Initialise the event list and being loading the events.
42      *
43      * @param {object} root The root element for the timeline dates view.
44      */
45     var load = function(root) {
46         var eventListContainer = root.find(SELECTORS.EVENT_LIST_CONTAINER);
47         Str.get_string('ariaeventlistpaginationnavdates', 'block_timeline')
48             .then(function(string) {
49                 EventList.init(eventListContainer, [5, 10, 25], {}, string);
50                 return string;
51             })
52             .catch(function() {
53                 // Ignore if we can't load the string. Still init the event list.
54                 EventList.init(eventListContainer, [5, 10, 25]);
55             });
56     };
58     /**
59      * Initialise the timeline dates view. Begin loading the events
60      * if this view is active.
61      *
62      * @param {object} root The root element for the timeline courses view.
63      */
64     var init = function(root) {
65         root = $(root);
66         if (root.hasClass('active')) {
67             load(root);
68             root.attr('data-seen', true);
69         }
70     };
72     /**
73      * Reset the view back to it's initial state. If this view is active then
74      * beging loading the events.
75      *
76      * @param {object} root The root element for the timeline courses view.
77      */
78     var reset = function(root) {
79         root.removeAttr('data-seen');
80         if (root.hasClass('active')) {
81             load(root);
82             root.attr('data-seen', true);
83         }
84     };
86     /**
87      * Load the events if this is the first time the view is displayed.
88      *
89      * @param {object} root The root element for the timeline courses view.
90      */
91     var shown = function(root) {
92         if (!root.attr('data-seen')) {
93             load(root);
94             root.attr('data-seen', true);
95         }
96     };
98     return {
99         init: init,
100         reset: reset,
101         shown: shown
102     };