Proyectos de Subversion Moodle

Rev

Autoría | Ultima modificación | Ver Log |

{"version":3,"file":"autoscroll.min.js","sources":["../src/autoscroll.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.\n\n/*\n * JavaScript to provide automatic scrolling, e.g. during a drag operation.\n *\n * Note: this module is defined statically. It is a singleton. You\n * can only have one use of it active at any time. However, since this\n * is usually used in relation to drag-drop, and since you only ever\n * drag one thing at a time, this is not a problem in practice.\n *\n * @module     core/autoscroll\n * @copyright  2016 The Open University\n * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since      3.6\n */\ndefine(['jquery'], function($) {\n    /**\n     * @alias module:core/autoscroll\n     */\n    var autoscroll = {\n        /**\n         * Size of area near edge of screen that triggers scrolling.\n         * @private\n         */\n        SCROLL_THRESHOLD: 30,\n\n        /**\n         * How frequently to scroll window.\n         * @private\n         */\n        SCROLL_FREQUENCY: 1000 / 60,\n\n        /**\n         * How many pixels to scroll per unit (1 = max scroll 30).\n         * @private\n         */\n        SCROLL_SPEED: 0.5,\n\n        /**\n         * Set if currently scrolling up/down.\n         * @private\n         */\n        scrollingId: null,\n\n        /**\n         * Speed we are supposed to scroll (range 1 to SCROLL_THRESHOLD).\n         * @private\n         */\n        scrollAmount: 0,\n\n        /**\n         * Optional callback called when it scrolls\n         * @private\n         */\n        callback: null,\n\n        /**\n         * Starts automatically scrolling if user moves near edge of window.\n         * This should be called in response to mouse down or touch start.\n         *\n         * @public\n         * @param {Function} callback Optional callback that is called every time it scrolls\n         */\n        start: function(callback) {\n            $(window).on('mousemove', autoscroll.mouseMove);\n            $(window).on('touchmove', autoscroll.touchMove);\n            autoscroll.callback = callback;\n        },\n\n        /**\n         * Stops automatically scrolling. This should be called in response to mouse up or touch end.\n         *\n         * @public\n         */\n        stop: function() {\n            $(window).off('mousemove', autoscroll.mouseMove);\n            $(window).off('touchmove', autoscroll.touchMove);\n            if (autoscroll.scrollingId !== null) {\n                autoscroll.stopScrolling();\n            }\n        },\n\n        /**\n         * Event handler for touch move.\n         *\n         * @private\n         * @param {Object} e Event\n         */\n        touchMove: function(e) {\n            for (var i = 0; i < e.changedTouches.length; i++) {\n                autoscroll.handleMove(e.changedTouches[i].clientX, e.changedTouches[i].clientY);\n            }\n        },\n\n        /**\n         * Event handler for mouse move.\n         *\n         * @private\n         * @param {Object} e Event\n         */\n        mouseMove: function(e) {\n            autoscroll.handleMove(e.clientX, e.clientY);\n        },\n\n        /**\n         * Handles user moving.\n         *\n         * @private\n         * @param {number} clientX X\n         * @param {number} clientY Y\n         */\n        handleMove: function(clientX, clientY) {\n            // If near the bottom or top, start auto-scrolling.\n            if (clientY < autoscroll.SCROLL_THRESHOLD) {\n                autoscroll.scrollAmount = -Math.min(autoscroll.SCROLL_THRESHOLD - clientY, autoscroll.SCROLL_THRESHOLD);\n            } else if (clientY > $(window).height() - autoscroll.SCROLL_THRESHOLD) {\n                autoscroll.scrollAmount = Math.min(clientY - ($(window).height() - autoscroll.SCROLL_THRESHOLD),\n                    autoscroll.SCROLL_THRESHOLD);\n            } else {\n                autoscroll.scrollAmount = 0;\n            }\n            if (autoscroll.scrollAmount && autoscroll.scrollingId === null) {\n                autoscroll.startScrolling();\n            } else if (!autoscroll.scrollAmount && autoscroll.scrollingId !== null) {\n                autoscroll.stopScrolling();\n            }\n        },\n\n        /**\n         * Starts automatic scrolling.\n         *\n         * @private\n         */\n        startScrolling: function() {\n            var maxScroll = $(document).height() - $(window).height();\n            autoscroll.scrollingId = window.setInterval(function() {\n                // Work out how much to scroll.\n                var y = $(window).scrollTop();\n                var offset = Math.round(autoscroll.scrollAmount * autoscroll.SCROLL_SPEED);\n                if (y + offset < 0) {\n                    offset = -y;\n                }\n                if (y + offset > maxScroll) {\n                    offset = maxScroll - y;\n                }\n                if (offset === 0) {\n                    return;\n                }\n\n                // Scroll.\n                $(window).scrollTop(y + offset);\n                var realOffset = $(window).scrollTop() - y;\n                if (realOffset === 0) {\n                    return;\n                }\n\n                // Inform callback\n                if (autoscroll.callback) {\n                    autoscroll.callback(realOffset);\n                }\n\n            }, autoscroll.SCROLL_FREQUENCY);\n        },\n\n        /**\n         * Stops the automatic scrolling.\n         *\n         * @private\n         */\n        stopScrolling: function() {\n            window.clearInterval(autoscroll.scrollingId);\n            autoscroll.scrollingId = null;\n        }\n    };\n\n    return {\n        /**\n         * Starts automatic scrolling if user moves near edge of window.\n         * This should be called in response to mouse down or touch start.\n         *\n         * @public\n         * @param {Function} callback Optional callback that is called every time it scrolls\n         */\n        start: autoscroll.start,\n\n        /**\n         * Stops automatic scrolling. This should be called in response to mouse up or touch end.\n         *\n         * @public\n         */\n        stop: autoscroll.stop\n    };\n\n});\n"],"names":["define","$","autoscroll","SCROLL_THRESHOLD","SCROLL_FREQUENCY","SCROLL_SPEED","scrollingId","scrollAmount","callback","start","window","on","mouseMove","touchMove","stop","off","stopScrolling","e","i","changedTouches","length","handleMove","clientX","clientY","Math","min","height","startScrolling","maxScroll","document","setInterval","y","scrollTop","offset","round","realOffset","clearInterval"],"mappings":";;;;;;;;;;;;;AA4BAA,yBAAO,CAAC,WAAW,SAASC,OAIpBC,WAAa,CAKbC,iBAAkB,GAMlBC,iBAAkB,IAAO,GAMzBC,aAAc,GAMdC,YAAa,KAMbC,aAAc,EAMdC,SAAU,KASVC,MAAO,SAASD,UACZP,EAAES,QAAQC,GAAG,YAAaT,WAAWU,WACrCX,EAAES,QAAQC,GAAG,YAAaT,WAAWW,WACrCX,WAAWM,SAAWA,UAQ1BM,KAAM,WACFb,EAAES,QAAQ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