From a4400cefdb186264e2b31a1cc475cbc836c81057 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 27 Oct 2013 23:34:24 +0800 Subject: [PATCH] MDL-40975 JavaScript: Make drag/drop respond to a single click as well as a drag --- lib/yui/dragdrop/dragdrop.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/yui/dragdrop/dragdrop.js b/lib/yui/dragdrop/dragdrop.js index 759d8494473..6aefcfd6994 100644 --- a/lib/yui/dragdrop/dragdrop.js +++ b/lib/yui/dragdrop/dragdrop.js @@ -21,7 +21,7 @@ YUI.add('moodle-core-dragdrop', function(Y) { parentnodeclass : null, groups : [], lastdroptarget : null, - initializer : function(params) { + initializer : function() { // Listen for all drag:start events Y.DD.DDM.on('drag:start', this.global_drag_start, this); // Listen for all drag:end events @@ -35,7 +35,8 @@ YUI.add('moodle-core-dragdrop', function(Y) { // Listen for all drop:miss events Y.DD.DDM.on('drag:dropmiss', this.global_drag_dropmiss, this); - Y.on('key', this.global_keydown, window, 'down:32,enter,esc', this); + Y.one(Y.config.doc.body).delegate('key', this.global_keydown, 'down:32, enter, esc', '.' + MOVEICON.cssclass, this); + Y.one(Y.config.doc.body).delegate('click', this.global_keydown, '.' + MOVEICON.cssclass , this); }, get_drag_handle: function(title, classname, iconclass, large) { @@ -437,11 +438,16 @@ YUI.add('moodle-core-dragdrop', function(Y) { * @param {Event} e The keydown / click event on the drag handle. */ global_keydown : function(e) { - var draghandle = e.target, + var draghandle = e.target.ancestor('.' + MOVEICON.cssclass, true), dragcontainer, draggroups; - if (e.keyCode == 27 ) { + if (draghandle === null) { + // The element clicked did not have a a draghandle in it's lineage. + return; + } + + if (e.keyCode === 27 ) { // Escape to cancel from anywhere. this.global_cancel_keyboard_drag(); e.preventDefault(); @@ -452,17 +458,19 @@ YUI.add('moodle-core-dragdrop', function(Y) { if (!draghandle.hasClass(MOVEICON.cssclass)) { return; } + // Do nothing if not space or enter. - if (e.keyCode != 13 && e.keyCode != 32) { + if (e.keyCode !== 13 && e.keyCode !== 32 && e.type !== 'click') { return; } + // Check the drag groups to see if we are the handler for this node. - draggroups = e.target.getAttribute('data-draggroups').split(' '); + draggroups = draghandle.getAttribute('data-draggroups').split(' '); var i, j, validgroup = false; for (i = 0; i < draggroups.length; i++) { for (j = 0; j < this.groups.length; j++) { - if (draggroups[i] == this.groups[j]) { + if (draggroups[i] === this.groups[j]) { validgroup = true; break; }