jquery.ui.effect-scale.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*!
  2. * jQuery UI Effects Scale 1.10.2
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2013 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/scale-effect/
  10. *
  11. * Depends:
  12. * jquery.ui.effect.js
  13. */
  14. (function( $, undefined ) {
  15. $.effects.effect.puff = function( o, done ) {
  16. var elem = $( this ),
  17. mode = $.effects.setMode( elem, o.mode || "hide" ),
  18. hide = mode === "hide",
  19. percent = parseInt( o.percent, 10 ) || 150,
  20. factor = percent / 100,
  21. original = {
  22. height: elem.height(),
  23. width: elem.width(),
  24. outerHeight: elem.outerHeight(),
  25. outerWidth: elem.outerWidth()
  26. };
  27. $.extend( o, {
  28. effect: "scale",
  29. queue: false,
  30. fade: true,
  31. mode: mode,
  32. complete: done,
  33. percent: hide ? percent : 100,
  34. from: hide ?
  35. original :
  36. {
  37. height: original.height * factor,
  38. width: original.width * factor,
  39. outerHeight: original.outerHeight * factor,
  40. outerWidth: original.outerWidth * factor
  41. }
  42. });
  43. elem.effect( o );
  44. };
  45. $.effects.effect.scale = function( o, done ) {
  46. // Create element
  47. var el = $( this ),
  48. options = $.extend( true, {}, o ),
  49. mode = $.effects.setMode( el, o.mode || "effect" ),
  50. percent = parseInt( o.percent, 10 ) ||
  51. ( parseInt( o.percent, 10 ) === 0 ? 0 : ( mode === "hide" ? 0 : 100 ) ),
  52. direction = o.direction || "both",
  53. origin = o.origin,
  54. original = {
  55. height: el.height(),
  56. width: el.width(),
  57. outerHeight: el.outerHeight(),
  58. outerWidth: el.outerWidth()
  59. },
  60. factor = {
  61. y: direction !== "horizontal" ? (percent / 100) : 1,
  62. x: direction !== "vertical" ? (percent / 100) : 1
  63. };
  64. // We are going to pass this effect to the size effect:
  65. options.effect = "size";
  66. options.queue = false;
  67. options.complete = done;
  68. // Set default origin and restore for show/hide
  69. if ( mode !== "effect" ) {
  70. options.origin = origin || ["middle","center"];
  71. options.restore = true;
  72. }
  73. options.from = o.from || ( mode === "show" ? {
  74. height: 0,
  75. width: 0,
  76. outerHeight: 0,
  77. outerWidth: 0
  78. } : original );
  79. options.to = {
  80. height: original.height * factor.y,
  81. width: original.width * factor.x,
  82. outerHeight: original.outerHeight * factor.y,
  83. outerWidth: original.outerWidth * factor.x
  84. };
  85. // Fade option to support puff
  86. if ( options.fade ) {
  87. if ( mode === "show" ) {
  88. options.from.opacity = 0;
  89. options.to.opacity = 1;
  90. }
  91. if ( mode === "hide" ) {
  92. options.from.opacity = 1;
  93. options.to.opacity = 0;
  94. }
  95. }
  96. // Animate
  97. el.effect( options );
  98. };
  99. $.effects.effect.size = function( o, done ) {
  100. // Create element
  101. var original, baseline, factor,
  102. el = $( this ),
  103. props0 = [ "position", "top", "bottom", "left", "right", "width", "height", "overflow", "opacity" ],
  104. // Always restore
  105. props1 = [ "position", "top", "bottom", "left", "right", "overflow", "opacity" ],
  106. // Copy for children
  107. props2 = [ "width", "height", "overflow" ],
  108. cProps = [ "fontSize" ],
  109. vProps = [ "borderTopWidth", "borderBottomWidth", "paddingTop", "paddingBottom" ],
  110. hProps = [ "borderLeftWidth", "borderRightWidth", "paddingLeft", "paddingRight" ],
  111. // Set options
  112. mode = $.effects.setMode( el, o.mode || "effect" ),
  113. restore = o.restore || mode !== "effect",
  114. scale = o.scale || "both",
  115. origin = o.origin || [ "middle", "center" ],
  116. position = el.css( "position" ),
  117. props = restore ? props0 : props1,
  118. zero = {
  119. height: 0,
  120. width: 0,
  121. outerHeight: 0,
  122. outerWidth: 0
  123. };
  124. if ( mode === "show" ) {
  125. el.show();
  126. }
  127. original = {
  128. height: el.height(),
  129. width: el.width(),
  130. outerHeight: el.outerHeight(),
  131. outerWidth: el.outerWidth()
  132. };
  133. if ( o.mode === "toggle" && mode === "show" ) {
  134. el.from = o.to || zero;
  135. el.to = o.from || original;
  136. } else {
  137. el.from = o.from || ( mode === "show" ? zero : original );
  138. el.to = o.to || ( mode === "hide" ? zero : original );
  139. }
  140. // Set scaling factor
  141. factor = {
  142. from: {
  143. y: el.from.height / original.height,
  144. x: el.from.width / original.width
  145. },
  146. to: {
  147. y: el.to.height / original.height,
  148. x: el.to.width / original.width
  149. }
  150. };
  151. // Scale the css box
  152. if ( scale === "box" || scale === "both" ) {
  153. // Vertical props scaling
  154. if ( factor.from.y !== factor.to.y ) {
  155. props = props.concat( vProps );
  156. el.from = $.effects.setTransition( el, vProps, factor.from.y, el.from );
  157. el.to = $.effects.setTransition( el, vProps, factor.to.y, el.to );
  158. }
  159. // Horizontal props scaling
  160. if ( factor.from.x !== factor.to.x ) {
  161. props = props.concat( hProps );
  162. el.from = $.effects.setTransition( el, hProps, factor.from.x, el.from );
  163. el.to = $.effects.setTransition( el, hProps, factor.to.x, el.to );
  164. }
  165. }
  166. // Scale the content
  167. if ( scale === "content" || scale === "both" ) {
  168. // Vertical props scaling
  169. if ( factor.from.y !== factor.to.y ) {
  170. props = props.concat( cProps ).concat( props2 );
  171. el.from = $.effects.setTransition( el, cProps, factor.from.y, el.from );
  172. el.to = $.effects.setTransition( el, cProps, factor.to.y, el.to );
  173. }
  174. }
  175. $.effects.save( el, props );
  176. el.show();
  177. $.effects.createWrapper( el );
  178. el.css( "overflow", "hidden" ).css( el.from );
  179. // Adjust
  180. if (origin) { // Calculate baseline shifts
  181. baseline = $.effects.getBaseline( origin, original );
  182. el.from.top = ( original.outerHeight - el.outerHeight() ) * baseline.y;
  183. el.from.left = ( original.outerWidth - el.outerWidth() ) * baseline.x;
  184. el.to.top = ( original.outerHeight - el.to.outerHeight ) * baseline.y;
  185. el.to.left = ( original.outerWidth - el.to.outerWidth ) * baseline.x;
  186. }
  187. el.css( el.from ); // set top & left
  188. // Animate
  189. if ( scale === "content" || scale === "both" ) { // Scale the children
  190. // Add margins/font-size
  191. vProps = vProps.concat([ "marginTop", "marginBottom" ]).concat(cProps);
  192. hProps = hProps.concat([ "marginLeft", "marginRight" ]);
  193. props2 = props0.concat(vProps).concat(hProps);
  194. el.find( "*[width]" ).each( function(){
  195. var child = $( this ),
  196. c_original = {
  197. height: child.height(),
  198. width: child.width(),
  199. outerHeight: child.outerHeight(),
  200. outerWidth: child.outerWidth()
  201. };
  202. if (restore) {
  203. $.effects.save(child, props2);
  204. }
  205. child.from = {
  206. height: c_original.height * factor.from.y,
  207. width: c_original.width * factor.from.x,
  208. outerHeight: c_original.outerHeight * factor.from.y,
  209. outerWidth: c_original.outerWidth * factor.from.x
  210. };
  211. child.to = {
  212. height: c_original.height * factor.to.y,
  213. width: c_original.width * factor.to.x,
  214. outerHeight: c_original.height * factor.to.y,
  215. outerWidth: c_original.width * factor.to.x
  216. };
  217. // Vertical props scaling
  218. if ( factor.from.y !== factor.to.y ) {
  219. child.from = $.effects.setTransition( child, vProps, factor.from.y, child.from );
  220. child.to = $.effects.setTransition( child, vProps, factor.to.y, child.to );
  221. }
  222. // Horizontal props scaling
  223. if ( factor.from.x !== factor.to.x ) {
  224. child.from = $.effects.setTransition( child, hProps, factor.from.x, child.from );
  225. child.to = $.effects.setTransition( child, hProps, factor.to.x, child.to );
  226. }
  227. // Animate children
  228. child.css( child.from );
  229. child.animate( child.to, o.duration, o.easing, function() {
  230. // Restore children
  231. if ( restore ) {
  232. $.effects.restore( child, props2 );
  233. }
  234. });
  235. });
  236. }
  237. // Animate
  238. el.animate( el.to, {
  239. queue: false,
  240. duration: o.duration,
  241. easing: o.easing,
  242. complete: function() {
  243. if ( el.to.opacity === 0 ) {
  244. el.css( "opacity", el.from.opacity );
  245. }
  246. if( mode === "hide" ) {
  247. el.hide();
  248. }
  249. $.effects.restore( el, props );
  250. if ( !restore ) {
  251. // we need to calculate our new positioning based on the scaling
  252. if ( position === "static" ) {
  253. el.css({
  254. position: "relative",
  255. top: el.to.top,
  256. left: el.to.left
  257. });
  258. } else {
  259. $.each([ "top", "left" ], function( idx, pos ) {
  260. el.css( pos, function( _, str ) {
  261. var val = parseInt( str, 10 ),
  262. toRef = idx ? el.to.left : el.to.top;
  263. // if original was "auto", recalculate the new value from wrapper
  264. if ( str === "auto" ) {
  265. return toRef + "px";
  266. }
  267. return val + toRef + "px";
  268. });
  269. });
  270. }
  271. }
  272. $.effects.removeWrapper( el );
  273. done();
  274. }
  275. });
  276. };
  277. })(jQuery);