MDL-69104 Questions: Drag and drop question family don't work with iOS
[moodle.git] / question / type / ddwtos / amd / build / ddwtos.min.js
CommitLineData
5fa8f1c8 1define ("qtype_ddwtos/ddwtos",["jquery","core/dragdrop","core/key_codes"],function(a,b,c){"use strict";function d(a,b){this.containerId=a;if(b){this.getRoot().addClass("qtype_ddwtos-readonly")}this.resizeAllDragsAndDrops();this.cloneDrags();this.positionDrags()}d.prototype.resizeAllDragsAndDrops=function(){var b=this;this.getRoot().find(".answercontainer > div").each(function(c,d){b.resizeAllDragsAndDropsInGroup(b.getClassnameNumericSuffix(a(d),"draggrouphomes"))})};d.prototype.resizeAllDragsAndDropsInGroup=function(a){var b=this,c=this.getRoot().find(".draggrouphomes"+a+" span.draghome"),d=0,e=0;c.each(function(a,b){d=Math.max(d,Math.ceil(b.offsetWidth));e=Math.max(e,Math.ceil(0+b.offsetHeight))});d+=8;e+=2;c.each(function(a,c){b.setElementSize(c,d,e)});this.getRoot().find("span.drop.group"+a).each(function(a,c){b.setElementSize(c,d,e)})};d.prototype.setElementSize=function(b,c,d){a(b).width(c).height(d).css("lineHeight",d+"px")};d.prototype.cloneDrags=function(){var b=this;b.getRoot().find("span.draghome").each(function(c,d){var e=a(d),f=e.clone();f.removeClass();f.addClass("draghome choice"+b.getChoice(e)+" group"+b.getGroup(e)+" dragplaceholder");e.before(f)})};d.prototype.positionDrags=function(){var b=this,c=this.getRoot();c.find("span.draghome").not(".dragplaceholder").each(function(c,d){var e=a(d),f=b.getClassnameNumericSuffix(e,"inplace");e.addClass("unplaced").removeClass("placed");e.removeAttr("tabindex");if(null!==f){e.removeClass("inplace"+f)}});c.find("input.placeinput").each(function(d,e){var f=a(e),g=f.val(),h=b.getPlace(f),i=c.find(".drop.place"+h),j=i.offset();i.data("prev-top",j.top).data("prev-left",j.left);if("0"===g){return}var k=b.getUnplacedChoice(b.getGroup(f),g),l=b.getDragClone(k);if(l.length){l.addClass("active")}b.sendDragToDrop(b.getUnplacedChoice(b.getGroup(f),g),i)})};d.prototype.handleDragStart=function(c){var d=this,e=a(c.target).closest(".draghome"),g=b.prepare(c);if(!g.start){return}e.addClass("beingdragged");var h=this.getClassnameNumericSuffix(e,"inplace");if(null!==h){this.setInputValue(h,0);e.removeClass("inplace"+h);var i=d.getDrop(e,h);if(i.length){i.addClass("active");e.offset(i.offset())}}else{var j=d.getDragClone(e);if(j.length){if(e.hasClass("infinite")){var k=this.noOfDropsInGroup(this.getGroup(e)),l=this.getInfiniteDragClones(e,!1);if(l.length<k){var m=e.clone();m.removeClass("beingdragged");j.after(m);f.addEventHandlersToDrag(m);e.offset(m.offset())}else{j.addClass("active");e.offset(j.offset())}}else{j.addClass("active");e.offset(j.offset())}}}b.start(c,e,function(a,b,c){d.dragMove(a,b,c)},function(a,b,c){d.dragEnd(a,b,c)})};d.prototype.dragMove=function(b,c,d){var e=this;this.getRoot().find("span.drop.group"+this.getGroup(d)).each(function(d,f){var g=a(f);if(e.isPointInDrop(b,c,g)){g.addClass("valid-drag-over-drop")}else{g.removeClass("valid-drag-over-drop")}});this.getRoot().find("span.draghome.placed.group"+this.getGroup(d)).not(".beingdragged").each(function(d,f){var g=a(f);if(e.isPointInDrop(b,c,g)){g.addClass("valid-drag-over-drop")}else{g.removeClass("valid-drag-over-drop")}})};d.prototype.dragEnd=function(b,c,d){var e=this,f=this.getRoot(),g=!1;f.find("span.drop.group"+this.getGroup(d)).each(function(f,h){var i=a(h);if(!e.isPointInDrop(b,c,i)){return!0}i.removeClass("valid-drag-over-drop");e.sendDragToDrop(d,i);g=!0;return!1});f.find("span.draghome.placed.group"+this.getGroup(d)).not(".beingdragged").each(function(f,h){var i=a(h);if(!e.isPointInDrop(b,c,i)){return!0}i.removeClass("valid-drag-over-drop");var j=e.getClassnameNumericSuffix(i,"inplace"),k=e.getDrop(d,j);e.sendDragToDrop(d,k);g=!0;return!1});if(!g){this.sendDragHome(d)}};d.prototype.sendDragToDrop=function(a,b){var c=this.getCurrentDragInPlace(this.getPlace(b));if(0!==c.length){var d=this.getClassnameNumericSuffix(c,"inplace"),e=this.getDrop(c,d);e.addClass("active");c.addClass("beingdragged");c.offset(e.offset());this.sendDragHome(c)}if(0===a.length){this.setInputValue(this.getPlace(b),0);if(b.data("isfocus")){b.focus()}}else{this.setInputValue(this.getPlace(b),this.getChoice(a));a.removeClass("unplaced").addClass("placed inplace"+this.getPlace(b));a.attr("tabindex",0);this.animateTo(a,b)}};d.prototype.sendDragHome=function(a){var b=this.getClassnameNumericSuffix(a,"inplace");if(null!==b){a.removeClass("inplace"+b)}a.data("unplaced",!0);this.animateTo(a,this.getDragHome(this.getGroup(a),this.getChoice(a)))};d.prototype.handleKeyPress=function(b){var d=a(b.target).closest(".drop");if(0===d.length){var e=a(b.target),g=this.getClassnameNumericSuffix(e,"inplace");if(null!==g){d=this.getDrop(e,g)}}var h=this.getCurrentDragInPlace(this.getPlace(d)),i=a();switch(b.keyCode){case c.space:case c.arrowRight:case c.arrowDown:i=this.getNextDrag(this.getGroup(d),h);break;case c.arrowLeft:case c.arrowUp:i=this.getPreviousDrag(this.getGroup(d),h);break;case c.escape:break;default:f.isKeyboardNavigation=!1;return;}if(i.length){i.data("isfocus",!0);i.addClass("beingdragged");var j=this.getDragClone(i);if(j.length){if(i.hasClass("infinite")){var k=this.noOfDropsInGroup(this.getGroup(i)),l=this.getInfiniteDragClones(i,!1);if(l.length<k){var m=i.clone();m.removeClass("beingdragged");m.removeAttr("tabindex");j.after(m);f.addEventHandlersToDrag(m);i.offset(m.offset())}else{j.addClass("active");i.offset(j.offset())}}else{j.addClass("active");i.offset(j.offset())}}}else{d.data("isfocus",!0)}b.preventDefault();this.sendDragToDrop(i,d)};d.prototype.getNextDrag=function(a,b){var c,d=this.noOfChoicesInGroup(a);if(0===b.length){c=1}else{c=this.getChoice(b)+1}var e=this.getUnplacedChoice(a,c);while(0===e.length&&c<d){c++;e=this.getUnplacedChoice(a,c)}return e};d.prototype.getPreviousDrag=function(a,b){var c;if(0===b.length){c=this.noOfChoicesInGroup(a)}else{c=this.getChoice(b)-1}var d=this.getUnplacedChoice(a,c);while(0===d.length&&1<c){c--;d=this.getUnplacedChoice(a,c)}return d};d.prototype.animateTo=function(b,c){var d=b.offset(),e=c.offset(),f=this;M.util.js_pending("qtype_ddwtos-animate-"+f.containerId);b.animate({left:parseInt(b.css("left"))+e.left-d.left,top:parseInt(b.css("top"))+e.top-d.top},{duration:"fast",done:function done(){a("body").trigger("qtype_ddwtos-dragmoved",[b,c,f]);M.util.js_complete("qtype_ddwtos-animate-"+f.containerId)}})};d.prototype.isPointInDrop=function(a,b,c){var d=c.offset();return a>=d.left&&a<d.left+c.width()&&b>=d.top&&b<d.top+c.height()};d.prototype.setInputValue=function(a,b){this.getRoot().find("input.placeinput.place"+a).val(b)};d.prototype.getRoot=function(){return a(document.getElementById(this.containerId))};d.prototype.getDragHome=function(a,b){if(!this.getRoot().find(".draghome.dragplaceholder.group"+a+".choice"+b).is(":visible")){return this.getRoot().find(".draggrouphomes"+a+" span.draghome.infinite.choice"+b+".group"+a)}return this.getRoot().find(".draghome.dragplaceholder.group"+a+".choice"+b)};d.prototype.getUnplacedChoice=function(a,b){return this.getRoot().find(".draghome.group"+a+".choice"+b+".unplaced").slice(0,1)};d.prototype.getCurrentDragInPlace=function(a){return this.getRoot().find("span.draghome.inplace"+a)};d.prototype.noOfDropsInGroup=function(a){return this.getRoot().find(".drop.group"+a).length};d.prototype.noOfChoicesInGroup=function(a){return this.getRoot().find(".draghome.group"+a).length};d.prototype.getClassnameNumericSuffix=function(a,b){var c=a.attr("class");if(""!==c){for(var d=c.split(" "),e=0,f;e<d.length;e++){f=new RegExp("^"+b+"([0-9])+$");if(f.test(d[e])){var g=/([0-9])+$/.exec(d[e]);return+g[0]}}}return null};d.prototype.getChoice=function(a){return this.getClassnameNumericSuffix(a,"choice")};d.prototype.getGroup=function(a){return this.getClassnameNumericSuffix(a,"group")};d.prototype.getPlace=function(a){return this.getClassnameNumericSuffix(a,"place")};d.prototype.getDragClone=function(a){return this.getRoot().find(".draggrouphomes"+this.getGroup(a)+" span.draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".dragplaceholder")};d.prototype.getInfiniteDragClones=function(a,b){if(b){return this.getRoot().find(".draggrouphomes"+this.getGroup(a)+" span.draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".infinite").not(".dragplaceholder")}return this.getRoot().find("span.draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".infinite").not(".dragplaceholder")};d.prototype.getDrop=function(a,b){return this.getRoot().find(".drop.group"+this.getGroup(a)+".place"+b)};var f={eventHandlersInitialised:!1,isKeyboardNavigation:!1,questions:{},init:function init(a,b){f.questions[a]=new d(a,b);if(!f.eventHandlersInitialised){f.setupEventHandlers();f.eventHandlersInitialised=!0}},setupEventHandlers:function setupEventHandlers(){f.addEventHandlersToDrag(a(".que.ddwtos:not(.qtype_ddwtos-readonly) span.draghome"));a("body").on("keydown",".que.ddwtos:not(.qtype_ddwtos-readonly) span.drop",f.handleKeyPress).on("keydown",".que.ddwtos:not(.qtype_ddwtos-readonly) span.draghome.placed:not(.beingdragged)",f.handleKeyPress).on("qtype_ddwtos-dragmoved",f.handleDragMoved)},addEventHandlersToDrag:function addEventHandlersToDrag(a){a.on("mousedown touchstart",f.handleDragStart)},handleDragStart:function handleDragStart(a){a.preventDefault();var b=f.getQuestionForEvent(a);if(b){b.handleDragStart(a)}},handleKeyPress:function handleKeyPress(a){if(f.isKeyboardNavigation){return}f.isKeyboardNavigation=!0;var b=f.getQuestionForEvent(a);if(b){b.handleKeyPress(a)}},getQuestionForEvent:function getQuestionForEvent(b){var c=a(b.currentTarget).closest(".que.ddwtos").attr("id");return f.questions[c]},handleDragMoved:function handleDragMoved(a,b,c,d){b.removeClass("beingdragged");b.css("top","").css("left","");c.after(b);c.removeClass("active");if("undefined"!=typeof b.data("unplaced")&&!0===b.data("unplaced")){b.removeClass("placed").addClass("unplaced");b.removeAttr("tabindex");b.removeData("unplaced");if(b.hasClass("infinite")&&1<d.getInfiniteDragClones(b,!0).length){d.getInfiniteDragClones(b,!0).first().remove()}}if("undefined"!=typeof b.data("isfocus")&&!0===b.data("isfocus")){b.focus();b.removeData("isfocus")}if("undefined"!=typeof c.data("isfocus")&&!0===c.data("isfocus")){c.removeData("isfocus")}if(f.isKeyboardNavigation){f.isKeyboardNavigation=!1}}};return{init:f.init}});
195ea448 2//# sourceMappingURL=ddwtos.min.js.map