MDL-55074 blocks: Remove default blocks from course and site
[moodle.git] / theme / boost / amd / src / drawer.js
CommitLineData
99061152
DW
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 * Contain the logic for a drawer.
18 *
19 * @package theme_boost
20 * @copyright 2016 Damyon Wiese
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
4a3d8169
DW
23define(['jquery', 'core/custom_interaction_events', 'core/log'],
24 function($, CustomEvents, Log) {
99061152
DW
25
26 var SELECTORS = {
27 TOGGLE_REGION: '[data-region="drawer-toggle"]',
28 TOGGLE_ACTION: '[data-action="toggle-drawer"]',
29 TOGGLE_TARGET: 'aria-controls',
30 TOGGLE_SIDE: 'left',
31 BODY: 'body'
32 };
33
34 /**
35 * Constructor for the Drawer.
36 *
37 * @param {object} root The root jQuery element for the modal
38 */
39 var Drawer = function() {
40
41 if (!$(SELECTORS.TOGGLE_REGION).length) {
4a3d8169 42 Log.debug('Page is missing a drawer region');
99061152
DW
43 }
44 if (!$(SELECTORS.TOGGLE_ACTION).length) {
4a3d8169 45 Log.debug('Page is missing a drawer toggle link');
99061152
DW
46 }
47 $(SELECTORS.TOGGLE_REGION).each(function(index, ele) {
48 var trigger = $(ele).find(SELECTORS.TOGGLE_ACTION);
4a3d8169
DW
49 var drawerid = trigger.attr('aria-controls');
50 var drawer = $(document.getElementById(drawerid));
99061152
DW
51 var hidden = trigger.attr('aria-expanded') == 'false';
52 var side = trigger.attr('data-side');
53 var body = $(SELECTORS.BODY);
54
4a3d8169
DW
55 drawer.on('mousewheel DOMMouseScroll', this.preventPageScroll);
56
99061152
DW
57 if (!hidden) {
58 body.addClass('drawer-open-' + side);
59 trigger.attr('aria-expanded', 'true');
60 } else {
61 trigger.attr('aria-expanded', 'false');
62 }
63 }.bind(this));
64
65 this.registerEventListeners();
66 };
67
68 Drawer.prototype.closeAll = function() {
69 $(SELECTORS.TOGGLE_REGION).each(function(index, ele) {
70 var trigger = $(ele).find(SELECTORS.TOGGLE_ACTION);
71 var side = trigger.attr('data-side');
72 var body = $(SELECTORS.BODY);
73 var drawerid = trigger.attr('aria-controls');
74 var drawer = $(document.getElementById(drawerid));
75 var preference = trigger.attr('data-preference');
76
77 trigger.attr('aria-expanded', 'false');
78 body.removeClass('drawer-open-' + side);
79 drawer.attr('aria-hidden', 'true');
80 drawer.addClass('closed');
81 M.util.set_user_preference(preference, 'false');
82 }.bind(this));
83 };
84
85 /**
86 * Open / close the blocks drawer.
87 *
88 * @method toggleDrawer
89 * @param {Event} e
90 */
91 Drawer.prototype.toggleDrawer = function(e) {
92 var trigger = $(e.target).closest('[data-action=toggle-drawer]');
93 var drawerid = trigger.attr('aria-controls');
94 var drawer = $(document.getElementById(drawerid));
95 var body = $(SELECTORS.BODY);
96 var side = trigger.attr('data-side');
97 var preference = trigger.attr('data-preference');
98
99 body.addClass('drawer-ease');
100 var open = trigger.attr('aria-expanded') == 'true';
101 if (!open) {
102 var small = $(document).width() < 512;
103 if (small) {
104 this.closeAll();
105 }
106 // Open.
107 trigger.attr('aria-expanded', 'true');
108 drawer.attr('aria-hidden', 'false');
109 body.addClass('drawer-open-' + side);
110 drawer.removeClass('closed');
111 M.util.set_user_preference(preference, 'true');
112 } else {
113 // Close.
114 body.removeClass('drawer-open-' + side);
115 trigger.attr('aria-expanded', 'false');
116 drawer.attr('aria-hidden', 'true');
117 drawer.addClass('closed');
118 M.util.set_user_preference(preference, 'false');
119 }
120 };
121
4a3d8169
DW
122 /**
123 * Prevent the page from scrolling when the drawer is at max scroll.
124 *
125 * @method preventPageScroll
126 */
127 Drawer.prototype.preventPageScroll = function(e) {
128 var delta = e.wheelDelta || (e.originalEvent && e.originalEvent.wheelDelta) || -e.originalEvent.detail,
129 bottomOverflow = (this.scrollTop + $(this).outerHeight() - this.scrollHeight) >= 0,
130 topOverflow = this.scrollTop <= 0;
131
132 if ((delta < 0 && bottomOverflow) || (delta > 0 && topOverflow)) {
133 e.preventDefault();
134 }
135 };
136
99061152
DW
137 /**
138 * Set up all of the event handling for the modal.
139 *
140 * @method registerEventListeners
141 */
142 Drawer.prototype.registerEventListeners = function() {
143 var body = $(SELECTORS.BODY);
144
145 CustomEvents.define(body, [CustomEvents.events.activate]);
146
147 body.on(CustomEvents.events.activate, SELECTORS.TOGGLE_ACTION, function(e, data) {
148 this.toggleDrawer(data.originalEvent);
149 data.originalEvent.preventDefault();
150 }.bind(this));
151 };
152
153 return {
154 'init': function() {
155 return new Drawer();
156 }
157 };
158});