Skip to content

Commit c89bbc1

Browse files
gnarfscottgonzalez
authored andcommitted
Class animation: Cleaned up detection of what we can animate and fixed border styles. Fixes #7109 - animateClass doesn't like going from borderStyle: none to borderStyle: *.
1 parent 8b8f5bf commit c89bbc1

File tree

1 file changed

+62
-73
lines changed

1 file changed

+62
-73
lines changed

ui/jquery.effects.core.js

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ var colors = {
146146
/****************************** CLASS ANIMATIONS ******************************/
147147
/******************************************************************************/
148148

149-
var classAnimationActions = [ 'add', 'remove', 'toggle' ],
149+
var classAnimationActions = [ "add", "remove", "toggle" ],
150150
shorthandStyles = {
151151
border: 1,
152152
borderBottom: 1,
@@ -157,7 +157,18 @@ var classAnimationActions = [ 'add', 'remove', 'toggle' ],
157157
borderWidth: 1,
158158
margin: 1,
159159
padding: 1
160+
},
161+
// prefix used for storing data on .data()
162+
dataSpace = "ec.storage.";
163+
164+
$.each([ "borderLeftStyle", "borderRightStyle", "borderBottomStyle", "borderTopStyle" ], function(_, prop) {
165+
$.fx.step[ prop ] = function( fx ) {
166+
if ( fx.end !== "none" && !fx.setAttr || fx.pos === 1 && !fx.setAttr ) {
167+
jQuery.style( fx.elem, prop, fx.end );
168+
fx.setAttr = true;
169+
}
160170
};
171+
})
161172

162173
function getElementStyles() {
163174
var style = document.defaultView
@@ -173,105 +184,83 @@ function getElementStyles() {
173184
len = style.length;
174185
while ( len-- ) {
175186
key = style[ len ];
176-
if ( typeof style[ key ] == 'string' ) {
177-
camelCase = key.replace( /\-(\w)/g, function( all, letter ) {
178-
return letter.toUpperCase();
179-
});
180-
newStyle[ camelCase ] = style[ key ];
187+
if ( typeof style[ key ] === "string" ) {
188+
newStyle[ $.camelCase( key ) ] = style[ key ];
181189
}
182190
}
183191
} else {
184192
for ( key in style ) {
185-
if ( typeof style[ key ] === 'string' ) {
193+
if ( typeof style[ key ] === "string" ) {
186194
newStyle[ key ] = style[ key ];
187195
}
188196
}
189197
}
190-
198+
191199
return newStyle;
192200
}
193201

194-
function filterStyles( styles ) {
195-
var name, value;
196-
for ( name in styles ) {
197-
value = styles[ name ];
198-
if (
199-
// ignore null and undefined values
200-
value == null ||
201-
// ignore functions (when does this occur?)
202-
$.isFunction( value ) ||
203-
// shorthand styles that need to be expanded
204-
name in shorthandStyles ||
205-
// ignore scrollbars (break in IE)
206-
( /scrollbar/ ).test( name ) ||
207-
208-
// only colors or values that can be converted to numbers
209-
( !( /color/i ).test( name ) && isNaN( parseFloat( value ) ) )
210-
) {
211-
delete styles[ name ];
212-
}
213-
}
214-
215-
return styles;
216-
}
217202

218203
function styleDifference( oldStyle, newStyle ) {
219-
var diff = { _: 0 }, // http://dev.jquery.com/ticket/5459
220-
name;
204+
var diff = {},
205+
name, value;
221206

222207
for ( name in newStyle ) {
223-
if ( oldStyle[ name ] != newStyle[ name ] ) {
224-
diff[ name ] = newStyle[ name ];
208+
value = newStyle[ name ];
209+
if ( oldStyle[ name ] != value ) {
210+
if ( !shorthandStyles[ name ] ) {
211+
if ( $.fx.step[ name ] || !isNaN( parseFloat( value ) ) ) {
212+
diff[ name ] = value;
213+
}
214+
}
225215
}
226216
}
227217

228218
return diff;
229219
}
230220

231221
$.effects.animateClass = function( value, duration, easing, callback ) {
232-
if ( $.isFunction( easing ) ) {
233-
callback = easing;
234-
easing = null;
235-
}
236-
237-
return this.queue(function() {
238-
var that = $( this ),
239-
originalStyleAttr = that.attr( 'style' ) || ' ',
240-
originalStyle = filterStyles( getElementStyles.call( this ) ),
222+
var o = $.speed( duration, easing, callback );
223+
224+
return this.queue( function() {
225+
var animated = $( this ),
226+
baseClass = animated.attr( "class" ),
227+
finalClass,
228+
originalStyleAttr = animated.attr( "style" ) || ' ',
229+
originalStyle = getElementStyles.call( this ),
241230
newStyle,
242-
className = that.attr( 'class' );
231+
diff,
232+
prop;
243233

244234
$.each( classAnimationActions, function(i, action) {
245235
if ( value[ action ] ) {
246-
that[ action + 'Class' ]( value[ action ] );
236+
animated[ action + "Class" ]( value[ action ] );
247237
}
248238
});
249-
newStyle = filterStyles( getElementStyles.call( this ) );
250-
that.attr( 'class', className );
251-
252-
that.animate( styleDifference( originalStyle, newStyle ), {
253-
queue: false,
254-
duration: duration,
255-
easing: easing,
256-
complete: function() {
257-
$.each( classAnimationActions, function( i, action ) {
258-
if ( value[ action ] ) {
259-
that[ action + 'Class' ]( value[ action ] );
239+
newStyle = getElementStyles.call( this );
240+
finalClass = animated.attr( "class" );
241+
animated.attr( "class", baseClass );
242+
243+
diff = styleDifference( originalStyle, newStyle );
244+
animated
245+
.animate( diff, {
246+
duration: duration,
247+
easing: o.easing,
248+
queue: false,
249+
complete: function() {
250+
animated.attr( "class", finalClass );
251+
252+
if ( typeof animated.attr( 'style' ) == 'object' ) {
253+
animated.attr( 'style' ).cssText = '';
254+
animated.attr( 'style' ).cssText = originalStyleAttr;
255+
} else {
256+
animated.attr( 'style', originalStyleAttr );
260257
}
261-
});
262-
// work around bug in IE by clearing the cssText before setting it
263-
if ( typeof that.attr( 'style' ) == 'object' ) {
264-
that.attr( 'style' ).cssText = '';
265-
that.attr( 'style' ).cssText = originalStyleAttr;
266-
} else {
267-
that.attr( 'style', originalStyleAttr );
268-
}
269-
if ( callback ) {
270-
callback.apply( this, arguments );
258+
259+
// this is guarnteed to be there if you use jQuery.speed()
260+
// it also handles dequeuing the next anim...
261+
o.complete.call( this );
271262
}
272-
$.dequeue( this );
273-
}
274-
});
263+
});
275264
});
276265
};
277266

@@ -300,7 +289,7 @@ $.fn.extend({
300289
return $.effects.animateClass.apply( this, [( force ? { add:classNames } : { remove:classNames }), speed, easing, callback ]);
301290
}
302291
} else {
303-
// without switch parameter;
292+
// without force parameter;
304293
return $.effects.animateClass.apply( this, [{ toggle: classNames }, force, speed, easing ]);
305294
}
306295
},
@@ -326,7 +315,7 @@ $.extend( $.effects, {
326315
save: function( element, set ) {
327316
for( var i=0; i < set.length; i++ ) {
328317
if ( set[ i ] !== null ) {
329-
element.data( "ec.storage." + set[ i ], element[ 0 ].style[ set[ i ] ] );
318+
element.data( dataSpace + set[ i ], element[ 0 ].style[ set[ i ] ] );
330319
}
331320
}
332321
},
@@ -335,7 +324,7 @@ $.extend( $.effects, {
335324
restore: function( element, set ) {
336325
for( var i=0; i < set.length; i++ ) {
337326
if ( set[ i ] !== null ) {
338-
element.css( set[ i ], element.data( "ec.storage." + set[ i ] ) );
327+
element.css( set[ i ], element.data( dataSpace + set[ i ] ) );
339328
}
340329
}
341330
},

0 commit comments

Comments
 (0)