MDL-47494 ddimageortext: Fix drag and drop on Android touch devices. #85108
authorM Kassaei <m.kassaei@open.ac.uk>
Fri, 12 Jun 2015 13:02:44 +0000 (14:02 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 16 Jun 2015 17:38:56 +0000 (18:38 +0100)
The initial suggestion for how to do this fix came from Davo Smith.

question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-debug.js
question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-min.js
question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd.js
question/type/ddimageortext/yui/src/ddimageortext/js/ddimageortext.js

index 3818881..57f2e21 100644 (file)
Binary files a/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-debug.js and b/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-debug.js differ
index 845933c..98965e4 100644 (file)
Binary files a/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-min.js and b/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd-min.js differ
index 3818881..57f2e21 100644 (file)
Binary files a/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd.js and b/question/type/ddimageortext/yui/build/moodle-qtype_ddimageortext-dd/moodle-qtype_ddimageortext-dd.js differ
index 5741acb..ecefd83 100644 (file)
@@ -172,7 +172,6 @@ Y.extend(DDIMAGEORTEXT_DD, Y.Base, {
 
                 drag.setData('group', group);
                 drag.setData('choice', choice);
-
             },
             draggable_for_form : function (drag) {
                 var dd = new Y.DD.Drag({
@@ -248,6 +247,7 @@ var DDIMAGEORTEXT_QUESTION = function() {
  * This is the code for question rendering.
  */
 Y.extend(DDIMAGEORTEXT_QUESTION, M.qtype_ddimageortext.dd_base_class, {
+    touchscrolldisable: null,
     pendingid: '',
     initializer : function() {
         this.pendingid = 'qtype_ddimageortext-' + Math.random().toString(36).slice(2); // Random string.
@@ -260,6 +260,38 @@ Y.extend(DDIMAGEORTEXT_QUESTION, M.qtype_ddimageortext.dd_base_class, {
                                                 false, 0, this.create_all_drag_and_drops);
         Y.later(500, this, this.reposition_drags_for_question, [this.pendingid], true);
     },
+
+    /**
+     * prevent_touchmove_from_scrolling allows users of touch screen devices to
+     * use drag and drop and normal scrolling at the same time. I.e. when
+     * touching and dragging a draggable item, the screen does not scroll, but
+     * you can scroll by touching other area of the screen apart from the
+     * draggable items.
+     */
+    prevent_touchmove_from_scrolling : function(drag) {
+        var touchstart = (Y.UA.ie) ? 'MSPointerStart' : 'touchstart';
+        var touchend = (Y.UA.ie) ? 'MSPointerEnd' : 'touchend';
+        var touchmove = (Y.UA.ie) ? 'MSPointerMove' : 'touchmove';
+
+        // Disable scrolling when touching the draggable items.
+        drag.on(touchstart, function() {
+            if (this.touchscrolldisable) {
+                return; // Already disabled.
+            }
+            this.touchscrolldisable = Y.one('body').on(touchmove, function(e) {
+                e = e || window.event;
+                e.preventDefault();
+            });
+        }, this);
+
+        // Allow scrolling after releasing the draggable items.
+        drag.on(touchend, function() {
+            if (this.touchscrolldisable) {
+                this.touchscrolldisable.detach();
+                this.touchscrolldisable = null;
+            }
+        }, this);
+    },
     create_all_drag_and_drops : function () {
         this.init_drops();
         this.update_padding_sizes_all();
@@ -273,6 +305,9 @@ Y.extend(DDIMAGEORTEXT_QUESTION, M.qtype_ddimageortext.dd_base_class, {
             i++;
             if (!this.get('readonly')) {
                 this.doc.draggable_for_question(dragnode, group, choice);
+
+                // Prevent scrolling whilst dragging on Adroid devices.
+                this.prevent_touchmove_from_scrolling(dragnode);
             }
             if (dragnode.hasClass('infinite')) {
                 var dragstocreate = groupsize - 1;
@@ -281,6 +316,9 @@ Y.extend(DDIMAGEORTEXT_QUESTION, M.qtype_ddimageortext.dd_base_class, {
                     i++;
                     if (!this.get('readonly')) {
                         this.doc.draggable_for_question(dragnode, group, choice);
+
+                        // Prevent scrolling whilst dragging on Adroid devices.
+                        this.prevent_touchmove_from_scrolling(dragnode);
                     }
                     dragstocreate--;
                 }