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