1 line
9.9 KiB
JavaScript
1 line
9.9 KiB
JavaScript
!function(m){var l=window.imageEdit={iasapi:{},hold:{},postid:"",_view:!1,handleCropToolClick:function(i,t,e){var a=m("#image-preview-"+i),s=this.iasapi.getSelection();isNaN(s.x1)&&(this.setCropSelection(i,{x1:0,y1:0,x2:a.innerWidth(),y2:a.innerHeight(),width:a.innerWidth(),height:a.innerHeight()}),s=this.iasapi.getSelection()),0===s.x1&&0===s.y1&&0===s.x2&&0===s.y2?(this.iasapi.setSelection(0,0,a.innerWidth(),a.innerHeight(),!0),this.iasapi.setOptions({show:!0}),this.iasapi.update()):l.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()),s=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=s,t.hold.xy_ratio=a/s,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})},toggleEditor:function(i,t){var e=m("#imgedit-wait-"+i);t?e.fadeIn("fast"):e.fadeOut("fast")},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),s=m("#imgedit-scale-height-"+i),o=m("#imgedit-scale-warn-"+i),n="",d="";!1!==this.validateNumeric(e)&&(t?(d=""!==a.val()?Math.round(a.val()/this.hold.xy_ratio):"",s.val(d)):(n=""!==s.val()?Math.round(s.val()*this.hold.xy_ratio):"",a.val(n)),d&&d>this.hold.oh||n&&n>this.hold.ow?o.css("visibility","visible"):o.css("visibility","hidden"))},getSelRatio:function(i){var t=this.hold.w,e=this.hold.h,a=this.intval(m("#imgedit-crop-width-"+i).val()),s=this.intval(m("#imgedit-crop-height-"+i).val());return a&&s?a+":"+s:t&&e?t+":"+e:"1:1"},filterHistory:function(i,t){var e,a,s,o,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,"";(s=(s=n[n.length-1]).c||s.r||s.f||!1)&&(this.hold.w=s.fw,this.hold.h=s.fh)}for(a in n)(o=n[a]).hasOwnProperty("c")?d[a]={c:{x:o.c.x,y:o.c.y,w:o.c.w,h:o.c.h}}:o.hasOwnProperty("r")?d[a]={r:o.r.r}:o.hasOwnProperty("f")&&(d[a]={f:o.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,s=m("#imgedit-crop-"+n),o=l;""!==i.data.history&&(a=JSON.parse(i.data.history))[a.length-1].hasOwnProperty("c")&&(o.setDisabled(m("#image-undo-"+n),!0),m("#image-undo-"+n).focus()),s.empty().append(r),t=Math.max(o.hold.w,o.hold.h),e=Math.max(m(r).width(),m(r).height()),o.hold.sizer=e<t?e/t:1,o.initCrop(n,r,s),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),o.toggleEditor(n,0)}).on("error",function(){m("#imgedit-crop-"+n).empty().append('<div class="error"><p>'+imageEditL10n.error+"</p></div>"),e.toggleEditor(n,0)}).attr("src",ajaxurl+"?"+m.param(t))},action:function(t,i,e){var a,s,o,n,d,r=this;if(r.notsaved(t))return!1;if(a={action:"image-editor",_ajax_nonce:i,postid:t},"scale"===e){if(s=m("#imgedit-scale-width-"+t),o=m("#imgedit-scale-height-"+t),n=r.intval(s.val()),d=r.intval(o.val()),n<1)return s.focus(),!1;if(d<1)return o.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),r.toggleEditor(t,0),r._view&&r._view.refresh()})},save:function(e,i){var t,a=this.getTarget(e),s=this.filterHistory(e,0),o=this;if(""===s)return!1;this.toggleEditor(e,1),t={action:"image-editor",_ajax_nonce:i,postid:e,history:s,target:a,context:m("#image-edit-context").length?m("#image-edit-context").val():null,do:"save"},m.post(ajaxurl,t,function(i){var t=JSON.parse(i);if(t.error)return m("#imgedit-response-"+e).html('<div class="error"><p>'+t.error+"</p></div>"),void l.close(e);t.fw&&t.fh&&m("#media-dims-"+e).html(t.fw+" × "+t.fh),t.thumbnail&&m(".thumbnail","#thumbnail-head-"+e).attr("src",""+t.thumbnail),t.msg&&m("#imgedit-response-"+e).html('<div class="updated"><p>'+t.msg+"</p></div>"),o._view?o._view.save():l.close(e)})},open:function(t,i,e){this._view=e;var a,s=m("#image-editor-"+t),o=m("#media-head-"+t),n=m("#imgedit-open-btn-"+t),d=n.siblings(".spinner");if(!n.hasClass("button-activated"))return d.addClass("is-active"),a={action:"image-editor",_ajax_nonce:i,postid:t,do:"open"},m.ajax({url:ajaxurl,type:"post",data:a,beforeSend:function(){n.addClass("button-activated")}}).done(function(i){s.html(i),o.fadeOut("fast",function(){s.fadeIn("fast"),n.removeClass("button-activated"),d.removeClass("is-active")}),l.init(t)})},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),m(".imgedit-wrap .imgedit-help-toggle").eq(0).focus()},initCrop:function(s,i,t){var o=this,a=m("#imgedit-sel-width-"+s),n=m("#imgedit-sel-height-"+s);o.iasapi=m(i).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=o.iasapi.getSelection(),e=o.getSelRatio(s),a=t&&t.width&&t.height?t.width+":"+t.height:e),o.iasapi.setOptions({aspectRatio:a})})},onSelectStart:function(){l.setDisabled(m("#imgedit-crop-sel-"+s),1)},onSelectEnd:function(i,t){l.setCropSelection(s,t)},onSelectChange:function(i,t){var e=l.hold.sizer;a.val(l.round(t.width/e)),n.val(l.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,s=m("#imgedit-history-"+t),o=""!==s.val()?JSON.parse(s.val()):[],n=m("#imgedit-undone-"+t),d=a.intval(n.val());0<d;)o.pop(),d--;n.val(0),o.push(i),s.val(JSON.stringify(o)),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(),s=this.intval(m("#imgedit-sel-width-"+i).val()),o=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<s&&0<o&&(a.fw=s,a.fh=o,this.addStep({c:a},i,t))},undo:function(e,i){var a=this,s=m("#image-undo-"+e),t=m("#imgedit-undone-"+e),o=a.intval(t.val())+1;s.hasClass("disabled")||(t.val(o),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(s,o<t.length),t.length===o&&m("#image-redo-"+e).focus()}))},redo:function(i,t){var e=this,a=m("#image-redo-"+i),s=m("#imgedit-undone-"+i),o=e.intval(s.val())-1;a.hasClass("disabled")||(s.val(o),e.refreshEditor(i,t,function(){e.setDisabled(m("#image-undo-"+i),!0),e.setDisabled(a,0<o),0==o&&m("#image-undo-"+i).focus()}))},setNumSelection:function(i,t){var e,a,s,o,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(),v=g.width(),p=this.hold.sizer,u=this.iasapi;if(!1!==this.validateNumeric(t))return l<1?(d.val(""),!1):h<1?(r.val(""),!1):void(l&&h&&(e=u.getSelection())&&(o=e.x1+Math.round(l*p),n=e.y1+Math.round(h*p),a=e.x1,s=e.y1,v<o&&(a=0,o=v,d.val(Math.round(o/p))),c<n&&(s=0,n=c,r.val(Math.round(n/p))),u.setSelection(a,s,o,n),u.update(),this.setCropSelection(i,u.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,s,o=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)&&o&&n&&(this.iasapi.setOptions({aspectRatio:o+":"+n}),(a=this.iasapi.getSelection(!0))&&(d<(s=Math.ceil(a.y1+(a.x2-a.x1)/(o/n)))&&(s=d,t?m("#imgedit-crop-height-"+i).val(""):m("#imgedit-crop-width-"+i).val("")),this.iasapi.setSelection(a.x1,a.y1,a.x2,s),this.iasapi.update()))},validateNumeric:function(i){if(!this.intval(m(i).val()))return m(i).val(""),!1}}}(jQuery); |