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