jquery-ui/ui/jquery.effects.scale.js

291 lines
7.3 KiB
JavaScript
Raw Normal View History

/*!
* jQuery UI Effects Scale @VERSION
*
2012-03-08 15:53:08 +00:00
* Copyright 2012, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Effects/Scale
*
* Depends:
* jquery.effects.core.js
*/
(function( $, undefined ) {
$.effects.effect.puff = function( o, done ) {
var elem = $( this ),
mode = $.effects.setMode( elem, o.mode || "hide" ),
2011-06-21 06:15:42 +00:00
hide = mode === "hide",
percent = parseInt( o.percent, 10 ) || 150,
factor = percent / 100,
2012-04-19 01:57:51 +00:00
original = {
height: elem.height(),
width: elem.width()
};
$.extend( o, {
effect: "scale",
queue: false,
fade: true,
mode: mode,
complete: done,
2011-06-21 06:15:42 +00:00
percent: hide ? percent : 100,
2012-04-02 19:55:50 +00:00
from: hide ?
original :
{
height: original.height * factor,
width: original.width * factor
}
});
elem.effect( o );
};
$.effects.effect.scale = function( o, done ) {
// Create element
var el = $( this ),
options = $.extend( true, {}, o ),
mode = $.effects.setMode( el, o.mode || "effect" ),
2012-04-02 23:12:21 +00:00
percent = parseInt( o.percent, 10 ) ||
( parseInt( o.percent, 10 ) === 0 ? 0 : ( mode === "hide" ? 0 : 100 ) ),
direction = o.direction || "both",
origin = o.origin,
2012-04-19 01:57:51 +00:00
original = {
height: el.height(),
width: el.width(),
outerHeight: el.outerHeight(),
outerWidth: el.outerWidth()
},
factor = {
2012-04-02 23:12:21 +00:00
y: direction !== "horizontal" ? (percent / 100) : 1,
x: direction !== "vertical" ? (percent / 100) : 1
2012-04-19 01:57:51 +00:00
};
2011-03-07 00:34:18 +00:00
// We are going to pass this effect to the size effect:
options.effect = "size";
options.queue = false;
options.complete = done;
// Set default origin and restore for show/hide
2012-04-19 01:57:51 +00:00
if ( mode !== "effect" ) {
options.origin = origin || ["middle","center"];
options.restore = true;
}
2012-04-19 01:57:51 +00:00
options.from = o.from || ( mode === "show" ? { height: 0, width: 0 } : original );
options.to = {
2012-04-19 01:57:51 +00:00
height: original.height * factor.y,
width: original.width * factor.x,
2012-04-19 01:57:51 +00:00
outerHeight: original.outerHeight * factor.y,
outerWidth: original.outerWidth * factor.x
2012-04-19 01:57:51 +00:00
};
2012-04-02 19:55:50 +00:00
// Fade option to support puff
if ( options.fade ) {
2012-04-02 23:12:21 +00:00
if ( mode === "show" ) {
2012-04-19 01:57:51 +00:00
options.from.opacity = 0;
options.to.opacity = 1;
}
2012-04-02 23:12:21 +00:00
if ( mode === "hide" ) {
2012-04-19 01:57:51 +00:00
options.from.opacity = 1;
options.to.opacity = 0;
}
2012-04-02 19:55:50 +00:00
}
2011-03-07 00:34:18 +00:00
// Animate
el.effect( options );
2011-03-07 00:34:18 +00:00
};
2011-03-07 00:34:18 +00:00
$.effects.effect.size = function( o, done ) {
// Create element
2012-04-19 01:57:51 +00:00
var el = $( this ),
props = [ "position", "top", "bottom", "left", "right", "width", "height", "overflow", "opacity" ],
// Always restore
props1 = [ "position", "top", "bottom", "left", "right", "overflow", "opacity" ],
// Copy for children
props2 = [ "width", "height", "overflow" ],
cProps = [ "fontSize" ],
vProps = [ "borderTopWidth", "borderBottomWidth", "paddingTop", "paddingBottom" ],
hProps = [ "borderLeftWidth", "borderRightWidth", "paddingLeft", "paddingRight" ],
// Set options
mode = $.effects.setMode( el, o.mode || "effect" ),
restore = o.restore || mode !== "effect",
scale = o.scale || "both",
origin = o.origin || [ "middle", "center" ],
original, baseline, factor,
position = el.css( "position" );
if ( mode === "show" ) {
el.show();
}
original = {
2012-04-19 01:57:51 +00:00
height: el.height(),
width: el.width(),
outerHeight: el.outerHeight(),
outerWidth: el.outerWidth()
};
el.from = o.from || original;
el.to = o.to || original;
// Set scaling factor
factor = {
from: {
2012-04-19 01:57:51 +00:00
y: el.from.height / original.height,
x: el.from.width / original.width
},
to: {
2012-04-19 01:57:51 +00:00
y: el.to.height / original.height,
x: el.to.width / original.width
}
};
2011-03-07 00:34:18 +00:00
// Scale the css box
2012-04-02 23:12:21 +00:00
if ( scale === "box" || scale === "both" ) {
// Vertical props scaling
2012-04-19 01:57:51 +00:00
if ( factor.from.y !== factor.to.y ) {
props = props.concat( vProps );
el.from = $.effects.setTransition( el, vProps, factor.from.y, el.from );
el.to = $.effects.setTransition( el, vProps, factor.to.y, el.to );
2012-04-02 19:55:50 +00:00
}
2011-03-07 00:34:18 +00:00
// Horizontal props scaling
2012-04-19 01:57:51 +00:00
if ( factor.from.x !== factor.to.x ) {
props = props.concat( hProps );
el.from = $.effects.setTransition( el, hProps, factor.from.x, el.from );
el.to = $.effects.setTransition( el, hProps, factor.to.x, el.to );
2012-04-02 19:55:50 +00:00
}
}
2011-03-07 00:34:18 +00:00
// Scale the content
2012-04-19 01:57:51 +00:00
if ( scale === "content" || scale === "both" ) {
// Vertical props scaling
2012-04-19 01:57:51 +00:00
if ( factor.from.y !== factor.to.y ) {
props = props.concat( cProps );
el.from = $.effects.setTransition( el, cProps, factor.from.y, el.from );
el.to = $.effects.setTransition( el, cProps, factor.to.y, el.to );
2012-04-02 19:55:50 +00:00
}
}
2012-04-19 01:57:51 +00:00
$.effects.save( el, restore ? props : props1 );
el.show();
$.effects.createWrapper( el );
2012-04-19 01:57:51 +00:00
el.css( "overflow", "hidden" ).css( el.from );
// Adjust
if (origin) { // Calculate baseline shifts
baseline = $.effects.getBaseline( origin, original );
el.from.top = ( original.outerHeight - el.outerHeight() ) * baseline.y;
el.from.left = ( original.outerWidth - el.outerWidth() ) * baseline.x;
el.to.top = ( original.outerHeight - el.to.outerHeight ) * baseline.y;
el.to.left = ( original.outerWidth - el.to.outerWidth ) * baseline.x;
}
el.css( el.from ); // set top & left
// Animate
2012-04-02 23:12:21 +00:00
if ( scale === "content" || scale === "both" ) { // Scale the children
// Add margins/font-size
vProps = vProps.concat([ "marginTop", "marginBottom" ]).concat(cProps);
hProps = hProps.concat([ "marginLeft", "marginRight" ]);
props2 = props.concat(vProps).concat(hProps);
el.find( "*[width]" ).each( function(){
var child = $( this ),
2012-04-19 01:57:51 +00:00
c_original = {
height: child.height(),
width: child.width()
};
2012-04-02 23:12:21 +00:00
if (restore) {
$.effects.save(child, props2);
}
child.from = {
2012-04-19 01:57:51 +00:00
height: c_original.height * factor.from.y,
width: c_original.width * factor.from.x
};
child.to = {
2012-04-19 01:57:51 +00:00
height: c_original.height * factor.to.y,
width: c_original.width * factor.to.x
};
2011-03-07 00:34:18 +00:00
// Vertical props scaling
2012-04-19 01:57:51 +00:00
if ( factor.from.y !== factor.to.y ) {
child.from = $.effects.setTransition( child, vProps, factor.from.y, child.from );
child.to = $.effects.setTransition( child, vProps, factor.to.y, child.to );
2012-04-02 19:55:50 +00:00
}
2011-03-07 00:34:18 +00:00
// Horizontal props scaling
2012-04-02 23:12:21 +00:00
if ( factor.from.x !== factor.to.x ) {
child.from = $.effects.setTransition( child, hProps, factor.from.x, child.from );
child.to = $.effects.setTransition( child, hProps, factor.to.x, child.to );
2012-04-02 19:55:50 +00:00
}
2011-03-07 00:34:18 +00:00
// Animate children
child.css( child.from );
child.animate( child.to, o.duration, o.easing, function() {
2011-03-07 00:34:18 +00:00
// Restore children
2012-04-02 23:12:21 +00:00
if ( restore ) {
$.effects.restore( child, props2 );
}
});
});
2012-04-02 19:55:50 +00:00
}
// Animate
2012-04-19 01:57:51 +00:00
el.animate( el.to, {
queue: false,
duration: o.duration,
easing: o.easing,
complete: function() {
if ( el.to.opacity === 0 ) {
el.css( "opacity", el.from.opacity );
}
2012-04-02 23:12:21 +00:00
if( mode === "hide" ) {
el.hide();
}
$.effects.restore( el, restore ? props : props1 );
if ( !restore ) {
// we need to calculate our new positioning based on the scaling
if ( position === "static" ) {
el.css({
position: "relative",
top: el.to.top,
left: el.to.left
});
} else {
$.each([ "top", "left" ], function( idx, pos ) {
el.css( pos, function( _, str ) {
var val = parseInt( str, 10 ),
2012-04-28 21:36:38 +00:00
toRef = idx ? el.to.left : el.to.top;
// if original was "auto", recalculate the new value from wrapper
if ( str === "auto" ) {
return toRef + "px";
}
2012-04-19 01:57:51 +00:00
return val + toRef + "px";
});
});
}
}
$.effects.removeWrapper( el );
done();
}
});
};
})(jQuery);