X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/6c8f14c09105d0afa4c1574215c59b5021040e76..4ea0dca21bda49aab5ccb91ec12bb4ef5924ed3e:/wp-admin/js/image-edit.js diff --git a/wp-admin/js/image-edit.js b/wp-admin/js/image-edit.js index b11e2d3a..6af28dc8 100644 --- a/wp-admin/js/image-edit.js +++ b/wp-admin/js/image-edit.js @@ -1,47 +1,58 @@ -var imageEdit; +/* global imageEditL10n, ajaxurl, confirm */ (function($) { -imageEdit = { +var imageEdit = window.imageEdit = { iasapi : {}, hold : {}, postid : '', + _view : false, intval : function(f) { + /* + * Bitwise OR operator: one of the obscure ways to truncate floating point figures, + * worth reminding JavaScript doesn't have a distinct "integer" type. + */ return f | 0; }, - setDisabled : function(el, s) { + setDisabled : function( el, s ) { + /* + * `el` can be a single form element or a fieldset. Before #28864, the disabled state on + * some text fields was handled targeting $('input', el). Now we need to handle the + * disabled state on buttons too so we can just target `el` regardless if it's a single + * element or a fieldset because when a fieldset is disabled, its descendants are disabled too. + */ if ( s ) { - el.removeClass('disabled'); - $('input', el).removeAttr('disabled'); + el.removeClass( 'disabled' ).prop( 'disabled', false ); } else { - el.addClass('disabled'); - $('input', el).prop('disabled', true); + el.addClass( 'disabled' ).prop( 'disabled', true ); } }, - init : function(postid, nonce) { + init : function(postid) { var t = this, old = $('#image-editor-' + t.postid), x = t.intval( $('#imgedit-x-' + postid).val() ), y = t.intval( $('#imgedit-y-' + postid).val() ); - if ( t.postid != postid && old.length ) + if ( t.postid !== postid && old.length ) { t.close(t.postid); + } - t.hold['w'] = t.hold['ow'] = x; - t.hold['h'] = t.hold['oh'] = y; - t.hold['xy_ratio'] = x / y; - t.hold['sizer'] = parseFloat( $('#imgedit-sizer-' + postid).val() ); + t.hold.w = t.hold.ow = x; + t.hold.h = t.hold.oh = y; + t.hold.xy_ratio = x / y; + t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() ); t.postid = postid; $('#imgedit-response-' + postid).empty(); $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) { var k = e.keyCode; - if ( 36 < k && k < 41 ) - $(this).blur() + if ( 36 < k && k < 41 ) { + $(this).blur(); + } - if ( 13 == k ) { + if ( 13 === k ) { e.preventDefault(); e.stopPropagation(); return false; @@ -52,14 +63,19 @@ imageEdit = { toggleEditor : function(postid, toggle) { var wait = $('#imgedit-wait-' + postid); - if ( toggle ) - wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast'); - else + if ( toggle ) { + wait.fadeIn( 'fast' ); + } else { wait.fadeOut('fast'); + } }, toggleHelp : function(el) { - $(el).siblings('.imgedit-help').slideToggle('fast'); + var $el = $( el ); + $el + .attr( 'aria-expanded', 'false' === $el.attr( 'aria-expanded' ) ? 'true' : 'false' ) + .parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' ); + return false; }, @@ -67,34 +83,41 @@ imageEdit = { return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full'; }, - scaleChanged : function(postid, x) { + scaleChanged : function( postid, x, el ) { var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid), warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = ''; + if ( false === this.validateNumeric( el ) ) { + return; + } + if ( x ) { - h1 = (w.val() != '') ? Math.round( w.val() / this.hold['xy_ratio'] ) : ''; + h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : ''; h.val( h1 ); } else { - w1 = (h.val() != '') ? Math.round( h.val() * this.hold['xy_ratio'] ) : ''; + w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : ''; w.val( w1 ); } - if ( ( h1 && h1 > this.hold['oh'] ) || ( w1 && w1 > this.hold['ow'] ) ) + if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) { warn.css('visibility', 'visible'); - else + } else { warn.css('visibility', 'hidden'); + } }, getSelRatio : function(postid) { - var x = this.hold['w'], y = this.hold['h'], + var x = this.hold.w, y = this.hold.h, X = this.intval( $('#imgedit-crop-width-' + postid).val() ), Y = this.intval( $('#imgedit-crop-height-' + postid).val() ); - if ( X && Y ) + if ( X && Y ) { return X + ':' + Y; + } - if ( x && y ) + if ( x && y ) { return x + ':' + y; + } return '1:1'; }, @@ -103,7 +126,7 @@ imageEdit = { // apply undo state to history var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = []; - if ( history != '' ) { + if ( history !== '' ) { history = JSON.parse(history); pop = this.intval( $('#imgedit-undone-' + postid).val() ); if ( pop > 0 ) { @@ -115,8 +138,8 @@ imageEdit = { if ( setSize ) { if ( !history.length ) { - this.hold['w'] = this.hold['ow']; - this.hold['h'] = this.hold['oh']; + this.hold.w = this.hold.ow; + this.hold.h = this.hold.oh; return ''; } @@ -125,8 +148,8 @@ imageEdit = { o = o.c || o.r || o.f || false; if ( o ) { - this.hold['w'] = o.fw; - this.hold['h'] = o.fh; + this.hold.w = o.fw; + this.hold.h = o.fh; } } @@ -158,40 +181,62 @@ imageEdit = { 'rand': t.intval(Math.random() * 1000000) }; - img = $(''); - img.load( function() { - var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit; + img = $( '' ) + .on( 'load', { history: data.history }, function( event ) { + var max1, max2, + parent = $( '#imgedit-crop-' + postid ), + t = imageEdit, + historyObj; + + if ( '' !== event.data.history ) { + historyObj = JSON.parse( event.data.history ); + // If last executed action in history is a crop action. + if ( historyObj[historyObj.length - 1].hasOwnProperty( 'c' ) ) { + /* + * A crop action has completed and the crop button gets disabled + * ensure the undo button is enabled. + */ + t.setDisabled( $( '#image-undo-' + postid) , true ); + // Move focus to the undo button to avoid a focus loss. + $( '#image-undo-' + postid ).focus(); + } + } - parent.empty().append(img); + parent.empty().append(img); - // w, h are the new full size dims - max1 = Math.max( t.hold.w, t.hold.h ); - max2 = Math.max( $(img).width(), $(img).height() ); - t.hold['sizer'] = max1 > max2 ? max2 / max1 : 1; + // w, h are the new full size dims + max1 = Math.max( t.hold.w, t.hold.h ); + max2 = Math.max( $(img).width(), $(img).height() ); + t.hold.sizer = max1 > max2 ? max2 / max1 : 1; - t.initCrop(postid, img, parent); - t.setCropSelection(postid, 0); + t.initCrop(postid, img, parent); + t.setCropSelection(postid, 0); - if ( (typeof callback != "unknown") && callback != null ) - callback(); + if ( (typeof callback !== 'undefined') && callback !== null ) { + callback(); + } - if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 ) - $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled'); - else - $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true); + if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) { + $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled'); + } else { + $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true); + } - t.toggleEditor(postid, 0); - }).error(function(){ - $('#imgedit-crop-' + postid).empty().append('
' + imageEditL10n.error + '
' + imageEditL10n.error + '
' + ret.error + '
' + ret.error + '
' + ret.msg + '