MDL-79871 qtype_ordering: Remove shipped core libs

Part of: MDL-79863
This commit is contained in:
Mathew May 2023-11-02 09:32:53 +08:00
parent bc2bfead5c
commit 416ffeed36
12 changed files with 4 additions and 666 deletions

View File

@ -1,21 +0,0 @@
/*
* JavaScript to provide automatic scrolling, e.g. during a drag operation.
*
* This is a copy of a library that was added to Moodle core in Moodle 3.6,
* so we can support older Moodle versions.
*
* Note: this module is defined statically. It is a singleton. You
* can only have one use of it active at any time. However, since this
* is usually used in relation to drag-drop, and since you only ever
* drag one thing at a time, this is not a problem in practice.
*
* @module qtype_ordering/autoscroll
* @class autoscroll
* @package qtype_ordering
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.6
*/
define("qtype_ordering/autoscroll",["jquery"],(function($){var autoscroll={SCROLL_THRESHOLD:30,SCROLL_FREQUENCY:1e3/60,SCROLL_SPEED:.5,scrollingId:null,scrollAmount:0,callback:null,start:function(callback){$(window).on("mousemove",autoscroll.mouseMove),$(window).on("touchmove",autoscroll.touchMove),autoscroll.callback=callback},stop:function(){$(window).off("mousemove",autoscroll.mouseMove),$(window).off("touchmove",autoscroll.touchMove),null!==autoscroll.scrollingId&&autoscroll.stopScrolling()},touchMove:function(e){for(var i=0;i<e.changedTouches.length;i++)autoscroll.handleMove(e.changedTouches[i].clientX,e.changedTouches[i].clientY)},mouseMove:function(e){autoscroll.handleMove(e.clientX,e.clientY)},handleMove:function(clientX,clientY){clientY<autoscroll.SCROLL_THRESHOLD?autoscroll.scrollAmount=-Math.min(autoscroll.SCROLL_THRESHOLD-clientY,autoscroll.SCROLL_THRESHOLD):clientY>$(window).height()-autoscroll.SCROLL_THRESHOLD?autoscroll.scrollAmount=Math.min(clientY-($(window).height()-autoscroll.SCROLL_THRESHOLD),autoscroll.SCROLL_THRESHOLD):autoscroll.scrollAmount=0,autoscroll.scrollAmount&&null===autoscroll.scrollingId?autoscroll.startScrolling():autoscroll.scrollAmount||null===autoscroll.scrollingId||autoscroll.stopScrolling()},startScrolling:function(){var maxScroll=$(document).height()-$(window).height();autoscroll.scrollingId=window.setInterval((function(){var y=$(window).scrollTop(),offset=Math.round(autoscroll.scrollAmount*autoscroll.SCROLL_SPEED);if(y+offset<0&&(offset=-y),y+offset>maxScroll&&(offset=maxScroll-y),0!==offset){$(window).scrollTop(y+offset);var realOffset=$(window).scrollTop()-y;0!==realOffset&&autoscroll.callback&&autoscroll.callback(realOffset)}}),autoscroll.SCROLL_FREQUENCY)},stopScrolling:function(){window.clearInterval(autoscroll.scrollingId),autoscroll.scrollingId=null}};return{start:autoscroll.start,stop:autoscroll.stop}}));
//# sourceMappingURL=autoscroll.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -8,6 +8,6 @@
* @copyright 2018 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define("qtype_ordering/drag_reorder",["jquery",require.specified("core/dragdrop")?"core/dragdrop":"qtype_ordering/dragdrop",require.specified("core/key_codes")?"core/key_codes":"qtype_ordering/key_codes"],(function($,drag,keys){return function(config){var outer,inner,combined,dragStart=null,originalOrder=null,itemDragging=null,itemMoving=null,proxy=null,orderList=null,dragMove=function(){var list=itemDragging.closest(config.list),closestItem=null,closestDistance=null;if(list.find(config.item).each((function(index,element){var distance=distanceBetweenElements(element,proxy);(null===closestItem||distance<closestDistance)&&(closestItem=$(element),closestDistance=distance)})),closestItem[0]!==itemDragging[0]){var offsetValue=0;midY(proxy)<midY(closestItem)?(offsetValue=20,window.console.log("For midY(proxy) < midY(closestItem) offset is: "+offsetValue)):(offsetValue=-20,window.console.log("For midY(proxy) < midY(closestItem) offset is: "+offsetValue)),midY(proxy)+offsetValue<midY(closestItem)?itemDragging.insertBefore(closestItem):itemDragging.insertAfter(closestItem),updateProxy(itemDragging)}},updateProxy=function(itemDragging){for(var items=itemDragging.closest("ol, ul").find("li"),count=items.length,i=0;i<count;++i)if(itemDragging[0]===items[i]){proxy.find("li").attr("value",i+1);break}},dragEnd=function(x,y){void 0!==config.reorderEnd&&config.reorderEnd(itemDragging.closest(config.list),itemDragging);var newOrder=getCurrentOrder();arrayEquals(originalOrder,newOrder)?(new Date).getTime()-dragStart.time<500&&Math.abs(dragStart.x-x)<10&&Math.abs(dragStart.y-y)<10&&itemDragging[0].focus():config.reorderDone(itemDragging.closest(config.list),itemDragging,newOrder),proxy.remove(),proxy=null,itemDragging.removeClass(config.itemMovingClass),itemDragging=null,dragStart=null},midX=function(jQuery){return jQuery.offset().left+jQuery.outerWidth()/2},midY=function(jQuery){return jQuery.offset().top+jQuery.outerHeight()/2},distanceBetweenElements=function(element1,element2){var e1=$(element1),e2=$(element2),dx=midX(e1)-midX(e2),dy=midY(e1)-midY(e2);return Math.sqrt(dx*dx+dy*dy)},getCurrentOrder=function(){return(itemDragging||itemMoving).closest(config.list).find(config.item).map((function(index,item){return config.idGetter(item)})).get()},arrayEquals=function(a1,a2){return a1.length===a2.length&&a1.every((function(v,i){return v===a2[i]}))};config.itemInPage=(outer=config.list,inner=config.item,combined=[],outer.split(",").forEach((function(firstSelector){inner.split(",").forEach((function(secondSelector){combined.push(firstSelector.trim()+" "+secondSelector.trim())}))})),combined.join(", ")),$(config.list).on("mousedown touchstart",config.item,(function(event){var details=drag.prepare(event);details.start&&function(event,details){orderList=$(config.list),dragStart={time:(new Date).getTime(),x:details.x,y:details.y},itemDragging=$(event.currentTarget).closest(config.itemInPage),void 0!==config.reorderStart&&config.reorderStart(itemDragging.closest(config.list),itemDragging),originalOrder=getCurrentOrder(),proxy=$(config.proxyHtml.replace("%%ITEM_HTML%%",itemDragging.html()).replace("%%ITEM_CLASS_NAME%%",itemDragging.attr("class")).replace("%%LIST_CLASS_NAME%%",orderList.attr("class"))),$(document.body).append(proxy),proxy.css("position","absolute"),proxy.css(itemDragging.offset()),proxy.width(itemDragging.outerWidth()),proxy.height(itemDragging.outerHeight()),itemDragging.addClass(config.itemMovingClass),updateProxy(itemDragging),drag.start(event,proxy,dragMove,dragEnd)}(event,details)})),$(config.list).on("keydown",config.item,(function(event){itemMoving=$(event.currentTarget).closest(config.itemInPage),originalOrder=getCurrentOrder(),function(e,current){switch(e.keyCode){case keys.space:case keys.arrowRight:case keys.arrowDown:e.preventDefault(),e.stopPropagation();var next=current.next();next.length&&next.insertBefore(current);break;case keys.arrowLeft:case keys.arrowUp:e.preventDefault(),e.stopPropagation();var prev=current.prev();prev.length&&prev.insertAfter(current)}}(event,itemMoving);var newOrder=getCurrentOrder();arrayEquals(originalOrder,newOrder)||config.reorderDone(itemMoving.closest(config.list),itemMoving,newOrder)})),$(config.itemInPage).attr("tabindex","0")}}));
define("qtype_ordering/drag_reorder",["jquery","core/dragdrop","core/key_codes"],(function($,drag,keys){return function(config){var outer,inner,combined,dragStart=null,originalOrder=null,itemDragging=null,itemMoving=null,proxy=null,orderList=null,dragMove=function(){var list=itemDragging.closest(config.list),closestItem=null,closestDistance=null;if(list.find(config.item).each((function(index,element){var distance=distanceBetweenElements(element,proxy);(null===closestItem||distance<closestDistance)&&(closestItem=$(element),closestDistance=distance)})),closestItem[0]!==itemDragging[0]){var offsetValue=0;midY(proxy)<midY(closestItem)?(offsetValue=20,window.console.log("For midY(proxy) < midY(closestItem) offset is: "+offsetValue)):(offsetValue=-20,window.console.log("For midY(proxy) < midY(closestItem) offset is: "+offsetValue)),midY(proxy)+offsetValue<midY(closestItem)?itemDragging.insertBefore(closestItem):itemDragging.insertAfter(closestItem),updateProxy(itemDragging)}},updateProxy=function(itemDragging){for(var items=itemDragging.closest("ol, ul").find("li"),count=items.length,i=0;i<count;++i)if(itemDragging[0]===items[i]){proxy.find("li").attr("value",i+1);break}},dragEnd=function(x,y){void 0!==config.reorderEnd&&config.reorderEnd(itemDragging.closest(config.list),itemDragging);var newOrder=getCurrentOrder();arrayEquals(originalOrder,newOrder)?(new Date).getTime()-dragStart.time<500&&Math.abs(dragStart.x-x)<10&&Math.abs(dragStart.y-y)<10&&itemDragging[0].focus():config.reorderDone(itemDragging.closest(config.list),itemDragging,newOrder),proxy.remove(),proxy=null,itemDragging.removeClass(config.itemMovingClass),itemDragging=null,dragStart=null},midX=function(jQuery){return jQuery.offset().left+jQuery.outerWidth()/2},midY=function(jQuery){return jQuery.offset().top+jQuery.outerHeight()/2},distanceBetweenElements=function(element1,element2){var e1=$(element1),e2=$(element2),dx=midX(e1)-midX(e2),dy=midY(e1)-midY(e2);return Math.sqrt(dx*dx+dy*dy)},getCurrentOrder=function(){return(itemDragging||itemMoving).closest(config.list).find(config.item).map((function(index,item){return config.idGetter(item)})).get()},arrayEquals=function(a1,a2){return a1.length===a2.length&&a1.every((function(v,i){return v===a2[i]}))};config.itemInPage=(outer=config.list,inner=config.item,combined=[],outer.split(",").forEach((function(firstSelector){inner.split(",").forEach((function(secondSelector){combined.push(firstSelector.trim()+" "+secondSelector.trim())}))})),combined.join(", ")),$(config.list).on("mousedown touchstart",config.item,(function(event){var details=drag.prepare(event);details.start&&function(event,details){orderList=$(config.list),dragStart={time:(new Date).getTime(),x:details.x,y:details.y},itemDragging=$(event.currentTarget).closest(config.itemInPage),void 0!==config.reorderStart&&config.reorderStart(itemDragging.closest(config.list),itemDragging),originalOrder=getCurrentOrder(),proxy=$(config.proxyHtml.replace("%%ITEM_HTML%%",itemDragging.html()).replace("%%ITEM_CLASS_NAME%%",itemDragging.attr("class")).replace("%%LIST_CLASS_NAME%%",orderList.attr("class"))),$(document.body).append(proxy),proxy.css("position","absolute"),proxy.css(itemDragging.offset()),proxy.width(itemDragging.outerWidth()),proxy.height(itemDragging.outerHeight()),itemDragging.addClass(config.itemMovingClass),updateProxy(itemDragging),drag.start(event,proxy,dragMove,dragEnd)}(event,details)})),$(config.list).on("keydown",config.item,(function(event){itemMoving=$(event.currentTarget).closest(config.itemInPage),originalOrder=getCurrentOrder(),function(e,current){switch(e.keyCode){case keys.space:case keys.arrowRight:case keys.arrowDown:e.preventDefault(),e.stopPropagation();var next=current.next();next.length&&next.insertBefore(current);break;case keys.arrowLeft:case keys.arrowUp:e.preventDefault(),e.stopPropagation();var prev=current.prev();prev.length&&prev.insertAfter(current)}}(event,itemMoving);var newOrder=getCurrentOrder();arrayEquals(originalOrder,newOrder)||config.reorderDone(itemMoving.closest(config.list),itemMoving,newOrder)})),$(config.itemInPage).attr("tabindex","0")}}));
//# sourceMappingURL=drag_reorder.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -1,20 +0,0 @@
/*
* JavaScript to handle drag operations, including automatic scrolling.
*
* This is a copy of a library that was added to Moodle core in Moodle 3.6,
* so we can support older Moodle versions.
*
* Note: this module is defined statically. It is a singleton. You
* can only have one use of it active at any time. However, you
* can only drag one thing at a time, this is not a problem in practice.
*
* @module qtype_ordering/dragdrop
* @class dragdrop
* @package qtype_ordering
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.6
*/
define("qtype_ordering/dragdrop",["jquery","qtype_ordering/autoscroll"],(function($,autoScroll){var dragdrop={eventCaptureOptions:{passive:!1,capture:!0},dragProxy:null,onMove:null,onDrop:null,initialPosition:null,initialX:null,initialY:null,touching:null,prepare:function(event){if(event.preventDefault(),"touchstart"===event.type?null===dragdrop.touching&&event.changedTouches.length>0:1===event.which){var details=dragdrop.getEventXY(event);return details.start=!0,details}return{start:!1}},start:function(event,dragProxy,onMove,onDrop){var xy=dragdrop.getEventXY(event);switch(dragdrop.initialX=xy.x,dragdrop.initialY=xy.y,dragdrop.initialPosition=dragProxy.offset(),dragdrop.dragProxy=dragProxy,dragdrop.onMove=onMove,dragdrop.onDrop=onDrop,event.type){case"mousedown":dragdrop.addEventSpecial("mousemove",dragdrop.mouseMove),dragdrop.addEventSpecial("mouseup",dragdrop.mouseUp);break;case"touchstart":dragdrop.addEventSpecial("touchend",dragdrop.touchEnd),dragdrop.addEventSpecial("touchcancel",dragdrop.touchEnd),dragdrop.addEventSpecial("touchmove",dragdrop.touchMove),dragdrop.touching=event.changedTouches[0].identifier;break;default:throw new Error("Unexpected event type: "+event.type)}autoScroll.start(dragdrop.scroll)},addEventSpecial:function(event,handler){try{window.addEventListener(event,handler,dragdrop.eventCaptureOptions)}catch(ex){dragdrop.eventCaptureOptions=!0,window.addEventListener(event,handler,dragdrop.eventCaptureOptions)}},getEventXY:function(event){switch(event.type){case"touchstart":return{x:event.changedTouches[0].pageX,y:event.changedTouches[0].pageY};case"mousedown":return{x:event.pageX,y:event.pageY};default:throw new Error("Unexpected event type: "+event.type)}},touchMove:function(e){e.preventDefault();for(var i=0;i<e.changedTouches.length;i++)e.changedTouches[i].identifier===dragdrop.touching&&dragdrop.handleMove(e.changedTouches[i].pageX,e.changedTouches[i].pageY)},mouseMove:function(e){dragdrop.handleMove(e.pageX,e.pageY)},handleMove:function(pageX,pageY){var current=dragdrop.dragProxy.offset(),topOffset=current.top-parseInt(dragdrop.dragProxy.css("top")),leftOffset=current.left-parseInt(dragdrop.dragProxy.css("left")),maxY=$(document).height()-dragdrop.dragProxy.outerHeight()-topOffset,maxX=$(document).width()-dragdrop.dragProxy.outerWidth()-leftOffset,minY=-topOffset,minX=-leftOffset,initial=dragdrop.initialPosition,position={top:Math.max(minY,Math.min(maxY,initial.top+(pageY-dragdrop.initialY)-topOffset)),left:Math.max(minX,Math.min(maxX,initial.left+(pageX-dragdrop.initialX)-leftOffset))};dragdrop.dragProxy.css(position),dragdrop.onMove(pageX,pageY,dragdrop.dragProxy)},touchEnd:function(e){e.preventDefault();for(var i=0;i<e.changedTouches.length;i++)e.changedTouches[i].identifier===dragdrop.touching&&dragdrop.handleEnd(e.changedTouches[i].pageX,e.changedTouches[i].pageY)},mouseUp:function(e){dragdrop.handleEnd(e.pageX,e.pageY)},handleEnd:function(pageX,pageY){null!==dragdrop.touching?(window.removeEventListener("touchend",dragdrop.touchEnd,dragdrop.eventCaptureOptions),window.removeEventListener("touchcancel",dragdrop.touchEnd,dragdrop.eventCaptureOptions),window.removeEventListener("touchmove",dragdrop.touchMove,dragdrop.eventCaptureOptions),dragdrop.touching=null):(window.removeEventListener("mousemove",dragdrop.mouseMove,dragdrop.eventCaptureOptions),window.removeEventListener("mouseup",dragdrop.mouseUp,dragdrop.eventCaptureOptions)),autoScroll.stop(),dragdrop.onDrop(pageX,pageY,dragdrop.dragProxy)},scroll:function(offset){var maxY=$(document).height()-dragdrop.dragProxy.outerHeight(),currentPosition=dragdrop.dragProxy.offset();currentPosition.top=Math.min(maxY,currentPosition.top+offset),dragdrop.dragProxy.css(currentPosition)}};return{prepare:dragdrop.prepare,start:dragdrop.start}}));
//# sourceMappingURL=dragdrop.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -1,16 +0,0 @@
/*
* A list of human readable names for the keycodes.
*
* This is a copy of a library that was added to Moodle core in Moodle 3.2,
* so we can support older Moodle versions.
*
* @module qtype_ordering/key_codes
* @class key_codes
* @package qtype_ordering
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.2
*/
define("qtype_ordering/key_codes",[],(function(){return{tab:9,enter:13,escape:27,space:32,end:35,home:36,arrowLeft:37,arrowUp:38,arrowRight:39,arrowDown:40,8:56,asterix:106,pageUp:33,pageDown:34}}));
//# sourceMappingURL=key_codes.min.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"key_codes.min.js","sources":["../src/key_codes.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 * A list of human readable names for the keycodes.\n *\n * This is a copy of a library that was added to Moodle core in Moodle 3.2,\n * so we can support older Moodle versions.\n *\n * @module qtype_ordering/key_codes\n * @class key_codes\n * @package qtype_ordering\n * @copyright 2016 Ryan Wyllie <ryan@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.2\n */\ndefine([], function() {\n\n return /** @alias module:qtype_ordering/key_codes */ {\n 'tab': 9,\n 'enter': 13,\n 'escape': 27,\n 'space': 32,\n 'end': 35,\n 'home': 36,\n 'arrowLeft': 37,\n 'arrowUp': 38,\n 'arrowRight': 39,\n 'arrowDown': 40,\n '8': 56,\n 'asterix': 106,\n 'pageUp': 33,\n 'pageDown': 34,\n };\n});\n"],"names":["define"],"mappings":";;;;;;;;;;;;;AA4BAA,kCAAO,IAAI,iBAE8C,KAC1C,QACE,UACC,SACD,OACF,QACC,aACK,WACF,cACG,aACD,KACR,WACM,WACD,YACE"}

