tinymce.PluginManager.add("image",function(e){function t(e,t){function i(e,i){n.parentNode&&n.parentNode.removeChild(n),t({width:e,height:i})}var n=document.createElement("img");n.onload=function(){i(n.clientWidth,n.clientHeight)},n.onerror=function(){i()};var a=n.style;a.visibility="hidden",a.position="fixed",a.bottom=a.left=0,a.width=a.height="auto",document.body.appendChild(n),n.src=e}function i(t){return function(){var i=e.settings.image_list;"string"==typeof i?tinymce.util.XHR.send({url:i,success:function(e){t(tinymce.util.JSON.parse(e))}}):t(i)}}function n(i){function n(t,i,n){var a,l=[];return tinymce.each(e.settings[t]||n,function(e){var t={text:e.text||e.title,value:e.value};l.push(t),(p[i]===e.value||!a&&e.selected)&&(a=t)}),a&&!p[i]&&(p[i]=a.value,a.selected=!0),l}function a(){var t=[{text:"None",value:""}];return tinymce.each(i,function(i){t.push({text:i.text||i.title,value:e.convertURL(i.value||i.url,"src"),menu:i.menu})}),t}function l(){var e,t,i,n;e=d.find("#width")[0],t=d.find("#height")[0],i=e.value(),n=t.value(),d.find("#constrain")[0].checked()&&u&&h&&i&&n&&(u!=i?(n=Math.round(i/u*n),t.value(n)):(i=Math.round(n/h*i),e.value(i))),u=i,h=n}function o(){function t(t){function i(){t.onload=t.onerror=null,e.selection.select(t),e.nodeChanged()}t.onload=function(){p.width||p.height||y.setAttribs(t,{width:t.clientWidth,height:t.clientHeight}),i()},t.onerror=i}c(),l(),p=tinymce.extend(p,d.toJSON()),""===p.width&&(p.width=null),""===p.height&&(p.height=null),""===p.style&&(p.style=null),p={src:p.src,alt:p.alt,width:p.width,height:p.height,style:p.style,"class":p["class"]},p["class"]||delete p["class"],e.undoManager.transact(function(){return p.src?(f?y.setAttribs(f,p):(p.id="__mcenew",e.focus(),e.selection.setContent(y.createHTML("img",p)),f=y.get("__mcenew"),y.setAttrib(f,"id",null)),void t(f)):void(f&&(y.remove(f),e.focus(),e.nodeChanged()))})}function r(e){return e&&(e=e.replace(/px$/,"")),e}function s(){g&&g.value(e.convertURL(this.value(),"src")),t(this.value(),function(e){e.width&&e.height&&(u=e.width,h=e.height,d.find("#width").value(u),d.find("#height").value(h))})}function c(){function t(e){return e.length>0&&/^[0-9]+$/.test(e)&&(e+="px"),e}if(e.settings.image_advtab){var i=d.toJSON(),n=y.parseStyle(i.style);delete n.margin,n["margin-top"]=n["margin-bottom"]=t(i.vspace),n["margin-left"]=n["margin-right"]=t(i.hspace),n["border-width"]=t(i.border),d.find("#style").value(y.serializeStyle(y.parseStyle(y.serializeStyle(n))))}}var d,u,h,g,m,p={},y=e.dom,f=e.selection.getNode();u=y.getAttrib(f,"width"),h=y.getAttrib(f,"height"),"IMG"!=f.nodeName||f.getAttribute("data-mce-object")||f.getAttribute("data-mce-placeholder")?f=null:p={src:y.getAttrib(f,"src"),alt:y.getAttrib(f,"alt"),"class":y.getAttrib(f,"class"),width:u,height:h},i&&(g={type:"listbox",label:"Image list",values:a(),value:p.src&&e.convertURL(p.src,"src"),onselect:function(e){var t=d.find("#alt");(!t.value()||e.lastControl&&t.value()==e.lastControl.text())&&t.value(e.control.text()),d.find("#src").value(e.control.value())},onPostRender:function(){g=this}}),e.settings.image_class_list&&(m={name:"class",type:"listbox",label:"Class",values:n("image_class_list","class")});var b=[{name:"src",type:"filepicker",filetype:"image",label:"Source",autofocus:!0,onchange:s},g,{name:"alt",type:"textbox",label:"Image description"},{type:"container",label:"Dimensions",layout:"flex",direction:"row",align:"center",spacing:5,items:[{name:"width",type:"textbox",maxLength:5,size:3,onchange:l,ariaLabel:"Width"},{type:"label",text:"x"},{name:"height",type:"textbox",maxLength:5,size:3,onchange:l,ariaLabel:"Height"},{name:"constrain",type:"checkbox",checked:!0,text:"Constrain proportions"}]},m];e.settings.image_advtab?(f&&(p.hspace=r(f.style.marginLeft||f.style.marginRight),p.vspace=r(f.style.marginTop||f.style.marginBottom),p.border=r(f.style.borderWidth),p.style=e.dom.serializeStyle(e.dom.parseStyle(e.dom.getAttrib(f,"style")))),d=e.windowManager.open({title:"Insert/edit image",data:p,bodyType:"tabpanel",body:[{title:"General",type:"form",items:b},{title:"Advanced",type:"form",pack:"start",items:[{label:"Style",name:"style",type:"textbox"},{type:"form",layout:"grid",packV:"start",columns:2,padding:0,alignH:["left","right"],defaults:{type:"textbox",maxWidth:50,onchange:c},items:[{label:"Vertical space",name:"vspace"},{label:"Horizontal space",name:"hspace"},{label:"Border",name:"border"}]}]}],onSubmit:o})):d=e.windowManager.open({title:"Insert/edit image",data:p,body:b,onSubmit:o})}e.addButton("image",{icon:"image",tooltip:"Insert/edit image",onclick:i(n),stateSelector:"img:not([data-mce-object],[data-mce-placeholder])"}),e.addMenuItem("image",{icon:"image",text:"Insert image",onclick:i(n),context:"insert",prependToContext:!0})});