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