]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-admin/js/image-edit.js
WordPress 3.8
[autoinstalls/wordpress.git] / wp-admin / js / image-edit.js
index 88eb2922e354d6a2e711e1ba70852ff7e89a24ca..9eaf51bf88c91bc073eeccea52e4896a21664879 100644 (file)
@@ -1,7 +1,7 @@
-var imageEdit;
+/* global imageEditL10n, ajaxurl, confirm */
 
 (function($) {
-imageEdit = {
+var imageEdit = window.imageEdit = {
        iasapi : {},
        hold : {},
        postid : '',
@@ -20,28 +20,30 @@ imageEdit = {
                }
        },
 
-       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,10 +54,11 @@ imageEdit = {
        toggleEditor : function(postid, toggle) {
                var wait = $('#imgedit-wait-' + postid);
 
-               if ( toggle )
+               if ( toggle ) {
                        wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
-               else
+               } else {
                        wait.fadeOut('fast');
+               }
        },
 
        toggleHelp : function(el) {
@@ -72,29 +75,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 +109,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 +121,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 +131,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;
                                }
                        }
 
@@ -167,18 +173,20 @@ imageEdit = {
                                // 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 +200,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 +210,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 +224,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;
@@ -237,8 +247,9 @@ imageEdit = {
        save : function(postid, nonce) {
                var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0);
 
-               if ( '' == history )
+               if ( '' === history ) {
                        return false;
+               }
 
                this.toggleEditor(postid, 1);
                data = {
@@ -260,14 +271,17 @@ imageEdit = {
                                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);
                });
@@ -316,7 +330,7 @@ imageEdit = {
                        minWidth: 3,
                        minHeight: 3,
 
-                       onInit: function(img, c) {
+                       onInit: function() {
                                parent.children().mousedown(function(e){
                                        var ratio = false, sel, defRatio;
 
@@ -332,7 +346,7 @@ imageEdit = {
                                });
                        },
 
-                       onSelectStart: function(img, c) {
+                       onSelectStart: function() {
                                imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
                        },
 
@@ -350,7 +364,7 @@ imageEdit = {
 
        setCropSelection : function(postid, c) {
                var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128',
-                       sizer = this.hold['sizer'];
+                       sizer = this.hold.sizer;
                        min = min.split(':');
                        c = c || 0;
 
@@ -377,8 +391,9 @@ imageEdit = {
        close : function(postid, warn) {
                warn = warn || false;
 
-               if ( warn && this.notsaved(postid) )
+               if ( warn && this.notsaved(postid) ) {
                        return false;
+               }
 
                this.iasapi = {};
                this.hold = {};
@@ -390,12 +405,13 @@ imageEdit = {
 
        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,7 +419,7 @@ imageEdit = {
 
        addStep : function(op, postid, nonce) {
                var t = this, elem = $('#imgedit-history-' + postid),
-               history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array(),
+               history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
                undone = $('#imgedit-undone-' + postid),
                pop = t.intval(undone.val());
 
@@ -423,17 +439,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 +459,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,13 +475,14 @@ 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);
@@ -473,8 +493,9 @@ 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() {
@@ -487,7 +508,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 +548,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 +579,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 +595,5 @@ imageEdit = {
                        }
                }
        }
-}
+};
 })(jQuery);