wp/wp-admin/js/image-edit.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
--- a/wp/wp-admin/js/image-edit.js	Mon Jun 08 16:11:51 2015 +0000
+++ b/wp/wp-admin/js/image-edit.js	Tue Jun 09 03:35:32 2015 +0200
@@ -1,10 +1,11 @@
-var imageEdit;
+/* global imageEditL10n, ajaxurl, confirm */
 
 (function($) {
-imageEdit = {
+var imageEdit = window.imageEdit = {
 	iasapi : {},
 	hold : {},
 	postid : '',
+	_view : false,
 
 	intval : function(f) {
 		return f | 0;
@@ -20,28 +21,30 @@
 		}
 	},
 
-	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 +55,15 @@
 	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) {
-		$(el).siblings('.imgedit-help').slideToggle('fast');
+		$( el ).parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' );
 		return false;
 	},
 
@@ -72,29 +76,32 @@
 		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 +110,7 @@
 		// 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 +122,8 @@
 
 			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 +132,8 @@
 				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 +174,20 @@
 				// 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 +201,9 @@
 	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 +211,7 @@
 			'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 +225,14 @@
 				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 +242,22 @@
 		$.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 +274,39 @@
 			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();
+		spin.addClass( 'is-active' );
 
 		data = {
 			'action': 'image-editor',
@@ -287,13 +315,20 @@
 			'do': 'open'
 		};
 
-		elem.load(ajaxurl, data, function() {
-			elem.fadeIn('fast');
+		dfd = $.ajax({
+			url:  ajaxurl,
+			type: 'post',
+			data: data
+		}).done(function( html ) {
+			elem.html( html );
 			head.fadeOut('fast', function(){
+				elem.fadeIn('fast');
 				btn.removeAttr('disabled');
-				spin.hide();
+				spin.removeClass( 'is-active' );
 			});
 		});
+
+		return dfd;
 	},
 
 	imgLoaded : function(postid) {
@@ -305,8 +340,10 @@
 	},
 
 	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 +353,13 @@
 			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 +375,7 @@
 				});
 			},
 
-			onSelectStart: function(img, c) {
+			onSelectStart: function() {
 				imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
 			},
 
@@ -349,10 +392,9 @@
 	},
 
 	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 +405,6 @@
 			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 +413,39 @@
 	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');
+				$(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,7 +453,7 @@
 
 	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 +473,19 @@
 	},
 
 	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 +493,14 @@
 			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 +509,14 @@
 		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 +527,9 @@
 		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 +542,7 @@
 		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 +582,17 @@
 		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 +613,15 @@
 			});
 
 			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 +629,5 @@
 			}
 		}
 	}
-}
+};
 })(jQuery);