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