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