PageRenderTime 31ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/jquery/src/fx.js

https://github.com/freels/fakequery
JavaScript | 283 lines | 142 code | 49 blank | 92 comment | 49 complexity | 223992f70048841f9f92c525ad5de559 MD5 | raw file
  1. jQuery.fn.extend({
  2. show: function(speed,callback){
  3. return speed ?
  4. this.animate({
  5. height: "show", width: "show", opacity: "show"
  6. }, speed, callback) :
  7. this.filter(":hidden").each(function(){
  8. this.setStyle('display', this.oldblock || "");
  9. if ( jQuery.css(this,"display") == "none" ) {
  10. var elem = jQuery("<" + this.getTagName() + " />").appendTo("body");
  11. this.setStyle('display', elem.css("display"));
  12. // handle an edge condition where css is - div { display:none; } or similar
  13. if (this.getStyle('display') == "none")
  14. this.setStyle('display', "block");
  15. elem.remove();
  16. }
  17. }).end();
  18. },
  19. hide: function(speed,callback){
  20. return speed ?
  21. this.animate({
  22. height: "hide", width: "hide", opacity: "hide"
  23. }, speed, callback) :
  24. this.filter(":visible").each(function(){
  25. this.oldblock = this.oldblock || jQuery.css(this,"display");
  26. this.setStyle('display', "none");
  27. }).end();
  28. },
  29. // Save the old toggle function
  30. _toggle: jQuery.fn.toggle,
  31. toggle: function( fn, fn2 ){
  32. return jQuery.isFunction(fn) && jQuery.isFunction(fn2) ?
  33. this._toggle.apply( this, arguments ) :
  34. fn ?
  35. this.animate({
  36. height: "toggle", width: "toggle", opacity: "toggle"
  37. }, fn, fn2) :
  38. this.each(function(){
  39. jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ]();
  40. });
  41. },
  42. slideDown: function(speed,callback){
  43. return this.animate({height: "show"}, speed, callback);
  44. },
  45. slideUp: function(speed,callback){
  46. return this.animate({height: "hide"}, speed, callback);
  47. },
  48. slideToggle: function(speed, callback){
  49. return this.animate({height: "toggle"}, speed, callback);
  50. },
  51. fadeIn: function(speed, callback){
  52. return this.animate({opacity: "show"}, speed, callback);
  53. },
  54. fadeOut: function(speed, callback){
  55. return this.animate({opacity: "hide"}, speed, callback);
  56. },
  57. fadeTo: function(speed,to,callback){
  58. return this.animate({opacity: to}, speed, callback);
  59. },
  60. animate: function( prop, speed, easing, callback ) {
  61. var optall = jQuery.speed(speed, easing, callback);
  62. // return this[ optall.queue === false ? "each" : "queue" ](function(){
  63. return this[ "each" ](function(){
  64. var opt = jQuery.extend({}, optall), p,
  65. hidden = jQuery(this).is(":hidden"), self = this;
  66. opt.prev = {};
  67. // setup properties
  68. for ( p in prop ) {
  69. if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
  70. return opt.complete.call(this);
  71. if ( p == "height" || p == "width" ) {
  72. // Store display property
  73. opt.display = jQuery.css(this, "display");
  74. // Make sure that nothing sneaks out
  75. opt.overflow = this.getStyle('overflow');
  76. }
  77. }
  78. // if ( opt.overflow != null )
  79. // this.setStyle('overflow', "hidden");
  80. // create property animations
  81. // use facebook's Animation object instead of jQuery.fx
  82. var a = Animation(this);
  83. jQuery.each( prop, function(key, val){
  84. var from = undefined;
  85. if (val == "toggle")
  86. val = hidden ? "show" : "hide";
  87. // translate show and hide values
  88. if (val == 'show') {
  89. val = (key == 'opacity' ? 1 : 'auto');
  90. from = 0;
  91. a.show();
  92. } else if (val == 'hide') {
  93. val = 0;
  94. opt.prev[key] = /height|width/.test(key) ?
  95. jQuery(self)[key]() :
  96. self.getStyle(key) || (key == 'opacity' ? 1 : 'auto');
  97. a.hide();
  98. }
  99. // relative animation
  100. if (/\+?=/.test(val)) {
  101. a.by(key, val.replace(/\+?=/, ''));
  102. // absolute animation
  103. } else {
  104. a.to(key, val);
  105. if (from !== undefined)
  106. a.from(from);
  107. }
  108. });
  109. // set the duration
  110. if ( opt.duration )
  111. a.duration( typeof opt.duration == 'string' ? jQuery.speeds[opt.duration] : opt.duration );
  112. else
  113. a.duration( jQuery.speeds['def'] );
  114. // set easing
  115. if ( opt.easing != 'linear' )
  116. a.ease( jQuery.easing[opt.easing] );
  117. // TODO: we should fire off the next function in the queue if it exists here...
  118. // if (opt.complete)
  119. a.checkpoint(1, function() {
  120. jQuery(self).css( opt.prev );
  121. return opt.complete.call( self );
  122. });
  123. a.go();
  124. // For JS strict compliance
  125. return true;
  126. });
  127. }
  128. // queue: function(type, fn){
  129. // if ( jQuery.isFunction(type) || ( type && typeof type != 'string' && type.slice )) {
  130. // fn = type;
  131. // type = "fx";
  132. // }
  133. //
  134. // if ( !type || (typeof type == "string" && !fn) )
  135. // return queue( this[0], type );
  136. //
  137. // return this.each(function(){
  138. // if ( fn.slice )
  139. // queue(this, type, fn);
  140. // else {
  141. // queue(this, type).push( fn );
  142. //
  143. // if ( queue(this, type).length == 1 )
  144. // fn.call(this);
  145. // }
  146. // });
  147. // },
  148. // stop: function(clearQueue, gotoEnd){
  149. // var timers = jQuery.timers;
  150. //
  151. // if (clearQueue)
  152. // this.queue([]);
  153. //
  154. // this.each(function(){
  155. // // go in reverse order so anything added to the queue during the loop is ignored
  156. // for ( var i = timers.length - 1; i >= 0; i-- )
  157. // if ( timers[i].elem == this ) {
  158. // if (gotoEnd)
  159. // // force the next step to be the last
  160. // timers[i](true);
  161. // timers.splice(i, 1);
  162. // }
  163. // });
  164. //
  165. // // start the next in the queue if the last step wasn't forced
  166. // if (!gotoEnd)
  167. // this.dequeue();
  168. //
  169. // return this;
  170. // }
  171. });
  172. // var queue = function( elem, type, array ) {
  173. // if ( elem ){
  174. //
  175. // type = type || "fx";
  176. //
  177. // var q = jQuery.data( elem, type + "queue" );
  178. //
  179. // if ( !q || array )
  180. // q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) );
  181. //
  182. // }
  183. // return q;
  184. // };
  185. //
  186. // jQuery.fn.dequeue = function(type){
  187. // type = type || "fx";
  188. //
  189. // return this.each(function(){
  190. // var q = queue(this, type);
  191. //
  192. // q.shift();
  193. //
  194. // if ( q.length )
  195. // q[0].call( this );
  196. // });
  197. // };
  198. jQuery.extend({
  199. speed: function(speed, easing, fn) {
  200. var opt = speed && typeof speed == 'object' ? speed : {
  201. complete: fn || !fn && easing ||
  202. jQuery.isFunction( speed ) && speed,
  203. duration: speed,
  204. easing: fn && easing || easing && typeof easing != 'function' && easing
  205. };
  206. opt.duration = (opt.duration && typeof opt.duration == 'number' ?
  207. opt.duration :
  208. jQuery.speeds[opt.duration]) || jQuery.speeds.def;
  209. // Queueing
  210. opt.old = opt.complete;
  211. opt.complete = function(){
  212. // TODO: reenable this once queueing works again.
  213. // if ( opt.queue !== false )
  214. // jQuery(this).dequeue();
  215. if ( jQuery.isFunction( opt.old ) )
  216. opt.old.call( this );
  217. };
  218. return opt;
  219. },
  220. speeds:{
  221. slow: 600,
  222. fast: 200,
  223. // Default speed
  224. def: 400
  225. },
  226. easing: {
  227. linear: null,
  228. swing: Animation.ease.both,
  229. begin: Animation.ease.begin,
  230. end: Animation.ease.end
  231. },
  232. timers: [],
  233. timerId: null
  234. });