text: define(["jquery","core/dragdrop","qtype_ddmarker/shapes","core/key_codes"],function(a,b,c,d){"use strict";function e(a,b,c,d){this.containerId=a,this.visibleDropZones=d,c&&this.getRoot().addClass("qtype_ddmarker-readonly"),this.loadImage(b)}e.prototype.loadImage=function(a){var b=this;this.getRoot().find(".dropbackground").one("load",function(){b.visibleDropZones.length>0&&b.drawDropzones(),b.repositionDrags()}).attr("src",a).css({border:"1px solid #000","max-width":"none"})},e.prototype.drawDropzones=function(){var a=this.getRoot().find("img.dropbackground");this.getRoot().find("div.dropzones").html('<svg xmlns="http://www.w3.org/2000/svg" class="dropzones" width="'+a.outerWidth()+'" height="'+a.outerHeight()+'"></svg>');var b=this.getRoot().find("svg.dropzones");b.css("position","absolute");for(var c=0,d=0;d<this.visibleDropZones.length;d++){var e="color"+c;c=(c+1)%8,this.addDropzone(b,d,e)}},e.prototype.addDropzone=function(a,b,d){var e,f=this.visibleDropZones[b],g=c.make(f.shape,"");if(g.parse(f.coords)){if(e=this.getRoot().find("div.markertexts span.markertext"+b),e.length)""!==f.markertext?e.html(f.markertext):e.remove();else if(""!==f.markertext){var h="markertext markertext"+b;this.getRoot().find("div.markertexts").append('<span class="'+h+'">'+f.markertext+"</span>")}var i=g.makeSvg(a[0]);i.setAttribute("class","dropzone "+d)}},e.prototype.repositionDropZones=function(){var a=this.getRoot().find("svg.dropzones");if(0!==a.length){var b=this.convertToWindowXY(new c.Point((-1),0));a.offset({left:b.x,top:b.y});for(var d=0;d<this.visibleDropZones.length;d++){var e=this.getRoot().find("div.ddarea div.markertexts span.markertext"+d);if(0!==e.length){var f=this.visibleDropZones[d],g=c.make(f.shape,"");if(g.parse(f.coords)){var h=g.getHandlePositions(),i=this.convertToWindowXY(h.moveHandle.offset(-e.outerWidth()/2,-e.outerHeight()/2));e.offset({left:i.x-4,top:i.y})}}}}},e.prototype.repositionDrags=function(){var b=this.getRoot(),c=this;b.find("div.dragitems .dragitem").each(function(b,c){a(c).addClass("unneeded")}),b.find("input.choices").each(function(a,b){for(var d=c.getChoiceNoFromElement(b),e=c.getCoords(b),f=c.dragHome(d),g=0;g<e.length;g++){var h=c.dragItem(d,g);!h.length||h.hasClass("beingdragged")?h=c.cloneNewDragItem(f,g):h.removeClass("unneeded"),h.offset({left:e[g].x,top:e[g].y})}}),b.find("div.dragitems .dragitem").each(function(b,c){var d=a(c);d.hasClass("unneeded")&&!d.hasClass("beingdragged")&&d.remove()}),this.repositionDropZones();var d=this.bgImage(),e=d.offset();d.data("prev-top",e.top).data("prev-left",e.left)},e.prototype.getCoords=function(b){var d=this.getRoot(),e=this.getChoiceNoFromElement(b),f=Number(this.getClassnameNumericSuffix(b,"noofdrags")),g=d.find("span.dragitem.beingdragged.choice"+e).length>0,h=[],i=a(b).val();if(""!==i)for(var j=i.split(";"),k=0;k<j.length;k++)h[k]=this.convertToWindowXY(c.Point.parse(j[k]));var l=h.length+(g?1:0);return(a(b).hasClass("infinite")||l<f)&&(h[h.length]=this.dragHomeXY(e)),h},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();return a.x>0&&a.x<=b.width()&&a.y>0&&a.y<=b.height()},e.prototype.dragHomeXY=function(a){var b=this.dragHome(a);return new c.Point(b.offset().left,b.offset().top)},e.prototype.getRoot=function(){return a(document.getElementById(this.containerId))},e.prototype.bgImage=function(){return this.getRoot().find("img.dropbackground")},e.prototype.dragHome=function(a){return this.getRoot().find("div.dragitems span.draghome.choice"+a)},e.prototype.dragItem=function(a,b){return this.getRoot().find("div.dragitems span.dragitem.choice"+a+".item"+b)},e.prototype.cloneNewDragItem=function(a,b){var c=a.clone(!0);return c.removeClass("draghome").addClass("dragitem").addClass("item"+b),a.after(c),c.attr("tabIndex",0),c},e.prototype.handleDragStart=function(c){var d=this,e=a(c.target).closest(".dragitem"),f=b.prepare(c);f.start&&(e.addClass("beingdragged"),b.start(c,e,function(){},function(a,b,c){d.dragEnd(c)}))},e.prototype.dragEnd=function(a){a.removeClass("beingdragged");var b=this.getChoiceNoFromElement(a);this.saveCoordsForChoice(b,a),this.repositionDrags()},e.prototype.saveCoordsForChoice=function(a,b){for(var d,e=[],f=this.getRoot().find("span.dragitem.choice"+a).length,g=!0,h=0;h<=f;h++){var i=this.dragItem(a,h);0!==i.length&&(i.hasClass("beingdragged")||(d=this.convertToBgImgXY(new c.Point(i.offset().left,i.offset().top)),this.coordsInBgImg(d)&&(e[e.length]=d)),b&&0!==b.length&&b[0].innerText===i[0].innerText&&(g=!1))}g&&(d=this.convertToBgImgXY(new c.Point(b.offset().left,b.offset().top)),this.coordsInBgImg(d)&&(e[e.length]=d)),this.getRoot().find("input.choice"+a).val(e.join(";"))},e.prototype.handleKeyPress=function(b){var e=a(b.target).closest(".dragitem"),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(),f=null!==f?this.constrainToBgImg(f):this.dragHomeXY(g),e.offset({left:f.x,top:f.y}),this.saveCoordsForChoice(g,e),this.repositionDrags()},e.prototype.constrainToBgImg=function(a){var b=this.bgImage(),c=this.convertToBgImgXY(a);return 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),this.convertToWindowXY(c)},e.prototype.getChoiceNoFromElement=function(a){return Number(this.getClassnameNumericSuffix(a,"choice"))},e.prototype.getClassnameNumericSuffix=function(b,c){var d=a(b).attr("class");if(void 0!==d&&""!==d)for(var e=d.split(" "),f=0;f<e.length;f++){var g=new RegExp("^"+c+"([0-9])+$");if(g.test(e[f])){var h=new RegExp("([0-9])+$"),i=h.exec(e[f]);return Number(i[0])}}return null},e.prototype.handleResize=function(){this.repositionDrags()},e.prototype.fixLayoutIfBackgroundMoved=function(){var a=this.bgImage(),b=a.offset(),c=a.data("prev-top"),d=a.data("prev-left");void 0!==d&&void 0!==c&&(c===b.top&&d===b.left||this.repositionDrags())};var f={eventHandlersInitialised:!1,questions:{},init:function(a,b,c,d){f.questions[a]=new e(a,b,c,d),f.eventHandlersInitialised||(f.setupEventHandlers(),f.eventHandlersInitialised=!0)},setupEventHandlers:function(){a("body").on("mousedown touchstart",".que.ddmarker:not(.qtype_ddmarker-readonly) div.dragitems .dragitem",f.handleDragStart).on("keydown keypress",".que.ddmarker:not(.qtype_ddmarker-readonly) div.dragitems .dragitem",f.handleKeyPress),a(window).on("resize",f.handleWindowResize),setTimeout(f.fixLayoutIfThingsMoved,100)},handleDragStart:function(a){a.preventDefault();var b=f.getQuestionForEvent(a);b&&b.handleDragStart(a)},handleKeyPress:function(a){var b=f.getQuestionForEvent(a);b&&b.handleKeyPress(a)},handleWindowResize:function(){for(var a in f.questions)f.questions.hasOwnProperty(a)&&f.questions[a].handleResize()},fixLayoutIfThingsMoved:function(){for(var a in f.questions)f.questions.hasOwnProperty(a)&&f.questions[a].fixLayoutIfBackgroundMoved();setTimeout(f.fixLayoutIfThingsMoved,100)},getQuestionForEvent:function(b){var c=a(b.currentTarget).closest(".que.ddmarker").attr("id");return f.questions[c]}};return{init:f.init}});