')}),d.each(z,function(b){a("#"+D,k).append('
')}),A=d.map(z,c),s(h),a(k).on("focusin focusout",function(b){a(b.target).attr("aria-grabbed","focus"===b.type)}),a(k).on("keydown",function(a){function b(a,b,d,e,f){a.stopPropagation(),a.preventDefault(),p(c,d,e,f)}var c;switch(d.each(z,function(b){if(a.target.id==D+"-"+b.name)return c=b,!1}),a.keyCode){case f.LEFT:b(a,c,h,-10,0);break;case f.RIGHT:b(a,c,h,10,0);break;case f.UP:b(a,c,h,0,-10);break;case f.DOWN:b(a,c,h,0,10);break;case f.ENTER:case f.SPACEBAR:a.preventDefault(),l()}})}function r(b){var c;c=d.map(z,function(a){return"#"+D+"-"+a.name}).concat(d.map(B,function(a){return"#"+D+"-"+a})).join(","),b?a(c,k).show():a(c,k).hide()}function s(b){function c(b,c){c.h<0&&(c.h=0),c.w<0&&(c.w=0),a("#"+D+"-"+b,k).css({left:c.x,top:c.y,width:c.w,height:c.h})}d.each(z,function(c){a("#"+D+"-"+c.name,k).css({left:b.w*c.xMul+b.x,top:b.h*c.yMul+b.y})}),c("top",{x:i.x,y:i.y,w:i.w,h:b.y-i.y}),c("right",{x:b.x+b.w,y:b.y,w:i.w-b.x-b.w+i.x,h:b.h}),c("bottom",{x:i.x,y:b.y+b.h,w:i.w,h:i.h-b.y-b.h+i.y}),c("left",{x:i.x,y:b.y,w:b.x-i.x,h:b.h}),c("move",b)}function t(a){h=a,s(h)}function u(a){i=a,s(h)}function v(a){t(m(j,a))}function w(a){j=a,s(h)}function x(){d.each(A,function(a){a.destroy()}),A=[]}var y,z,A,B,C="mce-",D=C+"crid-"+g++;return z=[{name:"move",xMul:0,yMul:0,deltaX:1,deltaY:1,deltaW:0,deltaH:0,label:"Crop Mask"},{name:"nw",xMul:0,yMul:0,deltaX:1,deltaY:1,deltaW:-1,deltaH:-1,label:"Top Left Crop Handle"},{name:"ne",xMul:1,yMul:0,deltaX:0,deltaY:1,deltaW:1,deltaH:-1,label:"Top Right Crop Handle"},{name:"sw",xMul:0,yMul:1,deltaX:1,deltaY:0,deltaW:-1,deltaH:1,label:"Bottom Left Crop Handle"},{name:"se",xMul:1,yMul:1,deltaX:0,deltaY:0,deltaW:1,deltaH:1,label:"Bottom Right Crop Handle"}],B=["top","right","bottom","left"],q(k),y=d.extend({toggleVisibility:r,setClampRect:w,setRect:t,getInnerRect:o,setInnerRect:v,setViewPortRect:u,destroy:x},e)}}),g("n",["x","y","z","6","7","10"],function(a,b,c,d,e,f){function g(a){return new d(function(b){function c(){a.removeEventListener("load",c),b(a)}a.complete?b(a):a.addEventListener("load",c)})}return b.extend({Defaults:{classes:"imagepanel"},selection:function(a){return arguments.length?(this.state.set("rect",a),this):this.state.get("rect")},imageSize:function(){var a=this.state.get("viewRect");return{w:a.w,h:a.h}},toggleCropRect:function(a){this.state.set("cropEnabled",a)},imageSrc:function(b){var c=this,d=new Image;d.src=b,g(d).then(function(){var b,e,f=c.state.get("viewRect");if(e=c.$el.find("img"),e[0])e.replaceWith(d);else{var g=document.createElement("div");g.className="mce-imagepanel-bg",c.getEl().appendChild(g),c.getEl().appendChild(d)}b={x:0,y:0,w:d.naturalWidth,h:d.naturalHeight},c.state.set("viewRect",b),c.state.set("rect",a.inflate(b,-20,-20)),f&&f.w==b.w&&f.h==b.h||c.zoomFit(),c.repaintImage(),c.fire("load")})},zoom:function(a){return arguments.length?(this.state.set("zoom",a),this):this.state.get("zoom")},postRender:function(){return this.imageSrc(this.settings.imageSrc),this._super()},zoomFit:function(){var a,b,c,d,e,f,g,h=this;g=10,a=h.$el.find("img"),b=h.getEl().clientWidth,c=h.getEl().clientHeight,d=a[0].naturalWidth,e=a[0].naturalHeight,f=Math.min((b-g)/d,(c-g)/e),f>=1&&(f=1),h.zoom(f)},repaintImage:function(){var a,b,c,d,e,f,g,h,i,j,k;k=this.getEl(),i=this.zoom(),j=this.state.get("rect"),g=this.$el.find("img"),h=this.$el.find(".mce-imagepanel-bg"),e=k.offsetWidth,f=k.offsetHeight,c=g[0].naturalWidth*i,d=g[0].naturalHeight*i,a=Math.max(0,e/2-c/2),b=Math.max(0,f/2-d/2),g.css({left:a,top:b,width:c,height:d}),h.css({left:a,top:b,width:c,height:d}),this.cropRect&&(this.cropRect.setRect({x:j.x*i+a,y:j.y*i+b,w:j.w*i,h:j.h*i}),this.cropRect.setClampRect({x:a,y:b,w:c,h:d}),this.cropRect.setViewPortRect({x:0,y:0,w:e,h:f}))},bindStates:function(){function a(a){b.cropRect=new f(a,b.state.get("viewRect"),b.state.get("viewRect"),b.getEl(),function(){b.fire("crop")}),b.cropRect.on("updateRect",function(a){var c=a.rect,d=b.zoom();c={x:Math.round(c.x/d),y:Math.round(c.y/d),w:Math.round(c.w/d),h:Math.round(c.h/d)},b.state.set("rect",c)}),b.on("remove",b.cropRect.destroy)}var b=this;b.state.on("change:cropEnabled",function(a){b.cropRect.toggleVisibility(a.value),b.repaintImage()}),b.state.on("change:zoom",function(){b.repaintImage()}),b.state.on("change:rect",function(c){var d=c.value;b.cropRect||a(d),b.cropRect.setRect(d)})}})}),g("o",[],function(){return function(){function a(a){var b;return b=f.splice(++g),f.push(a),{state:a,removed:b}}function b(){if(d())return f[--g]}function c(){if(e())return f[++g]}function d(){return g>0}function e(){return g!=-1&&g.1&&(a-=.1),T.zoom(a)}function E(){g=la.undo(),t(g),r()}function F(){g=la.redo(),t(g),r()}function G(){n(g.blob),M.close()}function H(a){return new f({layout:"flex",direction:"row",labelGap:5,border:"0 0 1 0",align:"center",pack:"center",padding:"0 10 0 10",spacing:5,flex:0,minHeight:60,defaults:{classes:"imagetool",type:"button"},items:a})}function I(b,c){return H([{text:"Back",onclick:A},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:B}]).hide().on("show",function(){s(),a.blobToImageResult(g.blob).then(function(a){return c(a)}).then(ma).then(function(a){var b=k(a);t(b),l(P),P=b})})}function J(b,c,d,e,f){function h(b){a.blobToImageResult(g.blob).then(function(a){return c(a,b)}).then(ma).then(function(a){var b=k(a);t(b),l(P),P=b})}return H([{text:"Back",onclick:A},{type:"spacer",flex:1},{type:"slider",flex:1,ondragend:function(a){h(a.value)},minValue:e,maxValue:f,value:d,previewFilter:q},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:B}]).hide().on("show",function(){this.find("slider").value(d),s()})}function K(b,c){function d(){var b,d,e;b=M.find("#r")[0].value(),d=M.find("#g")[0].value(),e=M.find("#b")[0].value(),a.blobToImageResult(g.blob).then(function(a){return c(a,b,d,e)}).then(ma).then(function(a){var b=k(a);t(b),l(P),P=b})}return H([{text:"Back",onclick:A},{type:"spacer",flex:1},{type:"slider",label:"R",name:"r",minValue:0,value:1,maxValue:2,ondragend:d,previewFilter:q},{type:"slider",label:"G",name:"g",minValue:0,value:1,maxValue:2,ondragend:d,previewFilter:q},{type:"slider",label:"B",name:"b",minValue:0,value:1,maxValue:2,ondragend:d,previewFilter:q},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:B}]).hide().on("show",function(){M.find("#r,#g,#b").value(1),s()})}function L(a){a.control.value()===!0&&(ja=ia/ha,ka=ha/ia)}var M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,$,_,aa,ba,ca,da,ea,fa,ga,ha,ia,ja,ka,la=new j,ma=function(a){return a.toBlob()};Q=H([{text:"Back",onclick:A},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:x}]).hide().on("show hide",function(a){T.toggleCropRect("show"==a.type)}).on("show",s),R=H([{text:"Back",onclick:A},{type:"spacer",flex:1},{type:"textbox",name:"w",label:"Width",size:4,onkeyup:p},{type:"textbox",name:"h",label:"Height",size:4,onkeyup:p},{type:"checkbox",name:"constrain",text:"Constrain proportions",checked:!0,onchange:L},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:"submit"}]).hide().on("submit",function(a){var c=parseInt(M.find("#w").value(),10),d=parseInt(M.find("#h").value(),10);
a.preventDefault(),z(b.resize,c,d)(),A()}).on("show",s),S=H([{text:"Back",onclick:A},{type:"spacer",flex:1},{icon:"fliph",tooltip:"Flip horizontally",onclick:y(b.flip,"h")},{icon:"flipv",tooltip:"Flip vertically",onclick:y(b.flip,"v")},{icon:"rotateleft",tooltip:"Rotate counterclockwise",onclick:y(b.rotate,-90)},{icon:"rotateright",tooltip:"Rotate clockwise",onclick:y(b.rotate,90)},{type:"spacer",flex:1},{text:"Apply",subtype:"primary",onclick:B}]).hide().on("show",s),W=I("Invert",b.invert),ca=I("Sharpen",b.sharpen),da=I("Emboss",b.emboss),X=J("Brightness",b.brightness,0,-1,1),Y=J("Hue",b.hue,180,0,360),Z=J("Saturate",b.saturate,0,-1,1),$=J("Contrast",b.contrast,0,-1,1),_=J("Grayscale",b.grayscale,0,0,1),aa=J("Sepia",b.sepia,0,0,1),ba=K("Colorize",b.colorize),ea=J("Gamma",b.gamma,0,-1,1),fa=J("Exposure",b.exposure,1,0,2),O=H([{text:"Back",onclick:A},{type:"spacer",flex:1},{text:"hue",icon:"hue",onclick:u(Y)},{text:"saturate",icon:"saturate",onclick:u(Z)},{text:"sepia",icon:"sepia",onclick:u(aa)},{text:"emboss",icon:"emboss",onclick:u(da)},{text:"exposure",icon:"exposure",onclick:u(fa)},{type:"spacer",flex:1}]).hide(),N=H([{tooltip:"Crop",icon:"crop",onclick:u(Q)},{tooltip:"Resize",icon:"resize2",onclick:u(R)},{tooltip:"Orientation",icon:"orientation",onclick:u(S)},{tooltip:"Brightness",icon:"sun",onclick:u(X)},{tooltip:"Sharpen",icon:"sharpen",onclick:u(ca)},{tooltip:"Contrast",icon:"contrast",onclick:u($)},{tooltip:"Color levels",icon:"drop",onclick:u(ba)},{tooltip:"Gamma",icon:"gamma",onclick:u(ea)},{tooltip:"Invert",icon:"invert",onclick:u(W)}]),T=new i({flex:1,imageSrc:g.url}),U=new d({layout:"flex",direction:"column",border:"0 1 0 0",padding:5,spacing:5,items:[{type:"button",icon:"undo",tooltip:"Undo",name:"undo",onclick:E},{type:"button",icon:"redo",tooltip:"Redo",name:"redo",onclick:F},{type:"button",icon:"zoomin",tooltip:"Zoom in",onclick:C},{type:"button",icon:"zoomout",tooltip:"Zoom out",onclick:D}]}),V=new d({type:"container",layout:"flex",direction:"row",align:"stretch",flex:1,items:[U,T]}),ga=[N,Q,R,S,O,W,X,Y,Z,$,_,aa,ba,ca,da,ea,fa],M=e.create("window",{layout:"flex",direction:"column",align:"stretch",minWidth:Math.min(c.DOM.getViewPort().w,800),minHeight:Math.min(c.DOM.getViewPort().h,650),title:"Edit image",items:ga.concat([V]),buttons:[{text:"Save",name:"save",subtype:"primary",onclick:G},{text:"Cancel",onclick:"close"}]}),M.renderTo(document.body).reflow(),M.on("close",function(){o(),m(la.data),la=null,P=null}),la.add(g),r(),T.on("load",function(){ha=T.imageSize().w,ia=T.imageSize().h,ja=ia/ha,ka=ha/ia,M.find("#w").value(ha),M.find("#h").value(ia)}),T.on("crop",x)}function o(a){return new g(function(b,c){return a.toBlob().then(function(a){n(k(a),b,c)})})}return{edit:o}}),g("0",["1","2","3","4","5","6","7","8","9","a","b"],function(a,b,c,d,e,f,g,h,i,j,k){var l=function(d){function l(a){d.notificationManager.open({text:a,type:"error"})}function m(){return d.selection.getNode()}function n(a){var b=a.match(/\/([^\/\?]+)?\.(?:jpeg|jpg|png|gif)(?:\?|$)/i);return b?d.dom.encode(b[1]):null}function o(){return"imagetools"+H++}function p(a){var b=a.src;return 0===b.indexOf("data:")||0===b.indexOf("blob:")||new h(b).host===d.documentBaseURI.host}function q(a){return g.inArray(d.settings.imagetools_cors_hosts,new h(a.src).host)!==-1}function r(){return d.settings.api_key||d.settings.imagetools_api_key}function s(b){var c,e=b.src;return q(b)?j.getUrl(b.src,null):p(b)?a.imageToBlob(b):(e=d.settings.imagetools_proxy,e+=(e.indexOf("?")===-1?"?":"&")+"url="+encodeURIComponent(b.src),c=r(),j.getUrl(e,c))}function t(){var a;return a=d.editorUpload.blobCache.getByUri(m().src),a?f.resolve(a.blob()):s(m())}function u(){F=e.setEditorTimeout(d,function(){d.editorUpload.uploadImagesAuto()},d.settings.images_upload_timeout||3e4)}function v(){clearTimeout(F)}function w(a,b){return a.toBlob().then(function(c){var e,f,g,h,i;return g=d.editorUpload.blobCache,i=m(),e=i.src,d.settings.images_reuse_filename&&(h=g.getByUri(e),h?(e=h.uri(),f=h.name()):f=n(e)),h=g.create({id:o(),blob:c,base64:a.toBase64(),uri:e,name:f}),g.add(h),d.undoManager.transact(function(){function a(){d.$(i).off("load",a),d.nodeChanged(),b?d.editorUpload.uploadImagesAuto():(v(),u())}d.$(i).on("load",a),d.$(i).attr({src:h.blobUri()}).removeAttr("data-mce-src")}),h})}function x(b){return function(){return d._scanForImages().then(t).then(a.blobToImageResult).then(b).then(w,l)}}function y(a){return function(){return x(function(c){var d=i.getImageSize(m());return d&&i.setImageSize(m(),{w:d.h,h:d.w}),b.rotate(c,a)})()}}function z(a){return function(){return x(function(c){return b.flip(c,a)})()}}function A(){var b=m(),c=i.getNaturalImageSize(b),d=function(d){return new f(function(e){a.blobToImage(d).then(function(a){var f=i.getNaturalImageSize(a);c.w==f.w&&c.h==f.h||i.getImageSize(b)&&i.setImageSize(b,f),URL.revokeObjectURL(a.src),e(d)})})},e=function(b){return k.edit(b).then(d).then(a.blobToImageResult).then(function(a){return w(a,!0)},function(){})};t().then(a.blobToImageResult).then(e,l)}function B(){d.addButton("rotateleft",{title:"Rotate counterclockwise",cmd:"mceImageRotateLeft"}),d.addButton("rotateright",{title:"Rotate clockwise",cmd:"mceImageRotateRight"}),d.addButton("flipv",{title:"Flip vertically",cmd:"mceImageFlipVertical"}),d.addButton("fliph",{title:"Flip horizontally",cmd:"mceImageFlipHorizontal"}),d.addButton("editimage",{title:"Edit image",cmd:"mceEditImage"}),d.addButton("imageoptions",{title:"Image options",icon:"options",cmd:"mceImage"})}function C(){d.on("NodeChange",function(a){G&&G.src!=a.element.src&&(v(),d.editorUpload.uploadImagesAuto(),G=void 0),D(a.element)&&(G=a.element)})}function D(a){var b=d.dom.is(a,"img:not([data-mce-object],[data-mce-placeholder])");return b&&(p(a)||q(a)||d.settings.imagetools_proxy)}function E(){var a=d.settings.imagetools_toolbar;a||(a="rotateleft rotateright | flipv fliph | crop editimage imageoptions"),d.addContextToolbar(D,a)}var F,G,H=0;c.fileApi&&(g.each({mceImageRotateLeft:y(-90),mceImageRotateRight:y(90),mceImageFlipVertical:z("v"),mceImageFlipHorizontal:z("h"),mceEditImage:A},function(a,b){d.addCommand(b,a)}),B(),E(),C())};return d.add("imagetools",l),function(){}}),d("0")()}();