13 setDisabled : function(el, s) {
15 el.removeClass('disabled');
16 $('input', el).removeAttr('disabled');
18 el.addClass('disabled');
19 $('input', el).prop('disabled', true);
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() );
28 if ( t.postid != postid && old.length )
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() );
36 $('#imgedit-response-' + postid).empty();
38 $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
41 if ( 36 < k && k < 41 )
52 toggleEditor : function(postid, toggle) {
53 var wait = $('#imgedit-wait-' + postid);
56 wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
61 toggleHelp : function(el) {
62 $(el).siblings('.imgedit-help').slideToggle('fast');
66 getTarget : function(postid) {
67 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
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 = '';
75 h1 = (w.val() != '') ? Math.round( w.val() / this.hold['xy_ratio'] ) : '';
78 w1 = (h.val() != '') ? Math.round( h.val() * this.hold['xy_ratio'] ) : '';
82 if ( ( h1 && h1 > this.hold['oh'] ) || ( w1 && w1 > this.hold['ow'] ) )
83 warn.css('visibility', 'visible');
85 warn.css('visibility', 'hidden');
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() );
102 filterHistory : function(postid, setSize) {
103 // apply undo state to history
104 var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
106 if ( history != '' ) {
107 history = JSON.parse(history);
108 pop = this.intval( $('#imgedit-undone-' + postid).val() );
117 if ( !history.length ) {
118 this.hold['w'] = this.hold['ow'];
119 this.hold['h'] = this.hold['oh'];
124 o = history[history.length - 1];
125 o = o.c || o.r || o.f || false;
128 this.hold['w'] = o.fw;
129 this.hold['h'] = o.fh;
134 for ( n in history ) {
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 };
144 return JSON.stringify(op);
149 refreshEditor : function(postid, nonce, callback) {
150 var t = this, data, img;
152 t.toggleEditor(postid, 1);
154 'action': 'imgedit-preview',
155 '_ajax_nonce': nonce,
157 'history': t.filterHistory(postid, 1),
158 'rand': t.intval(Math.random() * 1000000)
161 img = $('<img id="image-preview-' + postid + '" />');
162 img.load( function() {
163 var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit;
165 parent.empty().append(img);
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;
172 t.initCrop(postid, img, parent);
173 t.setCropSelection(postid, 0);
175 if ( (typeof callback != "unknown") && callback != null )
178 if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 )
179 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
181 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
183 t.toggleEditor(postid, 0);
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));
190 action : function(postid, nonce, action) {
191 var t = this, data, w, h, fw, fh;
193 if ( t.notsaved(postid) )
197 'action': 'image-editor',
198 '_ajax_nonce': nonce,
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());
211 } else if ( fh < 1 ) {
216 if ( fw == t.hold.ow || fh == t.hold.oh )
219 data['do'] = 'scale';
221 data['fheight'] = fh;
222 } else if ( 'restore' == action ) {
223 data['do'] = 'restore';
228 t.toggleEditor(postid, 1);
229 $.post(ajaxurl, data, function(r) {
230 $('#image-editor-' + postid).empty().append(r);
231 t.toggleEditor(postid, 0);
235 save : function(postid, nonce) {
236 var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0);
241 this.toggleEditor(postid, 1);
243 'action': 'image-editor',
244 '_ajax_nonce': nonce,
248 'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null,
252 $.post(ajaxurl, data, function(r) {
253 var ret = JSON.parse(r);
256 $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p><div>');
257 imageEdit.close(postid);
261 if ( ret.fw && ret.fh )
262 $('#media-dims-' + postid).html( ret.fw + ' × ' + ret.fh );
265 $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
268 $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
270 imageEdit.close(postid);
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');
278 btn.prop('disabled', true);
282 'action': 'image-editor',
283 '_ajax_nonce': nonce,
288 elem.load(ajaxurl, data, function() {
290 head.fadeOut('fast', function(){
291 btn.removeAttr('disabled');
297 imgLoaded : function(postid) {
298 var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
300 this.initCrop(postid, img, parent);
301 this.setCropSelection(postid, 0);
302 this.toggleEditor(postid, 0);
305 initCrop : function(postid, image, parent) {
306 var t = this, selW = $('#imgedit-sel-width-' + postid),
307 selH = $('#imgedit-sel-height-' + postid);
309 t.iasapi = $(image).imgAreaSelect({
317 onInit: function(img, c) {
318 parent.children().mousedown(function(e){
319 var ratio = false, sel, defRatio;
322 sel = t.iasapi.getSelection();
323 defRatio = t.getSelRatio(postid);
324 ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio;
327 t.iasapi.setOptions({
333 onSelectStart: function(img, c) {
334 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
337 onSelectEnd: function(img, c) {
338 imageEdit.setCropSelection(postid, c);
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) );
349 setCropSelection : function(postid, c) {
350 var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128',
351 sizer = this.hold['sizer'];
352 min = min.split(':');
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('');
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('');
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) );
375 close : function(postid, warn) {
376 warn = warn || false;
378 if ( warn && this.notsaved(postid) )
383 $('#image-editor-' + postid).fadeOut('fast', function() {
384 $('#media-head-' + postid).fadeIn('fast');
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() );
394 if ( pop < history.length ) {
395 if ( confirm( $('#imgedit-leaving-' + postid).html() ) )
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());
412 undone.val(0); // reset
415 elem.val( JSON.stringify(history) );
417 t.refreshEditor(postid, nonce, function() {
418 t.setDisabled($('#image-undo-' + postid), true);
419 t.setDisabled($('#image-redo-' + postid), false);
423 rotate : function(angle, postid, nonce, t) {
424 if ( $(t).hasClass('disabled') )
427 this.addStep({ 'r': { 'r': angle, 'fw': this.hold['h'], 'fh': this.hold['w'] }}, postid, nonce);
430 flip : function (axis, postid, nonce, t) {
431 if ( $(t).hasClass('disabled') )
434 this.addStep({ 'f': { 'f': axis, 'fw': this.hold['w'], 'fh': this.hold['h'] }}, postid, nonce);
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() );
442 if ( $(t).hasClass('disabled') || sel == '' )
445 sel = JSON.parse(sel);
446 if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
449 this.addStep({ 'c': sel }, postid, nonce);
453 undo : function (postid, nonce) {
454 var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
455 pop = t.intval( elem.val() ) + 1;
457 if ( button.hasClass('disabled') )
461 t.refreshEditor(postid, nonce, function() {
462 var elem = $('#imgedit-history-' + postid),
463 history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array();
465 t.setDisabled($('#image-redo-' + postid), true);
466 t.setDisabled(button, pop < history.length);
470 redo : function(postid, nonce) {
471 var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
472 pop = t.intval( elem.val() ) - 1;
474 if ( button.hasClass('disabled') )
478 t.refreshEditor(postid, nonce, function() {
479 t.setDisabled($('#image-undo-' + postid), true);
480 t.setDisabled(button, pop > 0);
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;
500 if ( x && y && ( sel = ias.getSelection() ) ) {
501 x2 = sel.x1 + Math.round( x * sizer );
502 y2 = sel.y1 + Math.round( y * sizer );
509 elX.val( Math.round( x2 / sizer ) );
515 elY.val( Math.round( y2 / sizer ) );
518 ias.setSelection( x1, y1, x2, y2 );
520 this.setCropSelection(postid, ias.getSelection());
524 round : function(num) {
526 num = Math.round(num);
528 if ( this.hold.sizer > 0.6 )
531 s = num.toString().slice(-1);
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();
546 if ( !this.intval( $(el).val() ) ) {
552 this.iasapi.setOptions({
553 aspectRatio: x + ':' + y
556 if ( sel = this.iasapi.getSelection(true) ) {
557 r = Math.ceil( sel.y1 + ((sel.x2 - sel.x1) / (x / y)) );
562 $('#imgedit-crop-height-' + postid).val('');
564 $('#imgedit-crop-width-' + postid).val('');
567 this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
568 this.iasapi.update();