MDL-69104 Questions: Drag and drop question family don't work with iOS
[moodle.git] / question / type / ddimageortext / amd / build / question.min.js
1 define ("qtype_ddimageortext/question",["jquery","core/dragdrop","core/key_codes"],function(a,b,c){"use strict";function d(a,b,c){this.containerId=a;M.util.js_pending("qtype_ddimageortext-init-"+this.containerId);this.places=c;this.allImagesLoaded=!1;this.imageLoadingTimeoutId=null;this.isPrinting=!1;if(b){this.getRoot().addClass("qtype_ddimageortext-readonly")}var d=this;this.getNotYetLoadedImages().one("load",function(){d.waitForAllImagesToBeLoaded()});this.waitForAllImagesToBeLoaded()}d.prototype.waitForAllImagesToBeLoaded=function(){var a=this;if(this.allImagesLoaded){return}if(null!==this.imageLoadingTimeoutId){clearTimeout(this.imageLoadingTimeoutId)}if(0<this.getNotYetLoadedImages().length){this.imageLoadingTimeoutId=setTimeout(function(){a.waitForAllImagesToBeLoaded()},100);return}this.allImagesLoaded=!0;a.setupQuestion()};d.prototype.getNotYetLoadedImages=function(){var a=this;return this.getRoot().find(".ddarea img").not(function(b,c){return a.imageIsLoaded(c)})};d.prototype.imageIsLoaded=function(a){return a.complete&&0!==a.naturalHeight};d.prototype.setupQuestion=function(){this.resizeAllDragsAndDrops();this.cloneDrags();this.positionDragsAndDrops();M.util.js_complete("qtype_ddimageortext-init-"+this.containerId)};d.prototype.resizeAllDragsAndDrops=function(){var b=this;this.getRoot().find(".draghomes > div").each(function(c,d){b.resizeAllDragsAndDropsInGroup(b.getClassnameNumericSuffix(a(d),"dragitemgroup"))})};d.prototype.resizeAllDragsAndDropsInGroup=function(b){var c=this.getRoot(),d=c.find(".dragitemgroup"+b+" .draghome"),e=0,f=0;d.each(function(a,b){e=Math.max(e,Math.ceil(b.offsetWidth));f=Math.max(f,Math.ceil(b.offsetHeight))});e+=10;f+=10;d.each(function(b,c){var d=Math.round((e-c.offsetWidth)/2),g=Math.floor((f-c.offsetHeight)/2);a(c).css({"padding-left":d+"px","padding-right":e-c.offsetWidth-d+"px","padding-top":g+"px","padding-bottom":f-c.offsetHeight-g+"px"})});for(var g in this.places){if(!this.places.hasOwnProperty(g)){continue}var h=this.places[g],i=h.text;if(parseInt(h.group)!==b){continue}if(""===i){i=M.util.get_string("blank","qtype_ddimageortext")}c.find(".dropzones").append("<div class=\"dropzone active group"+h.group+" place"+g+"\" tabindex=\"0\"><span class=\"accesshide\">"+i+"</span>&nbsp;</div>");c.find(".dropzone.place"+g).width(e-2).height(f-2)}};d.prototype.cloneDrags=function(){var b=this;b.getRoot().find(".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.cloneDragsForOneChoice=function(a){if(a.hasClass("infinite")){for(var b=this.noOfDropsInGroup(this.getGroup(a)),c=0;c<b;c++){this.cloneDrag(a)}}else{this.cloneDrag(a)}};d.prototype.cloneDrag=function(a){var b=a.clone();b.removeClass("draghome").addClass("drag unplaced moodle-has-zindex").offset(a.offset());this.getRoot().find(".dragitems").append(b)};d.prototype.positionDragsAndDrops=function(){var b=this,c=this.getRoot(),d=this.bgRatio();c.find(".ddarea .dropzone").each(function(c,e){var f=a(e),g=b.places[b.getPlace(f)];f.css("left",parseInt(g.xy[0])*d).css("top",parseInt(g.xy[1])*d);f.data("originX",parseInt(g.xy[0])).data("originY",parseInt(g.xy[1]));b.handleElementScale(f,"left top")});c.find(".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();if(0===g.length||0<g.length&&"0"===g){return}var h=b.getPlace(f),i=b.getUnplacedChoice(b.getGroup(f),g),j=b.getDragClone(i);if(j.length){if(i.hasClass("infinite")){var k=b.noOfDropsInGroup(b.getGroup(i)),l=b.getInfiniteDragClones(i,!1);if(l.length<k){var m=i.clone();m.removeClass("beingdragged");m.removeAttr("tabindex");j.after(m)}else{j.addClass("active")}}else{j.addClass("active")}}var n=c.find(".dropzone.place"+h);b.sendDragToDrop(i,n)})};d.prototype.handleDragStart=function(c){var d=this,e=a(c.target).closest(".draghome"),g=this.calculateZIndex(),h=b.prepare(c);if(!h.start){return}e.addClass("beingdragged").css("transform","").css("z-index",g+2);var i=this.getClassnameNumericSuffix(e,"inplace");if(null!==i){this.setInputValue(i,0);e.removeClass("inplace"+i);var j=d.getDrop(e,i);if(j.length){j.addClass("active");e.offset(j.offset())}}else{var k=d.getDragClone(e);if(k.length){if(e.hasClass("infinite")){var l=this.noOfDropsInGroup(d.getGroup(e)),m=this.getInfiniteDragClones(e,!1);if(m.length<l){var n=e.clone();n.removeClass("beingdragged");n.removeAttr("tabindex");k.after(n);f.addEventHandlersToDrag(n);e.offset(n.offset())}else{k.addClass("active");e.offset(k.offset())}}else{k.addClass("active");e.offset(k.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(".dropzone.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(".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(".dropzone.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(".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){c.addClass("beingdragged");c.offset(c.offset());var d=this.getClassnameNumericSuffix(c,"inplace"),e=this.getDrop(c,d);e.addClass("active");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(".dropzone");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:f.isKeyboardNavigation=!1;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_ddimageortext-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_ddimageortext-dragmoved",[b,c,f]);M.util.js_complete("qtype_ddimageortext-animate-"+f.containerId)}})};d.prototype.isPointInDrop=function(a,b,c){var d=c.offset();if(c.hasClass("draghome")){return a>=d.left&&a<d.left+c.outerWidth()&&b>=d.top&&b<d.top+c.outerHeight()}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.bgImage=function(){return this.getRoot().find("img.dropbackground")};d.prototype.getDragHome=function(a,b){if(!this.getRoot().find(".draghome.dragplaceholder.group"+a+".choice"+b).is(":visible")){return this.getRoot().find(".dragitemgroup"+a+" .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(".ddarea .draghome.group"+a+".choice"+b+".unplaced").slice(0,1)};d.prototype.getCurrentDragInPlace=function(a){return this.getRoot().find(".ddarea .draghome.inplace"+a)};d.prototype.noOfDropsInGroup=function(a){return this.getRoot().find(".dropzone.group"+a).length};d.prototype.noOfChoicesInGroup=function(a){return this.getRoot().find(".dragitemgroup"+a+" .draghome").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(".dragitemgroup"+this.getGroup(a)+" .draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".dragplaceholder")};d.prototype.getInfiniteDragClones=function(a,b){if(b){return this.getRoot().find(".dragitemgroup"+this.getGroup(a)+" .draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".infinite").not(".dragplaceholder")}return this.getRoot().find(".draghome.choice"+this.getChoice(a)+".group"+this.getGroup(a)+".infinite").not(".dragplaceholder")};d.prototype.getDrop=function(a,b){return this.getRoot().find(".dropzone.group"+this.getGroup(a)+".place"+b)};d.prototype.handleResize=function(){var b=this,c=this.bgRatio();if(this.isPrinting){c=1}this.getRoot().find(".ddarea .dropzone").each(function(d,e){a(e).css("left",parseInt(a(e).data("originX"))*parseFloat(c)).css("top",parseInt(a(e).data("originY"))*parseFloat(c));b.handleElementScale(e,"left top")});this.getRoot().find("div.droparea .draghome").not(".beingdragged").each(function(d,e){a(e).css("left",parseFloat(a(e).data("originX"))*parseFloat(c)).css("top",parseFloat(a(e).data("originY"))*parseFloat(c));b.handleElementScale(e,"left top")})};d.prototype.bgRatio=function(){var a=this.bgImage(),b=a.get(0).naturalWidth,c=a.width();return c/b};d.prototype.handleElementScale=function(b,c){var d=parseFloat(this.bgRatio());if(this.isPrinting){d=1}a(b).css({"-webkit-transform":"scale("+d+")","-moz-transform":"scale("+d+")","-ms-transform":"scale("+d+")","-o-transform":"scale("+d+")",transform:"scale("+d+")","transform-origin":c})};d.prototype.calculateZIndex=function(){var b=0;this.getRoot().find(".ddarea .dropzone, div.droparea .draghome").each(function(c,d){d=a(d);var e=d.css("z-index")?parseInt(d.css("z-index")):0;if(e>b){b=e}});return b};var f={eventHandlersInitialised:!1,isPrinting:!1,isKeyboardNavigation:!1,questions:{},init:function init(a,b,c){f.questions[a]=new d(a,b,c);if(!f.eventHandlersInitialised){f.setupEventHandlers();f.eventHandlersInitialised=!0}},setupEventHandlers:function setupEventHandlers(){f.addEventHandlersToDrag(a(".que.ddimageortext:not(.qtype_ddimageortext-readonly) .draghome"));a("body").on("keydown",".que.ddimageortext:not(.qtype_ddimageortext-readonly) .dropzones .dropzone",f.handleKeyPress).on("keydown",".que.ddimageortext:not(.qtype_ddimageortext-readonly) .draghome.placed:not(.beingdragged)",f.handleKeyPress).on("qtype_ddimageortext-dragmoved",f.handleDragMoved);a(window).on("resize",function(){f.handleWindowResize(!1)});window.addEventListener("beforeprint",function(){f.isPrinting=!0;f.handleWindowResize(f.isPrinting)});window.addEventListener("afterprint",function(){f.isPrinting=!1;f.handleWindowResize(f.isPrinting)});setTimeout(function(){f.fixLayoutIfThingsMoved()},100)},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)}},handleWindowResize:function handleWindowResize(a){for(var b in f.questions){if(f.questions.hasOwnProperty(b)){f.questions[b].isPrinting=a;f.questions[b].handleResize()}}},fixLayoutIfThingsMoved:function fixLayoutIfThingsMoved(){this.handleWindowResize(f.isPrinting);setTimeout(function(){f.fixLayoutIfThingsMoved(f.isPrinting)},100)},handleDragMoved:function handleDragMoved(a,b,c,d){b.removeClass("beingdragged").css("z-index","");b.css("top",c.position().top).css("left",c.position().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");b.css("top","").css("left","").css("transform","");if(b.hasClass("infinite")&&1<d.getInfiniteDragClones(b,!0).length){d.getInfiniteDragClones(b,!0).first().remove()}}else{b.data("originX",c.data("originX")).data("originY",c.data("originY"));d.handleElementScale(b,"left top")}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}},getQuestionForEvent:function getQuestionForEvent(b){var c=a(b.currentTarget).closest(".que.ddimageortext").attr("id");return f.questions[c]}};return{init:f.init}});
2 //# sourceMappingURL=question.min.js.map