1 /* global imageEditL10n, ajaxurl, confirm */
4 var imageEdit = window.imageEdit = {
10 intval : function(f) {
14 setDisabled : function( el, s ) {
16 * `el` can be a single form element or a fieldset. Before #28864, the disabled state on
17 * some text fields was handled targeting $('input', el). Now we need to handle the
18 * disabled state on buttons too so we can just target `el` regardless if it's a single
19 * element or a fieldset because when a fieldset is disabled, its descendants are disabled too.
22 el.removeClass( 'disabled' ).prop( 'disabled', false );
24 el.addClass( 'disabled' ).prop( 'disabled', true );
28 init : function(postid) {
29 var t = this, old = $('#image-editor-' + t.postid),
30 x = t.intval( $('#imgedit-x-' + postid).val() ),
31 y = t.intval( $('#imgedit-y-' + postid).val() );
33 if ( t.postid !== postid && old.length ) {
37 t.hold.w = t.hold.ow = x;
38 t.hold.h = t.hold.oh = y;
39 t.hold.xy_ratio = x / y;
40 t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() );
42 $('#imgedit-response-' + postid).empty();
44 $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
47 if ( 36 < k && k < 41 ) {
59 toggleEditor : function(postid, toggle) {
60 var wait = $('#imgedit-wait-' + postid);
63 wait.fadeIn( 'fast' );
69 toggleHelp : function(el) {
72 .attr( 'aria-expanded', 'false' === $el.attr( 'aria-expanded' ) ? 'true' : 'false' )
73 .parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' );
78 getTarget : function(postid) {
79 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
82 scaleChanged : function(postid, x) {
83 var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
84 warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
87 h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : '';
90 w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : '';
94 if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
95 warn.css('visibility', 'visible');
97 warn.css('visibility', 'hidden');
101 getSelRatio : function(postid) {
102 var x = this.hold.w, y = this.hold.h,
103 X = this.intval( $('#imgedit-crop-width-' + postid).val() ),
104 Y = this.intval( $('#imgedit-crop-height-' + postid).val() );
117 filterHistory : function(postid, setSize) {
118 // apply undo state to history
119 var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
121 if ( history !== '' ) {
122 history = JSON.parse(history);
123 pop = this.intval( $('#imgedit-undone-' + postid).val() );
132 if ( !history.length ) {
133 this.hold.w = this.hold.ow;
134 this.hold.h = this.hold.oh;
139 o = history[history.length - 1];
140 o = o.c || o.r || o.f || false;
149 for ( n in history ) {
151 if ( i.hasOwnProperty('c') ) {
152 op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } };
153 } else if ( i.hasOwnProperty('r') ) {
154 op[n] = { 'r': i.r.r };
155 } else if ( i.hasOwnProperty('f') ) {
156 op[n] = { 'f': i.f.f };
159 return JSON.stringify(op);
164 refreshEditor : function(postid, nonce, callback) {
165 var t = this, data, img;
167 t.toggleEditor(postid, 1);
169 'action': 'imgedit-preview',
170 '_ajax_nonce': nonce,
172 'history': t.filterHistory(postid, 1),
173 'rand': t.intval(Math.random() * 1000000)
176 img = $( '<img id="image-preview-' + postid + '" alt="" />' )
177 .on( 'load', { history: data.history }, function( event ) {
179 parent = $( '#imgedit-crop-' + postid ),
183 if ( '' !== event.data.history ) {
184 historyObj = JSON.parse( event.data.history );
185 // If last executed action in history is a crop action.
186 if ( historyObj[historyObj.length - 1].hasOwnProperty( 'c' ) ) {
188 * A crop action has completed and the crop button gets disabled
189 * ensure the undo button is enabled.
191 t.setDisabled( $( '#image-undo-' + postid) , true );
192 // Move focus to the undo button to avoid a focus loss.
193 $( '#image-undo-' + postid ).focus();
197 parent.empty().append(img);
199 // w, h are the new full size dims
200 max1 = Math.max( t.hold.w, t.hold.h );
201 max2 = Math.max( $(img).width(), $(img).height() );
202 t.hold.sizer = max1 > max2 ? max2 / max1 : 1;
204 t.initCrop(postid, img, parent);
205 t.setCropSelection(postid, 0);
207 if ( (typeof callback !== 'undefined') && callback !== null ) {
211 if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
212 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
214 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
217 t.toggleEditor(postid, 0);
219 .on('error', function() {
220 $('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
221 t.toggleEditor(postid, 0);
223 .attr('src', ajaxurl + '?' + $.param(data));
226 action : function(postid, nonce, action) {
227 var t = this, data, w, h, fw, fh;
229 if ( t.notsaved(postid) ) {
234 'action': 'image-editor',
235 '_ajax_nonce': nonce,
239 if ( 'scale' === action ) {
240 w = $('#imgedit-scale-width-' + postid),
241 h = $('#imgedit-scale-height-' + postid),
242 fw = t.intval(w.val()),
243 fh = t.intval(h.val());
248 } else if ( fh < 1 ) {
253 if ( fw === t.hold.ow || fh === t.hold.oh ) {
257 data['do'] = 'scale';
260 } else if ( 'restore' === action ) {
261 data['do'] = 'restore';
266 t.toggleEditor(postid, 1);
267 $.post(ajaxurl, data, function(r) {
268 $('#image-editor-' + postid).empty().append(r);
269 t.toggleEditor(postid, 0);
270 // refresh the attachment model so that changes propagate
277 save : function(postid, nonce) {
279 target = this.getTarget(postid),
280 history = this.filterHistory(postid, 0),
283 if ( '' === history ) {
287 this.toggleEditor(postid, 1);
289 'action': 'image-editor',
290 '_ajax_nonce': nonce,
294 'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null,
298 $.post(ajaxurl, data, function(r) {
299 var ret = JSON.parse(r);
302 $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p></div>');
303 imageEdit.close(postid);
307 if ( ret.fw && ret.fh ) {
308 $('#media-dims-' + postid).html( ret.fw + ' × ' + ret.fh );
311 if ( ret.thumbnail ) {
312 $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
316 $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
322 imageEdit.close(postid);
327 open : function( postid, nonce, view ) {
330 var dfd, data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
331 btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
334 * Instead of disabling the button, which causes a focus loss and makes screen
335 * readers announce "unavailable", return if the button was already clicked.
337 if ( btn.hasClass( 'button-activated' ) ) {
341 spin.addClass( 'is-active' );
344 'action': 'image-editor',
345 '_ajax_nonce': nonce,
354 beforeSend: function() {
355 btn.addClass( 'button-activated' );
357 }).done(function( html ) {
359 head.fadeOut('fast', function(){
361 btn.removeClass( 'button-activated' );
362 spin.removeClass( 'is-active' );
369 imgLoaded : function(postid) {
370 var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
372 this.initCrop(postid, img, parent);
373 this.setCropSelection(postid, 0);
374 this.toggleEditor(postid, 0);
375 // Editor is ready, move focus to the first focusable element.
376 $( '.imgedit-wrap .imgedit-help-toggle' ).eq( 0 ).focus();
379 initCrop : function(postid, image, parent) {
381 selW = $('#imgedit-sel-width-' + postid),
382 selH = $('#imgedit-sel-height-' + postid),
385 t.iasapi = $(image).imgAreaSelect({
393 onInit: function( img ) {
394 // Ensure that the imgareaselect wrapper elements are position:absolute
395 // (even if we're in a position:fixed modal)
397 $img.next().css( 'position', 'absolute' )
398 .nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' );
400 parent.children().mousedown(function(e){
401 var ratio = false, sel, defRatio;
404 sel = t.iasapi.getSelection();
405 defRatio = t.getSelRatio(postid);
406 ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio;
409 t.iasapi.setOptions({
415 onSelectStart: function() {
416 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
419 onSelectEnd: function(img, c) {
420 imageEdit.setCropSelection(postid, c);
423 onSelectChange: function(img, c) {
424 var sizer = imageEdit.hold.sizer;
425 selW.val( imageEdit.round(c.width / sizer) );
426 selH.val( imageEdit.round(c.height / sizer) );
431 setCropSelection : function(postid, c) {
436 if ( !c || ( c.width < 3 && c.height < 3 ) ) {
437 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
438 this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
439 $('#imgedit-sel-width-' + postid).val('');
440 $('#imgedit-sel-height-' + postid).val('');
441 $('#imgedit-selection-' + postid).val('');
445 sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
446 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
447 $('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
450 close : function(postid, warn) {
451 warn = warn || false;
453 if ( warn && this.notsaved(postid) ) {
460 // If we've loaded the editor in the context of a Media Modal, then switch to the previous view,
461 // whatever that might have been.
466 // In case we are not accessing the image editor in the context of a View, close the editor the old-skool way
468 $('#image-editor-' + postid).fadeOut('fast', function() {
469 $( '#media-head-' + postid ).fadeIn( 'fast', function() {
470 // Move focus back to the Edit Image button. Runs also when saving.
471 $( '#imgedit-open-btn-' + postid ).focus();
480 notsaved : function(postid) {
481 var h = $('#imgedit-history-' + postid).val(),
482 history = ( h !== '' ) ? JSON.parse(h) : [],
483 pop = this.intval( $('#imgedit-undone-' + postid).val() );
485 if ( pop < history.length ) {
486 if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
494 addStep : function(op, postid, nonce) {
495 var t = this, elem = $('#imgedit-history-' + postid),
496 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
497 undone = $( '#imgedit-undone-' + postid ),
498 pop = t.intval( undone.val() );
504 undone.val(0); // reset
507 elem.val( JSON.stringify(history) );
509 t.refreshEditor(postid, nonce, function() {
510 t.setDisabled($('#image-undo-' + postid), true);
511 t.setDisabled($('#image-redo-' + postid), false);
515 rotate : function(angle, postid, nonce, t) {
516 if ( $(t).hasClass('disabled') ) {
520 this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce);
523 flip : function (axis, postid, nonce, t) {
524 if ( $(t).hasClass('disabled') ) {
528 this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce);
531 crop : function (postid, nonce, t) {
532 var sel = $('#imgedit-selection-' + postid).val(),
533 w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
534 h = this.intval( $('#imgedit-sel-height-' + postid).val() );
536 if ( $(t).hasClass('disabled') || sel === '' ) {
540 sel = JSON.parse(sel);
541 if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
544 this.addStep({ 'c': sel }, postid, nonce);
548 undo : function (postid, nonce) {
549 var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
550 pop = t.intval( elem.val() ) + 1;
552 if ( button.hasClass('disabled') ) {
557 t.refreshEditor(postid, nonce, function() {
558 var elem = $('#imgedit-history-' + postid),
559 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
561 t.setDisabled($('#image-redo-' + postid), true);
562 t.setDisabled(button, pop < history.length);
563 // When undo gets disabled, move focus to the redo button to avoid a focus loss.
564 if ( history.length === pop ) {
565 $( '#image-redo-' + postid ).focus();
570 redo : function(postid, nonce) {
571 var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
572 pop = t.intval( elem.val() ) - 1;
574 if ( button.hasClass('disabled') ) {
579 t.refreshEditor(postid, nonce, function() {
580 t.setDisabled($('#image-undo-' + postid), true);
581 t.setDisabled(button, pop > 0);
582 // When redo gets disabled, move focus to the undo button to avoid a focus loss.
584 $( '#image-undo-' + postid ).focus();
589 setNumSelection : function(postid) {
590 var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
591 x = this.intval( elX.val() ), y = this.intval( elY.val() ),
592 img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
593 sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
605 if ( x && y && ( sel = ias.getSelection() ) ) {
606 x2 = sel.x1 + Math.round( x * sizer );
607 y2 = sel.y1 + Math.round( y * sizer );
614 elX.val( Math.round( x2 / sizer ) );
620 elY.val( Math.round( y2 / sizer ) );
623 ias.setSelection( x1, y1, x2, y2 );
625 this.setCropSelection(postid, ias.getSelection());
629 round : function(num) {
631 num = Math.round(num);
633 if ( this.hold.sizer > 0.6 ) {
637 s = num.toString().slice(-1);
641 } else if ( '9' === s ) {
648 setRatioSelection : function(postid, n, el) {
649 var sel, r, x = this.intval( $('#imgedit-crop-width-' + postid).val() ),
650 y = this.intval( $('#imgedit-crop-height-' + postid).val() ),
651 h = $('#image-preview-' + postid).height();
653 if ( !this.intval( $(el).val() ) ) {
659 this.iasapi.setOptions({
660 aspectRatio: x + ':' + y
663 if ( sel = this.iasapi.getSelection(true) ) {
664 r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
669 $('#imgedit-crop-height-' + postid).val('');
671 $('#imgedit-crop-width-' + postid).val('');
675 this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
676 this.iasapi.update();