@@ -36,6 +36,12 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
36
36
resetButtonId : resetButtonId
37
37
} ) ,
38
38
39
+ helpers = CKEDITOR . plugins . image2 ,
40
+
41
+ // Functions inherited from image2 plugin.
42
+ checkHasNaturalRatio = helpers . checkHasNaturalRatio ,
43
+ getNatural = helpers . getNatural ,
44
+
39
45
// Global variables referring to the dialog's context.
40
46
doc , widget , image ,
41
47
@@ -52,7 +58,9 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
52
58
lockRatio , userDefinedLock ,
53
59
54
60
// Global variables referring to dialog fields and elements.
55
- lockButton , resetButton , widthField , heightField ;
61
+ lockButton , resetButton , widthField , heightField ,
62
+
63
+ natural ;
56
64
57
65
// Validates dimension. Allowed values are:
58
66
// "123px", "123", "" (empty string)
@@ -124,7 +132,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
124
132
125
133
// There was problem loading the image. Unlock ratio.
126
134
if ( ! image )
127
- return toggleLockDimensions ( false ) ;
135
+ return toggleLockRatio ( false ) ;
128
136
129
137
// Fill width field with the width of the new image.
130
138
widthField . setValue ( width ) ;
@@ -139,7 +147,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
139
147
preLoadedHeight = height ;
140
148
141
149
// Check for new lock value if image exist.
142
- toggleLockDimensions ( 'check' ) ;
150
+ toggleLockRatio ( helpers . checkHasNaturalRatio ( image ) ) ;
143
151
} ) ;
144
152
145
153
srcChanged = true ;
@@ -183,7 +191,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
183
191
184
192
// If the value of the field is invalid (e.g. with %), unlock ratio.
185
193
if ( ! value . match ( regexGetSizeOrEmpty ) )
186
- toggleLockDimensions ( false ) ;
194
+ toggleLockRatio ( false ) ;
187
195
188
196
// No automatic re-scale when dimension is '0'.
189
197
if ( value === '0' )
@@ -235,7 +243,7 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
235
243
dialog . addFocusable ( lockButton , 4 ) ;
236
244
237
245
lockButton . on ( 'click' , function ( evt ) {
238
- toggleLockDimensions ( ) ;
246
+ toggleLockRatio ( ) ;
239
247
evt . data && evt . data . preventDefault ( ) ;
240
248
} , this . getDialog ( ) ) ;
241
249
@@ -270,46 +278,28 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
270
278
}
271
279
}
272
280
273
- function toggleLockDimensions ( enable ) {
281
+ function toggleLockRatio ( enable ) {
274
282
// No locking if there's no radio (i.e. due to ACF).
275
283
if ( ! lockButton )
276
284
return ;
277
285
278
- var width , height ;
279
-
280
- // Check image ratio and original image ratio, but respecting user's
281
- // preference. This is performed when a new image is pre-loaded
282
- // but not if user already manually locked the ratio.
283
- if ( enable == 'check' && ! userDefinedLock ) {
284
- width = widthField . getValue ( ) ;
285
- height = heightField . getValue ( ) ;
286
-
287
- var domRatio = preLoadedWidth * 1000 / preLoadedHeight ,
288
- ratio = width * 1000 / height ;
286
+ if ( typeof enable == 'boolean' ) {
287
+ // If user explicitly wants to decide whether
288
+ // to lock or not, don't do anything.
289
+ if ( userDefinedLock )
290
+ return ;
289
291
290
- lockRatio = false ;
291
-
292
- // Lock ratio, if there is no width and no height specified.
293
- if ( ! width && ! height )
294
- lockRatio = true ;
295
-
296
- // Lock ratio if there is at least width or height specified,
297
- // and the old ratio that matches the new one.
298
- else if ( ! isNaN ( domRatio + ratio ) && Math . round ( domRatio ) == Math . round ( ratio ) )
299
- lockRatio = true ;
300
- }
301
-
302
- // True or false.
303
- else if ( typeof enable == 'boolean' )
304
292
lockRatio = enable ;
293
+ }
305
294
306
295
// Undefined. User changed lock value.
307
296
else {
297
+ var width = widthField . getValue ( ) ,
298
+ height ;
299
+
308
300
userDefinedLock = true ;
309
301
lockRatio = ! lockRatio ;
310
302
311
- width = widthField . getValue ( ) ;
312
-
313
303
// Automatically adjust height to width to match
314
304
// the original ratio (based on dom- dimensions).
315
305
if ( lockRatio && width ) {
@@ -356,22 +346,16 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
356
346
image = widget . parts . image ;
357
347
358
348
// Reset global variables.
359
- preLoadedWidth = preLoadedHeight = srcChanged =
360
- userDefinedLock = lockRatio = false ;
349
+ srcChanged = userDefinedLock = lockRatio = false ;
350
+
351
+ // Natural dimensions of the image.
352
+ natural = getNatural ( image ) ;
361
353
362
- // TODO: IE8
363
354
// Get the natural width of the image.
364
- domWidth = image . $ . naturalWidth ;
355
+ preLoadedWidth = domWidth = natural . width ;
365
356
366
- // TODO: IE8
367
357
// Get the natural height of the image.
368
- domHeight = image . $ . naturalHeight ;
369
-
370
- // Determine image ratio lock on startup. Delayed, waiting for
371
- // fields to be filled with setup functions.
372
- setTimeout ( function ( ) {
373
- toggleLockDimensions ( 'check' ) ;
374
- } ) ;
358
+ preLoadedHeight = domHeight = natural . height ;
375
359
} ,
376
360
contents : [
377
361
{
@@ -473,6 +457,12 @@ CKEDITOR.dialog.add( 'image2', function( editor ) {
473
457
type : 'html' ,
474
458
style : lockResetStyle ,
475
459
onLoad : onLoadLockReset ,
460
+ setup : function ( widget ) {
461
+ toggleLockRatio ( widget . data . lock ) ;
462
+ } ,
463
+ commit : function ( widget ) {
464
+ widget . setData ( 'lock' , lockRatio ) ;
465
+ } ,
476
466
html : lockResetHtml
477
467
}
478
468
]
0 commit comments