View File

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

View File

@ -29,8 +29,8 @@
*/
define([
'jquery',
require.specified('core/dragdrop') ? 'core/dragdrop' : 'qtype_ordering/dragdrop',
require.specified('core/key_codes') ? 'core/key_codes' : 'qtype_ordering/key_codes'
'core/dragdrop',
'core/key_codes'
], function(
$,
drag,

View File

@ -1,343 +0,0 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* JavaScript to handle drag operations, including automatic scrolling.
*
* This is a copy of a library that was added to Moodle core in Moodle 3.6,
* so we can support older Moodle versions.
*
* Note: this module is defined statically. It is a singleton. You
* can only have one use of it active at any time. However, you
* can only drag one thing at a time, this is not a problem in practice.
*
* @module qtype_ordering/dragdrop
* @class dragdrop
* @package qtype_ordering
* @copyright 2016 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.6
*/
define(['jquery', 'qtype_ordering/autoscroll'], function($, autoScroll) {
/**
* @alias module:qtype_ordering/dragdrop
*/
var dragdrop = {
/**
* A boolean or options argument depending on whether browser supports passive events.
* @private
*/
eventCaptureOptions: {passive: false, capture: true},
/**
* Drag proxy if any.
* @private
*/
dragProxy: null,
/**
* Function called on move.
* @private
*/
onMove: null,
/**
* Function called on drop.
* @private
*/
onDrop: null,
/**
* Initial position of proxy at drag start.
*/
initialPosition: null,
/**
* Initial page X of cursor at drag start.
*/
initialX: null,
/**
* Initial page Y of cursor at drag start.
*/
initialY: null,
/**
* If touch event is in progress, this will be the id, otherwise null
*/
touching: null,
/**
* Prepares to begin a drag operation - call with a mousedown or touchstart event.
*
* If the returned object has 'start' true, then you can set up a drag proxy, and call
* start. This function will call preventDefault automatically regardless of whether
* starting or not.
*
* @public
* @param {Object} event Event (should be either mousedown or touchstart)
* @return {Object} Object with start (boolean flag) and x, y (only if flag true) values
*/
prepare: function(event) {
event.preventDefault();
var start;
if (event.type === 'touchstart') {
// For touch, start if there's at least one touch and we are not currently doing
// a touch event.
start = (dragdrop.touching === null) && event.changedTouches.length > 0;
} else {
// For mousedown, start if it's the left button.
start = event.which === 1;
}
if (start) {
var details = dragdrop.getEventXY(event);
details.start = true;
return details;
} else {
return {start: false};
}
},
/**
* Call to start a drag operation, in response to a mouse down or touch start event.
* Normally call this after calling prepare and receiving start true (you can probably
* skip prepare if only supporting drag not touch).
*
* Note: The caller is responsible for creating a 'drag proxy' which is the
* thing that actually gets dragged. At present, this doesn't really work
* properly unless it is added directly within the body tag.
*
* You also need to ensure that there is CSS so the proxy is absolutely positioned,
* and styled to look like it is floating.
*
* You also need to absolutely position the proxy where you want it to start.
*
* @public
* @param {Object} event Event (should be either mousedown or touchstart)
* @param {jQuery} dragProxy An absolute-positioned element for dragging
* @param {Object} onMove Function that receives X and Y page locations for a move
* @param {Object} onDrop Function that receives X and Y page locations when dropped
*/
start: function(event, dragProxy, onMove, onDrop) {
var xy = dragdrop.getEventXY(event);
dragdrop.initialX = xy.x;
dragdrop.initialY = xy.y;
dragdrop.initialPosition = dragProxy.offset();
dragdrop.dragProxy = dragProxy;
dragdrop.onMove = onMove;
dragdrop.onDrop = onDrop;
switch (event.type) {
case 'mousedown':
// Cannot use jQuery 'on' because events need to not be passive.
dragdrop.addEventSpecial('mousemove', dragdrop.mouseMove);
dragdrop.addEventSpecial('mouseup', dragdrop.mouseUp);
break;
case 'touchstart':
dragdrop.addEventSpecial('touchend', dragdrop.touchEnd);
dragdrop.addEventSpecial('touchcancel', dragdrop.touchEnd);
dragdrop.addEventSpecial('touchmove', dragdrop.touchMove);
dragdrop.touching = event.changedTouches[0].identifier;
break;
default:
throw new Error('Unexpected event type: ' + event.type);
}
autoScroll.start(dragdrop.scroll);
},
/**
* Adds an event listener with special event capture options (capture, not passive). If the
* browser does not support passive events, it will fall back to the boolean for capture.
*
* @private
* @param {Object} event Event type string
* @param {Object} handler Handler function
*/
addEventSpecial: function(event, handler) {
try {
window.addEventListener(event, handler, dragdrop.eventCaptureOptions);
} catch (ex) {
dragdrop.eventCaptureOptions = true;
window.addEventListener(event, handler, dragdrop.eventCaptureOptions);
}
},
/**
* Gets X/Y co-ordinates of an event, which can be either touchstart or mousedown.
*
* @private
* @param {Object} event Event (should be either mousedown or touchstart)
* @return {Object} X/Y co-ordinates
*/
getEventXY: function(event) {
switch (event.type) {
case 'touchstart':
return {x: event.changedTouches[0].pageX,
y: event.changedTouches[0].pageY};
case 'mousedown':
return {x: event.pageX, y: event.pageY};
default:
throw new Error('Unexpected event type: ' + event.type);
}
},
/**
* Event handler for touch move.
*
* @private
* @param {Object} e Event
*/
touchMove: function(e) {
e.preventDefault();
for (var i = 0; i < e.changedTouches.length; i++) {
if (e.changedTouches[i].identifier === dragdrop.touching) {
dragdrop.handleMove(e.changedTouches[i].pageX, e.changedTouches[i].pageY);
}
}
},
/**
* Event handler for mouse move.
*
* @private
* @param {Object} e Event
*/
mouseMove: function(e) {
dragdrop.handleMove(e.pageX, e.pageY);
},
/**
* Shared handler for move event (mouse or touch).
*
* @private
* @param {number} pageX X co-ordinate
* @param {number} pageY Y co-ordinate
*/
handleMove: function(pageX, pageY) {
// Move the drag proxy, not letting you move it out of screen or window bounds.
var current = dragdrop.dragProxy.offset();
var topOffset = current.top - parseInt(dragdrop.dragProxy.css('top'));
var leftOffset = current.left - parseInt(dragdrop.dragProxy.css('left'));
var maxY = $(document).height() - dragdrop.dragProxy.outerHeight() - topOffset;
var maxX = $(document).width() - dragdrop.dragProxy.outerWidth() - leftOffset;
var minY = -topOffset;
var minX = -leftOffset;
var initial = dragdrop.initialPosition;
var position = {
top: Math.max(minY, Math.min(maxY, initial.top + (pageY - dragdrop.initialY) - topOffset)),
left: Math.max(minX, Math.min(maxX, initial.left + (pageX - dragdrop.initialX) - leftOffset))
};
dragdrop.dragProxy.css(position);
// Trigger move handler.
dragdrop.onMove(pageX, pageY, dragdrop.dragProxy);
},
/**
* Event handler for touch end.
*
* @private
* @param {Object} e Event
*/
touchEnd: function(e) {
e.preventDefault();
for (var i = 0; i < e.changedTouches.length; i++) {
if (e.changedTouches[i].identifier === dragdrop.touching) {
dragdrop.handleEnd(e.changedTouches[i].pageX, e.changedTouches[i].pageY);
}
}
},
/**
* Event handler for mouse up.
*
* @private
* @param {Object} e Event
*/
mouseUp: function(e) {
dragdrop.handleEnd(e.pageX, e.pageY);
},
/**
* Shared handler for end drag (mouse or touch).
*
* @private
* @param {number} pageX X
* @param {number} pageY Y
*/
handleEnd: function(pageX, pageY) {
if (dragdrop.touching !== null) {
window.removeEventListener('touchend', dragdrop.touchEnd, dragdrop.eventCaptureOptions);
window.removeEventListener('touchcancel', dragdrop.touchEnd, dragdrop.eventCaptureOptions);
window.removeEventListener('touchmove', dragdrop.touchMove, dragdrop.eventCaptureOptions);
dragdrop.touching = null;
} else {
window.removeEventListener('mousemove', dragdrop.mouseMove, dragdrop.eventCaptureOptions);
window.removeEventListener('mouseup', dragdrop.mouseUp, dragdrop.eventCaptureOptions);
}
autoScroll.stop();
dragdrop.onDrop(pageX, pageY, dragdrop.dragProxy);
},
/**
* Called when the page scrolls.
*
* @private
* @param {number} offset Amount of scroll
*/
scroll: function(offset) {
// Move the proxy to match.
var maxY = $(document).height() - dragdrop.dragProxy.outerHeight();
var currentPosition = dragdrop.dragProxy.offset();
currentPosition.top = Math.min(maxY, currentPosition.top + offset);
dragdrop.dragProxy.css(currentPosition);
}
};
return {
/**
* Prepares to begin a drag operation - call with a mousedown or touchstart event.
*
* If the returned object has 'start' true, then you can set up a drag proxy, and call
* start. This function will call preventDefault automatically regardless of whether
* starting or not.
*
* @param {Object} event Event (should be either mousedown or touchstart)
* @return {Object} Object with start (boolean flag) and x, y (only if flag true) values
*/
prepare: dragdrop.prepare,
/**
* Call to start a drag operation, in response to a mouse down or touch start event.
* Normally call this after calling prepare and receiving start true (you can probably
* skip prepare if only supporting drag not touch).
*
* Note: The caller is responsible for creating a 'drag proxy' which is the
* thing that actually gets dragged. At present, this doesn't really work
* properly unless it is added directly within the body tag.
*
* You also need to ensure that there is CSS so the proxy is absolutely positioned,
* and styled to look like it is floating.
*
* You also need to absolutely position the proxy where you want it to start.
*
* @param {Object} event Event (should be either mousedown or touchstart)
* @param {jQuery} dragProxy An absolute-positioned element for dragging
* @param {Object} onMove Function that receives X and Y page locations for a move
* @param {Object} onDrop Function that receives X and Y page locations when dropped
*/
start: dragdrop.start
};
});

View File

@ -1,47 +0,0 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/*
* A list of human readable names for the keycodes.
*
* This is a copy of a library that was added to Moodle core in Moodle 3.2,
* so we can support older Moodle versions.
*
* @module qtype_ordering/key_codes
* @class key_codes
* @package qtype_ordering
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.2
*/
define([], function() {
return /** @alias module:qtype_ordering/key_codes */ {
'tab': 9,
'enter': 13,
'escape': 27,
'space': 32,
'end': 35,
'home': 36,
'arrowLeft': 37,
'arrowUp': 38,
'arrowRight': 39,
'arrowDown': 40,
'8': 56,
'asterix': 106,
'pageUp': 33,
'pageDown': 34,
};
});