]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/js/image-edit.js
WordPress 4.5
[autoinstalls/wordpress.git] / wp-admin / js / image-edit.js
index 88eb2922e354d6a2e711e1ba70852ff7e89a24ca..ae7599230168dad7dafb21f32acb3011d495edb9 100644 (file)
@@ -1,47 +1,54 @@
-var imageEdit;
+/* global imageEditL10n, ajaxurl, confirm */
 
 (function($) {
-imageEdit = {
+var imageEdit = window.imageEdit = {
        iasapi : {},
        hold : {},
        postid : '',
+       _view : false,
 
        intval : function(f) {
                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 +59,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;
        },
 
@@ -72,29 +84,32 @@ imageEdit = {
                warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
 
                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 +118,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 +130,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 +140,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,27 +173,46 @@ imageEdit = {
                        'rand': t.intval(Math.random() * 1000000)
                };
 
-               img = $('<img id="image-preview-' + postid + '" />')
-                       .on('load', function() {
-                               var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit;
+               img = $( '<img id="image-preview-' + postid + '" alt="" />' )
+                       .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);
 
                                // 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.hold.sizer = max1 > max2 ? max2 / max1 : 1;
 
                                t.initCrop(postid, img, parent);
                                t.setCropSelection(postid, 0);
 
-                               if ( (typeof callback != "unknown") && callback != null )
+                               if ( (typeof callback !== 'undefined') && callback !== null ) {
                                        callback();
+                               }
 
-                               if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 )
+                               if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
                                        $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
-                               else
+                               } else {
                                        $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
+                               }
 
                                t.toggleEditor(postid, 0);
                        })
@@ -192,8 +226,9 @@ imageEdit = {
        action : function(postid, nonce, action) {
                var t = this, data, w, h, fw, fh;
 
-               if ( t.notsaved(postid) )
+               if ( t.notsaved(postid) ) {
                        return false;
+               }
 
                data = {
                        'action': 'image-editor',
@@ -201,7 +236,7 @@ imageEdit = {
                        'postid': postid
                };
 
-               if ( 'scale' == action ) {
+               if ( 'scale' === action ) {
                        w = $('#imgedit-scale-width-' + postid),
                        h = $('#imgedit-scale-height-' + postid),
                        fw = t.intval(w.val()),
@@ -215,13 +250,14 @@ imageEdit = {
                                return false;
                        }
 
-                       if ( fw == t.hold.ow || fh == t.hold.oh )
+                       if ( fw === t.hold.ow || fh === t.hold.oh ) {
                                return false;
+                       }
 
                        data['do'] = 'scale';
-                       data['fwidth'] = fw;
-                       data['fheight'] = fh;
-               } else if ( 'restore' == action ) {
+                       data.fwidth = fw;
+                       data.fheight = fh;
+               } else if ( 'restore' === action ) {
                        data['do'] = 'restore';
                } else {
                        return false;
@@ -231,14 +267,22 @@ imageEdit = {
                $.post(ajaxurl, data, function(r) {
                        $('#image-editor-' + postid).empty().append(r);
                        t.toggleEditor(postid, 0);
+                       // refresh the attachment model so that changes propagate
+                       if ( t._view ) {
+                               t._view.refresh();
+                       }
                });
        },
 
        save : function(postid, nonce) {
-               var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0);
+               var data,
+                       target = this.getTarget(postid),
+                       history = this.filterHistory(postid, 0),
+                       self = this;
 
-               if ( '' == history )
+               if ( '' === history ) {
                        return false;
+               }
 
                this.toggleEditor(postid, 1);
                data = {
@@ -255,30 +299,46 @@ imageEdit = {
                        var ret = JSON.parse(r);
 
                        if ( ret.error ) {
-                               $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p><div>');
+                               $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p></div>');
                                imageEdit.close(postid);
                                return;
                        }
 
-                       if ( ret.fw && ret.fh )
+                       if ( ret.fw && ret.fh ) {
                                $('#media-dims-' + postid).html( ret.fw + ' &times; ' + ret.fh );
+                       }
 
-                       if ( ret.thumbnail )
+                       if ( ret.thumbnail ) {
                                $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
+                       }
 
-                       if ( ret.msg )
+                       if ( ret.msg ) {
                                $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
+                       }
 
-                       imageEdit.close(postid);
+                       if ( self._view ) {
+                               self._view.save();
+                       } else {
+                               imageEdit.close(postid);
+                       }
                });
        },
 
-       open : function(postid, nonce) {
-               var data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
+       open : function( postid, nonce, view ) {
+               this._view = view;
+
+               var dfd, data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
                        btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
 
-               btn.prop('disabled', true);
-               spin.show();
+               /*
+                * Instead of disabling the button, which causes a focus loss and makes screen
+                * readers announce "unavailable", return if the button was already clicked.
+                */
+               if ( btn.hasClass( 'button-activated' ) ) {
+                       return;
+               }
+
+               spin.addClass( 'is-active' );
 
                data = {
                        'action': 'image-editor',
@@ -287,13 +347,23 @@ imageEdit = {
                        'do': 'open'
                };
 
-               elem.load(ajaxurl, data, function() {
-                       elem.fadeIn('fast');
+               dfd = $.ajax({
+                       url:  ajaxurl,
+                       type: 'post',
+                       data: data,
+                       beforeSend: function() {
+                               btn.addClass( 'button-activated' );
+                       }
+               }).done(function( html ) {
+                       elem.html( html );
                        head.fadeOut('fast', function(){
-                               btn.removeAttr('disabled');
-                               spin.hide();
+                               elem.fadeIn('fast');
+                               btn.removeClass( 'button-activated' );
+                               spin.removeClass( 'is-active' );
                        });
                });
+
+               return dfd;
        },
 
        imgLoaded : function(postid) {
@@ -302,11 +372,15 @@ imageEdit = {
                this.initCrop(postid, img, parent);
                this.setCropSelection(postid, 0);
                this.toggleEditor(postid, 0);
+               // Editor is ready, move focus to the first focusable element.
+               $( '.imgedit-wrap .imgedit-help-toggle' ).eq( 0 ).focus();
        },
 
        initCrop : function(postid, image, parent) {
-               var t = this, selW = $('#imgedit-sel-width-' + postid),
-                       selH = $('#imgedit-sel-height-' + postid);
+               var t = this,
+                       selW = $('#imgedit-sel-width-' + postid),
+                       selH = $('#imgedit-sel-height-' + postid),
+                       $img;
 
                t.iasapi = $(image).imgAreaSelect({
                        parent: parent,
@@ -316,7 +390,13 @@ imageEdit = {
                        minWidth: 3,
                        minHeight: 3,
 
-                       onInit: function(img, c) {
+                       onInit: function( img ) {
+                               // Ensure that the imgareaselect wrapper elements are position:absolute
+                               // (even if we're in a position:fixed modal)
+                               $img = $( img );
+                               $img.next().css( 'position', 'absolute' )
+                                       .nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' );
+
                                parent.children().mousedown(function(e){
                                        var ratio = false, sel, defRatio;
 
@@ -332,7 +412,7 @@ imageEdit = {
                                });
                        },
 
-                       onSelectStart: function(img, c) {
+                       onSelectStart: function() {
                                imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
                        },
 
@@ -349,10 +429,9 @@ imageEdit = {
        },
 
        setCropSelection : function(postid, c) {
-               var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128',
-                       sizer = this.hold['sizer'];
-                       min = min.split(':');
-                       c = c || 0;
+               var sel;
+
+               c = c || 0;
 
                if ( !c || ( c.width < 3 && c.height < 3 ) ) {
                        this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
@@ -363,12 +442,6 @@ imageEdit = {
                        return false;
                }
 
-               if ( c.width < (min[0] * sizer) && c.height < (min[1] * sizer) ) {
-                       this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
-                       $('#imgedit-selection-' + postid).val('');
-                       return false;
-               }
-
                sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
                this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
                $('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
@@ -377,25 +450,42 @@ imageEdit = {
        close : function(postid, warn) {
                warn = warn || false;
 
-               if ( warn && this.notsaved(postid) )
+               if ( warn && this.notsaved(postid) ) {
                        return false;
+               }
 
                this.iasapi = {};
                this.hold = {};
-               $('#image-editor-' + postid).fadeOut('fast', function() {
-                       $('#media-head-' + postid).fadeIn('fast');
-                       $(this).empty();
-               });
+
+               // If we've loaded the editor in the context of a Media Modal, then switch to the previous view,
+               // whatever that might have been.
+               if ( this._view ){
+                       this._view.back();
+               }
+
+               // In case we are not accessing the image editor in the context of a View, close the editor the old-skool way
+               else {
+                       $('#image-editor-' + postid).fadeOut('fast', function() {
+                               $( '#media-head-' + postid ).fadeIn( 'fast', function() {
+                                       // Move focus back to the Edit Image button. Runs also when saving.
+                                       $( '#imgedit-open-btn-' + postid ).focus();
+                               });
+                               $(this).empty();
+                       });
+               }
+
+
        },
 
        notsaved : function(postid) {
                var h = $('#imgedit-history-' + postid).val(),
-                       history = (h != '') ? JSON.parse(h) : new Array(),
+                       history = ( h !== '' ) ? JSON.parse(h) : [],
                        pop = this.intval( $('#imgedit-undone-' + postid).val() );
 
                if ( pop < history.length ) {
-                       if ( confirm( $('#imgedit-leaving-' + postid).html() ) )
+                       if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
                                return false;
+                       }
                        return true;
                }
                return false;
@@ -403,9 +493,9 @@ imageEdit = {
 
        addStep : function(op, postid, nonce) {
                var t = this, elem = $('#imgedit-history-' + postid),
-               history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array(),
-               undone = $('#imgedit-undone-' + postid),
-               pop = t.intval(undone.val());
+                       history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
+                       undone = $( '#imgedit-undone-' + postid ),
+                       pop = t.intval( undone.val() );
 
                while ( pop > 0 ) {
                        history.pop();
@@ -423,17 +513,19 @@ imageEdit = {
        },
 
        rotate : function(angle, postid, nonce, t) {
-               if ( $(t).hasClass('disabled') )
+               if ( $(t).hasClass('disabled') ) {
                        return false;
+               }
 
-               this.addStep({ 'r': { 'r': angle, 'fw': this.hold['h'], 'fh': this.hold['w'] }}, postid, nonce);
+               this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce);
        },
 
        flip : function (axis, postid, nonce, t) {
-               if ( $(t).hasClass('disabled') )
+               if ( $(t).hasClass('disabled') ) {
                        return false;
+               }
 
-               this.addStep({ 'f': { 'f': axis, 'fw': this.hold['w'], 'fh': this.hold['h'] }}, postid, nonce);
+               this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce);
        },
 
        crop : function (postid, nonce, t) {
@@ -441,13 +533,14 @@ imageEdit = {
                        w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
                        h = this.intval( $('#imgedit-sel-height-' + postid).val() );
 
-               if ( $(t).hasClass('disabled') || sel == '' )
+               if ( $(t).hasClass('disabled') || sel === '' ) {
                        return false;
+               }
 
                sel = JSON.parse(sel);
                if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
-                       sel['fw'] = w;
-                       sel['fh'] = h;
+                       sel.fw = w;
+                       sel.fh = h;
                        this.addStep({ 'c': sel }, postid, nonce);
                }
        },
@@ -456,16 +549,21 @@ imageEdit = {
                var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
                        pop = t.intval( elem.val() ) + 1;
 
-               if ( button.hasClass('disabled') )
+               if ( button.hasClass('disabled') ) {
                        return;
+               }
 
                elem.val(pop);
                t.refreshEditor(postid, nonce, function() {
                        var elem = $('#imgedit-history-' + postid),
-                       history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array();
+                               history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
 
                        t.setDisabled($('#image-redo-' + postid), true);
                        t.setDisabled(button, pop < history.length);
+                       // When undo gets disabled, move focus to the redo button to avoid a focus loss.
+                       if ( history.length === pop ) {
+                               $( '#image-redo-' + postid ).focus();
+                       }
                });
        },
 
@@ -473,13 +571,18 @@ imageEdit = {
                var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
                        pop = t.intval( elem.val() ) - 1;
 
-               if ( button.hasClass('disabled') )
+               if ( button.hasClass('disabled') ) {
                        return;
+               }
 
                elem.val(pop);
                t.refreshEditor(postid, nonce, function() {
                        t.setDisabled($('#image-undo-' + postid), true);
                        t.setDisabled(button, pop > 0);
+                       // When redo gets disabled, move focus to the undo button to avoid a focus loss.
+                       if ( 0 === pop ) {
+                               $( '#image-undo-' + postid ).focus();
+                       }
                });
        },
 
@@ -487,7 +590,7 @@ imageEdit = {
                var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
                        x = this.intval( elX.val() ), y = this.intval( elY.val() ),
                        img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
-                       sizer = this.hold['sizer'], x1, y1, x2, y2, ias = this.iasapi;
+                       sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
 
                if ( x < 1 ) {
                        elX.val('');
@@ -527,15 +630,17 @@ imageEdit = {
                var s;
                num = Math.round(num);
 
-               if ( this.hold.sizer > 0.6 )
+               if ( this.hold.sizer > 0.6 ) {
                        return num;
+               }
 
                s = num.toString().slice(-1);
 
-               if ( '1' == s )
+               if ( '1' === s ) {
                        return num - 1;
-               else if ( '9' == s )
+               } else if ( '9' === s ) {
                        return num + 1;
+               }
 
                return num;
        },
@@ -556,14 +661,15 @@ imageEdit = {
                        });
 
                        if ( sel = this.iasapi.getSelection(true) ) {
-                               r = Math.ceil( sel.y1 + ((sel.x2 - sel.x1) / (x / y)) );
+                               r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
 
                                if ( r > h ) {
                                        r = h;
-                                       if ( n )
+                                       if ( n ) {
                                                $('#imgedit-crop-height-' + postid).val('');
-                                       else
+                                       } else {
                                                $('#imgedit-crop-width-' + postid).val('');
+                                       }
                                }
 
                                this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
@@ -571,5 +677,5 @@ imageEdit = {
                        }
                }
        }
-}
+};
 })(jQuery);