mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-13 06:07:23 +01:00
f426a1fa04
Clear the crop selection input fields after the crop action is complete. Fixes unexpected re-cropping behavior if the crop button was clicked more than once. Props davidbinda. Fixes #30155. Built from https://develop.svn.wordpress.org/trunk@49087 git-svn-id: http://core.svn.wordpress.org/trunk@48849 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
/*! This file is auto-generated */
|
|
!function(m){var l=wp.i18n.__,h=window.imageEdit={iasapi:{},hold:{},postid:"",_view:!1,handleCropToolClick:function(i,t,e){var a=m("#image-preview-"+i),o=this.iasapi.getSelection();isNaN(o.x1)&&(this.setCropSelection(i,{x1:0,y1:0,x2:a.innerWidth(),y2:a.innerHeight(),width:a.innerWidth(),height:a.innerHeight()}),o=this.iasapi.getSelection()),0===o.x1&&0===o.y1&&0===o.x2&&0===o.y2?(this.iasapi.setSelection(0,0,a.innerWidth(),a.innerHeight(),!0),this.iasapi.setOptions({show:!0}),this.iasapi.update()):h.crop(i,t,e)},intval:function(i){return 0|i},setDisabled:function(i,t){t?i.removeClass("disabled").prop("disabled",!1):i.addClass("disabled").prop("disabled",!0)},init:function(i){var t=this,e=m("#image-editor-"+t.postid),a=t.intval(m("#imgedit-x-"+i).val()),o=t.intval(m("#imgedit-y-"+i).val());t.postid!==i&&e.length&&t.close(t.postid),t.hold.w=t.hold.ow=a,t.hold.h=t.hold.oh=o,t.hold.xy_ratio=a/o,t.hold.sizer=parseFloat(m("#imgedit-sizer-"+i).val()),t.postid=i,m("#imgedit-response-"+i).empty(),m('input[type="text"]',"#imgedit-panel-"+i).keypress(function(i){var t=i.keyCode;if(36<t&&t<41&&m(this).blur(),13===t)return i.preventDefault(),i.stopPropagation(),!1}),m(document).on("image-editor-ui-ready",this.focusManager)},toggleEditor:function(i,t,e){var a=m("#imgedit-wait-"+i);t?a.fadeIn("fast"):a.fadeOut("fast",function(){e&&m(document).trigger("image-editor-ui-ready")})},toggleHelp:function(i){var t=m(i);return t.attr("aria-expanded","false"===t.attr("aria-expanded")?"true":"false").parents(".imgedit-group-top").toggleClass("imgedit-help-toggled").find(".imgedit-help").slideToggle("fast"),!1},getTarget:function(i){return m('input[name="imgedit-target-'+i+'"]:checked',"#imgedit-save-target-"+i).val()||"full"},scaleChanged:function(i,t,e){var a=m("#imgedit-scale-width-"+i),o=m("#imgedit-scale-height-"+i),s=m("#imgedit-scale-warn-"+i),n="",d="";!1!==this.validateNumeric(e)&&(t?(d=""!==a.val()?Math.round(a.val()/this.hold.xy_ratio):"",o.val(d)):(n=""!==o.val()?Math.round(o.val()*this.hold.xy_ratio):"",a.val(n)),d&&d>this.hold.oh||n&&n>this.hold.ow?s.css("visibility","visible"):s.css("visibility","hidden"))},getSelRatio:function(i){var t=this.hold.w,e=this.hold.h,a=this.intval(m("#imgedit-crop-width-"+i).val()),o=this.intval(m("#imgedit-crop-height-"+i).val());return a&&o?a+":"+o:t&&e?t+":"+e:"1:1"},filterHistory:function(i,t){var e,a,o,s,n=m("#imgedit-history-"+i).val(),d=[];if(""===n)return"";if(n=JSON.parse(n),0<(e=this.intval(m("#imgedit-undone-"+i).val())))for(;0<e;)n.pop(),e--;if(t){if(!n.length)return this.hold.w=this.hold.ow,this.hold.h=this.hold.oh,"";(o=(o=n[n.length-1]).c||o.r||o.f||!1)&&(this.hold.w=o.fw,this.hold.h=o.fh)}for(a in n)(s=n[a]).hasOwnProperty("c")?d[a]={c:{x:s.c.x,y:s.c.y,w:s.c.w,h:s.c.h}}:s.hasOwnProperty("r")?d[a]={r:s.r.r}:s.hasOwnProperty("f")&&(d[a]={f:s.f.f});return JSON.stringify(d)},refreshEditor:function(n,i,d){var t,r,e=this;e.toggleEditor(n,1),t={action:"imgedit-preview",_ajax_nonce:i,postid:n,history:e.filterHistory(n,1),rand:e.intval(1e6*Math.random())},r=m('<img id="image-preview-'+n+'" alt="" />').on("load",{history:t.history},function(i){var t,e,a,o=m("#imgedit-crop-"+n),s=h;""!==i.data.history&&(a=JSON.parse(i.data.history))[a.length-1].hasOwnProperty("c")&&(s.setDisabled(m("#image-undo-"+n),!0),m("#image-undo-"+n).focus()),o.empty().append(r),t=Math.max(s.hold.w,s.hold.h),e=Math.max(m(r).width(),m(r).height()),s.hold.sizer=e<t?e/t:1,s.initCrop(n,r,o),null!=d&&d(),m("#imgedit-history-"+n).val()&&"0"===m("#imgedit-undone-"+n).val()?m("input.imgedit-submit-btn","#imgedit-panel-"+n).removeAttr("disabled"):m("input.imgedit-submit-btn","#imgedit-panel-"+n).prop("disabled",!0),s.toggleEditor(n,0)}).on("error",function(){var i=l("Could not load the preview image. Please reload the page and try again.");m("#imgedit-crop-"+n).empty().append('<div class="notice notice-error" tabindex="-1" role="alert"><p>'+i+"</p></div>"),e.toggleEditor(n,0,!0),wp.a11y.speak(i,"assertive")}).attr("src",ajaxurl+"?"+m.param(t))},action:function(t,i,e){var a,o,s,n,d,r=this;if(r.notsaved(t))return!1;if(a={action:"image-editor",_ajax_nonce:i,postid:t},"scale"===e){if(o=m("#imgedit-scale-width-"+t),s=m("#imgedit-scale-height-"+t),n=r.intval(o.val()),d=r.intval(s.val()),n<1)return o.focus(),!1;if(d<1)return s.focus(),!1;if(n===r.hold.ow||d===r.hold.oh)return!1;a.do="scale",a.fwidth=n,a.fheight=d}else{if("restore"!==e)return!1;a.do="restore"}r.toggleEditor(t,1),m.post(ajaxurl,a,function(i){m("#image-editor-"+t).empty().append(i.data.html),r.toggleEditor(t,0,!0),r._view&&r._view.refresh()}).done(function(i){i&&i.data.message.msg?wp.a11y.speak(i.data.message.msg):i&&i.data.message.error&&wp.a11y.speak(i.data.message.error)})},save:function(t,i){var e,a=this.getTarget(t),o=this.filterHistory(t,0),s=this;if(""===o)return!1;this.toggleEditor(t,1),e={action:"image-editor",_ajax_nonce:i,postid:t,history:o,target:a,context:m("#image-edit-context").length?m("#image-edit-context").val():null,do:"save"},m.post(ajaxurl,e,function(i){if(i.data.error)return m("#imgedit-response-"+t).html('<div class="notice notice-error" tabindex="-1" role="alert"><p>'+i.data.error+"</p></div>"),h.close(t),void wp.a11y.speak(i.data.error);i.data.fw&&i.data.fh&&m("#media-dims-"+t).html(i.data.fw+" × "+i.data.fh),i.data.thumbnail&&m(".thumbnail","#thumbnail-head-"+t).attr("src",""+i.data.thumbnail),i.data.msg&&(m("#imgedit-response-"+t).html('<div class="notice notice-success" tabindex="-1" role="alert"><p>'+i.data.msg+"</p></div>"),wp.a11y.speak(i.data.msg)),s._view?s._view.save():h.close(t)})},open:function(e,i,t){this._view=t;var a,o=m("#image-editor-"+e),s=m("#media-head-"+e),n=m("#imgedit-open-btn-"+e),d=n.siblings(".spinner");if(!n.hasClass("button-activated"))return d.addClass("is-active"),a={action:"image-editor",_ajax_nonce:i,postid:e,do:"open"},m.ajax({url:ajaxurl,type:"post",data:a,beforeSend:function(){n.addClass("button-activated")}}).done(function(i){var t;"-1"===i&&(t=l("Could not load the preview image."),o.html('<div class="notice notice-error" tabindex="-1" role="alert"><p>'+t+"</p></div>")),i.data&&i.data.html&&o.html(i.data.html),s.fadeOut("fast",function(){o.fadeIn("fast",function(){t&&m(document).trigger("image-editor-ui-ready")}),n.removeClass("button-activated"),d.removeClass("is-active")}),h.init(e)})},imgLoaded:function(i){var t=m("#image-preview-"+i),e=m("#imgedit-crop-"+i);void 0===this.hold.sizer&&this.init(i),this.initCrop(i,t,e),this.setCropSelection(i,{x1:0,y1:0,x2:0,y2:0,width:t.innerWidth(),height:t.innerHeight()}),this.toggleEditor(i,0,!0)},focusManager:function(){setTimeout(function(){var i=m('.notice[role="alert"]');i.length||(i=m(".imgedit-wrap").find(":tabbable:first")),i.focus()},100)},initCrop:function(o,i,t){var s=this,a=m("#imgedit-sel-width-"+o),n=m("#imgedit-sel-height-"+o),e=m(i);e.data("imgAreaSelect")||(s.iasapi=e.imgAreaSelect({parent:t,instance:!0,handles:!0,keys:!0,minWidth:3,minHeight:3,onInit:function(i){m(i).next().css("position","absolute").nextAll(".imgareaselect-outer").css("position","absolute"),t.children().on("mousedown, touchstart",function(i){var t,e,a=!1;i.shiftKey&&(t=s.iasapi.getSelection(),e=s.getSelRatio(o),a=t&&t.width&&t.height?t.width+":"+t.height:e),s.iasapi.setOptions({aspectRatio:a})})},onSelectStart:function(){h.setDisabled(m("#imgedit-crop-sel-"+o),1)},onSelectEnd:function(i,t){h.setCropSelection(o,t)},onSelectChange:function(i,t){var e=h.hold.sizer;a.val(h.round(t.width/e)),n.val(h.round(t.height/e))}}))},setCropSelection:function(i,t){var e;if(!(t=t||0)||t.width<3&&t.height<3)return this.setDisabled(m(".imgedit-crop","#imgedit-panel-"+i),1),this.setDisabled(m("#imgedit-crop-sel-"+i),1),m("#imgedit-sel-width-"+i).val(""),m("#imgedit-sel-height-"+i).val(""),m("#imgedit-selection-"+i).val(""),!1;e={x:t.x1,y:t.y1,w:t.width,h:t.height},this.setDisabled(m(".imgedit-crop","#imgedit-panel-"+i),1),m("#imgedit-selection-"+i).val(JSON.stringify(e))},close:function(i,t){if((t=t||!1)&&this.notsaved(i))return!1;this.iasapi={},this.hold={},this._view?this._view.back():m("#image-editor-"+i).fadeOut("fast",function(){m("#media-head-"+i).fadeIn("fast",function(){m("#imgedit-open-btn-"+i).focus()}),m(this).empty()})},notsaved:function(i){var t=m("#imgedit-history-"+i).val(),e=""!==t?JSON.parse(t):[];return this.intval(m("#imgedit-undone-"+i).val())<e.length&&!confirm(m("#imgedit-leaving-"+i).html())},addStep:function(i,t,e){for(var a=this,o=m("#imgedit-history-"+t),s=""!==o.val()?JSON.parse(o.val()):[],n=m("#imgedit-undone-"+t),d=a.intval(n.val());0<d;)s.pop(),d--;n.val(0),s.push(i),o.val(JSON.stringify(s)),a.refreshEditor(t,e,function(){a.setDisabled(m("#image-undo-"+t),!0),a.setDisabled(m("#image-redo-"+t),!1)})},rotate:function(i,t,e,a){if(m(a).hasClass("disabled"))return!1;this.addStep({r:{r:i,fw:this.hold.h,fh:this.hold.w}},t,e)},flip:function(i,t,e,a){if(m(a).hasClass("disabled"))return!1;this.addStep({f:{f:i,fw:this.hold.w,fh:this.hold.h}},t,e)},crop:function(i,t,e){var a=m("#imgedit-selection-"+i).val(),o=this.intval(m("#imgedit-sel-width-"+i).val()),s=this.intval(m("#imgedit-sel-height-"+i).val());if(m(e).hasClass("disabled")||""===a)return!1;0<(a=JSON.parse(a)).w&&0<a.h&&0<o&&0<s&&(a.fw=o,a.fh=s,this.addStep({c:a},i,t)),m("#imgedit-sel-width-"+i).val(""),m("#imgedit-sel-height-"+i).val("")},undo:function(e,i){var a=this,o=m("#image-undo-"+e),t=m("#imgedit-undone-"+e),s=a.intval(t.val())+1;o.hasClass("disabled")||(t.val(s),a.refreshEditor(e,i,function(){var i=m("#imgedit-history-"+e),t=""!==i.val()?JSON.parse(i.val()):[];a.setDisabled(m("#image-redo-"+e),!0),a.setDisabled(o,s<t.length),t.length===s&&m("#image-redo-"+e).focus()}))},redo:function(i,t){var e=this,a=m("#image-redo-"+i),o=m("#imgedit-undone-"+i),s=e.intval(o.val())-1;a.hasClass("disabled")||(o.val(s),e.refreshEditor(i,t,function(){e.setDisabled(m("#image-undo-"+i),!0),e.setDisabled(a,0<s),0==s&&m("#image-undo-"+i).focus()}))},setNumSelection:function(i,t){var e,a,o,s,n,d=m("#imgedit-sel-width-"+i),r=m("#imgedit-sel-height-"+i),l=this.intval(d.val()),h=this.intval(r.val()),g=m("#image-preview-"+i),c=g.height(),p=g.width(),u=this.hold.sizer,v=this.iasapi;if(!1!==this.validateNumeric(t))return l<1?(d.val(""),!1):h<1?(r.val(""),!1):void(l&&h&&(e=v.getSelection())&&(s=e.x1+Math.round(l*u),n=e.y1+Math.round(h*u),a=e.x1,o=e.y1,p<s&&(a=0,s=p,d.val(Math.round(s/u))),c<n&&(o=0,n=c,r.val(Math.round(n/u))),v.setSelection(a,o,s,n),v.update(),this.setCropSelection(i,v.getSelection())))},round:function(i){var t;return i=Math.round(i),.6<this.hold.sizer?i:"1"===(t=i.toString().slice(-1))?i-1:"9"===t?i+1:i},setRatioSelection:function(i,t,e){var a,o,s=this.intval(m("#imgedit-crop-width-"+i).val()),n=this.intval(m("#imgedit-crop-height-"+i).val()),d=m("#image-preview-"+i).height();!1!==this.validateNumeric(e)?s&&n&&(this.iasapi.setOptions({aspectRatio:s+":"+n}),(a=this.iasapi.getSelection(!0))&&(d<(o=Math.ceil(a.y1+(a.x2-a.x1)/(s/n)))&&(o=d,t?m("#imgedit-crop-height-"+i).val(""):m("#imgedit-crop-width-"+i).val("")),this.iasapi.setSelection(a.x1,a.y1,a.x2,o),this.iasapi.update())):this.iasapi.setOptions({aspectRatio:null})},validateNumeric:function(i){if(!this.intval(m(i).val()))return m(i).val(""),!1}}}(jQuery); |