wp/wp-admin/js/image-edit.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     1 var imageEdit;
     1 /* global imageEditL10n, ajaxurl, confirm */
     2 
     2 
     3 (function($) {
     3 (function($) {
     4 imageEdit = {
     4 var imageEdit = window.imageEdit = {
     5 	iasapi : {},
     5 	iasapi : {},
     6 	hold : {},
     6 	hold : {},
     7 	postid : '',
     7 	postid : '',
       
     8 	_view : false,
     8 
     9 
     9 	intval : function(f) {
    10 	intval : function(f) {
    10 		return f | 0;
    11 		return f | 0;
    11 	},
    12 	},
    12 
    13 
    18 			el.addClass('disabled');
    19 			el.addClass('disabled');
    19 			$('input', el).prop('disabled', true);
    20 			$('input', el).prop('disabled', true);
    20 		}
    21 		}
    21 	},
    22 	},
    22 
    23 
    23 	init : function(postid, nonce) {
    24 	init : function(postid) {
    24 		var t = this, old = $('#image-editor-' + t.postid),
    25 		var t = this, old = $('#image-editor-' + t.postid),
    25 			x = t.intval( $('#imgedit-x-' + postid).val() ),
    26 			x = t.intval( $('#imgedit-x-' + postid).val() ),
    26 			y = t.intval( $('#imgedit-y-' + postid).val() );
    27 			y = t.intval( $('#imgedit-y-' + postid).val() );
    27 
    28 
    28 		if ( t.postid != postid && old.length )
    29 		if ( t.postid !== postid && old.length ) {
    29 			t.close(t.postid);
    30 			t.close(t.postid);
    30 
    31 		}
    31 		t.hold['w'] = t.hold['ow'] = x;
    32 
    32 		t.hold['h'] = t.hold['oh'] = y;
    33 		t.hold.w = t.hold.ow = x;
    33 		t.hold['xy_ratio'] = x / y;
    34 		t.hold.h = t.hold.oh = y;
    34 		t.hold['sizer'] = parseFloat( $('#imgedit-sizer-' + postid).val() );
    35 		t.hold.xy_ratio = x / y;
       
    36 		t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() );
    35 		t.postid = postid;
    37 		t.postid = postid;
    36 		$('#imgedit-response-' + postid).empty();
    38 		$('#imgedit-response-' + postid).empty();
    37 
    39 
    38 		$('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
    40 		$('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
    39 			var k = e.keyCode;
    41 			var k = e.keyCode;
    40 
    42 
    41 			if ( 36 < k && k < 41 )
    43 			if ( 36 < k && k < 41 ) {
    42 				$(this).blur()
    44 				$(this).blur();
    43 
    45 			}
    44 			if ( 13 == k ) {
    46 
       
    47 			if ( 13 === k ) {
    45 				e.preventDefault();
    48 				e.preventDefault();
    46 				e.stopPropagation();
    49 				e.stopPropagation();
    47 				return false;
    50 				return false;
    48 			}
    51 			}
    49 		});
    52 		});
    50 	},
    53 	},
    51 
    54 
    52 	toggleEditor : function(postid, toggle) {
    55 	toggleEditor : function(postid, toggle) {
    53 		var wait = $('#imgedit-wait-' + postid);
    56 		var wait = $('#imgedit-wait-' + postid);
    54 
    57 
    55 		if ( toggle )
    58 		if ( toggle ) {
    56 			wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
    59 			wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
    57 		else
    60 		} else {
    58 			wait.fadeOut('fast');
    61 			wait.fadeOut('fast');
       
    62 		}
    59 	},
    63 	},
    60 
    64 
    61 	toggleHelp : function(el) {
    65 	toggleHelp : function(el) {
    62 		$(el).siblings('.imgedit-help').slideToggle('fast');
    66 		$( el ).parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' );
    63 		return false;
    67 		return false;
    64 	},
    68 	},
    65 
    69 
    66 	getTarget : function(postid) {
    70 	getTarget : function(postid) {
    67 		return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
    71 		return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
    70 	scaleChanged : function(postid, x) {
    74 	scaleChanged : function(postid, x) {
    71 		var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
    75 		var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
    72 		warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
    76 		warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
    73 
    77 
    74 		if ( x ) {
    78 		if ( x ) {
    75 			h1 = (w.val() != '') ? Math.round( w.val() / this.hold['xy_ratio'] ) : '';
    79 			h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : '';
    76 			h.val( h1 );
    80 			h.val( h1 );
    77 		} else {
    81 		} else {
    78 			w1 = (h.val() != '') ? Math.round( h.val() * this.hold['xy_ratio'] ) : '';
    82 			w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : '';
    79 			w.val( w1 );
    83 			w.val( w1 );
    80 		}
    84 		}
    81 
    85 
    82 		if ( ( h1 && h1 > this.hold['oh'] ) || ( w1 && w1 > this.hold['ow'] ) )
    86 		if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
    83 			warn.css('visibility', 'visible');
    87 			warn.css('visibility', 'visible');
    84 		else
    88 		} else {
    85 			warn.css('visibility', 'hidden');
    89 			warn.css('visibility', 'hidden');
       
    90 		}
    86 	},
    91 	},
    87 
    92 
    88 	getSelRatio : function(postid) {
    93 	getSelRatio : function(postid) {
    89 		var x = this.hold['w'], y = this.hold['h'],
    94 		var x = this.hold.w, y = this.hold.h,
    90 			X = this.intval( $('#imgedit-crop-width-' + postid).val() ),
    95 			X = this.intval( $('#imgedit-crop-width-' + postid).val() ),
    91 			Y = this.intval( $('#imgedit-crop-height-' + postid).val() );
    96 			Y = this.intval( $('#imgedit-crop-height-' + postid).val() );
    92 
    97 
    93 		if ( X && Y )
    98 		if ( X && Y ) {
    94 			return X + ':' + Y;
    99 			return X + ':' + Y;
    95 
   100 		}
    96 		if ( x && y )
   101 
       
   102 		if ( x && y ) {
    97 			return x + ':' + y;
   103 			return x + ':' + y;
       
   104 		}
    98 
   105 
    99 		return '1:1';
   106 		return '1:1';
   100 	},
   107 	},
   101 
   108 
   102 	filterHistory : function(postid, setSize) {
   109 	filterHistory : function(postid, setSize) {
   103 		// apply undo state to history
   110 		// apply undo state to history
   104 		var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
   111 		var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
   105 
   112 
   106 		if ( history != '' ) {
   113 		if ( history !== '' ) {
   107 			history = JSON.parse(history);
   114 			history = JSON.parse(history);
   108 			pop = this.intval( $('#imgedit-undone-' + postid).val() );
   115 			pop = this.intval( $('#imgedit-undone-' + postid).val() );
   109 			if ( pop > 0 ) {
   116 			if ( pop > 0 ) {
   110 				while ( pop > 0 ) {
   117 				while ( pop > 0 ) {
   111 					history.pop();
   118 					history.pop();
   113 				}
   120 				}
   114 			}
   121 			}
   115 
   122 
   116 			if ( setSize ) {
   123 			if ( setSize ) {
   117 				if ( !history.length ) {
   124 				if ( !history.length ) {
   118 					this.hold['w'] = this.hold['ow'];
   125 					this.hold.w = this.hold.ow;
   119 					this.hold['h'] = this.hold['oh'];
   126 					this.hold.h = this.hold.oh;
   120 					return '';
   127 					return '';
   121 				}
   128 				}
   122 
   129 
   123 				// restore
   130 				// restore
   124 				o = history[history.length - 1];
   131 				o = history[history.length - 1];
   125 				o = o.c || o.r || o.f || false;
   132 				o = o.c || o.r || o.f || false;
   126 
   133 
   127 				if ( o ) {
   134 				if ( o ) {
   128 					this.hold['w'] = o.fw;
   135 					this.hold.w = o.fw;
   129 					this.hold['h'] = o.fh;
   136 					this.hold.h = o.fh;
   130 				}
   137 				}
   131 			}
   138 			}
   132 
   139 
   133 			// filter the values
   140 			// filter the values
   134 			for ( n in history ) {
   141 			for ( n in history ) {
   165 				parent.empty().append(img);
   172 				parent.empty().append(img);
   166 
   173 
   167 				// w, h are the new full size dims
   174 				// w, h are the new full size dims
   168 				max1 = Math.max( t.hold.w, t.hold.h );
   175 				max1 = Math.max( t.hold.w, t.hold.h );
   169 				max2 = Math.max( $(img).width(), $(img).height() );
   176 				max2 = Math.max( $(img).width(), $(img).height() );
   170 				t.hold['sizer'] = max1 > max2 ? max2 / max1 : 1;
   177 				t.hold.sizer = max1 > max2 ? max2 / max1 : 1;
   171 
   178 
   172 				t.initCrop(postid, img, parent);
   179 				t.initCrop(postid, img, parent);
   173 				t.setCropSelection(postid, 0);
   180 				t.setCropSelection(postid, 0);
   174 
   181 
   175 				if ( (typeof callback != "unknown") && callback != null )
   182 				if ( (typeof callback !== 'undefined') && callback !== null ) {
   176 					callback();
   183 					callback();
   177 
   184 				}
   178 				if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 )
   185 
       
   186 				if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
   179 					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
   187 					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
   180 				else
   188 				} else {
   181 					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
   189 					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
       
   190 				}
   182 
   191 
   183 				t.toggleEditor(postid, 0);
   192 				t.toggleEditor(postid, 0);
   184 			})
   193 			})
   185 			.on('error', function() {
   194 			.on('error', function() {
   186 				$('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
   195 				$('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
   190 	},
   199 	},
   191 
   200 
   192 	action : function(postid, nonce, action) {
   201 	action : function(postid, nonce, action) {
   193 		var t = this, data, w, h, fw, fh;
   202 		var t = this, data, w, h, fw, fh;
   194 
   203 
   195 		if ( t.notsaved(postid) )
   204 		if ( t.notsaved(postid) ) {
   196 			return false;
   205 			return false;
       
   206 		}
   197 
   207 
   198 		data = {
   208 		data = {
   199 			'action': 'image-editor',
   209 			'action': 'image-editor',
   200 			'_ajax_nonce': nonce,
   210 			'_ajax_nonce': nonce,
   201 			'postid': postid
   211 			'postid': postid
   202 		};
   212 		};
   203 
   213 
   204 		if ( 'scale' == action ) {
   214 		if ( 'scale' === action ) {
   205 			w = $('#imgedit-scale-width-' + postid),
   215 			w = $('#imgedit-scale-width-' + postid),
   206 			h = $('#imgedit-scale-height-' + postid),
   216 			h = $('#imgedit-scale-height-' + postid),
   207 			fw = t.intval(w.val()),
   217 			fw = t.intval(w.val()),
   208 			fh = t.intval(h.val());
   218 			fh = t.intval(h.val());
   209 
   219 
   213 			} else if ( fh < 1 ) {
   223 			} else if ( fh < 1 ) {
   214 				h.focus();
   224 				h.focus();
   215 				return false;
   225 				return false;
   216 			}
   226 			}
   217 
   227 
   218 			if ( fw == t.hold.ow || fh == t.hold.oh )
   228 			if ( fw === t.hold.ow || fh === t.hold.oh ) {
   219 				return false;
   229 				return false;
       
   230 			}
   220 
   231 
   221 			data['do'] = 'scale';
   232 			data['do'] = 'scale';
   222 			data['fwidth'] = fw;
   233 			data.fwidth = fw;
   223 			data['fheight'] = fh;
   234 			data.fheight = fh;
   224 		} else if ( 'restore' == action ) {
   235 		} else if ( 'restore' === action ) {
   225 			data['do'] = 'restore';
   236 			data['do'] = 'restore';
   226 		} else {
   237 		} else {
   227 			return false;
   238 			return false;
   228 		}
   239 		}
   229 
   240 
   230 		t.toggleEditor(postid, 1);
   241 		t.toggleEditor(postid, 1);
   231 		$.post(ajaxurl, data, function(r) {
   242 		$.post(ajaxurl, data, function(r) {
   232 			$('#image-editor-' + postid).empty().append(r);
   243 			$('#image-editor-' + postid).empty().append(r);
   233 			t.toggleEditor(postid, 0);
   244 			t.toggleEditor(postid, 0);
       
   245 			// refresh the attachment model so that changes propagate
       
   246 			if ( t._view ) {
       
   247 				t._view.refresh();
       
   248 			}
   234 		});
   249 		});
   235 	},
   250 	},
   236 
   251 
   237 	save : function(postid, nonce) {
   252 	save : function(postid, nonce) {
   238 		var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0);
   253 		var data,
   239 
   254 			target = this.getTarget(postid),
   240 		if ( '' == history )
   255 			history = this.filterHistory(postid, 0),
   241 			return false;
   256 			self = this;
       
   257 
       
   258 		if ( '' === history ) {
       
   259 			return false;
       
   260 		}
   242 
   261 
   243 		this.toggleEditor(postid, 1);
   262 		this.toggleEditor(postid, 1);
   244 		data = {
   263 		data = {
   245 			'action': 'image-editor',
   264 			'action': 'image-editor',
   246 			'_ajax_nonce': nonce,
   265 			'_ajax_nonce': nonce,
   253 
   272 
   254 		$.post(ajaxurl, data, function(r) {
   273 		$.post(ajaxurl, data, function(r) {
   255 			var ret = JSON.parse(r);
   274 			var ret = JSON.parse(r);
   256 
   275 
   257 			if ( ret.error ) {
   276 			if ( ret.error ) {
   258 				$('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p><div>');
   277 				$('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p></div>');
   259 				imageEdit.close(postid);
   278 				imageEdit.close(postid);
   260 				return;
   279 				return;
   261 			}
   280 			}
   262 
   281 
   263 			if ( ret.fw && ret.fh )
   282 			if ( ret.fw && ret.fh ) {
   264 				$('#media-dims-' + postid).html( ret.fw + ' &times; ' + ret.fh );
   283 				$('#media-dims-' + postid).html( ret.fw + ' &times; ' + ret.fh );
   265 
   284 			}
   266 			if ( ret.thumbnail )
   285 
       
   286 			if ( ret.thumbnail ) {
   267 				$('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
   287 				$('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
   268 
   288 			}
   269 			if ( ret.msg )
   289 
       
   290 			if ( ret.msg ) {
   270 				$('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
   291 				$('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
   271 
   292 			}
   272 			imageEdit.close(postid);
   293 
   273 		});
   294 			if ( self._view ) {
   274 	},
   295 				self._view.save();
   275 
   296 			} else {
   276 	open : function(postid, nonce) {
   297 				imageEdit.close(postid);
   277 		var data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
   298 			}
       
   299 		});
       
   300 	},
       
   301 
       
   302 	open : function( postid, nonce, view ) {
       
   303 		this._view = view;
       
   304 
       
   305 		var dfd, data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
   278 			btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
   306 			btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
   279 
   307 
   280 		btn.prop('disabled', true);
   308 		btn.prop('disabled', true);
   281 		spin.show();
   309 		spin.addClass( 'is-active' );
   282 
   310 
   283 		data = {
   311 		data = {
   284 			'action': 'image-editor',
   312 			'action': 'image-editor',
   285 			'_ajax_nonce': nonce,
   313 			'_ajax_nonce': nonce,
   286 			'postid': postid,
   314 			'postid': postid,
   287 			'do': 'open'
   315 			'do': 'open'
   288 		};
   316 		};
   289 
   317 
   290 		elem.load(ajaxurl, data, function() {
   318 		dfd = $.ajax({
   291 			elem.fadeIn('fast');
   319 			url:  ajaxurl,
       
   320 			type: 'post',
       
   321 			data: data
       
   322 		}).done(function( html ) {
       
   323 			elem.html( html );
   292 			head.fadeOut('fast', function(){
   324 			head.fadeOut('fast', function(){
       
   325 				elem.fadeIn('fast');
   293 				btn.removeAttr('disabled');
   326 				btn.removeAttr('disabled');
   294 				spin.hide();
   327 				spin.removeClass( 'is-active' );
   295 			});
   328 			});
   296 		});
   329 		});
       
   330 
       
   331 		return dfd;
   297 	},
   332 	},
   298 
   333 
   299 	imgLoaded : function(postid) {
   334 	imgLoaded : function(postid) {
   300 		var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
   335 		var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
   301 
   336 
   303 		this.setCropSelection(postid, 0);
   338 		this.setCropSelection(postid, 0);
   304 		this.toggleEditor(postid, 0);
   339 		this.toggleEditor(postid, 0);
   305 	},
   340 	},
   306 
   341 
   307 	initCrop : function(postid, image, parent) {
   342 	initCrop : function(postid, image, parent) {
   308 		var t = this, selW = $('#imgedit-sel-width-' + postid),
   343 		var t = this,
   309 			selH = $('#imgedit-sel-height-' + postid);
   344 			selW = $('#imgedit-sel-width-' + postid),
       
   345 			selH = $('#imgedit-sel-height-' + postid),
       
   346 			$img;
   310 
   347 
   311 		t.iasapi = $(image).imgAreaSelect({
   348 		t.iasapi = $(image).imgAreaSelect({
   312 			parent: parent,
   349 			parent: parent,
   313 			instance: true,
   350 			instance: true,
   314 			handles: true,
   351 			handles: true,
   315 			keys: true,
   352 			keys: true,
   316 			minWidth: 3,
   353 			minWidth: 3,
   317 			minHeight: 3,
   354 			minHeight: 3,
   318 
   355 
   319 			onInit: function(img, c) {
   356 			onInit: function( img ) {
       
   357 				// Ensure that the imgareaselect wrapper elements are position:absolute
       
   358 				// (even if we're in a position:fixed modal)
       
   359 				$img = $( img );
       
   360 				$img.next().css( 'position', 'absolute' )
       
   361 					.nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' );
       
   362 
   320 				parent.children().mousedown(function(e){
   363 				parent.children().mousedown(function(e){
   321 					var ratio = false, sel, defRatio;
   364 					var ratio = false, sel, defRatio;
   322 
   365 
   323 					if ( e.shiftKey ) {
   366 					if ( e.shiftKey ) {
   324 						sel = t.iasapi.getSelection();
   367 						sel = t.iasapi.getSelection();
   330 						aspectRatio: ratio
   373 						aspectRatio: ratio
   331 					});
   374 					});
   332 				});
   375 				});
   333 			},
   376 			},
   334 
   377 
   335 			onSelectStart: function(img, c) {
   378 			onSelectStart: function() {
   336 				imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
   379 				imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
   337 			},
   380 			},
   338 
   381 
   339 			onSelectEnd: function(img, c) {
   382 			onSelectEnd: function(img, c) {
   340 				imageEdit.setCropSelection(postid, c);
   383 				imageEdit.setCropSelection(postid, c);
   347 			}
   390 			}
   348 		});
   391 		});
   349 	},
   392 	},
   350 
   393 
   351 	setCropSelection : function(postid, c) {
   394 	setCropSelection : function(postid, c) {
   352 		var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128',
   395 		var sel;
   353 			sizer = this.hold['sizer'];
   396 
   354 			min = min.split(':');
   397 		c = c || 0;
   355 			c = c || 0;
       
   356 
   398 
   357 		if ( !c || ( c.width < 3 && c.height < 3 ) ) {
   399 		if ( !c || ( c.width < 3 && c.height < 3 ) ) {
   358 			this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
   400 			this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
   359 			this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
   401 			this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
   360 			$('#imgedit-sel-width-' + postid).val('');
   402 			$('#imgedit-sel-width-' + postid).val('');
   361 			$('#imgedit-sel-height-' + postid).val('');
   403 			$('#imgedit-sel-height-' + postid).val('');
   362 			$('#imgedit-selection-' + postid).val('');
   404 			$('#imgedit-selection-' + postid).val('');
   363 			return false;
   405 			return false;
   364 		}
   406 		}
   365 
   407 
   366 		if ( c.width < (min[0] * sizer) && c.height < (min[1] * sizer) ) {
       
   367 			this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
       
   368 			$('#imgedit-selection-' + postid).val('');
       
   369 			return false;
       
   370 		}
       
   371 
       
   372 		sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
   408 		sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
   373 		this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
   409 		this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
   374 		$('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
   410 		$('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
   375 	},
   411 	},
   376 
   412 
   377 	close : function(postid, warn) {
   413 	close : function(postid, warn) {
   378 		warn = warn || false;
   414 		warn = warn || false;
   379 
   415 
   380 		if ( warn && this.notsaved(postid) )
   416 		if ( warn && this.notsaved(postid) ) {
   381 			return false;
   417 			return false;
       
   418 		}
   382 
   419 
   383 		this.iasapi = {};
   420 		this.iasapi = {};
   384 		this.hold = {};
   421 		this.hold = {};
   385 		$('#image-editor-' + postid).fadeOut('fast', function() {
   422 
   386 			$('#media-head-' + postid).fadeIn('fast');
   423 		// If we've loaded the editor in the context of a Media Modal, then switch to the previous view,
   387 			$(this).empty();
   424 		// whatever that might have been.
   388 		});
   425 		if ( this._view ){
       
   426 			this._view.back();
       
   427 		}
       
   428 
       
   429 		// In case we are not accessing the image editor in the context of a View, close the editor the old-skool way
       
   430 		else {
       
   431 			$('#image-editor-' + postid).fadeOut('fast', function() {
       
   432 				$('#media-head-' + postid).fadeIn('fast');
       
   433 				$(this).empty();
       
   434 			});
       
   435 		}
       
   436 
       
   437 
   389 	},
   438 	},
   390 
   439 
   391 	notsaved : function(postid) {
   440 	notsaved : function(postid) {
   392 		var h = $('#imgedit-history-' + postid).val(),
   441 		var h = $('#imgedit-history-' + postid).val(),
   393 			history = (h != '') ? JSON.parse(h) : new Array(),
   442 			history = ( h !== '' ) ? JSON.parse(h) : [],
   394 			pop = this.intval( $('#imgedit-undone-' + postid).val() );
   443 			pop = this.intval( $('#imgedit-undone-' + postid).val() );
   395 
   444 
   396 		if ( pop < history.length ) {
   445 		if ( pop < history.length ) {
   397 			if ( confirm( $('#imgedit-leaving-' + postid).html() ) )
   446 			if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
   398 				return false;
   447 				return false;
       
   448 			}
   399 			return true;
   449 			return true;
   400 		}
   450 		}
   401 		return false;
   451 		return false;
   402 	},
   452 	},
   403 
   453 
   404 	addStep : function(op, postid, nonce) {
   454 	addStep : function(op, postid, nonce) {
   405 		var t = this, elem = $('#imgedit-history-' + postid),
   455 		var t = this, elem = $('#imgedit-history-' + postid),
   406 		history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array(),
   456 		history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
   407 		undone = $('#imgedit-undone-' + postid),
   457 		undone = $('#imgedit-undone-' + postid),
   408 		pop = t.intval(undone.val());
   458 		pop = t.intval(undone.val());
   409 
   459 
   410 		while ( pop > 0 ) {
   460 		while ( pop > 0 ) {
   411 			history.pop();
   461 			history.pop();
   421 			t.setDisabled($('#image-redo-' + postid), false);
   471 			t.setDisabled($('#image-redo-' + postid), false);
   422 		});
   472 		});
   423 	},
   473 	},
   424 
   474 
   425 	rotate : function(angle, postid, nonce, t) {
   475 	rotate : function(angle, postid, nonce, t) {
   426 		if ( $(t).hasClass('disabled') )
   476 		if ( $(t).hasClass('disabled') ) {
   427 			return false;
   477 			return false;
   428 
   478 		}
   429 		this.addStep({ 'r': { 'r': angle, 'fw': this.hold['h'], 'fh': this.hold['w'] }}, postid, nonce);
   479 
       
   480 		this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce);
   430 	},
   481 	},
   431 
   482 
   432 	flip : function (axis, postid, nonce, t) {
   483 	flip : function (axis, postid, nonce, t) {
   433 		if ( $(t).hasClass('disabled') )
   484 		if ( $(t).hasClass('disabled') ) {
   434 			return false;
   485 			return false;
   435 
   486 		}
   436 		this.addStep({ 'f': { 'f': axis, 'fw': this.hold['w'], 'fh': this.hold['h'] }}, postid, nonce);
   487 
       
   488 		this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce);
   437 	},
   489 	},
   438 
   490 
   439 	crop : function (postid, nonce, t) {
   491 	crop : function (postid, nonce, t) {
   440 		var sel = $('#imgedit-selection-' + postid).val(),
   492 		var sel = $('#imgedit-selection-' + postid).val(),
   441 			w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
   493 			w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
   442 			h = this.intval( $('#imgedit-sel-height-' + postid).val() );
   494 			h = this.intval( $('#imgedit-sel-height-' + postid).val() );
   443 
   495 
   444 		if ( $(t).hasClass('disabled') || sel == '' )
   496 		if ( $(t).hasClass('disabled') || sel === '' ) {
   445 			return false;
   497 			return false;
       
   498 		}
   446 
   499 
   447 		sel = JSON.parse(sel);
   500 		sel = JSON.parse(sel);
   448 		if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
   501 		if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
   449 			sel['fw'] = w;
   502 			sel.fw = w;
   450 			sel['fh'] = h;
   503 			sel.fh = h;
   451 			this.addStep({ 'c': sel }, postid, nonce);
   504 			this.addStep({ 'c': sel }, postid, nonce);
   452 		}
   505 		}
   453 	},
   506 	},
   454 
   507 
   455 	undo : function (postid, nonce) {
   508 	undo : function (postid, nonce) {
   456 		var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
   509 		var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
   457 			pop = t.intval( elem.val() ) + 1;
   510 			pop = t.intval( elem.val() ) + 1;
   458 
   511 
   459 		if ( button.hasClass('disabled') )
   512 		if ( button.hasClass('disabled') ) {
   460 			return;
   513 			return;
       
   514 		}
   461 
   515 
   462 		elem.val(pop);
   516 		elem.val(pop);
   463 		t.refreshEditor(postid, nonce, function() {
   517 		t.refreshEditor(postid, nonce, function() {
   464 			var elem = $('#imgedit-history-' + postid),
   518 			var elem = $('#imgedit-history-' + postid),
   465 			history = (elem.val() != '') ? JSON.parse(elem.val()) : new Array();
   519 			history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
   466 
   520 
   467 			t.setDisabled($('#image-redo-' + postid), true);
   521 			t.setDisabled($('#image-redo-' + postid), true);
   468 			t.setDisabled(button, pop < history.length);
   522 			t.setDisabled(button, pop < history.length);
   469 		});
   523 		});
   470 	},
   524 	},
   471 
   525 
   472 	redo : function(postid, nonce) {
   526 	redo : function(postid, nonce) {
   473 		var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
   527 		var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
   474 			pop = t.intval( elem.val() ) - 1;
   528 			pop = t.intval( elem.val() ) - 1;
   475 
   529 
   476 		if ( button.hasClass('disabled') )
   530 		if ( button.hasClass('disabled') ) {
   477 			return;
   531 			return;
       
   532 		}
   478 
   533 
   479 		elem.val(pop);
   534 		elem.val(pop);
   480 		t.refreshEditor(postid, nonce, function() {
   535 		t.refreshEditor(postid, nonce, function() {
   481 			t.setDisabled($('#image-undo-' + postid), true);
   536 			t.setDisabled($('#image-undo-' + postid), true);
   482 			t.setDisabled(button, pop > 0);
   537 			t.setDisabled(button, pop > 0);
   485 
   540 
   486 	setNumSelection : function(postid) {
   541 	setNumSelection : function(postid) {
   487 		var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
   542 		var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
   488 			x = this.intval( elX.val() ), y = this.intval( elY.val() ),
   543 			x = this.intval( elX.val() ), y = this.intval( elY.val() ),
   489 			img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
   544 			img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
   490 			sizer = this.hold['sizer'], x1, y1, x2, y2, ias = this.iasapi;
   545 			sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
   491 
   546 
   492 		if ( x < 1 ) {
   547 		if ( x < 1 ) {
   493 			elX.val('');
   548 			elX.val('');
   494 			return false;
   549 			return false;
   495 		}
   550 		}
   525 
   580 
   526 	round : function(num) {
   581 	round : function(num) {
   527 		var s;
   582 		var s;
   528 		num = Math.round(num);
   583 		num = Math.round(num);
   529 
   584 
   530 		if ( this.hold.sizer > 0.6 )
   585 		if ( this.hold.sizer > 0.6 ) {
   531 			return num;
   586 			return num;
       
   587 		}
   532 
   588 
   533 		s = num.toString().slice(-1);
   589 		s = num.toString().slice(-1);
   534 
   590 
   535 		if ( '1' == s )
   591 		if ( '1' === s ) {
   536 			return num - 1;
   592 			return num - 1;
   537 		else if ( '9' == s )
   593 		} else if ( '9' === s ) {
   538 			return num + 1;
   594 			return num + 1;
       
   595 		}
   539 
   596 
   540 		return num;
   597 		return num;
   541 	},
   598 	},
   542 
   599 
   543 	setRatioSelection : function(postid, n, el) {
   600 	setRatioSelection : function(postid, n, el) {
   554 			this.iasapi.setOptions({
   611 			this.iasapi.setOptions({
   555 				aspectRatio: x + ':' + y
   612 				aspectRatio: x + ':' + y
   556 			});
   613 			});
   557 
   614 
   558 			if ( sel = this.iasapi.getSelection(true) ) {
   615 			if ( sel = this.iasapi.getSelection(true) ) {
   559 				r = Math.ceil( sel.y1 + ((sel.x2 - sel.x1) / (x / y)) );
   616 				r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
   560 
   617 
   561 				if ( r > h ) {
   618 				if ( r > h ) {
   562 					r = h;
   619 					r = h;
   563 					if ( n )
   620 					if ( n ) {
   564 						$('#imgedit-crop-height-' + postid).val('');
   621 						$('#imgedit-crop-height-' + postid).val('');
   565 					else
   622 					} else {
   566 						$('#imgedit-crop-width-' + postid).val('');
   623 						$('#imgedit-crop-width-' + postid).val('');
       
   624 					}
   567 				}
   625 				}
   568 
   626 
   569 				this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
   627 				this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
   570 				this.iasapi.update();
   628 				this.iasapi.update();
   571 			}
   629 			}
   572 		}
   630 		}
   573 	}
   631 	}
   574 }
   632 };
   575 })(jQuery);
   633 })(jQuery);