MDL-57447 block_myoverview: add context for courses view
[moodle.git] / blocks / myoverview / amd / src / paging_bar.js
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/>.
16 /**
17  * Javascript to load and render the list of calendar events for a
18  * given day range.
19  *
20  * @module     block_myoverview/event_list
21  * @package    block_myoverview
22  * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
25 define(['jquery', 'core/custom_interaction_events'],
26         function($, CustomEvents) {
28     var SELECTORS = {
29         PAGE_LINK: '[data-region="page-link"]',
30         ACTIVE_PAGE_LINK: '.active > [data-region="page-link"]'
31     };
33     var EVENTS = {
34         PAGE_SELECTED: 'block_myoverview-paging-bar-page-selected',
35     };
37     var registerEventListeners = function(root) {
38         root = $(root);
39         CustomEvents.define(root, [
40             CustomEvents.events.activate
41         ]);
43         root.one(CustomEvents.events.activate, SELECTORS.PAGE_LINK, function(e, data) {
44             var page = $(e.target).closest(SELECTORS.PAGE_LINK);
45             var activePage = root.find(SELECTORS.ACTIVE_PAGE_LINK);
46             var isSamePage = page.is(activePage);
48             if (!isSamePage) {
49                 root.find(SELECTORS.PAGE_LINK).removeClass('active');
50                 page.addClass('active');
51             }
53             root.trigger(EVENTS.PAGE_SELECTED, [{
54                 pageNumber: page.attr('data-page-number'),
55                 isSamePage: isSamePage,
56             }]);
58             data.originalEvent.preventDefault();
59         });
60     };
62     return {
63         registerEventListeners: registerEventListeners,
64         events: EVENTS,
65     };
66 });