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