1 // This file is part of Moodle - http://moodle.org/
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.
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/>.
17 * JavaScript to provide automatic scrolling, e.g. during a drag operation.
19 * Note: this module is defined statically. It is a singleton. You
20 * can only have one use of it active at any time. However, since this
21 * is usually used in relation to drag-drop, and since you only ever
22 * drag one thing at a time, this is not a problem in practice.
24 * @module core/autoscroll
27 * @copyright 2016 The Open University
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 define(['jquery'], function($) {
33 * @alias module:core/autoscroll
37 * Size of area near edge of screen that triggers scrolling.
43 * How frequently to scroll window.
46 SCROLL_FREQUENCY: 1000 / 60,
49 * How many pixels to scroll per unit (1 = max scroll 30).
55 * Set if currently scrolling up/down.
61 * Speed we are supposed to scroll (range 1 to SCROLL_THRESHOLD).
67 * Optional callback called when it scrolls
73 * Starts automatically scrolling if user moves near edge of window.
74 * This should be called in response to mouse down or touch start.
77 * @param {Function} callback Optional callback that is called every time it scrolls
79 start: function(callback) {
80 $(window).on('mousemove', autoscroll.mouseMove);
81 $(window).on('touchmove', autoscroll.touchMove);
82 autoscroll.callback = callback;
86 * Stops automatically scrolling. This should be called in response to mouse up or touch end.
91 $(window).off('mousemove', autoscroll.mouseMove);
92 $(window).off('touchmove', autoscroll.touchMove);
93 if (autoscroll.scrollingId !== null) {
94 autoscroll.stopScrolling();
99 * Event handler for touch move.
102 * @param {Object} e Event
104 touchMove: function(e) {
105 for (var i = 0; i < e.changedTouches.length; i++) {
106 autoscroll.handleMove(e.changedTouches[i].clientX, e.changedTouches[i].clientY);
111 * Event handler for mouse move.
114 * @param {Object} e Event
116 mouseMove: function(e) {
117 autoscroll.handleMove(e.clientX, e.clientY);
121 * Handles user moving.
124 * @param {number} clientX X
125 * @param {number} clientY Y
127 handleMove: function(clientX, clientY) {
128 // If near the bottom or top, start auto-scrolling.
129 if (clientY < autoscroll.SCROLL_THRESHOLD) {
130 autoscroll.scrollAmount = -Math.min(autoscroll.SCROLL_THRESHOLD - clientY, autoscroll.SCROLL_THRESHOLD);
131 } else if (clientY > $(window).height() - autoscroll.SCROLL_THRESHOLD) {
132 autoscroll.scrollAmount = Math.min(clientY - ($(window).height() - autoscroll.SCROLL_THRESHOLD),
133 autoscroll.SCROLL_THRESHOLD);
135 autoscroll.scrollAmount = 0;
137 if (autoscroll.scrollAmount && autoscroll.scrollingId === null) {
138 autoscroll.startScrolling();
139 } else if (!autoscroll.scrollAmount && autoscroll.scrollingId !== null) {
140 autoscroll.stopScrolling();
145 * Starts automatic scrolling.
149 startScrolling: function() {
150 var maxScroll = $(document).height() - $(window).height();
151 autoscroll.scrollingId = window.setInterval(function() {
152 // Work out how much to scroll.
153 var y = $(window).scrollTop();
154 var offset = Math.round(autoscroll.scrollAmount * autoscroll.SCROLL_SPEED);
155 if (y + offset < 0) {
158 if (y + offset > maxScroll) {
159 offset = maxScroll - y;
166 $(window).scrollTop(y + offset);
167 var realOffset = $(window).scrollTop() - y;
168 if (realOffset === 0) {
173 if (autoscroll.callback) {
174 autoscroll.callback(realOffset);
177 }, autoscroll.SCROLL_FREQUENCY);
181 * Stops the automatic scrolling.
185 stopScrolling: function() {
186 window.clearInterval(autoscroll.scrollingId);
187 autoscroll.scrollingId = null;
193 * Starts automatic scrolling if user moves near edge of window.
194 * This should be called in response to mouse down or touch start.
197 * @param {Function} callback Optional callback that is called every time it scrolls
199 start: autoscroll.start,
202 * Stops automatic scrolling. This should be called in response to mouse up or touch end.
206 stop: autoscroll.stop