Merge branch 'MDL-70114-310' of git://github.com/bmbrands/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / ddmarker / amd / build / form.min.js
1 define ("qtype_ddmarker/form",["jquery","core/dragdrop","qtype_ddmarker/shapes"],function(a,b,c){"use strict";function d(a){this.dropzoneNo=a;this.svgEl=null;this.shape=c.make(this.getShapeType(),this.getLabel());this.updateCoordinatesFromForm()}d.prototype.updateCoordinatesFromForm=function(a){var b=this.getCoordinates(),c="polygon"===this.shape.getType()&&this.shape.points.length;if(this.shape.getCoordinates()===b){return}if(!this.shape.parse(b,1)){return}if("polygon"===this.shape.getType()&&c!==this.shape.points.length){var d=this.isActive();this.removeFromSvg();if(a){this.addToSvg(a);if(d){this.setActive()}}}else{this.updateSvgEl()}this.setCoordinatesInForm()};d.prototype.updateLabel=function(){var a=this.getLabel();if(this.shape.label!==a){this.shape.label=a;this.updateSvgEl()}};d.prototype.changeShape=function(a){var b=this.getShapeType(),d=this.isActive();if(b===this.shape.getType()){return}this.removeFromSvg();this.shape=c.getSimilar(b,this.shape);if(a){this.addToSvg(a);if(d){this.setActive()}}this.setCoordinatesInForm()};d.prototype.addToSvg=function(a){if(null!==this.svgEl){throw new Error("this.svgEl already set")}this.svgEl=this.shape.makeSvg(a);if(!this.svgEl){return}this.svgEl.setAttribute("class","dropzone");this.svgEl.setAttribute("data-dropzone-no",this.dropzoneNo);var b=this.shape.getHandlePositions();if(null===b){return}var d=c.createSvgElement(this.svgEl,"circle");d.setAttribute("cx",b.moveHandle.x);d.setAttribute("cy",b.moveHandle.y);d.setAttribute("r",7);d.setAttribute("class","handle move");for(var e=0;e<b.editHandles.length;++e){this.makeEditHandle(e,b.editHandles[e])}};d.prototype.makeEditHandle=function(a,b){var d=c.createSvgElement(this.svgEl,"rect");d.setAttribute("x",b.x-6);d.setAttribute("y",b.y-6);d.setAttribute("width",11);d.setAttribute("height",11);d.setAttribute("class","handle edit");d.setAttribute("data-edit-handle-no",a)};d.prototype.removeFromSvg=function(){if(null!==this.svgEl){this.svgEl.parentNode.removeChild(this.svgEl);this.svgEl=null}};d.prototype.updateSvgEl=function(){if(null===this.svgEl){return}this.shape.updateSvg(this.svgEl);var a=this.shape.getHandlePositions();if(null===a){return}this.svgEl.childNodes[2].setAttribute("cx",a.moveHandle.x);this.svgEl.childNodes[2].setAttribute("cy",a.moveHandle.y);for(var b=0;b<a.editHandles.length;++b){this.svgEl.childNodes[3+b].setAttribute("x",a.editHandles[b].x-6);this.svgEl.childNodes[3+b].setAttribute("y",a.editHandles[b].y-6)}};d.prototype.isActive=function(){return null!==this.svgEl&&this.svgEl.getAttribute("class").match(/\bactive\b/)};d.prototype.setActive=function(){var a=this.svgEl.parentNode;a.removeChild(this.svgEl);a.appendChild(this.svgEl);this.svgEl.setAttribute("class",this.svgEl.getAttribute("class")+" active")};d.prototype.setCoordinatesInForm=function(){f.form.setFormValue("drops",[this.dropzoneNo,"coords"],this.shape.getCoordinates())};d.prototype.getCoordinates=function(){return f.form.getFormValue("drops",[this.dropzoneNo,"coords"]).replace(/\s*/g,"")};d.prototype.getChoiceNo=function(){return f.form.getFormValue("drops",[this.dropzoneNo,"choice"])};d.prototype.getLabel=function(){return f.form.getMarkerText(this.getChoiceNo())};d.prototype.getShapeType=function(){return f.form.getFormValue("drops",[this.dropzoneNo,"shape"])};d.prototype.handleMove=function(c){var d=b.prepare(c);if(!d.start){return}var e=this,f=d.x,g=d.y,h=this.makeDragProxy(d.x,d.y),i=a("fieldset#id_previewareaheader .dropbackground"),j=i.width(),k=i.height();b.start(c,a(h),function(a,b){e.shape.move(a-f,b-g,j,k);f=a;g=b;e.updateSvgEl();e.setCoordinatesInForm()},function(){document.body.removeChild(h)})};d.prototype.handleEdit=function(c,d,e){var f=b.prepare(c);if(!f.start){return}if("polygon"===this.shape.getType()&&(c.ctrlKey||c.metaKey)){this.shape.addNewPointAfter(d);this.removeFromSvg();this.addToSvg(e);this.setActive()}var g=this,h=f.x,i=f.y,j=this.makeDragProxy(f.x,f.y),k=a("fieldset#id_previewareaheader .dropbackground"),l=k.width(),m=k.height();b.start(c,a(j),function(a,b){g.shape.edit(d,a-h,b-i,l,m);h=a;i=b;g.updateSvgEl();g.setCoordinatesInForm()},function(){document.body.removeChild(j);g.shape.normalizeShape();g.updateSvgEl();g.setCoordinatesInForm()})};d.prototype.makeDragProxy=function(a,b){var c=document.createElement("div");c.style.position="absolute";c.style.top=b+"px";c.style.left=a+"px";c.style.width="1px";c.style.height="1px";document.body.appendChild(c);return c};var f={fp:null,noDropZones:null,dropZones:[],init:function init(){f.fp=f.filePickers();f.noDropZones=f.form.getFormValue("nodropzone",[]);f.setupPreviewArea();f.setOptionsForDragItemSelectors();f.createShapes();f.setupEventHandlers();f.waitForFilePickerToInitialise()},setupPreviewArea:function setupPreviewArea(){a("fieldset#id_previewareaheader div.fcontainer").append("<div class=\"ddarea que ddmarker\">   <div id=\"ddm-droparea\" class=\"droparea\">       <img class=\"dropbackground\" />       <div id=\"ddm-dropzone\" class=\"dropzones\">       </div>   </div></div>")},setOptionsForDragItemSelectors:function setOptionsForDragItemSelectors(){var b={0:""},c=f.form.getFormValue("noitems",[]),d=[],e,g,h;for(g=1;g<=c;g++){h=f.form.getMarkerText(g);if(""!==h){b[g]=a("<div/>").text(h).html()}}for(g=0;g<f.noDropZones;g++){e=a("#id_drops_"+g+"_choice");d[g]=+e.val()}for(g=0;g<f.noDropZones;g++){e=a("#id_drops_"+g+"_choice");e.find("option").remove();for(var j in b){j=+j;var l="<option value=\""+j+"\">"+b[j]+"</option>";e.append(l);var m=e.find("option[value=\""+j+"\"]");if(0===j){continue}if(j===d[g]){m.attr("selected",!0);continue}var n=f.form.getFormValue("drags",[j-1,"noofdrags"]);if(0===+n){continue}for(var o in d){if(+d[o]!==j){continue}if(1===+n){m.attr("disabled",!0);break}else{n--}}}if(0<f.dropZones.length){f.dropZones[g].updateLabel()}}},createShapes:function createShapes(){for(var a=0;a<f.noDropZones;a++){f.dropZones[a]=new d(a)}},setupEventHandlers:function setupEventHandlers(){a("fieldset#id_draggableitemheader").on("change input","input, select",function(){f.setOptionsForDragItemSelectors()});a("fieldset#id_dropzoneheader").on("change input","input, select",function(a){var b=a.currentTarget.name.match(/^drops\[(\d+)]\[([a-z]*)]$/);if(!b){return}var c=b[1],d=b[2],e=f.dropZones[c];switch(d){case"shape":e.changeShape(f.form.getSvg());break;case"coords":e.updateCoordinatesFromForm(f.form.getSvg());break;case"choice":e.updateLabel();break;}});var b=a("fieldset#id_previewareaheader");b.on("click","g.dropzone",function(b){var c=a(b.currentTarget).data("dropzone-no"),d=f.dropZones[c].isActive();a(f.form.getSvg()).find(".dropzone.active").removeClass("active");if(!d){f.dropZones[c].setActive()}});b.on("mousedown touchstart",".dropzone .handle.move",function(b){var c=a(b.currentTarget).closest("g").data("dropzoneNo");f.dropZones[c].handleMove(b)});b.on("mousedown touchstart",".dropzone .handle.edit",function(b){var c=a(b.currentTarget).closest("g").data("dropzoneNo"),d=b.currentTarget.getAttribute("data-edit-handle-no");f.dropZones[c].handleEdit(b,d,f.form.getSvg())})},waitForFilePickerToInitialise:function waitForFilePickerToInitialise(){if(null===f.fp.file("bgimage").href){setTimeout(f.waitForFilePickerToInitialise,1e3);return}a("form.mform").on("change","#id_bgimage",f.loadPreviewImage);f.loadPreviewImage()},loadPreviewImage:function loadPreviewImage(){a("fieldset#id_previewareaheader .dropbackground").one("load",f.afterPreviewImageLoaded).attr("src",f.fp.file("bgimage").href)},afterPreviewImageLoaded:function afterPreviewImageLoaded(){var b=a("fieldset#id_previewareaheader .dropbackground");a("#ddm-dropzone").css("position","relative").css("top",-1*(b.height()+1));a("#ddm-droparea").css("height",b.height()+20);f.updateSvgDisplay()},updateSvgDisplay:function updateSvgDisplay(){var b=a("fieldset#id_previewareaheader .dropbackground"),c;if(f.form.getSvg()){for(c=0;c<f.noDropZones;c++){f.dropZones[c].updateSvgEl()}}else{a("#ddm-dropzone").html("<svg xmlns=\"http://www.w3.org/2000/svg\" class=\"dropzones\" width=\""+b.outerWidth()+"\" height=\""+b.outerHeight()+"\"></svg>");for(c=0;c<f.noDropZones;c++){f.dropZones[c].addToSvg(f.form.getSvg())}}},form:{getMarkerText:function getMarkerText(a){if(0!==+a){var b=f.form.getFormValue("drags",[a-1,"label"]);return b.replace(/^\s*(.*)\s*$/,"$1")}else{return""}},getSvg:function getSvg(){var b=a("fieldset#id_previewareaheader svg");if(0===b.length){return null}else{return b[0]}},toNameWithIndex:function toNameWithIndex(a,b){for(var c=a,d=0;d<b.length;d++){c=c+"["+b[d]+"]"}return c},getEl:function getEl(b,c){var d=a("form.mform")[0];return d.elements[this.toNameWithIndex(b,c)]},getFormValue:function getFormValue(a,b){var c=this.getEl(a,b);if("checkbox"===c.type){return c.checked}else{return c.value}},setFormValue:function setFormValue(a,b,c){var d=this.getEl(a,b);if("checkbox"===d.type){d.checked=c}else{d.value=c}}},filePickers:function filePickers(){var b,c;if(b===void 0){b={};c={};a("form.mform input.filepickerhidden").each(function(a,d){b[d.value]=d.name;c[d.name]=d.parentNode})}return{file:function file(b){var d=a(c[b]).find("div.filepicker-filelist a");if(d.length){return{href:d.get(0).href,name:d.get(0).innerHTML}}else{return{href:null,name:null}}},name:function name(a){return b[a]}}}};return{init:f.init}});
2 //# sourceMappingURL=form.min.js.map