weekly release 4.0dev
[moodle.git] / lib / editor / atto / plugins / image / yui / build / moodle-atto_image-button / moodle-atto_image-button-min.js
CommitLineData
a1ccefe2 1YUI.add("moodle-atto_image-button",function(m,e){var u={RESPONSIVE:"img-fluid",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"},t={INPUTURL:"."+u.INPUTURL},a=[{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"}],I=/\d+%/,_="atto_image",p='<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}}/>';m.namespace("M.atto_image").Button=m.Base.create("button",m.M.editor_atto.EditorPlugin,[],{_currentSelection:null,_selectedImage:null,_form:null,_rawImageDimensions:null,initializer:function(){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(e){e.preventDefault()},this),this.editor.on("dragenter",function(e){e.preventDefault()},this)},_handleDragDrop:function(e){return!e._event||!e._event.dataTransfer||this._handlePasteOrDropHelper(e,e._event.dataTransfer)},_handlePaste:function(e){return!e._event||!e._event.clipboardData||this._handlePasteOrDropHelper(e,e._event.clipboardData)},_handlePasteOrDropHelper:function(e,t){var i,n,s=t.items,a=!1;for(i=0;i<s.length;i++)"file"===(n=s[i]).kind&&this._isImage(n.type)&&(this._uploadImage(n.getAsFile()),a=!0);return!a||(e.preventDefault(),e.stopPropagation(),!1)},_isImage:function(e){return 0===e.indexOf("image/")},_uploadImage:function(e){var t,i,n,s,a,o,l,r,h,g=this,d=this.get("host"),c=m.Handlebars.compile(p);for(d.saveSelection(),require(["core_form/events"],function(e){e.notifyUploadStarted(g.editor.get("id"))}),i=(t=d.get("filepickeroptions").image).savepath===undefined?"/":t.savepath,n=new FormData,s=0,a="",o=new XMLHttpRequest,l="",r=Object.keys(t.repositories),n.append("repo_upload_file",e),n.append("itemid",t.itemid),h=0;h<r.length;h++)if("upload"===t.repositories[r[h]].type){n.append("repo_id",t.repositories[r[h]].id);break}n.append("env",t.env),n.append("sesskey",M.cfg.sesskey),n.append("client_id",t.client_id),n.append("savepath",i),n.append("ctx_id",t.context.id),s=(new Date).getTime(),a="moodleimage_"+Math.round(1e5*Math.random())+"-"+s,d.focus(),d.restoreSelection(),l=c({url:M.util.image_url("i/loading_small","moodle"),alt:M.util.get_string("uploading",_),id:a}),d.insertContentAtFocusPoint(l),g.markUpdated(),o.onreadystatechange=function(){var e,t,i,n,s=g.editor.one("#"+a);if(4===o.readyState){if(200===o.status){if(e=JSON.parse(o.responseText)){if(e.error)throw s&&s.remove(!0),require(["core_form/events"],function(e){e.notifyUploadCompleted(g.editor.get("id"))}),new M.core.ajaxException(e);(t=e).event&&"fileexists"===e.event&&(t=e.newfile),i=c({url:t.url,presentation:!0,classlist:u.RESPONSIVE}),n=m.Node.create(i),s?s.replace(n):g.editor.appendChild(n),g.markUpdated()}}else m.use("moodle-core-notification-alert",function(){require(["core_form/events"],function(e){e.notifyUploadCompleted(g.editor.get("id"))}),new M.core.alert({message:M.util.get_string("servererror","moodle")})}),s&&s.remove(!0);require(["core_form/events"],function(e){e.notifyUploadCompleted(g.editor.get("id"))})}},o.open("POST",M.cfg.wwwroot+"/repository/repository_ajax.php?action=upload",!0),o.send(n)},_handleClick:function(e){var t=e.target,i=this.get("host").getSelectionFromNode(t);this.get("host").getSelection()!==i&&this.get("host").setSelection(i)},_displayDialogue:function(){if(this._currentSelection=this.get("host").getSelection(),!1!==this._currentSelection){this._rawImageDimensions=null;var e=this.getDialogue({headerContent:M.util.get_string("imageproperties",_),width:"auto",focusAfterHide:!0,focusOnShowSelector:t.INPUTURL});e.get("boundingBox").setStyle("maxWidth","90%"),e.set("bodyContent",this._getDialogueContent()).show()}},_loadPreviewImage:function(e){var t=new Image,a=this;t.onerror=function(){a._form.one("."+u.IMAGEPREVIEW).setStyles({display:"none"}),a.getDialogue().centerDialogue()},t.onload=function(){var e,t,i,n,s;a._rawImageDimensions={width:this.width,height:this.height},""===(t=(e=a._form.one("."+u.INPUTWIDTH)).get("value"))&&(e.set("value",this.width),t=""+this.width),""===(i=(e=a._form.one("."+u.INPUTHEIGHT)).get("value"))&&(e.set("value",this.height),i=""+this.height),(e=a._form.one("."+u.IMAGEPREVIEW)).setAttribute("src",this.src),e.setStyles({display:"inline"}),e=a._form.one("."+u.INPUTCONSTRAIN),t.match(I)&&i.match(I)?e.set("checked",t===i):(0===this.width&&(this.width=1),0===this.height&&(this.height=1),n=Math.round(1e3*parseInt(t,10)/this.width),s=Math.round(1e3*parseInt(i,10)/this.height),e.set("checked",n===s)),a._autoAdjustSize(a),a.getDialogue().centerDialogue()},t.src=e},_getDialogueContent:function(){var e=m.Handlebars.compile(
c6fb80b6
HN
2'<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 class="{{CSS.IMAGEPREVIEW}}" alt="" style="display: none;"/></div><button class="btn btn-secondary {{CSS.INPUTSUBMIT}}" type="submit">{{get_string "saveimage" component}}</button></div></form>'),t=this.get("host").canShowFilepicker("image"),i=m.Node.create(e({elementid:this.get("host").get("elementid"),CSS:u,component:_,showFilepicker:t,alignments:a}));return this._form=i,this._applyImageProperties(this._form),this._form.one("."+u.INPUTURL).on("blur",this._urlChanged,this),this._form.one("."+u.IMAGEPRESENTATION).on("change",this._updateWarning,this),this._form.one("."+u.INPUTALT).on("change",this._updateWarning,this),this._form.one("."+u.INPUTWIDTH).on("blur",this._autoAdjustSize,this),this._form.one("."+u.INPUTHEIGHT).on("blur",this._autoAdjustSize,this,!0),this._form.one("."+u.INPUTCONSTRAIN).on("change",function(e){e.target.get("checked")&&this._autoAdjustSize(e)},this),this._form.one("."+u.INPUTURL).on("blur",this._urlChanged,this),this._form.one("."+u.INPUTSUBMIT).on("click",this._setImage,this),t&&this._form.one("."+u.IMAGEBROWSER).on("click",function(){this.get("host").showFilepicker("image",this._filepickerCallback,this)},this),this._form.one("."+u.INPUTALT).on("keyup",this._handleKeyup,this),i},_autoAdjustSize:function(e,t){var i,n,s,a,o,l,r,h,g,d,c;t=t||!1,i=this._form.one("."+u.INPUTWIDTH),n="width",s=this._form.one("."+u.INPUTHEIGHT),a="height",o=this._form.one("."+u.INPUTCONSTRAIN),l=i.get("value"),r=s.get("value"),h=this._form.one("."+u.IMAGEPREVIEW),this._rawImageDimensions&&(""===l&&(l=this._rawImageDimensions[n],i.set("value",l),l=i.get("value")),h.setStyles({width:null,height:null}),o.get("checked")?(t&&(c=i,i=s,s=c,c=n,n=a,a=c,c=l,l=r,r=c),l.match(I)?(r=l,g=parseInt(l,10),d=this._rawImageDimensions.width/100*g,h.setStyle("width",d),d=this._rawImageDimensions.height/100*g,h.setStyle("height",d)):(r=Math.round(l/this._rawImageDimensions[n]*this._rawImageDimensions[a]),t?h.setStyles({width:r,height:l}):h.setStyles({width:l,height:r})),s.set("value",r)):(l.match(I)?(g=parseInt(l,10),d=this._rawImageDimensions.width/100*g,h.setStyle("width",d+"px")):h.setStyle("width",l+"px"),r.match(I)?(g=parseInt(r,10),d=this._rawImageDimensions.height/100*g,h.setStyle("height",d+"px")):h.setStyle("height",r+"px")))},_filepickerCallback:function(e){""!==e.url&&(this._form.one("."+u.INPUTURL).set("value",e.url),this._form.one("."+u.INPUTWIDTH).set("value",""),this._form.one("."+u.INPUTHEIGHT).set("value",""),this._loadPreviewImage(e.url))},_applyImageProperties:function(t){var e=this._getSelectedImageProperties(),i=t.one("."+u.IMAGEPREVIEW);if(!1===e)return i.setStyle("display","none"),void a.some(function(e){return!!e.isDefault&&(t.one("."+u.INPUTALIGNMENT).set("value",e.value),!0)},this);e.align&&t.one("."+u.INPUTALIGNMENT).set("value",e.align),e.customstyle&&t.one("."+u.INPUTCUSTOMSTYLE).set("value",e.customstyle),e.width&&t.one("."+u.INPUTWIDTH).set("value",e.width),e.height&&t.one("."+u.INPUTHEIGHT).set("value",e.height),e.alt&&t.one("."+u.INPUTALT).set("value",e.alt),e.src&&(t.one("."+u.INPUTURL).set("value",e.src),
3this._loadPreviewImage(e.src)),e.presentation&&t.one("."+u.IMAGEPRESENTATION).set("checked","checked"),this._autoAdjustSize()},_getSelectedImageProperties:function(){var e,t,i,n,s={src:null,alt:null,width:null,height:null,align:"",presentation:!1},a=this.get("host").getSelectedNodes();return(a=a&&a.filter("img"))&&a.size()?(n=this._removeLegacyAlignment(a.item(0)),i=(this._selectedImage=n).getAttribute("style"),s.customstyle=i,(e=n.getAttribute("width")).match(I)||(e=parseInt(e,10)),(t=n.getAttribute("height")).match(I)||(t=parseInt(t,10)),0!==e&&(s.width=e),0!==t&&(s.height=t),this._getAlignmentPropeties(n,s),s.src=n.getAttribute("src"),s.alt=n.getAttribute("alt")||"",s.presentation="presentation"===n.get("role"),s):(this._selectedImage=null,!1)},_getAlignmentPropeties:function(i,n){var s;!a.some(function(e){var t=this._getAlignmentClass(e.value);return i.hasClass(t)?(n.align=e.value,!0):(e.isDefault&&(s=e.value),!1)},this)&&s&&(n.align=s)},_urlChanged:function(){var e=this._form.one("."+u.INPUTURL);""!==e.get("value")&&this._loadPreviewImage(e.get("value"))},_setImage:function(e){var t,i=this._form,n=i.one("."+u.INPUTURL).get("value"),s=i.one("."+u.INPUTALT).get("value"),a=i.one("."+u.INPUTWIDTH).get("value"),o=i.one("."+u.INPUTHEIGHT).get("value"),l=this._getAlignmentClass(i.one("."+u.INPUTALIGNMENT).get("value")),r=i.one("."+u.IMAGEPRESENTATION).get("checked"),h=i.one("."+u.INPUTCONSTRAIN).get("checked"),g=i.one("."+u.INPUTCUSTOMSTYLE).get("value"),d=[],c=this.get("host");if(e.preventDefault(),!this._updateWarning()){if(c.focus(),""!==n){if(this._selectedImage?c.setSelection(c.getSelectionFromNode(this._selectedImage)):c.setSelection(this._currentSelection),h&&d.push(u.RESPONSIVE),d.push(l),!a.match(I)&&isNaN(parseInt(a,10)))return void i.one("."+u.INPUTWIDTH).focus();if(!o.match(I)&&isNaN(parseInt(o,10)))return void i.one("."+u.INPUTHEIGHT).focus();t=m.Handlebars.compile(p)({url:n,alt:s,width:a,height:o,presentation:r,customstyle:g,classlist:d.join(" ")}),this.get("host").insertContentAtFocusPoint(t),this.markUpdated()}this.getDialogue({focusAfterHide:null}).hide()}},_removeLegacyAlignment:function(i){return i.getStyle("margin")&&a.some(function(e){if(i.getStyle(e.name)!==e.value)return!1;var t=m.Node.create("<div>");return t.setStyle("margin",e.margin),i.getStyle("margin")===t.getStyle("margin")&&(i.addClass(this._getAlignmentClass(e.value)),i.setStyle(e.name,null),i.setStyle("margin",null),!0)},this),i},_getAlignmentClass:function(e){return u.ALIGNSETTINGS+"_"+e},_updateWarning:function(){var e=this._form,t=!0,i=e.one("."+u.INPUTALT).get("value"),n=e.one("."+u.IMAGEPRESENTATION).get("checked");return t=""!==i||n?(e.one("."+u.IMAGEALTWARNING).setStyle("display","none"),e.one("."+u.INPUTALT).setAttribute("aria-invalid",!1),e.one("."+u.IMAGEPRESENTATION).setAttribute("aria-invalid",!1),!1):(e.one("."+u.IMAGEALTWARNING).setStyle("display","block"),e.one("."+u.INPUTALT).setAttribute("aria-invalid",!0),e.one("."+u.IMAGEPRESENTATION).setAttribute("aria-invalid",!0),!0),this.getDialogue().centerDialogue(),t},_handleKeyup:function(){var e=this._form,t=e.one("."+u.INPUTALT).get("value").length;e.one("#currentcount").setHTML(t)}})},"@VERSION@",{requires:["moodle-editor_atto-plugin"]});