WordPress/wp-admin/js/image-edit.js

1 line
5.9 KiB
JavaScript

var imageEdit;(function(a){imageEdit={iasapi:{},intval:function(b){return b|0},setState:function(c,b){if(b){c.removeAttr("disabled")}else{c.attr("disabled","disabled")}},setClass:function(b,d){if(d){b.removeClass("disabled")}else{b.addClass("disabled")}},gcd:function(d,c){var e;if(d==0||c==0){return 0}else{if(d==c){return d}else{do{e=d%c;d=c;c=e}while(e!=0);return d}}},toggleEditor:function(d,b){var c=a("#imgedit-wait-"+d);if(b){c.height(a("#imgedit-panel-"+d).height()).fadeIn("fast")}else{c.height(500).fadeOut("fast")}},isChecked:function(b){return(!b.attr("disabled")&&b[0].checked)},getAspect:function(e){var b=this.isChecked(a("#imgedit-scale-switch-"+e)),d,c;if(b){d=this.intval(a("#imgedit-aspect-x-"+e).val());c=this.intval(a("#imgedit-aspect-y-"+e).val());return d/c}else{return 0}},scaleWidthChanged:function(d){var c=a("#imgedit-scale-width-"+d),b;if(!c.attr("disabled")){b=this.getAspect(d);if(b!=0){a("#imgedit-scale-height-"+d).val((c.val()!="")?this.intval(c.val()/b):"")}}},scaleHeightChanged:function(d){var c=a("#imgedit-scale-height-"+d),b;if(!c.attr("disabled")){b=this.getAspect(d);if(b!=0){a("#imgedit-scale-width-"+d).val((c.val()!="")?this.intval(c.val()*b):"")}}},setDefaultAspect:function(h){var b=this,d,c=a("#image-preview-"+h),f=c.attr("width"),e=c.attr("height");while((d=b.gcd(f,e))>1){f=b.intval(Math.ceil(f/d));e=b.intval(Math.ceil(e/d))}if(f>10&&e>10){while(f>10&&e>10){f=b.intval(Math.ceil(f/10));e=b.intval(Math.ceil(e/10))}while((d=b.gcd(f,e))>1){f=b.intval(Math.ceil(f/d));e=b.intval(Math.ceil(e/d))}}a("#imgedit-aspect-x-"+h).val(f);a("#imgedit-aspect-y-"+h).val(e)},filterHistory:function(d){var c=a("#imgedit-history-"+d).val(),b;if(c!=""){b=this.intval(a("#imgedit-undone-"+d).val());if(b>0){c=JSON.parse(c);while(b>0){c.pop();b--}c=JSON.stringify(c)}}return c},refreshEditor:function(g,c,f){var b=this,e,d;b.toggleEditor(g,1);e={action:"load-preview-image",_ajax_nonce:c,postid:g,history:b.filterHistory(g),rand:b.intval(Math.random()*1000000)};d=a('<img id="image-preview-'+g+'" />');d.load(function(){var h=a("#imgedit-crop-"+g);h.empty().append(d);b.initCrop(g,d,h);a("#imgedit-panel-"+g).show();if((typeof f!="unknown")&&f!=null){f()}b.toggleEditor(g,0)}).attr("src",ajaxurl+"?"+a.param(e))},save:function(b,i){var l=this,f=-1,c=-1,k,g,e,d=l.isChecked(a("#imgedit-scale-switch-"+b)),j=a("#imgedit-save-target-"+b).val();if(d){k=a("#imgedit-scale-width-"+b);g=a("#imgedit-scale-height-"+b);f=l.intval(k.val());c=l.intval(g.val());if(f<=0){k.focus();return}else{if(c<=0){g.focus();return}}}l.toggleEditor(b,1);e={action:"image-edit-save",_ajax_nonce:i,postid:b,history:l.filterHistory(b),target:j,fwidth:f,fheight:c};a.post(ajaxurl,e,function(p){var m=p.split("!"),s,o,q,h,n,t;for(n=0;n<m.length;n++){s=m[n].split("=");if(s.length==2){switch(s[0]){case"full":o=s[1].split("x");if(o.length==2){q=o[0];h=o[1];a("#image-dims-"+b).html(q+"&nbsp;&times;&nbsp;"+h)}a("#imgedit-history-"+b).val("");a("#imgedit-undone-"+b).val(0);l.setClass(a("#image-undo-"+b),false);l.setClass(a("#image-redo-"+b),false);break;case"thumbnail":t=a("#media-item-"+b);if(t.length==0){t=a("#media-dims-"+b).closest(".media-item-info")}t=t.find(".thumbnail");t.attr("src",s[1]);break;case"error":a("#imgedit-panel-"+b).html(s[1])}}}l.toggleEditor(b,0)})},open:function(i,e){var d=this,g,f=a("#image-editor-"+i),c=a("#media-head-"+i),b=a("#imgedit-open-btn-"+i),h=b.siblings("img");b.attr("disabled","disabled");h.css("visibility","visible");g={action:"open-image-editor",_ajax_nonce:e,postid:i};f.load(ajaxurl,g,function(){f.fadeIn("fast");c.fadeOut("fast",function(){b.removeAttr("disabled");h.css("visibility","hidden")});d.toggleEditor(i,1);a("#image-preview-"+i).load(function(){var j=imageEdit,k=a("#imgedit-crop-"+i);j.initCrop(i,this,k);j.setDefaultAspect(i);j.toggleEditor(i,0)})})},initCrop:function(e,d,c){var b=this;b.iasapi=a(d).imgAreaSelect({parent:c,instance:true,handles:true,keys:true,minHeight:5,minWidth:5,onInit:function(f,g){c.children().mousedown(function(j){var i,h=false,l=b.intval(a("#imgedit-aspect-x-"+e).val()),k=b.intval(a("#imgedit-aspect-y-"+e).val());defRatio=(l&&k)?l+":"+k:"1:1";if(j.shiftKey){i=b.iasapi.getSelection();h=(i.width&&i.height)?i.width+":"+i.height:defRatio}b.iasapi.setOptions({aspectRatio:h})})},onSelectEnd:function(f,h){var g={x:h.x1,y:h.y1,w:h.width,h:h.height};a("#imgedit-selection-"+e).val(JSON.stringify(g))}})},close:function(b){a("#image-editor-"+b).fadeOut("fast",function(){a("#media-head-"+b).fadeIn("fast")})},addStep:function(i,h,d){var c=this,e=a("#imgedit-history-"+h),g=(e.val()!="")?JSON.parse(e.val()):new Array(),f=a("#imgedit-undone-"+h),b=c.intval(f.val());while(b>0){g.pop();b--}f.val(0);g.push(i);e.val(JSON.stringify(g));c.refreshEditor(h,d,function(){c.setClass(a("#image-undo-"+h),true);c.setClass(a("#image-redo-"+h),false)})},rotate:function(c,d,b){this.addStep({r:c},d,b)},flip:function(c,d,b){this.addStep({f:c},d,b)},crop:function(d,b){var c=a("#imgedit-selection-"+d).val();if(c!=""){c=JSON.parse(c);if(c.w>0&&c.h>0){this.addStep({c:c},d,b)}}},undo:function(g,e){var d=this,c=a("#image-undo-"+g),f=a("#imgedit-undone-"+g),b=d.intval(f.val())+1;if(c.hasClass("disabled")){return}f.val(b);d.refreshEditor(g,e,function(){var h=a("#imgedit-history-"+g),i=(h.val()!="")?JSON.parse(h.val()):new Array();d.setClass(a("#image-redo-"+g),true);d.setClass(c,b<i.length)})},redo:function(g,e){var d=this,c=a("#image-redo-"+g),f=a("#imgedit-undone-"+g),b=d.intval(f.val())-1;if(c.hasClass("disabled")){return}f.val(b);d.refreshEditor(g,e,function(){d.setClass(a("#image-undo-"+g),true);d.setClass(c,b>0)})},scaleSwitched:function(c){var b=this.isChecked(a("#imgedit-scale-switch-"+c));this.setState(a("#imgedit-scale-width-"+c),b);this.setState(a("#imgedit-scale-height-"+c),b);this.scaleWidthChanged(c)},targetChanged:function(d){var c=a("#imgedit-save-target-"+d).val(),b=(c=="full"||c=="all");this.setState(a("#imgedit-scale-switch-"+d),b);this.setClass(a("#imgedit-scale-"+d),!b);this.scaleSwitched(d)}}})(jQuery);