1 /* global imageEditL10n, ajaxurl, confirm */
4 var imageEdit = window.imageEdit = {
13 setDisabled : function(el, s) {
15 el.removeClass('disabled');
16 $('input', el).removeAttr('disabled');
18 el.addClass('disabled');
19 $('input', el).prop('disabled', true);
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() );
28 if ( t.postid !== postid && old.length ) {
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() );
37 $('#imgedit-response-' + postid).empty();
39 $('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
42 if ( 36 < k && k < 41 ) {
54 toggleEditor : function(postid, toggle) {
55 var wait = $('#imgedit-wait-' + postid);
58 wait.height( $('#imgedit-panel-' + postid).height() ).fadeIn('fast');
64 toggleHelp : function(el) {
65 $(el).siblings('.imgedit-help').slideToggle('fast');
69 getTarget : function(postid) {
70 return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
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 = '';
78 h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : '';
81 w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : '';
85 if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
86 warn.css('visibility', 'visible');
88 warn.css('visibility', 'hidden');
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() );
108 filterHistory : function(postid, setSize) {
109 // apply undo state to history
110 var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
112 if ( history !== '' ) {
113 history = JSON.parse(history);
114 pop = this.intval( $('#imgedit-undone-' + postid).val() );
123 if ( !history.length ) {
124 this.hold.w = this.hold.ow;
125 this.hold.h = this.hold.oh;
130 o = history[history.length - 1];
131 o = o.c || o.r || o.f || false;
140 for ( n in history ) {
142 if ( i.hasOwnProperty('c') ) {
143 op[n] = { 'c': { 'x': i.c.x, 'y': i.c.y, 'w': i.c.w, 'h': i.c.h } };
144 } else if ( i.hasOwnProperty('r') ) {
145 op[n] = { 'r': i.r.r };
146 } else if ( i.hasOwnProperty('f') ) {
147 op[n] = { 'f': i.f.f };
150 return JSON.stringify(op);
155 refreshEditor : function(postid, nonce, callback) {
156 var t = this, data, img;
158 t.toggleEditor(postid, 1);
160 'action': 'imgedit-preview',
161 '_ajax_nonce': nonce,
163 'history': t.filterHistory(postid, 1),
164 'rand': t.intval(Math.random() * 1000000)
167 img = $('<img id="image-preview-' + postid + '" />')
168 .on('load', function() {
169 var max1, max2, parent = $('#imgedit-crop-' + postid), t = imageEdit;
171 parent.empty().append(img);
173 // w, h are the new full size dims
174 max1 = Math.max( t.hold.w, t.hold.h );
175 max2 = Math.max( $(img).width(), $(img).height() );
176 t.hold.sizer = max1 > max2 ? max2 / max1 : 1;
178 t.initCrop(postid, img, parent);
179 t.setCropSelection(postid, 0);
181 if ( (typeof callback !== 'undefined') && callback !== null ) {
185 if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
186 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
188 $('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
191 t.toggleEditor(postid, 0);
193 .on('error', function() {
194 $('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
195 t.toggleEditor(postid, 0);
197 .attr('src', ajaxurl + '?' + $.param(data));
200 action : function(postid, nonce, action) {
201 var t = this, data, w, h, fw, fh;
203 if ( t.notsaved(postid) ) {
208 'action': 'image-editor',
209 '_ajax_nonce': nonce,
213 if ( 'scale' === action ) {
214 w = $('#imgedit-scale-width-' + postid),
215 h = $('#imgedit-scale-height-' + postid),
216 fw = t.intval(w.val()),
217 fh = t.intval(h.val());
222 } else if ( fh < 1 ) {
227 if ( fw === t.hold.ow || fh === t.hold.oh ) {
231 data['do'] = 'scale';
234 } else if ( 'restore' === action ) {
235 data['do'] = 'restore';
240 t.toggleEditor(postid, 1);
241 $.post(ajaxurl, data, function(r) {
242 $('#image-editor-' + postid).empty().append(r);
243 t.toggleEditor(postid, 0);
247 save : function(postid, nonce) {
248 var data, target = this.getTarget(postid), history = this.filterHistory(postid, 0);
250 if ( '' === history ) {
254 this.toggleEditor(postid, 1);
256 'action': 'image-editor',
257 '_ajax_nonce': nonce,
261 'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null,
265 $.post(ajaxurl, data, function(r) {
266 var ret = JSON.parse(r);
269 $('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p><div>');
270 imageEdit.close(postid);
274 if ( ret.fw && ret.fh ) {
275 $('#media-dims-' + postid).html( ret.fw + ' × ' + ret.fh );
278 if ( ret.thumbnail ) {
279 $('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
283 $('#imgedit-response-' + postid).html('<div class="updated"><p>' + ret.msg + '</p></div>');
286 imageEdit.close(postid);
290 open : function(postid, nonce) {
291 var data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
292 btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
294 btn.prop('disabled', true);
298 'action': 'image-editor',
299 '_ajax_nonce': nonce,
304 elem.load(ajaxurl, data, function() {
306 head.fadeOut('fast', function(){
307 btn.removeAttr('disabled');
313 imgLoaded : function(postid) {
314 var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
316 this.initCrop(postid, img, parent);
317 this.setCropSelection(postid, 0);
318 this.toggleEditor(postid, 0);
321 initCrop : function(postid, image, parent) {
322 var t = this, selW = $('#imgedit-sel-width-' + postid),
323 selH = $('#imgedit-sel-height-' + postid);
325 t.iasapi = $(image).imgAreaSelect({
334 parent.children().mousedown(function(e){
335 var ratio = false, sel, defRatio;
338 sel = t.iasapi.getSelection();
339 defRatio = t.getSelRatio(postid);
340 ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio;
343 t.iasapi.setOptions({
349 onSelectStart: function() {
350 imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
353 onSelectEnd: function(img, c) {
354 imageEdit.setCropSelection(postid, c);
357 onSelectChange: function(img, c) {
358 var sizer = imageEdit.hold.sizer;
359 selW.val( imageEdit.round(c.width / sizer) );
360 selH.val( imageEdit.round(c.height / sizer) );
365 setCropSelection : function(postid, c) {
366 var sel, min = $('#imgedit-minthumb-' + postid).val() || '128:128',
367 sizer = this.hold.sizer;
368 min = min.split(':');
371 if ( !c || ( c.width < 3 && c.height < 3 ) ) {
372 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
373 this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
374 $('#imgedit-sel-width-' + postid).val('');
375 $('#imgedit-sel-height-' + postid).val('');
376 $('#imgedit-selection-' + postid).val('');
380 if ( c.width < (min[0] * sizer) && c.height < (min[1] * sizer) ) {
381 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
382 $('#imgedit-selection-' + postid).val('');
386 sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
387 this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
388 $('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
391 close : function(postid, warn) {
392 warn = warn || false;
394 if ( warn && this.notsaved(postid) ) {
400 $('#image-editor-' + postid).fadeOut('fast', function() {
401 $('#media-head-' + postid).fadeIn('fast');
406 notsaved : function(postid) {
407 var h = $('#imgedit-history-' + postid).val(),
408 history = ( h !== '' ) ? JSON.parse(h) : [],
409 pop = this.intval( $('#imgedit-undone-' + postid).val() );
411 if ( pop < history.length ) {
412 if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
420 addStep : function(op, postid, nonce) {
421 var t = this, elem = $('#imgedit-history-' + postid),
422 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
423 undone = $('#imgedit-undone-' + postid),
424 pop = t.intval(undone.val());
430 undone.val(0); // reset
433 elem.val( JSON.stringify(history) );
435 t.refreshEditor(postid, nonce, function() {
436 t.setDisabled($('#image-undo-' + postid), true);
437 t.setDisabled($('#image-redo-' + postid), false);
441 rotate : function(angle, postid, nonce, t) {
442 if ( $(t).hasClass('disabled') ) {
446 this.addStep({ 'r': { 'r': angle, 'fw': this.hold.h, 'fh': this.hold.w }}, postid, nonce);
449 flip : function (axis, postid, nonce, t) {
450 if ( $(t).hasClass('disabled') ) {
454 this.addStep({ 'f': { 'f': axis, 'fw': this.hold.w, 'fh': this.hold.h }}, postid, nonce);
457 crop : function (postid, nonce, t) {
458 var sel = $('#imgedit-selection-' + postid).val(),
459 w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
460 h = this.intval( $('#imgedit-sel-height-' + postid).val() );
462 if ( $(t).hasClass('disabled') || sel === '' ) {
466 sel = JSON.parse(sel);
467 if ( sel.w > 0 && sel.h > 0 && w > 0 && h > 0 ) {
470 this.addStep({ 'c': sel }, postid, nonce);
474 undo : function (postid, nonce) {
475 var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
476 pop = t.intval( elem.val() ) + 1;
478 if ( button.hasClass('disabled') ) {
483 t.refreshEditor(postid, nonce, function() {
484 var elem = $('#imgedit-history-' + postid),
485 history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
487 t.setDisabled($('#image-redo-' + postid), true);
488 t.setDisabled(button, pop < history.length);
492 redo : function(postid, nonce) {
493 var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
494 pop = t.intval( elem.val() ) - 1;
496 if ( button.hasClass('disabled') ) {
501 t.refreshEditor(postid, nonce, function() {
502 t.setDisabled($('#image-undo-' + postid), true);
503 t.setDisabled(button, pop > 0);
507 setNumSelection : function(postid) {
508 var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
509 x = this.intval( elX.val() ), y = this.intval( elY.val() ),
510 img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
511 sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
523 if ( x && y && ( sel = ias.getSelection() ) ) {
524 x2 = sel.x1 + Math.round( x * sizer );
525 y2 = sel.y1 + Math.round( y * sizer );
532 elX.val( Math.round( x2 / sizer ) );
538 elY.val( Math.round( y2 / sizer ) );
541 ias.setSelection( x1, y1, x2, y2 );
543 this.setCropSelection(postid, ias.getSelection());
547 round : function(num) {
549 num = Math.round(num);
551 if ( this.hold.sizer > 0.6 ) {
555 s = num.toString().slice(-1);
559 } else if ( '9' === s ) {
566 setRatioSelection : function(postid, n, el) {
567 var sel, r, x = this.intval( $('#imgedit-crop-width-' + postid).val() ),
568 y = this.intval( $('#imgedit-crop-height-' + postid).val() ),
569 h = $('#image-preview-' + postid).height();
571 if ( !this.intval( $(el).val() ) ) {
577 this.iasapi.setOptions({
578 aspectRatio: x + ':' + y
581 if ( sel = this.iasapi.getSelection(true) ) {
582 r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
587 $('#imgedit-crop-height-' + postid).val('');
589 $('#imgedit-crop-width-' + postid).val('');
593 this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
594 this.iasapi.update();