1 var imageEdit;(function(a){imageEdit={iasapi:{},hold:{},postid:"",intval:function(b){return b|0},setDisabled:function(c,b){if(b){c.removeClass("disabled");a("input",c).removeAttr("disabled")}else{c.addClass("disabled");a("input",c).prop("disabled",true)}},init:function(g,e){var d=this,c=a("#image-editor-"+d.postid),b=d.intval(a("#imgedit-x-"+g).val()),f=d.intval(a("#imgedit-y-"+g).val());if(d.postid!=g&&c.length){d.close(d.postid)}d.hold.w=d.hold.ow=b;d.hold.h=d.hold.oh=f;d.hold.xy_ratio=b/f;d.hold.sizer=parseFloat(a("#imgedit-sizer-"+g).val());d.postid=g;a("#imgedit-response-"+g).empty();a('input[type="text"]',"#imgedit-panel-"+g).keypress(function(i){var h=i.keyCode;if(36<h&&h<41){a(this).blur()}if(13==h){i.preventDefault();i.stopPropagation();return false}})},toggleEditor:function(d,b){var c=a("#imgedit-wait-"+d);if(b){c.height(a("#imgedit-panel-"+d).height()).fadeIn("fast")}else{c.fadeOut("fast")}},toggleHelp:function(b){a(b).siblings(".imgedit-help").slideToggle("fast");return false},getTarget:function(b){return a('input[name="imgedit-target-'+b+'"]:checked',"#imgedit-save-target-"+b).val()||"full"},scaleChanged:function(i,b){var d=a("#imgedit-scale-width-"+i),f=a("#imgedit-scale-height-"+i),g=a("#imgedit-scale-warn-"+i),c="",e="";if(b){e=(d.val()!="")?this.intval(d.val()/this.hold.xy_ratio):"";f.val(e)}else{c=(f.val()!="")?this.intval(f.val()*this.hold.xy_ratio):"";d.val(c)}if((e&&e>this.hold.oh)||(c&&c>this.hold.ow)){g.css("visibility","visible")}else{g.css("visibility","hidden")}},getSelRatio:function(f){var b=this.hold.w,e=this.hold.h,d=this.intval(a("#imgedit-crop-width-"+f).val()),c=this.intval(a("#imgedit-crop-height-"+f).val());if(d&&c){return d+":"+c}if(b&&e){return b+":"+e}return"1:1"},filterHistory:function(j,f){var d=a("#imgedit-history-"+j).val(),b,h,e,c,g=[];if(d!=""){d=JSON.parse(d);b=this.intval(a("#imgedit-undone-"+j).val());if(b>0){while(b>0){d.pop();b--}}if(f){if(!d.length){this.hold.w=this.hold.ow;this.hold.h=this.hold.oh;return""}e=d[d.length-1];e=e.c||e.r||e.f||false;if(e){this.hold.w=e.fw;this.hold.h=e.fh}}for(h in d){c=d[h];if(c.hasOwnProperty("c")){g[h]={c:{x:c.c.x,y:c.c.y,w:c.c.w,h:c.c.h}}}else{if(c.hasOwnProperty("r")){g[h]={r:c.r.r}}else{if(c.hasOwnProperty("f")){g[h]={f:c.f.f}}}}}return JSON.stringify(g)}return""},refreshEditor:function(g,d,f){var c=this,e,b;c.toggleEditor(g,1);e={action:"imgedit-preview",_ajax_nonce:d,postid:g,history:c.filterHistory(g,1),rand:c.intval(Math.random()*1000000)};b=a('<img id="image-preview-'+g+'" />');b.load(function(){var i,h,k=a("#imgedit-crop-"+g),j=imageEdit;k.empty().append(b);i=Math.max(j.hold.w,j.hold.h);h=Math.max(a(b).width(),a(b).height());j.hold.sizer=i>h?h/i:1;j.initCrop(g,b,k);j.setCropSelection(g,0);if((typeof f!="unknown")&&f!=null){f()}if(a("#imgedit-history-"+g).val()&&a("#imgedit-undone-"+g).val()==0){a("input.imgedit-submit-btn","#imgedit-panel-"+g).removeAttr("disabled")}else{a("input.imgedit-submit-btn","#imgedit-panel-"+g).prop("disabled",true)}j.toggleEditor(g,0)}).error(function(){a("#imgedit-crop-"+g).empty().append('<div class="error"><p>'+imageEditL10n.error+"</p></div>");c.toggleEditor(g,0)}).attr("src",ajaxurl+"?"+a.param(e))},action:function(b,g,c){var j=this,e,i,f,d,k;if(j.notsaved(b)){return false}e={action:"image-editor",_ajax_nonce:g,postid:b};if("scale"==c){i=a("#imgedit-scale-width-"+b),f=a("#imgedit-scale-height-"+b),d=j.intval(i.val()),k=j.intval(f.val());if(d<1){i.focus();return false}else{if(k<1){f.focus();return false}}if(d==j.hold.ow||k==j.hold.oh){return false}e["do"]="scale";e.fwidth=d;e.fheight=k}else{if("restore"==c){e["do"]="restore"}else{return false}}j.toggleEditor(b,1);a.post(ajaxurl,e,function(h){a("#image-editor-"+b).empty().append(h);j.toggleEditor(b,0)})},save:function(f,b){var c,e=this.getTarget(f),d=this.filterHistory(f,0);if(""==d){return false}this.toggleEditor(f,1);c={action:"image-editor",_ajax_nonce:b,postid:f,history:d,target:e,"do":"save"};a.post(ajaxurl,c,function(h){var g=JSON.parse(h);if(g.error){a("#imgedit-response-"+f).html('<div class="error"><p>'+g.error+"</p><div>");imageEdit.close(f);return}if(g.fw&&g.fh){a("#media-dims-"+f).html(g.fw+" × "+g.fh)}if(g.thumbnail){a(".thumbnail","#thumbnail-head-"+f).attr("src",""+g.thumbnail)}if(g.msg){a("#imgedit-response-"+f).html('<div class="updated"><p>'+g.msg+"</p></div>")}imageEdit.close(f)})},open:function(h,d){var f,e=a("#image-editor-"+h),c=a("#media-head-"+h),b=a("#imgedit-open-btn-"+h),g=b.siblings("img");b.prop("disabled",true);g.css("visibility","visible");f={action:"image-editor",_ajax_nonce:d,postid:h,"do":"open"};e.load(ajaxurl,f,function(){e.fadeIn("fast");c.fadeOut("fast",function(){b.removeAttr("disabled");g.css("visibility","hidden")})})},imgLoaded:function(d){var b=a("#image-preview-"+d),c=a("#imgedit-crop-"+d);this.initCrop(d,b,c);this.setCropSelection(d,0);this.toggleEditor(d,0)},initCrop:function(g,e,c){var b=this,d=a("#imgedit-sel-width-"+g),f=a("#imgedit-sel-height-"+g);b.iasapi=a(e).imgAreaSelect({parent:c,instance:true,handles:true,keys:true,minWidth:3,minHeight:3,onInit:function(h,i){c.children().mousedown(function(m){var k=false,l,j;if(m.shiftKey){l=b.iasapi.getSelection();j=b.getSelRatio(g);k=(l&&l.width&&l.height)?l.width+":"+l.height:j}b.iasapi.setOptions({aspectRatio:k})})},onSelectStart:function(h,i){imageEdit.setDisabled(a("#imgedit-crop-sel-"+g),1)},onSelectEnd:function(h,i){imageEdit.setCropSelection(g,i)},onSelectChange:function(h,j){var i=imageEdit.hold.sizer;d.val(imageEdit.round(j.width/i));f.val(imageEdit.round(j.height/i))}})},setCropSelection:function(g,f){var e,b=a("#imgedit-minthumb-"+g).val()||"128:128",d=this.hold.sizer;b=b.split(":");f=f||0;if(!f||(f.width<3&&f.height<3)){this.setDisabled(a(".imgedit-crop","#imgedit-panel-"+g),0);this.setDisabled(a("#imgedit-crop-sel-"+g),0);a("#imgedit-sel-width-"+g).val("");a("#imgedit-sel-height-"+g).val("");a("#imgedit-selection-"+g).val("");return false}if(f.width<(b[0]*d)&&f.height<(b[1]*d)){this.setDisabled(a(".imgedit-crop","#imgedit-panel-"+g),0);a("#imgedit-selection-"+g).val("");return false}e={x:f.x1,y:f.y1,w:f.width,h:f.height};this.setDisabled(a(".imgedit-crop","#imgedit-panel-"+g),1);a("#imgedit-selection-"+g).val(JSON.stringify(e))},close:function(c,b){b=b||false;if(b&&this.notsaved(c)){return false}this.iasapi={};this.hold={};a("#image-editor-"+c).fadeOut("fast",function(){a("#media-head-"+c).fadeIn("fast");a(this).empty()})},notsaved:function(e){var c=a("#imgedit-history-"+e).val(),d=(c!="")?JSON.parse(c):new Array(),b=this.intval(a("#imgedit-undone-"+e).val());if(b<d.length){if(confirm(a("#imgedit-leaving-"+e).html())){return false}return true}return false},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.setDisabled(a("#image-undo-"+h),true);c.setDisabled(a("#image-redo-"+h),false)})},rotate:function(d,e,c,b){if(a(b).hasClass("disabled")){return false}this.addStep({r:{r:d,fw:this.hold.h,fh:this.hold.w}},e,c)},flip:function(d,e,c,b){if(a(b).hasClass("disabled")){return false}this.addStep({f:{f:d,fw:this.hold.w,fh:this.hold.h}},e,c)},crop:function(g,e,c){var f=a("#imgedit-selection-"+g).val(),b=this.intval(a("#imgedit-sel-width-"+g).val()),d=this.intval(a("#imgedit-sel-height-"+g).val());if(a(c).hasClass("disabled")||f==""){return false}f=JSON.parse(f);if(f.w>0&&f.h>0&&b>0&&d>0){f.fw=b;f.fh=d;this.addStep({c:f},g,e)}},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.setDisabled(a("#image-redo-"+g),true);d.setDisabled(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.setDisabled(a("#image-undo-"+g),true);d.setDisabled(c,b>0)})},setNumSelection:function(c){var g,k=a("#imgedit-sel-width-"+c),j=a("#imgedit-sel-height-"+c),o=this.intval(k.val()),m=this.intval(j.val()),i=a("#image-preview-"+c),p=i.height(),h=i.width(),b=this.hold.sizer,f,n,e,l,d=this.iasapi;if(o<1){k.val("");return false}if(m<1){j.val("");return false}if(o&&m&&(g=d.getSelection())){e=g.x1+Math.round(o*b);l=g.y1+Math.round(m*b);f=g.x1;n=g.y1;if(e>h){f=0;e=h;k.val(Math.round(e/b))}if(l>p){n=0;l=p;j.val(Math.round(l/b))}d.setSelection(f,n,e,l);d.update();this.setCropSelection(c,d.getSelection())}},round:function(b){var c;b=Math.round(b);if(this.hold.sizer>0.6){return b}c=b.toString().slice(-1);if("1"==c){return b-1}else{if("9"==c){return b+1}}return b},setRatioSelection:function(j,i,d){var f,e,b=this.intval(a("#imgedit-crop-width-"+j).val()),g=this.intval(a("#imgedit-crop-height-"+j).val()),c=a("#image-preview-"+j).height();if(!this.intval(a(d).val())){a(d).val("");return}if(b&&g){this.iasapi.setOptions({aspectRatio:b+":"+g});if(f=this.iasapi.getSelection(true)){e=Math.ceil(f.y1+((f.x2-f.x1)/(b/g)));if(e>c){e=c;if(i){a("#imgedit-crop-height-"+j).val("")}else{a("#imgedit-crop-width-"+j).val("")}}this.iasapi.setSelection(f.x1,f.y1,f.x2,e);this.iasapi.update()}}}}})(jQuery); |
1 var imageEdit; |
|
2 |
|
3 (function($) { |
|
4 imageEdit = { |
|
5 iasapi : {}, |
|
6 hold : {}, |
|
7 postid : '', |
|
8 |
|
9 intval : function(f) { |
|
10 return f | 0; |
|
11 }, |
|
12 |
|
13 setDisabled : function(el, s) { |
|
14 if ( s ) { |
|
15 el.removeClass('disabled'); |
|
16 $('input', el).removeAttr('disabled'); |
|
17 } else { |
|
18 el.addClass('disabled'); |
|
19 $('input', el).prop('disabled', true); |
|
20 } |
|
21 }, |
|
22 |
|
23 init : function(postid, nonce) { |
|
24 var t = this, old = $('#image-editor-' + t.postid), |
|
25 x = t.intval( $('#imgedit-x-' + postid).val() ), |
|
26 y = t.intval( $('#imgedit-y-' + postid).val() ); |
|
27 |
|
28 if ( t.postid != postid && old.length ) |
|
29 t.close(t.postid); |
|
30 |
|
31 t.hold['w'] = t.hold['ow'] = x; |
|
32 t.hold['h'] = t.hold['oh'] = y; |
|
33 t.hold['xy_ratio'] = x / y; |
|
34 t.hold['sizer'] = parseFloat( $('#imgedit-sizer-' + postid).val() ); |
|
35 t.postid = postid; |
|
36 $('#imgedit-response-' + postid).empty(); |
|
37 |
|
38 $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) { |
|
39 var k = e.keyCode; |
|
40 |
|
41 if ( 36 < k && k < 41 ) |
|
42 $(this).blur() |
|
43 |
|
44 if ( 13 == k ) { |
|
45 e.preventDefault(); |
|
46 e.stopPropagation(); |
|
47 return false; |
|
48 } |
|
49 }); |
|
50 }, |
|
51 |
|
52 toggleEditor : function(postid, toggle) { |
|
53 var wait = $('#imgedit-wait-' + postid); |
|
54 |
|
55 if ( toggle ) |
|
56 wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast'); |
|
57 else |
|
58 wait.fadeOut('fast'); |
|
59 }, |
|
60 |
|
61 toggleHelp : function(el) { |
|
62 $(el).siblings('.imgedit-help').slideToggle('fast'); |
|
63 return false; |
|
64 }, |
|
65 |
|
66 getTarget : function(postid) { |
|
67 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full'; |
|
68 }, |
|
69 |
|
70 scaleChanged : function(postid, x) { |
|
71 var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid), |
|
72 warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = ''; |
|
73 |
|
74 if ( x ) { |
|
75 h1 = (w.val() != '') ? Math.round( w.val() / this.hold['xy_ratio'] ) : ''; |
|
76 h.val( h1 ); |
|
77 } else { |
|
78 w1 = (h.val() != '') ? Math.round( h.val() * this.hold['xy_ratio'] ) : ''; |
|
79 w.val( w1 ); |
|
80 } |
|
81 |
|
82 if ( ( h1 && h1 > this.hold['oh'] ) || ( w1 && w1 > this.hold['ow'] ) ) |
|
83 warn.css('visibility', 'visible'); |
|
84 else |
|
85 warn.css('visibility', 'hidden'); |
|
86 }, |
|
87 |
|
88 getSelRatio : function(postid) { |
|
89 var x = this.hold['w'], y = this.hold['h'], |
|
90 X = this.intval( $('#imgedit-crop-width-' + postid).val() ), |
|
91 Y = this.intval( $('#imgedit-crop-height-' + postid).val() ); |
|
92 |
|
93 if ( X && Y ) |
|
94 return X + ':' + Y; |
|
95 |
|
96 if ( x && y ) |
|
97 return x + ':' + y; |
|
98 |
|
99 return '1:1'; |
|
100 }, |
|
101 |
|
102 filterHistory : function(postid, setSize) { |
|
103 // apply undo state to history |
|
104 var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = []; |
|
105 |
|
106 if ( history != '' ) { |
|
107 history = JSON.parse(history); |
|
108 pop = this.intval( $('#imgedit-undone-' + postid).val() ); |
|
109 if ( pop > 0 ) { |
|
110 while ( pop > 0 ) { |
|
111 history.pop(); |
|
112 pop--; |
|
113 } |
|
114 } |
|
115 |
|
116 if ( setSize ) { |
|
117 if ( !history.length ) { |
|
118 this.hold['w'] = this.hold['ow']; |
|
119 this.hold['h'] = this.hold['oh']; |
|
120 return ''; |
|
121 } |
|
122 |
|
123 // restore |
|
124 o = history[history.length - 1]; |
|
125 o = o.c || o.r || o.f || false; |
|
126 |
|
127 if ( o ) { |
|
128 this.hold['w'] = o.fw; |
|
129 this.hold['h'] = o.fh; |
|
130 } |
|
131 } |
|
132 |
|
133 // filter the values |
|
134 for ( n in history ) { |
|
135 i = history[n]; |
|
136 if ( i.hasOwnProperty('c') ) { |
|
137 op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } }; |
|
138 } else if ( i.hasOwnProperty('r') ) { |
|
139 op[n] = { 'r': i.r.r }; |
|
140 } else if ( i.hasOwnProperty('f') ) { |
|
141 op[n] = { 'f': i.f.f }; |
|
142 } |
|
143 } |
|
144 return JSON.stringify(op); |
|
145 } |
|
146 return ''; |
|
147 }, |
|
148 |
|
149 refreshEditor : function(postid, nonce, callback) { |
|
150 var t = this, data, img; |
|
151 |
|
152 t.toggleEditor(postid, 1); |
|
153 data = { |
|
154 'action': 'imgedit-preview', |
|
155 '_ajax_nonce': nonce, |
|
156 'postid': postid, |
|
157 'history': t.filterHistory(postid, 1), |
|
158 'rand': t.intval(Math.random() * 1000000) |
|
159 }; |
|
160 |
|
161 img = $('<img id="image-preview-' + postid + '" />'); |
|
162 img.load( function() { |
|
163 var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit; |
|
164 |
|
165 parent.empty().append(img); |
|
166 |
|
167 // w, h are the new full size dims |
|
168 max1 = Math.max( t.hold.w, t.hold.h ); |
|
169 max2 = Math.max( $(img).width(), $(img).height() ); |
|
170 t.hold['sizer'] = max1 > max2 ? max2 / max1 : 1; |
|
171 |
|
172 t.initCrop(postid, img, parent); |
|
173 t.setCropSelection(postid, 0); |
|
174 |
|
175 if ( (typeof callback != "unknown") && callback != null ) |
|
176 callback(); |
|
177 |
|
178 if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 ) |
|
179 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled'); |
|
180 else |
|
181 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true); |
|
182 |
|
183 t.toggleEditor(postid, 0); |
|
184 }).error(function(){ |
|
185 $('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>'); |
|
186 t.toggleEditor(postid, 0); |
|
187 }).attr('src', ajaxurl + '?' + $.param(data)); |
|
188 }, |
|
189 |
|
190 action : function(postid, nonce, action) { |
|
191 var t = this, data, w, h, fw, fh; |
|
192 |
|
193 if ( t.notsaved(postid) ) |
|
194 return false; |
|
195 |
|
196 data = { |
|
197 'action': 'image-editor', |
|
198 '_ajax_nonce': nonce, |
|
199 'postid': postid |
|
200 }; |
|
201 |
|
202 if ( 'scale' == action ) { |
|
203 w = $('#imgedit-scale-width-' + postid), |
|
204 h = $('#imgedit-scale-height-' + postid), |
|
205 fw = t.intval(w.val()), |
|
206 fh = t.intval(h.val()); |
|
207 |
|
208 if ( fw < 1 ) { |
|
209 w.focus(); |
|
210 return false; |
|
211 } else if ( fh < 1 ) { |
|
212 h.focus(); |
|
213 return false; |
|
214 } |
|
215 |
|
216 if ( fw == t.hold.ow || fh == t.hold.oh ) |
|
217 return false; |
|
218 |
|
219 data['do'] = 'scale'; |
|
220 data['fwidth'] = fw; |
|
221 data['fheight'] = fh; |
|
222 } else if ( 'restore' == action ) { |
|
223 data['do'] = 'restore'; |
|
224 } else { |
|
225 return false; |
|
226 } |
|
227 |
|
228 t.toggleEditor(postid, 1); |
|
229 $.post(ajaxurl, data, function(r) { |
|
230 $('#image-editor-' + postid).empty().append(r); |
|
231 t.toggleEditor(postid, 0); |
|
232 }); |
|
233 }, |
|
234 |
|
235 save : function(postid, nonce) { |
|
236 var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0); |
|
237 |
|
238 if ( '' == history ) |
|
239 return false; |
|
240 |
|
241 this.toggleEditor(postid, 1); |
|
242 data = { |
|
243 'action': 'image-editor', |
|
244 '_ajax_nonce': nonce, |
|
245 'postid': postid, |
|
246 'history': history, |
|
247 'target': target, |
|
248 'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null, |
|
249 'do': 'save' |
|
250 }; |
|
251 |
|
252 $.post(ajaxurl, data, function(r) { |
|
253 var ret = JSON.parse(r); |
|
254 |
|
255 if ( ret.error ) { |
|
256 $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p><div>'); |
|
257 imageEdit.close(postid); |
|
258 return; |
|
259 } |
|
260 |
|
261 if ( ret.fw && ret.fh ) |
|
262 $('#media-dims-' + postid).html( ret.fw + ' × ' + ret.fh ); |
|
263 |
|
264 if ( ret.thumbnail ) |
|
265 $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail); |
|
266 |
|
267 if ( ret.msg ) |
|
268 $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>'); |
|
269 |
|
270 imageEdit.close(postid); |
|
271 }); |
|
272 }, |
|
273 |
|
274 open : function(postid, nonce) { |
|
275 var data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid), |
|
276 btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner'); |
|
277 |
|
278 btn.prop('disabled', true); |
|
279 spin.show(); |
|
280 |
|
281 data = { |
|
282 'action': 'image-editor', |
|
283 '_ajax_nonce': nonce, |
|
284 'postid': postid, |
|
285 'do': 'open' |
|
286 }; |
|
287 |
|
288 elem.load(ajaxurl, data, function() { |
|
289 elem.fadeIn('fast'); |
|
290 head.fadeOut('fast', function(){ |
|
291 btn.removeAttr('disabled'); |
|
292 spin.hide(); |
|
293 }); |
|
294 }); |
|
295 }, |
|
296 |
|
297 imgLoaded : function(postid) { |
|
298 var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid); |
|
299 |
|
300 this.initCrop(postid, img, parent); |
|
301 this.setCropSelection(postid, 0); |
|
302 this.toggleEditor(postid, 0); |
|
303 }, |
|
304 |
|
305 initCrop : function(postid, image, parent) { |
|
306 var t = this, selW = $('#imgedit-sel-width-' + postid), |
|
307 selH = $('#imgedit-sel-height-' + postid); |
|
308 |
|
309 t.iasapi = $(image).imgAreaSelect({ |
|
310 parent: parent, |
|
311 instance: true, |
|
312 handles: true, |
|
313 keys: true, |
|
314 minWidth: 3, |
|
315 minHeight: 3, |
|
316 |
|
317 onInit: function(img, c) { |
|
318 parent.children().mousedown(function(e){ |
|
319 var ratio = false, sel, defRatio; |
|
320 |
|
321 if ( e.shiftKey ) { |
|
322 sel = t.iasapi.getSelection(); |
|
323 defRatio = t.getSelRatio(postid); |
|
324 ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio; |
|
325 } |
|
326 |
|
327 t.iasapi.setOptions({ |
|
328 aspectRatio: ratio |
|
329 }); |
|
330 }); |
|
331 }, |
|
332 |
|
333 onSelectStart: function(img, c) { |
|
334 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1); |
|
335 }, |
|
336 |
|
337 onSelectEnd: function(img, c) { |
|
338 imageEdit.setCropSelection(postid, c); |
|
339 }, |
|
340 |
|
341 onSelectChange: function(img, c) { |
|
342 var sizer = imageEdit.hold.sizer; |
|
343 selW.val( imageEdit.round(c.width / sizer) ); |
|
344 selH.val( imageEdit.round(c.height / sizer) ); |
|
345 } |
|
346 }); |
|
347 }, |
|
348 |
|
349 setCropSelection : function(postid, c) { |
|
350 var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128', |
|
351 sizer = this.hold['sizer']; |
|
352 min = min.split(':'); |
|
353 c = c || 0; |
|
354 |
|
355 if ( !c || ( c.width < 3 && c.height < 3 ) ) { |
|
356 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0); |
|
357 this.setDisabled($('#imgedit-crop-sel-' + postid), 0); |
|
358 $('#imgedit-sel-width-' + postid).val(''); |
|
359 $('#imgedit-sel-height-' + postid).val(''); |
|
360 $('#imgedit-selection-' + postid).val(''); |
|
361 return false; |
|
362 } |
|
363 |
|
364 if ( c.width < (min[0] * sizer) && c.height < (min[1] * sizer) ) { |
|
365 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0); |
|
366 $('#imgedit-selection-' + postid).val(''); |
|
367 return false; |
|
368 } |
|
369 |
|
370 sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height }; |
|
371 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1); |
|
372 $('#imgedit-selection-' + postid).val( JSON.stringify(sel) ); |
|
373 }, |
|
374 |
|
375 close : function(postid, warn) { |
|
376 warn = warn || false; |
|
377 |
|
378 if ( warn && this.notsaved(postid) ) |
|
379 return false; |
|
380 |
|
381 this.iasapi = {}; |
|
382 this.hold = {}; |
|
383 $('#image-editor-' + postid).fadeOut('fast', function() { |
|
384 $('#media-head-' + postid).fadeIn('fast'); |
|
385 $(this).empty(); |
|
386 }); |
|
387 }, |
|
388 |
|
389 notsaved : function(postid) { |
|
390 var h = $('#imgedit-history-' + postid).val(), |
|
391 history = (h != '') ? JSON.parse(h) : new Array(), |
|
392 pop = this.intval( $('#imgedit-undone-' + postid).val() ); |
|
393 |
|
394 if ( pop < history.length ) { |
|
395 if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) |
|
396 return false; |
|
397 return true; |
|
398 } |
|
399 return false; |
|
400 }, |
|
401 |
|
402 addStep : function(op, postid, nonce) { |
|
403 var t = this, elem = $('#imgedit-history-' + postid), |
|
404 history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array(), |
|
405 undone = $('#imgedit-undone-' + postid), |
|
406 pop = t.intval(undone.val()); |
|
407 |
|
408 while ( pop > 0 ) { |
|
409 history.pop(); |
|
410 pop--; |
|
411 } |
|
412 undone.val(0); // reset |
|
413 |
|
414 history.push(op); |
|
415 elem.val( JSON.stringify(history) ); |
|
416 |
|
417 t.refreshEditor(postid, nonce, function() { |
|
418 t.setDisabled($('#image-undo-' + postid), true); |
|
419 t.setDisabled($('#image-redo-' + postid), false); |
|
420 }); |
|
421 }, |
|
422 |
|
423 rotate : function(angle, postid, nonce, t) { |
|
424 if ( $(t).hasClass('disabled') ) |
|
425 return false; |
|
426 |
|
427 this.addStep({ 'r': { 'r': angle, 'fw': this.hold['h'], 'fh': this.hold['w'] }}, postid, nonce); |
|
428 }, |
|
429 |
|
430 flip : function (axis, postid, nonce, t) { |
|
431 if ( $(t).hasClass('disabled') ) |
|
432 return false; |
|
433 |
|
434 this.addStep({ 'f': { 'f': axis, 'fw': this.hold['w'], 'fh': this.hold['h'] }}, postid, nonce); |
|
435 }, |
|
436 |
|
437 crop : function (postid, nonce, t) { |
|
438 var sel = $('#imgedit-selection-' + postid).val(), |
|
439 w = this.intval( $('#imgedit-sel-width-' + postid).val() ), |
|
440 h = this.intval( $('#imgedit-sel-height-' + postid).val() ); |
|
441 |
|
442 if ( $(t).hasClass('disabled') || sel == '' ) |
|
443 return false; |
|
444 |
|
445 sel = JSON.parse(sel); |
|
446 if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) { |
|
447 sel['fw'] = w; |
|
448 sel['fh'] = h; |
|
449 this.addStep({ 'c': sel }, postid, nonce); |
|
450 } |
|
451 }, |
|
452 |
|
453 undo : function (postid, nonce) { |
|
454 var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid), |
|
455 pop = t.intval( elem.val() ) + 1; |
|
456 |
|
457 if ( button.hasClass('disabled') ) |
|
458 return; |
|
459 |
|
460 elem.val(pop); |
|
461 t.refreshEditor(postid, nonce, function() { |
|
462 var elem = $('#imgedit-history-' + postid), |
|
463 history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array(); |
|
464 |
|
465 t.setDisabled($('#image-redo-' + postid), true); |
|
466 t.setDisabled(button, pop < history.length); |
|
467 }); |
|
468 }, |
|
469 |
|
470 redo : function(postid, nonce) { |
|
471 var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid), |
|
472 pop = t.intval( elem.val() ) - 1; |
|
473 |
|
474 if ( button.hasClass('disabled') ) |
|
475 return; |
|
476 |
|
477 elem.val(pop); |
|
478 t.refreshEditor(postid, nonce, function() { |
|
479 t.setDisabled($('#image-undo-' + postid), true); |
|
480 t.setDisabled(button, pop > 0); |
|
481 }); |
|
482 }, |
|
483 |
|
484 setNumSelection : function(postid) { |
|
485 var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid), |
|
486 x = this.intval( elX.val() ), y = this.intval( elY.val() ), |
|
487 img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(), |
|
488 sizer = this.hold['sizer'], x1, y1, x2, y2, ias = this.iasapi; |
|
489 |
|
490 if ( x < 1 ) { |
|
491 elX.val(''); |
|
492 return false; |
|
493 } |
|
494 |
|
495 if ( y < 1 ) { |
|
496 elY.val(''); |
|
497 return false; |
|
498 } |
|
499 |
|
500 if ( x && y && ( sel = ias.getSelection() ) ) { |
|
501 x2 = sel.x1 + Math.round( x * sizer ); |
|
502 y2 = sel.y1 + Math.round( y * sizer ); |
|
503 x1 = sel.x1; |
|
504 y1 = sel.y1; |
|
505 |
|
506 if ( x2 > imgw ) { |
|
507 x1 = 0; |
|
508 x2 = imgw; |
|
509 elX.val( Math.round( x2 / sizer ) ); |
|
510 } |
|
511 |
|
512 if ( y2 > imgh ) { |
|
513 y1 = 0; |
|
514 y2 = imgh; |
|
515 elY.val( Math.round( y2 / sizer ) ); |
|
516 } |
|
517 |
|
518 ias.setSelection( x1, y1, x2, y2 ); |
|
519 ias.update(); |
|
520 this.setCropSelection(postid, ias.getSelection()); |
|
521 } |
|
522 }, |
|
523 |
|
524 round : function(num) { |
|
525 var s; |
|
526 num = Math.round(num); |
|
527 |
|
528 if ( this.hold.sizer > 0.6 ) |
|
529 return num; |
|
530 |
|
531 s = num.toString().slice(-1); |
|
532 |
|
533 if ( '1' == s ) |
|
534 return num - 1; |
|
535 else if ( '9' == s ) |
|
536 return num + 1; |
|
537 |
|
538 return num; |
|
539 }, |
|
540 |
|
541 setRatioSelection : function(postid, n, el) { |
|
542 var sel, r, x = this.intval( $('#imgedit-crop-width-' + postid).val() ), |
|
543 y = this.intval( $('#imgedit-crop-height-' + postid).val() ), |
|
544 h = $('#image-preview-' + postid).height(); |
|
545 |
|
546 if ( !this.intval( $(el).val() ) ) { |
|
547 $(el).val(''); |
|
548 return; |
|
549 } |
|
550 |
|
551 if ( x && y ) { |
|
552 this.iasapi.setOptions({ |
|
553 aspectRatio: x + ':' + y |
|
554 }); |
|
555 |
|
556 if ( sel = this.iasapi.getSelection(true) ) { |
|
557 r = Math.ceil( sel.y1 + ((sel.x2 - sel.x1) / (x / y)) ); |
|
558 |
|
559 if ( r > h ) { |
|
560 r = h; |
|
561 if ( n ) |
|
562 $('#imgedit-crop-height-' + postid).val(''); |
|
563 else |
|
564 $('#imgedit-crop-width-' + postid).val(''); |
|
565 } |
|
566 |
|
567 this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r ); |
|
568 this.iasapi.update(); |
|
569 } |
|
570 } |
|
571 } |
|
572 } |
|
573 })(jQuery); |