weekly release 4.0dev
[moodle.git] / question / type / ddimageortext / amd / build / form.min.js
CommitLineData
a03b08ab 1define ("qtype_ddimageortext/form",["jquery","core/dragdrop"],function(a,b){"use strict";var c={maxBgImageSize:null,maxDragImageSize:null,fp:null,init:function init(){c.fp=c.filePickers();a("#id_previewareaheader").append("<div class=\"ddarea que ddimageortext\"> <div class=\"droparea\"> <img class=\"dropbackground\" /> <div class=\"dropzones\"></div> </div> <div class=\"dragitems\"></div></div>");c.updateVisibilityOfFilePickers();c.setOptionsForDragItemSelectors();c.setupEventHandlers();c.waitForFilePickerToInitialise()},waitForFilePickerToInitialise:function waitForFilePickerToInitialise(){if(null===c.fp.file("bgimage").href){setTimeout(c.waitForFilePickerToInitialise,1e3);return}M.util.js_pending("dragDropToImageForm");a("form.mform[data-qtype=\"ddimageortext\"]").on("change",".filepickerhidden",function(){M.util.js_pending("dragDropToImageForm");c.loadPreviewImage()});c.loadPreviewImage()},loadPreviewImage:function loadPreviewImage(){a("fieldset#id_previewareaheader .dropbackground").one("load",c.afterPreviewImageLoaded).attr("src",c.fp.file("bgimage").href)},afterPreviewImageLoaded:function afterPreviewImageLoaded(){c.createDropZones();M.util.js_complete("dragDropToImageForm")},createDropZones:function createDropZones(){var b=a(".dropzones");b.empty();var d=c.fp.file("bgimage").href;if(null===d){return}for(var e=c.form.getFormValue("nodropzone",[]),f=0,g;f<e;f++){g=c.form.getFormValue("drops",[f,"choice"]);if("0"===g){continue}g=g-1;var h=c.form.getFormValue("drags",[g,"draggroup"]),i=c.form.getFormValue("draglabel",[g]);if("image"===c.form.getFormValue("drags",[g,"dragitemtype"])){var j=c.fp.file("dragitem["+g+"]").href;if(null===j){continue}b.append("<img class=\"droppreview group"+h+" drop"+f+"\" src=\""+j+"\" alt=\""+i+"\" data-drop-no=\""+f+"\">")}else if(""!==i){b.append("<div class=\"droppreview group"+h+" drop"+f+"\" data-drop-no=\""+f+"\">"+i+"</div>")}}c.waitForAllDropImagesToBeLoaded()},waitForAllDropImagesToBeLoaded:function waitForAllDropImagesToBeLoaded(){var b=a(".dropzones img").not(function(a,b){return c.imageIsLoaded(b)});if(0<b.length){setTimeout(function(){c.waitForAllDropImagesToBeLoaded()},100);return}c.updateDropZones()},imageIsLoaded:function imageIsLoaded(a){return a.complete&&0!==a.naturalHeight},updateDropZones:function updateDropZones(){var b=c.fp.file("bgimage").href;if(null===b){return}for(var d=a("fieldset#id_previewareaheader .dropbackground").offset(),e=c.form.getFormValue("nodropzone",[]),f=0,g;f<e;f++){g=a(".dropzones .drop"+f);if(0===g.length){continue}var h=c.form.getFormValue("drops",[f,"choice"])-1;g.offset({left:d.left+parseInt(c.form.getFormValue("drops",[f,"xleft"])),top:d.top+parseInt(c.form.getFormValue("drops",[f,"ytop"]))});var i=c.form.getFormValue("draglabel",[h]);if(g.is("img")){g.attr("alt",i)}else{g.html(i)}}a(".dropzones .droppreview").css("padding","0");for(var j=a(".draggroup select").first().find("option").length,k=1;k<=j;k++){c.resizeAllDragsAndDropsInGroup(k)}},resizeAllDragsAndDropsInGroup:function resizeAllDragsAndDropsInGroup(b){var c=a(".dropzones .droppreview.group"+b),d=0,e=0;c.each(function(a,b){d=Math.max(d,Math.ceil(b.offsetWidth));e=Math.max(e,Math.ceil(b.offsetHeight))});d+=10;e+=10;c.each(function(b,c){var f=Math.round((d-c.offsetWidth)/2),g=Math.floor((e-c.offsetHeight)/2);a(c).css({"padding-left":f+"px","padding-right":d-c.offsetWidth-f+"px","padding-top":g+"px","padding-bottom":e-c.offsetHeight-g+"px"})})},setupEventHandlers:function setupEventHandlers(){a("fieldset#id_draggableitemheader").on("change input","input, select",function(b){var d=a(b.target).closest("select, input");if(d.hasClass("dragitemtype")){c.updateVisibilityOfFilePickers()}c.setOptionsForDragItemSelectors();if(d.is(".dragitemtype, .draggroup")){c.createDropZones()}else if(d.is(".draglabel")){c.updateDropZones()}});a("fieldset#id_dropzoneheader").on("change input","input, select",function(b){var d=a(b.target).closest("select, input");if(d.is("select")){c.createDropZones()}else{c.updateDropZones()}});a("fieldset#id_previewareaheader").on("mousedown touchstart",".droppreview",function(a){c.dragStart(a)});a(window).on("resize",function(){c.updateDropZones()})},updateVisibilityOfFilePickers:function updateVisibilityOfFilePickers(){for(var b=c.form.getFormValue("noitems",[]),d=0,e;d<b;d++){e=a("input#id_dragitem_"+d).closest(".fitem_ffilepicker");if("image"===c.form.getFormValue("drags",[d,"dragitemtype"])){e.show()}else{e.hide()}}},setOptionsForDragItemSelectors:function setOptionsForDragItemSelectors(){for(var b={0:""},d=c.form.getFormValue("noitems",[]),e=c.form.getFormValue("nodropzone",[]),f=0;f<d;f++){var g=c.form.getFormValue("draglabel",[f]),h=c.fp.file(c.form.toNameWithIndex("dragitem",[f]));if("image"===c.form.getFormValue("drags",[f,"dragitemtype"])&&null!==h.name){b[f+1]=f+1+". "+g+" ("+h.name+")"}else if(""!==g){b[f+1]=f+1+". "+g}}for(var i=0;i<e;i++){var j=a("#id_drops_"+i+"_choice"),k=j.val();j.find("option").remove();for(var l in b){if(!b.hasOwnProperty(l)){continue}j.append("<option value=\""+l+"\">"+b[l]+"</option>");var m=j.find("option[value=\""+l+"\"]");if(parseInt(l)===parseInt(k)){m.attr("selected",!0)}else if(c.isItemUsed(parseInt(l))){m.attr("disabled",!0)}}}},isItemUsed:function isItemUsed(b){if(0===b){return!1}if(c.form.getFormValue("drags",[b-1,"infinite"])){return!1}return 0!==a("fieldset#id_dropzoneheader select").filter(function(c,d){return parseInt(a(d).val())===b}).length},dragStart:function dragStart(d){var e=a(d.target).closest(".droppreview"),f=b.prepare(d);if(!f.start){return}b.start(d,e,function(a,b,d){c.dragMove(d)},function(){c.dragEnd()})},dragMove:function dragMove(b){var d=a("fieldset#id_previewareaheader .dropbackground"),e=d.offset(),f=b.data("dropNo"),g=b.offset(),h=Math.round(g.left-e.left),i=Math.round(g.top-e.top);h=Math.round(Math.max(0,Math.min(h,d.outerWidth()-b.outerWidth())));i=Math.round(Math.max(0,Math.min(i,d.outerHeight()-b.outerHeight())));c.form.setFormValue("drops",[f,"xleft"],h);c.form.setFormValue("drops",[f,"ytop"],i)},dragEnd:function dragEnd(){c.updateDropZones()},form:{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[data-qtype=\"ddimageortext\"]")[0];return d.elements[this.toNameWithIndex(b,c)]},getFormValue:function getFormValue(a,b){var c=this.getEl(a,b);if(!c.type){c=c[c.length-1]}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={};var d=a("form.mform[data-qtype=\"ddimageortext\"] input.filepickerhidden");d.each(function(a,d){b[d.value]=d.name;c[d.name]=d.parentNode})}return{file:function file(b){var d=a(c[b]),e=d.find("div.filepicker-filelist a");if(e.length){return{href:e.get(0).href,name:e.get(0).innerHTML}}else{return{href:null,name:null}}},name:function name(a){return b[a]}}}};return{init:c.init}});
195ea448 2//# sourceMappingURL=form.min.js.map