wp/wp-admin/js/image-edit.js
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 17:39:30 +0200
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
permissions -rw-r--r--
resynchronize code repo with production
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 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     2
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     3
 * @summary   The functions necessary for editing images.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     4
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     5
 * @since     2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     6
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
(function($) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     9
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    10
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    11
	 * Contains all the methods to initialise and control the image editor.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    12
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    13
	 * @namespace imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    14
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    15
	var imageEdit = window.imageEdit = {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	iasapi : {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	hold : {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	postid : '',
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    19
	_view : false,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    21
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    22
	 * @summary Converts a value to an integer.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    23
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    24
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    25
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    26
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    27
	 * @param {number} f The float value that should be converted.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    28
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    29
	 * @return {number} The integer representation from the float value.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    30
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	intval : function(f) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    32
		/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    33
		 * Bitwise OR operator: one of the obscure ways to truncate floating point figures,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    34
		 * worth reminding JavaScript doesn't have a distinct "integer" type.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    35
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		return f | 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    39
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    40
	 * @summary Adds the disabled attribute and class to a single form element
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    41
	 *          or a field set.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    42
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    43
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    44
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    45
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    46
	 * @param {jQuery}         el The element that should be modified.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    47
	 * @param {bool|number}    s  The state for the element. If set to true
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    48
	 *                            the element is disabled,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    49
	 *                            otherwise the element is enabled.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    50
	 *                            The function is sometimes called with a 0 or 1
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    51
	 *                            instead of true or false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    52
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    53
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    54
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    55
	setDisabled : function( el, s ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    56
		/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    57
		 * `el` can be a single form element or a fieldset. Before #28864, the disabled state on
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    58
		 * some text fields  was handled targeting $('input', el). Now we need to handle the
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    59
		 * disabled state on buttons too so we can just target `el` regardless if it's a single
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    60
		 * element or a fieldset because when a fieldset is disabled, its descendants are disabled too.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    61
		 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		if ( s ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    63
			el.removeClass( 'disabled' ).prop( 'disabled', false );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		} else {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    65
			el.addClass( 'disabled' ).prop( 'disabled', true );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    69
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    70
	 * @summary Initializes the image editor.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    71
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    72
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    73
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    74
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    75
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    76
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    77
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    78
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
	init : function(postid) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		var t = this, old = $('#image-editor-' + t.postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
			x = t.intval( $('#imgedit-x-' + postid).val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
			y = t.intval( $('#imgedit-y-' + postid).val() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
		if ( t.postid !== postid && old.length ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			t.close(t.postid);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
		t.hold.w = t.hold.ow = x;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
		t.hold.h = t.hold.oh = y;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
		t.hold.xy_ratio = x / y;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
		t.hold.sizer = parseFloat( $('#imgedit-sizer-' + postid).val() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		t.postid = postid;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		$('#imgedit-response-' + postid).empty();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		$('input[type="text"]', '#imgedit-panel-' + postid).keypress(function(e) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			var k = e.keyCode;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    98
			// Key codes 37 thru 40 are the arrow keys.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
			if ( 36 < k && k < 41 ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
				$(this).blur();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   103
			// The key code 13 is the enter key.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   104
			if ( 13 === k ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
				e.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
				e.stopPropagation();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   112
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   113
	 * @summary Toggles the wait/load icon in the editor.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   114
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   115
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   116
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   117
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   118
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   119
	 * @param {number} toggle Is 0 or 1, fades the icon in then 1 and out when 0.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   120
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   121
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   122
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	toggleEditor : function(postid, toggle) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		var wait = $('#imgedit-wait-' + postid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   126
		if ( toggle ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   127
			wait.fadeIn( 'fast' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   128
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
			wait.fadeOut('fast');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   130
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   134
	 * @summary Shows or hides the image edit help box.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   135
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   136
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   137
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   138
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   139
	 * @param {HTMLElement} el The element to create the help window in.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   140
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   141
	 * @returns {boolean} Always returns false.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   142
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
	toggleHelp : function(el) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   144
		var $el = $( el );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   145
		$el
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   146
			.attr( 'aria-expanded', 'false' === $el.attr( 'aria-expanded' ) ? 'true' : 'false' )
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   147
			.parents( '.imgedit-group-top' ).toggleClass( 'imgedit-help-toggled' ).find( '.imgedit-help' ).slideToggle( 'fast' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   148
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   152
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   153
	 * @summary Gets the value from the image edit target.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   154
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   155
	 * The image edit target contains the image sizes where the (possible) changes
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   156
	 * have to be applied to.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   157
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   158
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   159
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   160
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   161
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   162
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   163
	 * @returns {string} The value from the imagedit-save-target input field when available,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   164
	 *                   or 'full' when not available.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   165
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	getTarget : function(postid) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   170
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   171
	 * @summary Recalculates the height or width and keeps the original aspect ratio.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   172
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   173
	 * If the original image size is exceeded a red exclamation mark is shown.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   174
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   175
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   176
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   177
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   178
	 * @param {number}         postid The current post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   179
	 * @param {number}         x      Is 0 when it applies the y-axis
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   180
	 *                                and 1 when applicable for the x-axis.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   181
	 * @param {jQuery}         el     Element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   182
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   183
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   184
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   185
	scaleChanged : function( postid, x, el ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
		var w = $('#imgedit-scale-width-' + postid), h = $('#imgedit-scale-height-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
		warn = $('#imgedit-scale-warn-' + postid), w1 = '', h1 = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   189
		if ( false === this.validateNumeric( el ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   190
			return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   191
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   192
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
		if ( x ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   194
			h1 = ( w.val() !== '' ) ? Math.round( w.val() / this.hold.xy_ratio ) : '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			h.val( h1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		} else {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   197
			w1 = ( h.val() !== '' ) ? Math.round( h.val() * this.hold.xy_ratio ) : '';
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
			w.val( w1 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
		if ( ( h1 && h1 > this.hold.oh ) || ( w1 && w1 > this.hold.ow ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
			warn.css('visibility', 'visible');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
			warn.css('visibility', 'hidden');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   208
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   209
	 * @summary Gets the selected aspect ratio.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   210
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   211
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   212
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   213
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   214
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   215
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   216
	 * @returns {string} The aspect ratio.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   217
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
	getSelRatio : function(postid) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   219
		var x = this.hold.w, y = this.hold.h,
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
			X = this.intval( $('#imgedit-crop-width-' + postid).val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
			Y = this.intval( $('#imgedit-crop-height-' + postid).val() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
		if ( X && Y ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
			return X + ':' + Y;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
		if ( x && y ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
			return x + ':' + y;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   229
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
		return '1:1';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   234
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   235
	 * @summary Removes the last action from the image edit history
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   236
	 * The history consist of (edit) actions performed on the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   237
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   238
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   239
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   240
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   241
	 * @param {number} postid  The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   242
	 * @param {number} setSize 0 or 1, when 1 the image resets to its original size.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   243
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   244
	 * @returns {string} JSON string containing the history or an empty string if no history exists.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   245
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
	filterHistory : function(postid, setSize) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   247
		// Apply undo state to history.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
		var history = $('#imgedit-history-' + postid).val(), pop, n, o, i, op = [];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   250
		if ( history !== '' ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   251
			// Read the JSON string with the image edit history.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
			history = JSON.parse(history);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
			pop = this.intval( $('#imgedit-undone-' + postid).val() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
			if ( pop > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
				while ( pop > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
					history.pop();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
					pop--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   261
			// Reset size to it's original state.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
			if ( setSize ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
				if ( !history.length ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
					this.hold.w = this.hold.ow;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   265
					this.hold.h = this.hold.oh;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
					return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   269
				// Restore original 'o'.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
				o = history[history.length - 1];
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   271
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   272
				// c = 'crop', r = 'rotate', f = 'flip'
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
				o = o.c || o.r || o.f || false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
				if ( o ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   276
					// fw = Full image width
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
					this.hold.w = o.fw;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   278
					// fh = Full image height
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
					this.hold.h = o.fh;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   283
			// Filter the last step/action from the history.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			for ( n in history ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
				i = history[n];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
				if ( i.hasOwnProperty('c') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
					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
   288
				} else if ( i.hasOwnProperty('r') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
					op[n] = { 'r': i.r.r };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
				} else if ( i.hasOwnProperty('f') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
					op[n] = { 'f': i.f.f };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
			return JSON.stringify(op);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
	},
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   298
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   299
	 * @summary Binds the necessary events to the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   300
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   301
	 * When the image source is reloaded the image will be reloaded.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   302
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   303
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   304
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   305
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   306
	 * @param {number}   postid   The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   307
	 * @param {string}   nonce    The nonce to verify the request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   308
	 * @param {function} callback Function to execute when the image is loaded.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   309
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   310
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   311
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
	refreshEditor : function(postid, nonce, callback) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
		var t = this, data, img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		t.toggleEditor(postid, 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
		data = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
			'action': 'imgedit-preview',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
			'_ajax_nonce': nonce,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			'postid': postid,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
			'history': t.filterHistory(postid, 1),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			'rand': t.intval(Math.random() * 1000000)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   324
		img = $( '<img id="image-preview-' + postid + '" alt="" />' )
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   325
			.on( 'load', { history: data.history }, function( event ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   326
				var max1, max2,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   327
					parent = $( '#imgedit-crop-' + postid ),
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   328
					t = imageEdit,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   329
					historyObj;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   330
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   331
				// Checks if there already is some image-edit history.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   332
				if ( '' !== event.data.history ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   333
					historyObj = JSON.parse( event.data.history );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   334
					// If last executed action in history is a crop action.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   335
					if ( historyObj[historyObj.length - 1].hasOwnProperty( 'c' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   336
						/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   337
						 * A crop action has completed and the crop button gets disabled
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   338
						 * ensure the undo button is enabled.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   339
						 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   340
						t.setDisabled( $( '#image-undo-' + postid) , true );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   341
						// Move focus to the undo button to avoid a focus loss.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   342
						$( '#image-undo-' + postid ).focus();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   343
					}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   344
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
				parent.empty().append(img);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				// w, h are the new full size dims
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
				max1 = Math.max( t.hold.w, t.hold.h );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
				max2 = Math.max( $(img).width(), $(img).height() );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
				t.hold.sizer = max1 > max2 ? max2 / max1 : 1;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
				t.initCrop(postid, img, parent);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
				t.setCropSelection(postid, 0);
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
				if ( (typeof callback !== 'undefined') && callback !== null ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
					callback();
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   358
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   360
				if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() === '0' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
					$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).prop('disabled', true);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   364
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
				t.toggleEditor(postid, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
			})
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
			.on('error', function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
				$('#imgedit-crop-' + postid).empty().append('<div class="error"><p>' + imageEditL10n.error + '</p></div>');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
				t.toggleEditor(postid, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
			})
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
			.attr('src', ajaxurl + '?' + $.param(data));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
	},
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   374
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   375
	 * @summary Performs an image edit action.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   376
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   377
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   378
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   379
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   380
	 * @param  {number}  postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   381
	 * @param  {string}  nonce  The nonce to verify the request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   382
	 * @param  {string}  action The action to perform on the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   383
	 *                          The possible actions are: "scale" and "restore".
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   384
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   385
	 * @returns {boolean|void} Executes a post request that refreshes the page
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   386
	 *                         when the action is performed.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   387
	 *                         Returns false if a invalid action is given,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   388
	 *                         or when the action cannot be performed.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   389
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
	action : function(postid, nonce, action) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
		var t = this, data, w, h, fw, fh;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   393
		if ( t.notsaved(postid) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   395
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		data = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
			'action': 'image-editor',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
			'_ajax_nonce': nonce,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			'postid': postid
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   403
		if ( 'scale' === action ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
			w = $('#imgedit-scale-width-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
			h = $('#imgedit-scale-height-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
			fw = t.intval(w.val()),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
			fh = t.intval(h.val());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			if ( fw < 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
				w.focus();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
			} else if ( fh < 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
				h.focus();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
			if ( fw === t.hold.ow || fh === t.hold.oh ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
				return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
			data['do'] = 'scale';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
			data.fwidth = fw;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   423
			data.fheight = fh;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   424
		} else if ( 'restore' === action ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
			data['do'] = 'restore';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
		t.toggleEditor(postid, 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
		$.post(ajaxurl, data, function(r) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			$('#image-editor-' + postid).empty().append(r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
			t.toggleEditor(postid, 0);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   434
			// refresh the attachment model so that changes propagate
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   435
			if ( t._view ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   436
				t._view.refresh();
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
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   441
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   442
	 * @summary Stores the changes that are made to the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   443
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   444
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   445
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   446
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   447
	 * @param {number}  postid   The post id to get the image from the database.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   448
	 * @param {string}  nonce    The nonce to verify the request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   449
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   450
	 * @returns {boolean|void}  If the actions are successfully saved a response message is shown.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   451
	 *                          Returns false if there is no image editing history,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   452
	 *                          thus there are not edit-actions performed on the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   453
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
	save : function(postid, nonce) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   455
		var data,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   456
			target = this.getTarget(postid),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   457
			history = this.filterHistory(postid, 0),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   458
			self = this;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
		if ( '' === history ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		this.toggleEditor(postid, 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		data = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			'action': 'image-editor',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
			'_ajax_nonce': nonce,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
			'postid': postid,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
			'history': history,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
			'target': target,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
			'context': $('#image-edit-context').length ? $('#image-edit-context').val() : null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
			'do': 'save'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
		};
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   474
		// Post the image edit data to the backend.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
		$.post(ajaxurl, data, function(r) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   476
			// Read the response.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
			var ret = JSON.parse(r);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   479
			// If a response is returned, close the editor and show an error.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
			if ( ret.error ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   481
				$('#imgedit-response-' + postid).html('<div class="error"><p>' + ret.error + '</p></div>');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
				imageEdit.close(postid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   486
			if ( ret.fw && ret.fh ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
				$('#media-dims-' + postid).html( ret.fw + ' &times; ' + ret.fh );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   488
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   490
			if ( ret.thumbnail ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
				$('.thumbnail', '#thumbnail-head-' + postid).attr('src', ''+ret.thumbnail);
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   492
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   494
			if ( ret.msg ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
				$('#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
   496
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   498
			if ( self._view ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   499
				self._view.save();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   500
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   501
				imageEdit.close(postid);
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   502
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   506
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   507
	 * @summary Creates the image edit window.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   508
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   509
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   510
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   511
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   512
	 * @param {number} postid   The post id for the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   513
	 * @param {string} nonce    The nonce to verify the request.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   514
	 * @param {object} view     The image editor view to be used for the editing.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   515
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   516
	 * @returns {void|promise} Either returns void if the button was already activated
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   517
	 *                         or returns an instance of the image editor, wrapped in a promise.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   518
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
	open : function( postid, nonce, view ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   520
		this._view = view;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   521
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   522
		var dfd, data, elem = $('#image-editor-' + postid), head = $('#media-head-' + postid),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
			btn = $('#imgedit-open-btn-' + postid), spin = btn.siblings('.spinner');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   525
		/*
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   526
		 * Instead of disabling the button, which causes a focus loss and makes screen
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   527
		 * readers announce "unavailable", return if the button was already clicked.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   528
		 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   529
		if ( btn.hasClass( 'button-activated' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   530
			return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   531
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   532
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   533
		spin.addClass( 'is-active' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
		data = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
			'action': 'image-editor',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
			'_ajax_nonce': nonce,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
			'postid': postid,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
			'do': 'open'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   542
		dfd = $.ajax({
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   543
			url:  ajaxurl,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   544
			type: 'post',
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   545
			data: data,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   546
			beforeSend: function() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   547
				btn.addClass( 'button-activated' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   548
			}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   549
		}).done(function( html ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   550
			elem.html( html );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
			head.fadeOut('fast', function(){
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   552
				elem.fadeIn('fast');
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   553
				btn.removeClass( 'button-activated' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   554
				spin.removeClass( 'is-active' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
			});
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   556
			// Initialise the Image Editor now that everything is ready.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   557
			imageEdit.init( postid );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
		});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   559
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   560
		return dfd;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   563
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   564
	 * @summary Initializes the cropping tool and sets a default cropping selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   565
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   566
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   567
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   568
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   569
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   570
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   571
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   572
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
	imgLoaded : function(postid) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
		var img = $('#image-preview-' + postid), parent = $('#imgedit-crop-' + postid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   576
		// Ensure init has run even when directly loaded.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   577
		if ( 'undefined' === typeof this.hold.sizer ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   578
			this.init( postid );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   579
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   580
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
		this.initCrop(postid, img, parent);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
		this.setCropSelection(postid, 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
		this.toggleEditor(postid, 0);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   584
		// Editor is ready, move focus to the first focusable element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   585
		$( '.imgedit-wrap .imgedit-help-toggle' ).eq( 0 ).focus();
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   588
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   589
	 * @summary Initializes the cropping tool.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   590
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   591
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   592
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   593
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   594
	 * @param {number}      postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   595
	 * @param {HTMLElement} image  The preview image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   596
	 * @param {HTMLElement} parent The preview image container.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   597
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   598
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   599
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
	initCrop : function(postid, image, parent) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   601
		var t = this,
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   602
			selW = $('#imgedit-sel-width-' + postid),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   603
			selH = $('#imgedit-sel-height-' + postid),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   604
			$img;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
		t.iasapi = $(image).imgAreaSelect({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
			parent: parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
			instance: true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
			handles: true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
			keys: true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
			minWidth: 3,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
			minHeight: 3,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   614
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   615
			 * @summary Sets the CSS styles and binds events for locking the aspect ratio.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   616
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   617
			 * @param {jQuery} img The preview image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   618
			 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   619
			onInit: function( img ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   620
				// Ensure that the imgAreaSelect wrapper elements are position:absolute.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   621
				// (even if we're in a position:fixed modal)
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   622
				$img = $( img );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   623
				$img.next().css( 'position', 'absolute' )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   624
					.nextAll( '.imgareaselect-outer' ).css( 'position', 'absolute' );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   625
				/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   626
				 * @summary Binds mouse down event to the cropping container.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   627
				 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   628
				 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   629
				 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   630
				parent.children().on( 'mousedown, touchstart', function(e){
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
					var ratio = false, sel, defRatio;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
					if ( e.shiftKey ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
						sel = t.iasapi.getSelection();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
						defRatio = t.getSelRatio(postid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
						ratio = ( sel && sel.width && sel.height ) ? sel.width + ':' + sel.height : defRatio;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
					t.iasapi.setOptions({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
						aspectRatio: ratio
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
					});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
				});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
			},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   645
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   646
			 * @summary Event triggered when starting a selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   647
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   648
			 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   649
			 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   650
			onSelectStart: function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
				imageEdit.setDisabled($('#imgedit-crop-sel-' + postid), 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
			},
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   653
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   654
			 * @summary Event triggered when the selection is ended.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   655
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   656
			 * @param {object} img jQuery object representing the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   657
			 * @param {object} c   The selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   658
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   659
			 * @returns {object}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   660
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
			onSelectEnd: function(img, c) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
				imageEdit.setCropSelection(postid, c);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
			},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   665
			/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   666
			 * @summary Event triggered when the selection changes.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   667
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   668
			 * @param {object} img jQuery object representing the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   669
			 * @param {object} c   The selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   670
			 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   671
			 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   672
			 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
			onSelectChange: function(img, c) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
				var sizer = imageEdit.hold.sizer;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
				selW.val( imageEdit.round(c.width / sizer) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
				selH.val( imageEdit.round(c.height / sizer) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   681
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   682
	 * @summary Stores the current crop selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   683
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   684
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   685
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   686
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   687
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   688
	 * @param {object} c      The selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   689
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   690
	 * @returns {boolean}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   691
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
	setCropSelection : function(postid, c) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   693
		var sel;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   694
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   695
		c = c || 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
		if ( !c || ( c.width < 3 && c.height < 3 ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
			this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
			this.setDisabled($('#imgedit-crop-sel-' + postid), 0);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
			$('#imgedit-sel-width-' + postid).val('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
			$('#imgedit-sel-height-' + postid).val('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
			$('#imgedit-selection-' + postid).val('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
		sel = { 'x': c.x1, 'y': c.y1, 'w': c.width, 'h': c.height };
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
		this.setDisabled($('.imgedit-crop', '#imgedit-panel-' + postid), 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
		$('#imgedit-selection-' + postid).val( JSON.stringify(sel) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   711
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   712
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   713
	 * @summary Closes the image editor.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   714
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   715
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   716
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   717
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   718
	 * @param {number}  postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   719
	 * @param {bool}    warn   Warning message.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   720
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   721
	 * @returns {void|bool} Returns false if there is a warning.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   722
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
	close : function(postid, warn) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
		warn = warn || false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   726
		if ( warn && this.notsaved(postid) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   728
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		this.iasapi = {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		this.hold = {};
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   732
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   733
		// 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
   734
		// whatever that might have been.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   735
		if ( this._view ){
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   736
			this._view.back();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   737
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   738
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   739
		// 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
   740
		else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   741
			$('#image-editor-' + postid).fadeOut('fast', function() {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   742
				$( '#media-head-' + postid ).fadeIn( 'fast', function() {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   743
					// Move focus back to the Edit Image button. Runs also when saving.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   744
					$( '#imgedit-open-btn-' + postid ).focus();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   745
				});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   746
				$(this).empty();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   747
			});
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   748
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   749
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   750
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   753
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   754
	 * @summary Checks if the image edit history is saved.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   755
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   756
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   757
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   758
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   759
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   760
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   761
	 * @returns {boolean} Returns true if the history is not saved.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   762
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
	notsaved : function(postid) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
		var h = $('#imgedit-history-' + postid).val(),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   765
			history = ( h !== '' ) ? JSON.parse(h) : [],
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
			pop = this.intval( $('#imgedit-undone-' + postid).val() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
		if ( pop < history.length ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   769
			if ( confirm( $('#imgedit-leaving-' + postid).html() ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
				return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   771
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   777
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   778
	 * @summary Adds an image edit action to the history.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   779
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   780
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   781
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   782
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   783
	 * @param {object} op     The original position.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   784
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   785
	 * @param {string} nonce  The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   786
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   787
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   788
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
	addStep : function(op, postid, nonce) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
		var t = this, elem = $('#imgedit-history-' + postid),
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   791
			history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [],
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   792
			undone = $( '#imgedit-undone-' + postid ),
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   793
			pop = t.intval( undone.val() );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
		while ( pop > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
			history.pop();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
			pop--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
		undone.val(0); // reset
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
		history.push(op);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
		elem.val( JSON.stringify(history) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
		t.refreshEditor(postid, nonce, function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
			t.setDisabled($('#image-undo-' + postid), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
			t.setDisabled($('#image-redo-' + postid), false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   810
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   811
	 * @summary Rotates the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   812
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   813
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   814
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   815
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   816
	 * @param {string} angle  The angle the image is rotated with.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   817
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   818
	 * @param {string} nonce  The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   819
	 * @param {object} t      The target element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   820
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   821
	 * @returns {boolean}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   822
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
	rotate : function(angle, postid, nonce, t) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   824
		if ( $(t).hasClass('disabled') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   826
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   828
		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
   829
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   831
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   832
	 * @summary Flips the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   833
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   834
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   835
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   836
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   837
	 * @param {number} axis   The axle the image is flipped on.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   838
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   839
	 * @param {string} nonce  The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   840
	 * @param {object} t      The target element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   841
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   842
	 * @returns {boolean}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   843
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
	flip : function (axis, postid, nonce, t) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   845
		if ( $(t).hasClass('disabled') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   847
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   849
		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
   850
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   852
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   853
	 * @summary Crops the image.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   854
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   855
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   856
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   857
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   858
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   859
	 * @param {string} nonce  The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   860
	 * @param {object} t      The target object.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   861
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   862
	 * @returns {void|boolean} Returns false if the crop button is disabled.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   863
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
	crop : function (postid, nonce, t) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
		var sel = $('#imgedit-selection-' + postid).val(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
			w = this.intval( $('#imgedit-sel-width-' + postid).val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
			h = this.intval( $('#imgedit-sel-height-' + postid).val() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   869
		if ( $(t).hasClass('disabled') || sel === '' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
			return false;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   871
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
		sel = JSON.parse(sel);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
		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
   875
			sel.fw = w;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   876
			sel.fh = h;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
			this.addStep({ 'c': sel }, postid, nonce);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   881
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   882
	 * @summary Undoes an image edit action.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   883
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   884
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   885
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   886
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   887
	 * @param {number} postid   The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   888
	 * @param {string} nonce    The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   889
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   890
	 * @returns {void|false} Returns false if the undo button is disabled.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   891
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	undo : function (postid, nonce) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
		var t = this, button = $('#image-undo-' + postid), elem = $('#imgedit-undone-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
			pop = t.intval( elem.val() ) + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   896
		if ( button.hasClass('disabled') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   898
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
		elem.val(pop);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
		t.refreshEditor(postid, nonce, function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
			var elem = $('#imgedit-history-' + postid),
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   903
				history = ( elem.val() !== '' ) ? JSON.parse( elem.val() ) : [];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
			t.setDisabled($('#image-redo-' + postid), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
			t.setDisabled(button, pop < history.length);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   907
			// When undo gets disabled, move focus to the redo button to avoid a focus loss.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   908
			if ( history.length === pop ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   909
				$( '#image-redo-' + postid ).focus();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   910
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   914
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   915
	 * Reverts a undo action.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   916
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   917
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   918
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   919
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   920
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   921
	 * @param {string} nonce  The nonce.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   922
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   923
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   924
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
	redo : function(postid, nonce) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
		var t = this, button = $('#image-redo-' + postid), elem = $('#imgedit-undone-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
			pop = t.intval( elem.val() ) - 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   929
		if ( button.hasClass('disabled') ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   931
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
		elem.val(pop);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
		t.refreshEditor(postid, nonce, function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
			t.setDisabled($('#image-undo-' + postid), true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
			t.setDisabled(button, pop > 0);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   937
			// When redo gets disabled, move focus to the undo button to avoid a focus loss.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   938
			if ( 0 === pop ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   939
				$( '#image-undo-' + postid ).focus();
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   940
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   944
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   945
	 * @summary Sets the selection for the height and width in pixels.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   946
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   947
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   948
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   949
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   950
	 * @param {number} postid The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   951
	 * @param {jQuery} el     The element containing the values.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   952
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   953
	 * @returns {void|boolean} Returns false when the x or y value is lower than 1,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   954
	 *                         void when the value is not numeric or when the operation
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   955
	 *                         is successful.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   956
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   957
	setNumSelection : function( postid, el ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		var sel, elX = $('#imgedit-sel-width-' + postid), elY = $('#imgedit-sel-height-' + postid),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
			x = this.intval( elX.val() ), y = this.intval( elY.val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
			img = $('#image-preview-' + postid), imgh = img.height(), imgw = img.width(),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   961
			sizer = this.hold.sizer, x1, y1, x2, y2, ias = this.iasapi;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   963
		if ( false === this.validateNumeric( el ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   964
			return;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   965
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   966
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
		if ( x < 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
			elX.val('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
		if ( y < 1 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
			elY.val('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
		if ( x && y && ( sel = ias.getSelection() ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
			x2 = sel.x1 + Math.round( x * sizer );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
			y2 = sel.y1 + Math.round( y * sizer );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
			x1 = sel.x1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
			y1 = sel.y1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
			if ( x2 > imgw ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
				x1 = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
				x2 = imgw;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
				elX.val( Math.round( x2 / sizer ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
			if ( y2 > imgh ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
				y1 = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
				y2 = imgh;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
				elY.val( Math.round( y2 / sizer ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
			ias.setSelection( x1, y1, x2, y2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
			ias.update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
			this.setCropSelection(postid, ias.getSelection());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1001
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1002
	 * Rounds a number to a whole.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1003
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1004
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1005
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1006
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1007
	 * @param {number} num The number.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1008
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1009
	 * @returns {number} The number rounded to a whole number.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1010
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
	round : function(num) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
		var s;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
		num = Math.round(num);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1015
		if ( this.hold.sizer > 0.6 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
			return num;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1017
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
		s = num.toString().slice(-1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1021
		if ( '1' === s ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
			return num - 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1023
		} else if ( '9' === s ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
			return num + 1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1025
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
		return num;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1030
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1031
	 * Sets a locked aspect ratio for the selection.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1032
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1033
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1034
	 * @since    2.9.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1035
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1036
	 * @param {number} postid     The post id.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1037
	 * @param {number} n          The ratio to set.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1038
	 * @param {jQuery} el         The element containing the values.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1039
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1040
	 * @returns {void}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1041
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	setRatioSelection : function(postid, n, el) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
		var sel, r, x = this.intval( $('#imgedit-crop-width-' + postid).val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
			y = this.intval( $('#imgedit-crop-height-' + postid).val() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
			h = $('#image-preview-' + postid).height();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1047
		if ( false === this.validateNumeric( el ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
		if ( x && y ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
			this.iasapi.setOptions({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
				aspectRatio: x + ':' + y
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
			if ( sel = this.iasapi.getSelection(true) ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1057
				r = Math.ceil( sel.y1 + ( ( sel.x2 - sel.x1 ) / ( x / y ) ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
				if ( r > h ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
					r = h;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1061
					if ( n ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
						$('#imgedit-crop-height-' + postid).val('');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1063
					} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
						$('#imgedit-crop-width-' + postid).val('');
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1065
					}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
				this.iasapi.setSelection( sel.x1, sel.y1, sel.x2, r );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
				this.iasapi.update();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1072
	},
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1073
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1074
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1075
	 * Validates if a value in a jQuery.HTMLElement is numeric.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1076
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1077
	 * @memberof imageEdit
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1078
	 * @since    4.6
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1079
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1080
	 * @param {jQuery} el The html element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1081
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1082
	 * @returns {void|boolean} Returns false if the value is not numeric,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1083
	 *                         void when it is.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1084
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1085
	validateNumeric: function( el ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1086
		if ( ! this.intval( $( el ).val() ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1087
			$( el ).val( '' );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1088
			return false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
  1089
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
	}
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
  1091
};
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
})(jQuery);