MDL-69104 Questions: Drag and drop question family don't work with iOS
[moodle.git] / question / type / ddmarker / amd / build / question.min.js
1 define ("qtype_ddmarker/question",["jquery","core/dragdrop","qtype_ddmarker/shapes","core/key_codes"],function(a,b,c,d){"use strict";function e(a,b,c){var d=this;this.containerId=a;this.visibleDropZones=c;this.shapes=[];this.shapeSVGs=[];this.isPrinting=!1;if(b){this.getRoot().addClass("qtype_ddmarker-readonly")}d.cloneDrags();d.repositionDrags();d.drawDropzones()}e.prototype.drawDropzones=function(){if(0<this.visibleDropZones.length){var a=this.bgImage();this.getRoot().find("div.dropzones").html("<svg xmlns=\"http://www.w3.org/2000/svg\" class=\"dropzones\" width=\""+a.outerWidth()+"\" height=\""+a.outerHeight()+"\"></svg>");for(var b=this.getRoot().find("svg.dropzones"),c=0,d=0,e;d<this.visibleDropZones.length;d++){e="color"+c;c=(c+1)%8;this.addDropzone(b,d,e)}}};e.prototype.addDropzone=function(a,b,d){var e=this.visibleDropZones[b],f=c.make(e.shape,""),g,h=this.bgRatio();if(!f.parse(e.coords,h)){return}g=this.getRoot().find("div.markertexts span.markertext"+b);if(g.length){if(""!==e.markertext){g.html(e.markertext)}else{g.remove()}}else if(""!==e.markertext){this.getRoot().find("div.markertexts").append("<span class=\""+("markertext markertext"+b)+"\">"+e.markertext+"</span>");var i=this.getRoot().find("div.ddarea div.markertexts span.markertext"+b);if(i.length){var j=f.getHandlePositions(),k=j.moveHandle.x-i.outerWidth()/2-4,l=j.moveHandle.y-i.outerHeight()/2;i.css("left",k).css("top",l);i.data("originX",i.position().left/h).data("originY",i.position().top/h);this.handleElementScale(i,"center")}}var m=f.makeSvg(a[0]);m.setAttribute("class","dropzone "+d);this.shapes[this.shapes.length]=f;this.shapeSVGs[this.shapeSVGs.length]=m};e.prototype.repositionDrags=function(){var b=this.getRoot(),c=this;b.find("div.draghomes .marker").not(".dragplaceholder").each(function(b,c){a(c).addClass("unneeded")});b.find("input.choices").each(function(a,b){var d=c.getChoiceNoFromElement(b),e=c.getCoords(b);if(e.length){var f=c.getRoot().find(".draghomes span.marker.choice"+d).not(".dragplaceholder");f.remove();for(var g=0,h;g<e.length;g++){h=f.clone();h.data("pagex",e[g].x).data("pagey",e[g].y);c.sendDragToDrop(h,!1)}c.getDragClone(f).addClass("active");c.cloneDragIfNeeded(f)}})};e.prototype.getCoords=function(b){var d=[],e=a(b).val();if(""!==e){for(var f=e.split(";"),g=0;g<f.length;g++){d[g]=this.convertToWindowXY(c.Point.parse(f[g]))}}return d};e.prototype.convertToWindowXY=function(a){var b=this.bgImage();return a.offset(b.offset().left+1,b.offset().top+1)};e.prototype.convertToBgImgXY=function(a){var b=this.bgImage();return a.offset(-b.offset().left-1,-b.offset().top-1)};e.prototype.coordsInBgImg=function(a){var b=this.bgImage(),c=b.offset();return a.x>=c.left&&a.x<c.left+b.width()&&a.y>=c.top&&a.y<c.top+b.height()};e.prototype.getRoot=function(){return a(document.getElementById(this.containerId))};e.prototype.bgImage=function(){return this.getRoot().find("img.dropbackground")};e.prototype.handleDragStart=function(c){var d=this,e=a(c.target).closest(".marker"),f=b.prepare(c);if(!f.start){return}e.addClass("beingdragged").css("transform","");var g=!e.hasClass("unneeded");if(!g){var h=d.getDragClone(e);if(h.length){h.addClass("active");e.offset(h.offset())}}b.start(c,e,function(){},function(a,b,c){d.dragEnd(c)})};e.prototype.dragEnd=function(a){var b=!1,d=this.getChoiceNoFromElement(a),e=this.bgRatio(),f;a.data("pagex",a.offset().left).data("pagey",a.offset().top);f=new c.Point(a.data("pagex"),a.data("pagey"));if(this.coordsInBgImg(f)){this.sendDragToDrop(a,!0);b=!0;var g=this.convertToBgImgXY(f);g=new c.Point(g.x/e,g.y/e);a.data("originX",g.x).data("originY",g.y)}if(!b){this.sendDragHome(a);this.removeDragIfNeeded(a)}else{this.cloneDragIfNeeded(a)}this.saveCoordsForChoice(d)};e.prototype.saveCoordsForChoice=function(b){var d=[],e=this.getRoot().find("div.droparea span.marker.choice"+b),f=this,g=this.bgRatio();if(e.length){e.each(function(){var b=a(this);if(!b.hasClass("beingdragged")){var e=new c.Point(b.data("pagex"),b.data("pagey"));if(f.coordsInBgImg(e)){var h=f.convertToBgImgXY(e);h=new c.Point(h.x/g,h.y/g);d[d.length]=h}}})}this.getRoot().find("input.choice"+b).val(d.join(";"))};e.prototype.handleKeyPress=function(b){var e=a(b.target).closest(".marker"),f=new c.Point(e.offset().left,e.offset().top),g=this.getChoiceNoFromElement(e);switch(b.keyCode){case d.arrowLeft:case 65:f.x-=1;break;case d.arrowRight:case 68:f.x+=1;break;case d.arrowDown:case 83:f.y+=1;break;case d.arrowUp:case 87:f.y-=1;break;case d.space:case d.escape:f=null;break;default:return;}b.preventDefault();if(null!==f){f=this.constrainToBgImg(f);e.offset({left:f.x,top:f.y});e.data("pagex",e.offset().left).data("pagey",e.offset().top);var h=this.convertToBgImgXY(new c.Point(e.data("pagex"),e.data("pagey")));e.data("originX",h.x/this.bgRatio()).data("originY",h.y/this.bgRatio());if(this.coordsInBgImg(new c.Point(e.offset().left,e.offset().top))){if(e.hasClass("unneeded")){this.sendDragToDrop(e,!0);var i=this.getDragClone(e);if(i.length){i.addClass("active")}this.cloneDragIfNeeded(e)}}}else{e.css("left","").css("top","");e.data("pagex",e.offset().left).data("pagey",e.offset().top);this.sendDragHome(e);this.removeDragIfNeeded(e)}e.focus();this.saveCoordsForChoice(g)};e.prototype.constrainToBgImg=function(a){var b=this.bgImage(),c=this.convertToBgImgXY(a);c.x=Math.max(0,c.x);c.y=Math.max(0,c.y);c.x=Math.min(b.width(),c.x);c.y=Math.min(b.height(),c.y);return this.convertToWindowXY(c)};e.prototype.getChoiceNoFromElement=function(a){return+this.getClassnameNumericSuffix(a,"choice")};e.prototype.getClassnameNumericSuffix=function(b,c){var d=a(b).attr("class");if(d!==void 0&&""!==d){for(var e=d.split(" "),f=0,g;f<e.length;f++){g=new RegExp("^"+c+"([0-9])+$");if(g.test(e[f])){var h=/([0-9])+$/.exec(e[f]);return+h[0]}}}return null};e.prototype.handleResize=function(){var b=this,c=this.bgRatio();if(this.isPrinting){c=1}this.getRoot().find("div.droparea .marker").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")});this.getRoot().find("div.droparea svg.dropzones").width(this.bgImage().width()).height(this.bgImage().height());for(var d=0;d<this.visibleDropZones.length;d++){var e=b.visibleDropZones[d],f=e.coords,g=b.shapes[d],h=b.shapeSVGs[d];g.parse(f,c);g.updateSvg(h);var i=g.getHandlePositions(),j=this.getRoot().find("div.ddarea div.markertexts span.markertext"+d);j.css("left",i.moveHandle.x-j.outerWidth()/2-4).css("top",i.moveHandle.y-j.outerHeight()/2);b.handleElementScale(j,"center")}};e.prototype.cloneDrags=function(){var b=this;this.getRoot().find("div.draghomes span.marker").each(function(c,d){var e=a(d),f=e.clone();f.removeClass();f.addClass("marker choice"+b.getChoiceNoFromElement(e)+" dragno"+b.getDragNo(e)+" dragplaceholder");e.before(f)})};e.prototype.getDragNo=function(a){return this.getClassnameNumericSuffix(a,"dragno")};e.prototype.getDragClone=function(a){return this.getRoot().find(".draghomes span.marker.choice"+this.getChoiceNoFromElement(a)+".dragno"+this.getDragNo(a)+".dragplaceholder")};e.prototype.dropArea=function(){return this.getRoot().find("div.droparea")};e.prototype.sendDragHome=function(a){a.removeClass("beingdragged").addClass("unneeded").css("top","").css("left","").css("transform","");var b=this.getDragClone(a);b.after(a);b.removeClass("active")};e.prototype.sendDragToDrop=function(a,b){var d=this.dropArea(),e=this.bgRatio();a.removeClass("beingdragged").removeClass("unneeded");var f=this.convertToBgImgXY(new c.Point(a.data("pagex"),a.data("pagey")));if(b){a.data("originX",f.x/e).data("originY",f.y/e);a.css("left",f.x).css("top",f.y)}else{a.data("originX",f.x).data("originY",f.y);a.css("left",f.x*e).css("top",f.y*e)}d.append(a);this.handleElementScale(a,"left top")};e.prototype.cloneDragIfNeeded=function(a){var b=this.getInput(a),c=+this.getClassnameNumericSuffix(b,"noofdrags"),d=this.getRoot().find("div.droparea .marker.choice"+this.getChoiceNoFromElement(a)+".dragno"+this.getDragNo(a)).length,e=this.getRoot().find("div.draghomes .marker.choice"+this.getChoiceNoFromElement(a)+".dragno"+this.getDragNo(a)).not(".dragplaceholder").length;if(d<c&&0===e){var g=a.clone();g.addClass("unneeded").css("top","").css("left","").css("transform","");this.getDragClone(a).removeClass("active").after(g);f.addEventHandlersToMarker(g)}};e.prototype.removeDragIfNeeded=function(a){var b=this.getRoot().find("div.draghomes .marker.choice"+this.getChoiceNoFromElement(a)+".dragno"+this.getDragNo(a)).not(".dragplaceholder").length;if(1<b){this.getRoot().find("div.draghomes .marker.choice"+this.getChoiceNoFromElement(a)+".dragno"+this.getDragNo(a)).not(".dragplaceholder").first().remove()}};e.prototype.getInput=function(a){var b=this.getChoiceNoFromElement(a);return this.getRoot().find("input.choices.choice"+b)};e.prototype.bgRatio=function(){var a=this.bgImage(),b=a.get(0).naturalWidth,c=a.width();return c/b};e.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})};var f={eventHandlersInitialised:!1,isPrinting:!1,isKeyboardNavigation:!1,questions:{},init:function init(a,b,c){f.questions[a]=new e(a,b,c);if(!f.eventHandlersInitialised){f.setupEventHandlers();f.eventHandlersInitialised=!0}},setupEventHandlers:function setupEventHandlers(){f.addEventHandlersToMarker(a(".que.ddmarker:not(.qtype_ddmarker-readonly) div.draghomes .marker"));f.addEventHandlersToMarker(a(".que.ddmarker:not(.qtype_ddmarker-readonly) div.droparea .marker"));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)},addEventHandlersToMarker:function addEventHandlersToMarker(a){a.on("mousedown touchstart",f.handleDragStart).on("keydown keypress",f.handleKeyPress).focusin(function(a){f.handleKeyboardFocus(a,!0)}).focusout(function(a){f.handleKeyboardFocus(a,!1)})},handleDragStart:function handleDragStart(a){a.preventDefault();var b=f.getQuestionForEvent(a);if(b){b.handleDragStart(a)}},handleKeyPress:function handleKeyPress(a){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()}}},handleKeyboardFocus:function handleKeyboardFocus(a,b){f.isKeyboardNavigation=b},fixLayoutIfThingsMoved:function fixLayoutIfThingsMoved(){if(!f.isKeyboardNavigation){this.handleWindowResize(f.isPrinting)}setTimeout(function(){f.fixLayoutIfThingsMoved(f.isPrinting)},100)},getQuestionForEvent:function getQuestionForEvent(b){var c=a(b.currentTarget).closest(".que.ddmarker").attr("id");return f.questions[c]}};return{init:f.init}});
2 //# sourceMappingURL=question.min.js.map