Merge branch 'MDL-66601-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / editor / atto / plugins / image / yui / build / button / js / button.min.js
1 var CSS={RESPONSIVE:"img-responsive",INPUTALIGNMENT:"atto_image_alignment",INPUTALT:"atto_image_altentry",INPUTHEIGHT:"atto_image_heightentry",INPUTSUBMIT:"atto_image_urlentrysubmit",INPUTURL:"atto_image_urlentry",INPUTSIZE:"atto_image_size",INPUTWIDTH:"atto_image_widthentry",IMAGEALTWARNING:"atto_image_altwarning",IMAGEBROWSER:"openimagebrowser",IMAGEPRESENTATION:"atto_image_presentation",INPUTCONSTRAIN:"atto_image_constrain",INPUTCUSTOMSTYLE:"atto_image_customstyle",IMAGEPREVIEW:"atto_image_preview",IMAGEPREVIEWBOX:"atto_image_preview_box",ALIGNSETTINGS:"atto_image_button"},SELECTORS={INPUTURL:"."+CSS.INPUTURL},ALIGNMENTS=[{name:"verticalAlign",str:"alignment_top",value:"text-top",margin:"0 0.5em"},{name:"verticalAlign",str:"alignment_middle",value:"middle",margin:"0 0.5em"},{name:"verticalAlign",str:"alignment_bottom",value:"text-bottom",margin:"0 0.5em",isDefault:!0},{name:"float",str:"alignment_left",value:"left",margin:"0 0.5em 0 0"},{name:"float",str:"alignment_right",value:"right",margin:"0 0 0 0.5em"}],REGEX={ISPERCENT:/\d+%/},COMPONENTNAME="atto_image",TEMPLATE="<form class=\"atto_form\">{{#if showFilepicker}}<div class=\"mb-1\"><label for=\"{{elementid}}_{{CSS.INPUTURL}}\">{{get_string \"enterurl\" component}}</label><div class=\"input-group input-append w-100\"><input class=\"form-control {{CSS.INPUTURL}}\" type=\"url\" id=\"{{elementid}}_{{CSS.INPUTURL}}\" size=\"32\"/><span class=\"input-group-append\"><button class=\"btn btn-secondary {{CSS.IMAGEBROWSER}}\" type=\"button\">{{get_string \"browserepositories\" component}}</button></span></div></div>{{else}}<div class=\"mb-1\"><label for=\"{{elementid}}_{{CSS.INPUTURL}}\">{{get_string \"enterurl\" component}}</label><input class=\"form-control fullwidth {{CSS.INPUTURL}}\" type=\"url\" id=\"{{elementid}}_{{CSS.INPUTURL}}\" size=\"32\"/></div>{{/if}}<div style=\"display:none\" role=\"alert\" class=\"alert alert-warning mb-1 {{CSS.IMAGEALTWARNING}}\">{{get_string \"presentationoraltrequired\" component}}</div><div class=\"mb-1\"><label for=\"{{elementid}}_{{CSS.INPUTALT}}\">{{get_string \"enteralt\" component}}</label><textarea class=\"form-control fullwidth {{CSS.INPUTALT}}\" id=\"{{elementid}}_{{CSS.INPUTALT}}\" maxlength=\"125\"></textarea><div id=\"the-count\" class=\"d-flex justify-content-end small\"><span id=\"currentcount\">0</span><span id=\"maximumcount\"> / 125</span></div><div class=\"form-check\"><input type=\"checkbox\" class=\"form-check-input {{CSS.IMAGEPRESENTATION}}\" id=\"{{elementid}}_{{CSS.IMAGEPRESENTATION}}\"/><label class=\"form-check-label\" for=\"{{elementid}}_{{CSS.IMAGEPRESENTATION}}\">{{get_string \"presentation\" component}}</label></div></div><div class=\"mb-1\"><label class=\"\" for=\"{{elementid}}_{{CSS.INPUTSIZE}}\">{{get_string \"size\" component}}</label><div id=\"{{elementid}}_{{CSS.INPUTSIZE}}\" class=\"form-inline {{CSS.INPUTSIZE}}\"><label class=\"accesshide\" for=\"{{elementid}}_{{CSS.INPUTWIDTH}}\">{{get_string \"width\" component}}</label><input type=\"text\" class=\"form-control mr-1 input-mini {{CSS.INPUTWIDTH}}\" id=\"{{elementid}}_{{CSS.INPUTWIDTH}}\" size=\"4\"/> x<label class=\"accesshide\" for=\"{{elementid}}_{{CSS.INPUTHEIGHT}}\">{{get_string \"height\" component}}</label><input type=\"text\" class=\"form-control ml-1 input-mini {{CSS.INPUTHEIGHT}}\" id=\"{{elementid}}_{{CSS.INPUTHEIGHT}}\" size=\"4\"/><div class=\"form-check ml-2\"><input type=\"checkbox\" class=\"form-check-input {{CSS.INPUTCONSTRAIN}}\" id=\"{{elementid}}_{{CSS.INPUTCONSTRAIN}}\"/><label class=\"form-check-label\" for=\"{{elementid}}_{{CSS.INPUTCONSTRAIN}}\">{{get_string \"constrain\" component}}</label></div></div></div><div class=\"form-inline mb-1\"><label class=\"for=\"{{elementid}}_{{CSS.INPUTALIGNMENT}}\">{{get_string \"alignment\" component}}</label><select class=\"custom-select {{CSS.INPUTALIGNMENT}}\" id=\"{{elementid}}_{{CSS.INPUTALIGNMENT}}\">{{#each alignments}}<option value=\"{{value}}\">{{get_string str ../component}}</option>{{/each}}</select></div><input type=\"hidden\" class=\"{{CSS.INPUTCUSTOMSTYLE}}\"/><br/><div class=\"mdl-align\"><div class=\"{{CSS.IMAGEPREVIEWBOX}}\"><img src=\"#\" class=\"{{CSS.IMAGEPREVIEW}}\" alt=\"\" style=\"display: none;\"/></div><button class=\"btn btn-secondary {{CSS.INPUTSUBMIT}}\" type=\"submit\">{{get_string \"saveimage\" component}}</button></div></form>",IMAGETEMPLATE="<img src=\"{{url}}\" alt=\"{{alt}}\" {{#if width}}width=\"{{width}}\" {{/if}}{{#if height}}height=\"{{height}}\" {{/if}}{{#if presentation}}role=\"presentation\" {{/if}}{{#if customstyle}}style=\"{{customstyle}}\" {{/if}}{{#if classlist}}class=\"{{classlist}}\" {{/if}}{{#if id}}id=\"{{id}}\" {{/if}}/>";Y.namespace("M.atto_image").Button=Y.Base.create("button",Y.M.editor_atto.EditorPlugin,[],{_currentSelection:null,_selectedImage:null,_form:null,_rawImageDimensions:null,initializer:function initializer(){this.addButton({icon:"e/insert_edit_image",callback:this._displayDialogue,tags:"img",tagMatchRequiresAll:!1});this.editor.delegate("dblclick",this._displayDialogue,"img",this);this.editor.delegate("click",this._handleClick,"img",this);this.editor.on("paste",this._handlePaste,this);this.editor.on("drop",this._handleDragDrop,this);this.editor.on("dragover",function(a){a.preventDefault()},this);this.editor.on("dragenter",function(a){a.preventDefault()},this)},_handleDragDrop:function _handleDragDrop(a){if(!a._event||!a._event.dataTransfer){return!0}return this._handlePasteOrDropHelper(a,a._event.dataTransfer)},_handlePaste:function _handlePaste(a){if(!a._event||!a._event.clipboardData){return!0}return this._handlePasteOrDropHelper(a,a._event.clipboardData)},_handlePasteOrDropHelper:function _handlePasteOrDropHelper(a,b){for(var c=b.items,d=!1,e=0,f;e<c.length;e++){f=c[e];if("file"!==f.kind){continue}if(!this._isImage(f.type)){continue}this._uploadImage(f.getAsFile());d=!0}if(d){a.preventDefault();a.stopPropagation();return!1}else{return!0}},_isImage:function _isImage(a){return 0===a.indexOf("image/")},_uploadImage:function _uploadImage(a){var b=this,c=this.get("host"),d=Y.Handlebars.compile(IMAGETEMPLATE);c.saveSelection();var e=c.get("filepickeroptions").image,f=e.savepath===void 0?"/":e.savepath,g=new FormData,h=0,j="",k=new XMLHttpRequest,l="",m=Object.keys(e.repositories);g.append("repo_upload_file",a);g.append("itemid",e.itemid);for(var n=0;n<m.length;n++){if("upload"===e.repositories[m[n]].type){g.append("repo_id",e.repositories[m[n]].id);break}}g.append("env",e.env);g.append("sesskey",M.cfg.sesskey);g.append("client_id",e.client_id);g.append("savepath",f);g.append("ctx_id",e.context.id);h=new Date().getTime();j="moodleimage_"+Math.round(1e5*Math.random())+"-"+h;c.focus();c.restoreSelection();l=d({url:M.util.image_url("i/loading_small","moodle"),alt:M.util.get_string("uploading",COMPONENTNAME),id:j});c.insertContentAtFocusPoint(l);b.markUpdated();k.onreadystatechange=function(){var a=b.editor.one("#"+j),c,e,f,g;if(4===k.readyState){if(200===k.status){c=JSON.parse(k.responseText);if(c){if(c.error){if(a){a.remove(!0)}throw new M.core.ajaxException(c)}e=c;if(c.event&&"fileexists"===c.event){e=c.newfile}f=d({url:e.url,presentation:!0});g=Y.Node.create(f);if(a){a.replace(g)}else{b.editor.appendChild(g)}b.markUpdated()}}else{Y.use("moodle-core-notification-alert",function(){new M.core.alert({message:M.util.get_string("servererror","moodle")})});if(a){a.remove(!0)}}}};k.open("POST",M.cfg.wwwroot+"/repository/repository_ajax.php?action=upload",!0);k.send(g)},_handleClick:function _handleClick(a){var b=a.target,c=this.get("host").getSelectionFromNode(b);if(this.get("host").getSelection()!==c){this.get("host").setSelection(c)}},_displayDialogue:function _displayDialogue(){this._currentSelection=this.get("host").getSelection();if(!1===this._currentSelection){return}this._rawImageDimensions=null;var a=this.getDialogue({headerContent:M.util.get_string("imageproperties",COMPONENTNAME),width:"auto",focusAfterHide:!0,focusOnShowSelector:SELECTORS.INPUTURL});a.get("boundingBox").setStyle("maxWidth","90%");a.set("bodyContent",this._getDialogueContent()).show()},_loadPreviewImage:function _loadPreviewImage(a){var b=new Image,c=this;b.onerror=function(){var a=c._form.one("."+CSS.IMAGEPREVIEW);a.setStyles({display:"none"});c.getDialogue().centerDialogue()};b.onload=function(){var a,b,d,e,f;c._rawImageDimensions={width:this.width,height:this.height};a=c._form.one("."+CSS.INPUTWIDTH);b=a.get("value");if(""===b){a.set("value",this.width);b=""+this.width}a=c._form.one("."+CSS.INPUTHEIGHT);d=a.get("value");if(""===d){a.set("value",this.height);d=""+this.height}a=c._form.one("."+CSS.IMAGEPREVIEW);a.setAttribute("src",this.src);a.setStyles({display:"inline"});a=c._form.one("."+CSS.INPUTCONSTRAIN);if(b.match(REGEX.ISPERCENT)&&d.match(REGEX.ISPERCENT)){a.set("checked",b===d)}else{if(0===this.width){this.width=1}if(0===this.height){this.height=1}e=Math.round(1e3*parseInt(b,10)/this.width);f=Math.round(1e3*parseInt(d,10)/this.height);a.set("checked",e===f)}c._autoAdjustSize(c);c.getDialogue().centerDialogue()};b.src=a},_getDialogueContent:function _getDialogueContent(){var a=Y.Handlebars.compile(TEMPLATE),b=this.get("host").canShowFilepicker("image"),c=Y.Node.create(a({elementid:this.get("host").get("elementid"),CSS:CSS,component:COMPONENTNAME,showFilepicker:b,alignments:ALIGNMENTS}));this._form=c;this._applyImageProperties(this._form);this._form.one("."+CSS.INPUTURL).on("blur",this._urlChanged,this);this._form.one("."+CSS.IMAGEPRESENTATION).on("change",this._updateWarning,this);this._form.one("."+CSS.INPUTALT).on("change",this._updateWarning,this);this._form.one("."+CSS.INPUTWIDTH).on("blur",this._autoAdjustSize,this);this._form.one("."+CSS.INPUTHEIGHT).on("blur",this._autoAdjustSize,this,!0);this._form.one("."+CSS.INPUTCONSTRAIN).on("change",function(a){if(a.target.get("checked")){this._autoAdjustSize(a)}},this);this._form.one("."+CSS.INPUTURL).on("blur",this._urlChanged,this);this._form.one("."+CSS.INPUTSUBMIT).on("click",this._setImage,this);if(b){this._form.one("."+CSS.IMAGEBROWSER).on("click",function(){this.get("host").showFilepicker("image",this._filepickerCallback,this)},this)}this._form.one("."+CSS.INPUTALT).on("keyup",this._handleKeyup,this);return c},_autoAdjustSize:function _autoAdjustSize(a,b){b=b||!1;var c=this._form.one("."+CSS.INPUTWIDTH),d="width",e=this._form.one("."+CSS.INPUTHEIGHT),f="height",g=this._form.one("."+CSS.INPUTCONSTRAIN),h=c.get("value"),i=e.get("value"),j=this._form.one("."+CSS.IMAGEPREVIEW),k,l;if(!this._rawImageDimensions){return}if(""===h){h=this._rawImageDimensions[d];c.set("value",h);h=c.get("value")}j.setStyles({width:null,height:null});if(!g.get("checked")){if(h.match(REGEX.ISPERCENT)){k=parseInt(h,10);l=this._rawImageDimensions.width/100*k;j.setStyle("width",l+"px")}else{j.setStyle("width",h+"px")}if(i.match(REGEX.ISPERCENT)){k=parseInt(i,10);l=this._rawImageDimensions.height/100*k;j.setStyle("height",l+"px")}else{j.setStyle("height",i+"px")}}else{if(b){var m=c;c=e;e=m;m=d;d=f;f=m;m=h;h=i;i=m}if(h.match(REGEX.ISPERCENT)){i=h;k=parseInt(h,10);l=this._rawImageDimensions.width/100*k;j.setStyle("width",l);l=this._rawImageDimensions.height/100*k;j.setStyle("height",l)}else{i=Math.round(h/this._rawImageDimensions[d]*this._rawImageDimensions[f]);if(b){j.setStyles({width:i,height:h})}else{j.setStyles({width:h,height:i})}}e.set("value",i)}},_filepickerCallback:function _filepickerCallback(a){if(""!==a.url){var b=this._form.one("."+CSS.INPUTURL);b.set("value",a.url);this._form.one("."+CSS.INPUTWIDTH).set("value","");this._form.one("."+CSS.INPUTHEIGHT).set("value","");this._loadPreviewImage(a.url)}},_applyImageProperties:function _applyImageProperties(a){var b=this._getSelectedImageProperties(),c=a.one("."+CSS.IMAGEPREVIEW);if(!1===b){c.setStyle("display","none");ALIGNMENTS.some(function(b){if(b.isDefault){a.one("."+CSS.INPUTALIGNMENT).set("value",b.value);return!0}return!1},this);return}if(b.align){a.one("."+CSS.INPUTALIGNMENT).set("value",b.align)}if(b.customstyle){a.one("."+CSS.INPUTCUSTOMSTYLE).set("value",b.customstyle)}if(b.width){a.one("."+CSS.INPUTWIDTH).set("value",b.width)}if(b.height){a.one("."+CSS.INPUTHEIGHT).set("value",b.height)}if(b.alt){a.one("."+CSS.INPUTALT).set("value",b.alt)}if(b.src){a.one("."+CSS.INPUTURL).set("value",b.src);this._loadPreviewImage(b.src)}if(b.presentation){a.one("."+CSS.IMAGEPRESENTATION).set("checked","checked")}this._autoAdjustSize()},_getSelectedImageProperties:function _getSelectedImageProperties(){var a={src:null,alt:null,width:null,height:null,align:"",presentation:!1},b=this.get("host").getSelectedNodes(),c,d,e,f;if(b){b=b.filter("img")}if(b&&b.size()){f=this._removeLegacyAlignment(b.item(0));this._selectedImage=f;e=f.getAttribute("style");a.customstyle=e;c=f.getAttribute("width");if(!c.match(REGEX.ISPERCENT)){c=parseInt(c,10)}d=f.getAttribute("height");if(!d.match(REGEX.ISPERCENT)){d=parseInt(d,10)}if(0!==c){a.width=c}if(0!==d){a.height=d}this._getAlignmentPropeties(f,a);a.src=f.getAttribute("src");a.alt=f.getAttribute("alt")||"";a.presentation="presentation"===f.get("role");return a}this._selectedImage=null;return!1},_getAlignmentPropeties:function _getAlignmentPropeties(a,b){var c=!1,d;c=ALIGNMENTS.some(function(c){var e=this._getAlignmentClass(c.value);if(a.hasClass(e)){b.align=c.value;Y.log("Found alignment "+c.value,"debug","atto_image-button");return!0}if(c.isDefault){d=c.value}return!1},this);if(!c&&d){b.align=d}},_urlChanged:function _urlChanged(){var a=this._form.one("."+CSS.INPUTURL);if(""!==a.get("value")){this._loadPreviewImage(a.get("value"))}},_setImage:function _setImage(a){var b=this._form,c=b.one("."+CSS.INPUTURL).get("value"),d=b.one("."+CSS.INPUTALT).get("value"),e=b.one("."+CSS.INPUTWIDTH).get("value"),f=b.one("."+CSS.INPUTHEIGHT).get("value"),g=this._getAlignmentClass(b.one("."+CSS.INPUTALIGNMENT).get("value")),h=b.one("."+CSS.IMAGEPRESENTATION).get("checked"),i=b.one("."+CSS.INPUTCONSTRAIN).get("checked"),j,k=b.one("."+CSS.INPUTCUSTOMSTYLE).get("value"),l=[],m=this.get("host");a.preventDefault();if(this._updateWarning()){return}m.focus();if(""!==c){if(this._selectedImage){m.setSelection(m.getSelectionFromNode(this._selectedImage))}else{m.setSelection(this._currentSelection)}if(i){l.push(CSS.RESPONSIVE)}l.push(g);if(!e.match(REGEX.ISPERCENT)&&isNaN(parseInt(e,10))){b.one("."+CSS.INPUTWIDTH).focus();return}if(!f.match(REGEX.ISPERCENT)&&isNaN(parseInt(f,10))){b.one("."+CSS.INPUTHEIGHT).focus();return}var n=Y.Handlebars.compile(IMAGETEMPLATE);j=n({url:c,alt:d,width:e,height:f,presentation:h,customstyle:k,classlist:l.join(" ")});this.get("host").insertContentAtFocusPoint(j);this.markUpdated()}this.getDialogue({focusAfterHide:null}).hide()},_removeLegacyAlignment:function _removeLegacyAlignment(a){if(!a.getStyle("margin")){return a}ALIGNMENTS.some(function(b){if(a.getStyle(b.name)!==b.value){return!1}var c=Y.Node.create("<div>");c.setStyle("margin",b.margin);if(a.getStyle("margin")!==c.getStyle("margin")){return!1}Y.log("Legacy alignment found and removed.","info","atto_image-button");a.addClass(this._getAlignmentClass(b.value));a.setStyle(b.name,null);a.setStyle("margin",null);return!0},this);return a},_getAlignmentClass:function _getAlignmentClass(a){return CSS.ALIGNSETTINGS+"_"+a},_updateWarning:function _updateWarning(){var a=this._form,b=!0,c=a.one("."+CSS.INPUTALT).get("value"),d=a.one("."+CSS.IMAGEPRESENTATION).get("checked");if(""===c&&!d){a.one("."+CSS.IMAGEALTWARNING).setStyle("display","block");a.one("."+CSS.INPUTALT).setAttribute("aria-invalid",!0);a.one("."+CSS.IMAGEPRESENTATION).setAttribute("aria-invalid",!0);b=!0}else{a.one("."+CSS.IMAGEALTWARNING).setStyle("display","none");a.one("."+CSS.INPUTALT).setAttribute("aria-invalid",!1);a.one("."+CSS.IMAGEPRESENTATION).setAttribute("aria-invalid",!1);b=!1}this.getDialogue().centerDialogue();return b},_handleKeyup:function _handleKeyup(){var a=this._form,b=a.one("."+CSS.INPUTALT).get("value"),c=b.length,d=a.one("#currentcount");d.setHTML(c)}});
2 //# sourceMappingURL=button.min.js.map