/
var
/
www
/
html
/
gte
/
question
/
type
/
ddimageortext
/
amd
/
build
/
Upload File
HOME
define(["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,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){if(null!==this.imageLoadingTimeoutId&&clearTimeout(this.imageLoadingTimeoutId),this.getNotYetLoadedImages().length>0)return void(this.imageLoadingTimeoutId=setTimeout(function(){a.waitForAllImagesToBeLoaded()},100));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)){var h=this.places[g],i=h.text;parseInt(h.group)===b&&(""===i&&(i=M.util.get_string("blank","qtype_ddimageortext")),c.find(".dropzones").append('<div class="dropzone group'+h.group+" place"+g+'" tabindex="0"><span class="accesshide">'+i+"</span> </div>"),c.find(".dropzone.place"+g).width(e-2).height(f-2))}},d.prototype.cloneDrags=function(){var b=this;this.getRoot().find(".ddarea .draghome").each(function(c,d){b.cloneDragsForOneChoice(a(d))})},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.bgImage().offset();c.find(".ddarea .dropzone").each(function(c,e){var f=a(e),g=b.places[b.getPlace(f)];f.offset({left:d.left+parseInt(g.xy[0]),top:d.top+parseInt(g.xy[1])})}),c.find(".ddarea .drag").each(function(c,d){var e=a(d),f=b.getClassnameNumericSuffix(e,"inplace");e.addClass("unplaced").removeClass("placed").offset(b.getDragHome(b.getGroup(e),b.getChoice(e)).offset()),null!==f&&e.removeClass("inplace"+f)}),c.find("input.placeinput").each(function(d,e){var f=a(e),g=f.val();if("0"!==g){var h=b.getPlace(f);b.getUnplacedChoice(b.getGroup(f),g).removeClass("unplaced").addClass("placed inplace"+h).offset(c.find(".dropzone.place"+h).offset())}}),this.bgImage().data("prev-top",d.top).data("prev-left",d.left)},d.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.positionDragsAndDrops())},d.prototype.handleDragStart=function(c){var d=this,e=a(c.target).closest(".drag"),f=b.prepare(c);if(f.start){var g=this.getClassnameNumericSuffix(e,"inplace");null!==g&&(this.setInputValue(g,0),e.removeClass("inplace"+g)),e.addClass("beingdragged"),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);e.isPointInDrop(b,c,g)?g.addClass("valid-drag-over-drop"):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);return!e.isPointInDrop(b,c,i)||(i.removeClass("valid-drag-over-drop"),e.sendDragToDrop(d,i),g=!0,!1)}),g||this.sendDragHome(d)},d.prototype.sendDragToDrop=function(a,b){var c=this.getCurrentDragInPlace(this.getPlace(b));0!==c.length&&this.sendDragHome(c),0===a.length?this.setInputValue(this.getPlace(b),0):(this.setInputValue(this.getPlace(b),this.getChoice(a)),a.removeClass("unplaced").addClass("placed inplace"+this.getPlace(b)),this.animateTo(a,b))},d.prototype.sendDragHome=function(a){a.removeClass("placed").addClass("unplaced");var b=this.getClassnameNumericSuffix(a,"inplace");null!==b&&a.removeClass("inplace"+b),this.animateTo(a,this.getDragHome(this.getGroup(a),this.getChoice(a)))},d.prototype.handleKeyPress=function(b){var d=a(b.target).closest(".dropzone"),e=this.getCurrentDragInPlace(this.getPlace(d)),f=a();switch(b.keyCode){case c.space:case c.arrowRight:case c.arrowDown:f=this.getNextDrag(this.getGroup(d),e);break;case c.arrowLeft:case c.arrowUp:f=this.getPreviousDrag(this.getGroup(d),e);break;case c.escape:break;default:return}b.preventDefault(),this.sendDragToDrop(f,d)},d.prototype.getNextDrag=function(a,b){var c,d=this.noOfChoicesInGroup(a);c=0===b.length?1:this.getChoice(b)+1;for(var e=this.getUnplacedChoice(a,c);0===e.length&&c<d;)c++,e=this.getUnplacedChoice(a,c);return e},d.prototype.getPreviousDrag=function(a,b){var c;c=0===b.length?this.noOfChoicesInGroup(a):this.getChoice(b)-1;for(var d=this.getUnplacedChoice(a,c);0===d.length&&c>1;)c--,d=this.getUnplacedChoice(a,c);return d},d.prototype.animateTo=function(a,b){var c=a.offset(),d=b.offset();a.addClass("beingdragged"),a.animate({left:parseInt(a.css("left"))+d.left-c.left,top:parseInt(a.css("top"))+d.top-c.top},{duration:"fast",done:function(){a.removeClass("beingdragged"),a.offset(d)}})},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.bgImage=function(){return this.getRoot().find("img.dropbackground")},d.prototype.getDragHome=function(a,b){return this.getRoot().find(".ddarea .draghome.group"+a+".choice"+b)},d.prototype.getUnplacedChoice=function(a,b){return this.getRoot().find(".ddarea .drag.group"+a+".choice"+b+".unplaced").slice(0,1)},d.prototype.getCurrentDragInPlace=function(a){return this.getRoot().find(".ddarea .drag.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;e<d.length;e++){var f=new RegExp("^"+b+"([0-9])+$");if(f.test(d[e])){var g=new RegExp("([0-9])+$"),h=g.exec(d[e]);return Number(h[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")};var e={eventHandlersInitialised:!1,questions:{},init:function(a,b,c){e.questions[a]=new d(a,b,c),e.eventHandlersInitialised||(e.setupEventHandlers(),e.eventHandlersInitialised=!0)},setupEventHandlers:function(){a("body").on("mousedown touchstart",".que.ddimageortext:not(.qtype_ddimageortext-readonly) .dragitems .drag",e.handleDragStart).on("keydown",".que.ddimageortext:not(.qtype_ddimageortext-readonly) .dropzones .dropzone",e.handleKeyPress),a(window).on("resize",e.handleWindowResize),setTimeout(e.fixLayoutIfThingsMoved,100)},handleDragStart:function(a){a.preventDefault();var b=e.getQuestionForEvent(a);b&&b.handleDragStart(a)},handleKeyPress:function(a){var b=e.getQuestionForEvent(a);b&&b.handleKeyPress(a)},handleWindowResize:function(){for(var a in e.questions)e.questions.hasOwnProperty(a)&&e.questions[a].positionDragsAndDrops()},fixLayoutIfThingsMoved:function(){for(var a in e.questions)e.questions.hasOwnProperty(a)&&e.questions[a].fixLayoutIfBackgroundMoved();setTimeout(e.fixLayoutIfThingsMoved,100)},getQuestionForEvent:function(b){var c=a(b.currentTarget).closest(".que.ddimageortext").attr("id");return e.questions[c]}};return{init:e.init}});