From 489c938b85a4e10963f4f7071d104fbe6438ad63 Mon Sep 17 00:00:00 2001 From: Gordon Bateson Date: Wed, 17 Jan 2018 08:12:54 +0900 Subject: [PATCH] MDL-79863 qtype_ordering: qtype/ordering add AMD folder and files cor compatability with Moodle >= 2.9. Many thanks to Jakob Ackermann (github.com/jacac) --- .../jquery.ui.touch-punch-improved.min.js | 1 + .../type/ordering/amd/build/ordering.min.js | 1 + .../amd/src/jquery.ui.touch-punch-improved.js | 207 ++++++++++++++++++ question/type/ordering/amd/src/ordering.js | 17 ++ 4 files changed, 226 insertions(+) create mode 100644 question/type/ordering/amd/build/jquery.ui.touch-punch-improved.min.js create mode 100644 question/type/ordering/amd/build/ordering.min.js create mode 100644 question/type/ordering/amd/src/jquery.ui.touch-punch-improved.js create mode 100644 question/type/ordering/amd/src/ordering.js diff --git a/question/type/ordering/amd/build/jquery.ui.touch-punch-improved.min.js b/question/type/ordering/amd/build/jquery.ui.touch-punch-improved.min.js new file mode 100644 index 00000000000..eec6aa92ff8 --- /dev/null +++ b/question/type/ordering/amd/build/jquery.ui.touch-punch-improved.min.js @@ -0,0 +1 @@ +!function(a){"function"==typeof define&&define.amd?define(["jquery","jqueryui"],a):a(window.jQuery)}(function(a){function b(a){var b=window.pageXOffset,c=window.pageYOffset,d=a.clientX,e=a.clientY;return 0===a.pageY&&Math.floor(e)>Math.floor(a.pageY)||0===a.pageX&&Math.floor(d)>Math.floor(a.pageX)?(d-=b,e-=c):(e1||d&&!c.isPrimary)){var f=d?c.originalEvent:c.originalEvent.changedTouches[0],g=document.createEvent("MouseEvents"),h=b(f);a(f.target).is("input")||a(f.target).is("textarea")?c.stopPropagation():c.preventDefault(),g.initMouseEvent(e,!0,!0,window,1,c.screenX||f.screenX,c.screenY||f.screenY,c.clientX||h.clientX,c.clientY||h.clientY,!1,!1,!1,!1,0,null),c.target.dispatchEvent(g)}}var d=window.navigator.pointerEnabled||window.navigator.msPointerEnabled;if(a.support.touch="ontouchend"in document||d,a.support.touch&&a.ui.mouse){var e,f=a.ui.mouse.prototype,g=f._mouseInit;f._touchStart=function(a){var b=this;e||!d&&!b._mouseCapture(a.originalEvent.changedTouches[0])||(e=!0,b._touchMoved=!1,c(a,"mouseover"),c(a,"mousemove"),c(a,"mousedown"))},f._touchMove=function(a){e&&(this._touchMoved=!0,c(a,"mousemove"))},f._touchEnd=function(a){e&&(c(a,"mouseup"),c(a,"mouseout"),this._touchMoved||c(a,"click"),e=!1)},f._mouseInit=function(){var b=this;b.element.on({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd"),pointerDown:a.proxy(b,"_touchStart"),pointerMove:a.proxy(b,"_touchMove"),pointerUp:a.proxy(b,"_touchEnd"),MSPointerDown:a.proxy(b,"_touchStart"),MSPointerMove:a.proxy(b,"_touchMove"),MSPointerUp:a.proxy(b,"_touchEnd")}),g.call(b)}}}); \ No newline at end of file diff --git a/question/type/ordering/amd/build/ordering.min.js b/question/type/ordering/amd/build/ordering.min.js new file mode 100644 index 00000000000..cf2eb05d42b --- /dev/null +++ b/question/type/ordering/amd/build/ordering.min.js @@ -0,0 +1 @@ +define(["jquery","jqueryui","qtype_ordering/jquery.ui.touch-punch-improved"],function(a){return{init:function(b,c,d,e){a("#"+b).sortable({axis:e,containment:a("#"+b),opacity:.6,update:function(){var b=a(this).sortable("toArray").toString();a("#"+c).attr("value",b)}})}}}); \ No newline at end of file diff --git a/question/type/ordering/amd/src/jquery.ui.touch-punch-improved.js b/question/type/ordering/amd/src/jquery.ui.touch-punch-improved.js new file mode 100644 index 00000000000..19119b217cd --- /dev/null +++ b/question/type/ordering/amd/src/jquery.ui.touch-punch-improved.js @@ -0,0 +1,207 @@ +/*! + * jQuery UI Touch Punch Improved 0.3.1 + * + * + * Copyright 2013, Chris Hutchinson + * Original jquery-ui-touch-punch Copyright 2011, Dave Furfero + * Dual licensed under the MIT or GPL Version 2 licenses. + * + * Depends: + * jquery.ui.widget.js + * jquery.ui.mouse.js + */ +(function( factory ) { + if ( typeof define === "function" && define.amd ) { + + // AMD. Register as an anonymous module. + define([ "jquery", "jqueryui" ], factory ); + } else { + + // Browser globals + factory( window.jQuery ); + } +}(function ($) { + var pointerEnabled = window.navigator.pointerEnabled + || window.navigator.msPointerEnabled; + + // Detect touch support + $.support.touch = 'ontouchend' in document || pointerEnabled; + + // Ignore browsers without touch support or mouse support + if (!$.support.touch || !$.ui.mouse) { + return; + } + + var mouseProto = $.ui.mouse.prototype, + _mouseInit = mouseProto._mouseInit, + touchHandled; + + // see http://stackoverflow.com/a/12714084/220825 + function fixTouch(touch) { + var winPageX = window.pageXOffset, + winPageY = window.pageYOffset, + x = touch.clientX, + y = touch.clientY; + + if (touch.pageY === 0 && Math.floor(y) > Math.floor(touch.pageY) + || touch.pageX === 0 && Math.floor(x) > Math.floor(touch.pageX)) { + // iOS4 clientX/clientY have the value that should have been + // in pageX/pageY. While pageX/page/ have the value 0 + x = x - winPageX; + y = y - winPageY; + } else if (y < (touch.pageY - winPageY) || x < (touch.pageX - winPageX)) { + // Some Android browsers have totally bogus values for clientX/Y + // when scrolling/zooming a page. Detectable since clientX/clientY + // should never be smaller than pageX/pageY minus page scroll + x = touch.pageX - winPageX; + y = touch.pageY - winPageY; + } + + return { + clientX: x, + clientY: y + }; + } + + /** + * Simulate a mouse event based on a corresponding touch event + * @param {Object} event A touch event + * @param {String} simulatedType The corresponding mouse event + */ + function simulateMouseEvent (event, simulatedType) { + // Ignore multi-touch events + if ((!pointerEnabled && event.originalEvent.touches.length > 1) || (pointerEnabled && !event.isPrimary)) { + return; + } + + var touch = pointerEnabled ? event.originalEvent : event.originalEvent.changedTouches[0], + simulatedEvent = document.createEvent('MouseEvents'), + coord = fixTouch(touch); + + // Check if element is an input or a textarea + if ($(touch.target).is("input") || $(touch.target).is("textarea")) { + event.stopPropagation(); + } else { + event.preventDefault(); + } + + // Initialize the simulated mouse event using the touch event's coordinates + simulatedEvent.initMouseEvent( + simulatedType, // type + true, // bubbles + true, // cancelable + window, // view + 1, // detail + event.screenX || touch.screenX, // screenX + event.screenY || touch.screenY, // screenY + event.clientX || coord.clientX, // clientX + event.clientY || coord.clientY, // clientY + false, // ctrlKey + false, // altKey + false, // shiftKey + false, // metaKey + 0, // button + null // relatedTarget + ); + + // Dispatch the simulated event to the target element + event.target.dispatchEvent(simulatedEvent); + } + + /** + * Handle the jQuery UI widget's touchstart events + * @param {Object} event The widget element's touchstart event + */ + mouseProto._touchStart = function (event) { + var self = this; + + // Ignore the event if another widget is already being handled + if (touchHandled || (!pointerEnabled && !self._mouseCapture(event.originalEvent.changedTouches[0]))) { + return; + } + + // Set the flag to prevent other widgets from inheriting the touch event + touchHandled = true; + + // Track movement to determine if interaction was a click + self._touchMoved = false; + + // Simulate the mouseover event + simulateMouseEvent(event, 'mouseover'); + + // Simulate the mousemove event + simulateMouseEvent(event, 'mousemove'); + + // Simulate the mousedown event + simulateMouseEvent(event, 'mousedown'); + }; + + /** + * Handle the jQuery UI widget's touchmove events + * @param {Object} event The document's touchmove event + */ + mouseProto._touchMove = function (event) { + // Ignore event if not handled + if (!touchHandled) { + return; + } + + // Interaction was not a click + this._touchMoved = true; + + // Simulate the mousemove event + simulateMouseEvent(event, 'mousemove'); + }; + + /** + * Handle the jQuery UI widget's touchend events + * @param {Object} event The document's touchend event + */ + mouseProto._touchEnd = function (event) { + // Ignore event if not handled + if (!touchHandled) { + return; + } + + // Simulate the mouseup event + simulateMouseEvent(event, 'mouseup'); + + // Simulate the mouseout event + simulateMouseEvent(event, 'mouseout'); + + // If the touch interaction did not move, it should trigger a click + if (!this._touchMoved) { + // Simulate the click event + simulateMouseEvent(event, 'click'); + } + + // Unset the flag to allow other widgets to inherit the touch event + touchHandled = false; + }; + + /** + * A duck punch of the $.ui.mouse _mouseInit method to support touch events. + * This method extends the widget with bound touch event handlers that + * translate touch events to mouse events and pass them to the widget's + * original mouse event handling methods. + */ + mouseProto._mouseInit = function () { + var self = this; + + self.element.on({ + 'touchstart': $.proxy(self, '_touchStart'), + 'touchmove': $.proxy(self, '_touchMove'), + 'touchend': $.proxy(self, '_touchEnd'), + 'pointerDown': $.proxy(self, '_touchStart'), + 'pointerMove': $.proxy(self, '_touchMove'), + 'pointerUp': $.proxy(self, '_touchEnd'), + 'MSPointerDown': $.proxy(self, '_touchStart'), + 'MSPointerMove': $.proxy(self, '_touchMove'), + 'MSPointerUp': $.proxy(self, '_touchEnd') + }); + + // Call the original $.ui.mouse init method + _mouseInit.call(self); + }; + +})); \ No newline at end of file diff --git a/question/type/ordering/amd/src/ordering.js b/question/type/ordering/amd/src/ordering.js new file mode 100644 index 00000000000..f2b3aec3abb --- /dev/null +++ b/question/type/ordering/amd/src/ordering.js @@ -0,0 +1,17 @@ +define(['jquery', 'jqueryui', 'qtype_ordering/jquery.ui.touch-punch-improved'], function($) { + + return { + init: function (sortableid, responseid, ablockid, axis) { + $('#' + sortableid).sortable({ + axis : axis, + containment: $('#'+sortableid), + opacity : 0.6, + update : function () { + var ItemsOrder = $(this).sortable('toArray').toString(); + $('#' + responseid).attr('value', ItemsOrder); + } + }); + } + }; + +}); \ No newline at end of file