1 /* global imageEditL10n, ajaxurl, confirm */
4 var imageEdit = window.imageEdit = {
10 intval : function(f) {
14 setDisabled : function(el, s) {
16 el.removeClass('disabled');
17 $('input', el).removeAttr('disabled');
19 el.addClass('disabled');
20 $('input', el).prop('disabled', true);
24 init : function(postid) {
25 var t = this, old = $('#image-editor-' + t.postid),
26 x = t.intval( $('#imgedit-x-' + postid).val() ),
27 y = t.intval( $('#imgedit-y-' + postid).val() );
29 if ( t.postid !== postid && old.length ) {
33 t.hold.w = t.hold.ow = x;
34 t.hold.h = t.hold.oh = y;
35 t.hold.xy_ratio = x / y;
36 t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() );
38 $('#imgedit-response-' + postid).empty();
40 $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
43 if ( 36 < k && k < 41 ) {
55 toggleEditor : function(postid, toggle) {
56 var wait = $('#imgedit-wait-' + postid);
59 wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
65 toggleHelp : function(el) {
66 $( el ).parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' );
70 getTarget : function(postid) {
71 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
74 scaleChanged : function(postid, x) {
75 var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
76 warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
79 h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : '';
82 w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : '';
86 if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
87 warn.css('visibility', 'visible');
89 warn.css('visibility', 'hidden');
93 getSelRatio : function(postid) {
94 var x = this.hold.w, y = this.hold.h,
95 X = this.intval( $('#imgedit-crop-width-' + postid).val() ),
96 Y = this.intval( $('#imgedit-crop-height-' + postid).val() );
109 filterHistory : function(postid, setSize) {
110 // apply undo state to history
111 var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
113 if ( history !== '' ) {
114 history = JSON.parse(history);
115 pop = this.intval( $('#imgedit-undone-' + postid).val() );
124 if ( !history.length ) {
125 this.hold.w = this.hold.ow;
126 this.hold.h = this.hold.oh;
131 o = history[history.length - 1];
132 o = o.c || o.r || o.f || false;
141 for ( n in history ) {
143 if ( i.hasOwnProperty('c') ) {
144 op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } };
145 } else if ( i.hasOwnProperty('r') ) {
146 op[n] = { 'r': i.r.r };
147 } else if ( i.hasOwnProperty('f') ) {
148 op[n] = { 'f': i.f.f };
151 return JSON.stringify(op);
156 refreshEditor : function(postid, nonce, callback) {
157 var t = this, data, img;
159 t.toggleEditor(postid, 1);
161 'action': 'imgedit-preview',
162 '_ajax_nonce': nonce,
164 'history': t.filterHistory(postid, 1),
165 'rand': t.intval(Math.random() * 1000000)
168 img = $('<img id="image-preview-' + postid + '" />')
169 .on('load', function() {
170 var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit;
172 parent.empty().append(img);
174 // w, h are the new full size dims
175 max1 = Math.max( t.hold.w, t.hold.h );
176 max2 = Math.max( $(img).width(), $(img).height() );
177 t.hold.sizer = max1 > max2 ? max2 / max1 : 1;
179 t.initCrop(postid, img, parent);
180 t.setCropSelection(postid, 0);
182 if ( (typeof callback !== 'undefined') && callback !== null ) {
186 if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
187 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
189 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
192 t.toggleEditor(postid, 0);
194 .on('error', function() {
195 $('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
196 t.toggleEditor(postid, 0);
198 .attr('src', ajaxurl + '?' + $.param(data));
201 action : function(postid, nonce, action) {
202 var t = this, data, w, h, fw, fh;
204 if ( t.notsaved(postid) ) {
209 'action': 'image-editor',
210 '_ajax_nonce': nonce,
214 if ( 'scale' === action ) {
215 w = $('#imgedit-scale-width-' + postid),
216 h = $('#imgedit-scale-height-' + postid),
217 fw = t.intval(w.val()),
218 fh = t.intval(h.val());
223 } else if ( fh < 1 ) {
228 if ( fw === t.hold.ow || fh === t.hold.oh ) {
232 data['do'] = 'scale';
235 } else if ( 'restore' === action ) {
236 data['do'] = 'restore';
241 t.toggleEditor(postid, 1);
242 $.post(ajaxurl, data, function(r) {
243 $('#image-editor-' + postid).empty().append(r);
244 t.toggleEditor(postid, 0);
245 // refresh the attachment model so that changes propagate
252 save : function(postid, nonce) {
254 target = this.getTarget(postid),
255 history = this.filterHistory(postid, 0),
258 if ( '' === history ) {
262 this.toggleEditor(postid, 1);
264 'action': 'image-editor',
265 '_ajax_nonce': nonce,
269 'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null,
273 $.post(ajaxurl, data, function(r) {
274 var ret = JSON.parse(r);
277 $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p></div>');
278 imageEdit.close(postid);
282 if ( ret.fw && ret.fh ) {
283 $('#media-dims-' + postid).html( ret.fw + ' × ' + ret.fh );
286 if ( ret.thumbnail ) {
287 $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
291 $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
297 imageEdit.close(postid);
302 open : function( postid, nonce, view ) {
305 var dfd, data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
306 btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
308 btn.prop('disabled', true);
312 'action': 'image-editor',
313 '_ajax_nonce': nonce,
322 }).done(function( html ) {
324 head.fadeOut('fast', function(){
326 btn.removeAttr('disabled');
334 imgLoaded : function(postid) {
335 var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
337 this.initCrop(postid, img, parent);
338 this.setCropSelection(postid, 0);
339 this.toggleEditor(postid, 0);
342 initCrop : function(postid, image, parent) {
344 selW = $('#imgedit-sel-width-' + postid),
345 selH = $('#imgedit-sel-height-' + postid),
348 t.iasapi = $(image).imgAreaSelect({
356 onInit: function( img ) {
357 // Ensure that the imgareaselect wrapper elements are position:absolute
358 // (even if we're in a position:fixed modal)
360 $img.next().css( 'position', 'absolute' )
361 .nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' );
363 parent.children().mousedown(function(e){
364 var ratio = false, sel, defRatio;
367 sel = t.iasapi.getSelection();
368 defRatio = t.getSelRatio(postid);
369 ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio;
372 t.iasapi.setOptions({
378 onSelectStart: function() {
379 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
382 onSelectEnd: function(img, c) {
383 imageEdit.setCropSelection(postid, c);
386 onSelectChange: function(img, c) {
387 var sizer = imageEdit.hold.sizer;
388 selW.val( imageEdit.round(c.width / sizer) );
389 selH.val( imageEdit.round(c.height / sizer) );
394 setCropSelection : function(postid, c) {
399 if ( !c || ( c.width < 3 && c.height < 3 ) ) {
400 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
401 this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
402 $('#imgedit-sel-width-' + postid).val('');
403 $('#imgedit-sel-height-' + postid).val('');
404 $('#imgedit-selection-' + postid).val('');
408 sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
409 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
410 $('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
413 close : function(postid, warn) {
414 warn = warn || false;
416 if ( warn && this.notsaved(postid) ) {
423 // If we've loaded the editor in the context of a Media Modal, then switch to the previous view,
424 // whatever that might have been.
429 // In case we are not accessing the image editor in the context of a View, close the editor the old-skool way
431 $('#image-editor-' + postid).fadeOut('fast', function() {
432 $('#media-head-' + postid).fadeIn('fast');
440 notsaved : function(postid) {
441 var h = $('#imgedit-history-' + postid).val(),
442 history = ( h !== '' ) ? JSON.parse(h) : [],
443 pop = this.intval( $('#imgedit-undone-' + postid).val() );
445 if ( pop < history.length ) {
446 if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
454 addStep : function(op, postid, nonce) {
455 var t = this, elem = $('#imgedit-history-' + postid),
456 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
457 undone = $('#imgedit-undone-' + postid),
458 pop = t.intval(undone.val());
464 undone.val(0); // reset
467 elem.val( JSON.stringify(history) );
469 t.refreshEditor(postid, nonce, function() {
470 t.setDisabled($('#image-undo-' + postid), true);
471 t.setDisabled($('#image-redo-' + postid), false);
475 rotate : function(angle, postid, nonce, t) {
476 if ( $(t).hasClass('disabled') ) {
480 this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce);
483 flip : function (axis, postid, nonce, t) {
484 if ( $(t).hasClass('disabled') ) {
488 this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce);
491 crop : function (postid, nonce, t) {
492 var sel = $('#imgedit-selection-' + postid).val(),
493 w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
494 h = this.intval( $('#imgedit-sel-height-' + postid).val() );
496 if ( $(t).hasClass('disabled') || sel === '' ) {
500 sel = JSON.parse(sel);
501 if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
504 this.addStep({ 'c': sel }, postid, nonce);
508 undo : function (postid, nonce) {
509 var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
510 pop = t.intval( elem.val() ) + 1;
512 if ( button.hasClass('disabled') ) {
517 t.refreshEditor(postid, nonce, function() {
518 var elem = $('#imgedit-history-' + postid),
519 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
521 t.setDisabled($('#image-redo-' + postid), true);
522 t.setDisabled(button, pop < history.length);
526 redo : function(postid, nonce) {
527 var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
528 pop = t.intval( elem.val() ) - 1;
530 if ( button.hasClass('disabled') ) {
535 t.refreshEditor(postid, nonce, function() {
536 t.setDisabled($('#image-undo-' + postid), true);
537 t.setDisabled(button, pop > 0);
541 setNumSelection : function(postid) {
542 var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
543 x = this.intval( elX.val() ), y = this.intval( elY.val() ),
544 img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
545 sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
557 if ( x && y && ( sel = ias.getSelection() ) ) {
558 x2 = sel.x1 + Math.round( x * sizer );
559 y2 = sel.y1 + Math.round( y * sizer );
566 elX.val( Math.round( x2 / sizer ) );
572 elY.val( Math.round( y2 / sizer ) );
575 ias.setSelection( x1, y1, x2, y2 );
577 this.setCropSelection(postid, ias.getSelection());
581 round : function(num) {
583 num = Math.round(num);
585 if ( this.hold.sizer > 0.6 ) {
589 s = num.toString().slice(-1);
593 } else if ( '9' === s ) {
600 setRatioSelection : function(postid, n, el) {
601 var sel, r, x = this.intval( $('#imgedit-crop-width-' + postid).val() ),
602 y = this.intval( $('#imgedit-crop-height-' + postid).val() ),
603 h = $('#image-preview-' + postid).height();
605 if ( !this.intval( $(el).val() ) ) {
611 this.iasapi.setOptions({
612 aspectRatio: x + ':' + y
615 if ( sel = this.iasapi.getSelection(true) ) {
616 r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
621 $('#imgedit-crop-height-' + postid).val('');
623 $('#imgedit-crop-width-' + postid).val('');
627 this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
628 this.iasapi.update();