PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jquery/src/effects.js

https://gitlab.com/Blueprint-Marketing/rdom
JavaScript | 482 lines | 340 code | 101 blank | 41 comment | 111 complexity | 6df1fe703aaf163c9c6163831e2c43ff MD5 | raw file
  1. var elemdisplay = {},
  2. rfxtypes = /toggle|show|hide/,
  3. rfxnum = /^([+\-]=)?([\d+.\-]+)(.*)$/,
  4. timerId,
  5. fxAttrs = [
  6. // height animations
  7. [ "height", "marginTop", "marginBottom", "paddingTop", "paddingBottom" ],
  8. // width animations
  9. [ "width", "marginLeft", "marginRight", "paddingLeft", "paddingRight" ],
  10. // opacity animations
  11. [ "opacity" ]
  12. ];
  13. jQuery.fn.extend({
  14. show: function( speed, callback ) {
  15. if ( speed || speed === 0) {
  16. return this.animate( genFx("show", 3), speed, callback);
  17. } else {
  18. for ( var i = 0, l = this.length; i < l; i++ ) {
  19. var old = jQuery.data(this[i], "olddisplay");
  20. this[i].style.display = old || "";
  21. if ( jQuery.css(this[i], "display") === "none" ) {
  22. var nodeName = this[i].nodeName, display;
  23. if ( elemdisplay[ nodeName ] ) {
  24. display = elemdisplay[ nodeName ];
  25. } else {
  26. var elem = jQuery("<" + nodeName + " />").appendTo("body");
  27. display = elem.css("display");
  28. if ( display === "none" ) {
  29. display = "block";
  30. }
  31. elem.remove();
  32. elemdisplay[ nodeName ] = display;
  33. }
  34. jQuery.data(this[i], "olddisplay", display);
  35. }
  36. }
  37. // Set the display of the elements in a second loop
  38. // to avoid the constant reflow
  39. for ( var j = 0, k = this.length; j < k; j++ ) {
  40. this[j].style.display = jQuery.data(this[j], "olddisplay") || "";
  41. }
  42. return this;
  43. }
  44. },
  45. hide: function( speed, callback ) {
  46. if ( speed || speed === 0 ) {
  47. return this.animate( genFx("hide", 3), speed, callback);
  48. } else {
  49. for ( var i = 0, l = this.length; i < l; i++ ) {
  50. var old = jQuery.data(this[i], "olddisplay");
  51. if ( !old && old !== "none" ) {
  52. jQuery.data(this[i], "olddisplay", jQuery.css(this[i], "display"));
  53. }
  54. }
  55. // Set the display of the elements in a second loop
  56. // to avoid the constant reflow
  57. for ( var j = 0, k = this.length; j < k; j++ ) {
  58. this[j].style.display = "none";
  59. }
  60. return this;
  61. }
  62. },
  63. // Save the old toggle function
  64. _toggle: jQuery.fn.toggle,
  65. toggle: function( fn, fn2 ) {
  66. var bool = typeof fn === "boolean";
  67. if ( jQuery.isFunction(fn) && jQuery.isFunction(fn2) ) {
  68. this._toggle.apply( this, arguments );
  69. } else if ( fn == null || bool ) {
  70. this.each(function() {
  71. var state = bool ? fn : jQuery(this).is(":hidden");
  72. jQuery(this)[ state ? "show" : "hide" ]();
  73. });
  74. } else {
  75. this.animate(genFx("toggle", 3), fn, fn2);
  76. }
  77. return this;
  78. },
  79. fadeTo: function( speed, to, callback ) {
  80. return this.filter(":hidden").css("opacity", 0).show().end()
  81. .animate({opacity: to}, speed, callback);
  82. },
  83. animate: function( prop, speed, easing, callback ) {
  84. var optall = jQuery.speed(speed, easing, callback);
  85. if ( jQuery.isEmptyObject( prop ) ) {
  86. return this.each( optall.complete );
  87. }
  88. return this[ optall.queue === false ? "each" : "queue" ](function() {
  89. var opt = jQuery.extend({}, optall), p,
  90. hidden = this.nodeType === 1 && jQuery(this).is(":hidden"),
  91. self = this;
  92. for ( p in prop ) {
  93. var name = p.replace(rdashAlpha, fcamelCase);
  94. if ( p !== name ) {
  95. prop[ name ] = prop[ p ];
  96. delete prop[ p ];
  97. p = name;
  98. }
  99. if ( prop[p] === "hide" && hidden || prop[p] === "show" && !hidden ) {
  100. return opt.complete.call(this);
  101. }
  102. if ( ( p === "height" || p === "width" ) && this.style ) {
  103. // Store display property
  104. opt.display = jQuery.css(this, "display");
  105. // Make sure that nothing sneaks out
  106. opt.overflow = this.style.overflow;
  107. }
  108. if ( jQuery.isArray( prop[p] ) ) {
  109. // Create (if needed) and add to specialEasing
  110. (opt.specialEasing = opt.specialEasing || {})[p] = prop[p][1];
  111. prop[p] = prop[p][0];
  112. }
  113. }
  114. if ( opt.overflow != null ) {
  115. this.style.overflow = "hidden";
  116. }
  117. opt.curAnim = jQuery.extend({}, prop);
  118. jQuery.each( prop, function( name, val ) {
  119. var e = new jQuery.fx( self, opt, name );
  120. if ( rfxtypes.test(val) ) {
  121. e[ val === "toggle" ? hidden ? "show" : "hide" : val ]( prop );
  122. } else {
  123. var parts = rfxnum.exec(val),
  124. start = e.cur(true) || 0;
  125. if ( parts ) {
  126. var end = parseFloat( parts[2] ),
  127. unit = parts[3] || "px";
  128. // We need to compute starting value
  129. if ( unit !== "px" ) {
  130. self.style[ name ] = (end || 1) + unit;
  131. start = ((end || 1) / e.cur(true)) * start;
  132. self.style[ name ] = start + unit;
  133. }
  134. // If a +=/-= token was provided, we're doing a relative animation
  135. if ( parts[1] ) {
  136. end = ((parts[1] === "-=" ? -1 : 1) * end) + start;
  137. }
  138. e.custom( start, end, unit );
  139. } else {
  140. e.custom( start, val, "" );
  141. }
  142. }
  143. });
  144. // For JS strict compliance
  145. return true;
  146. });
  147. },
  148. stop: function( clearQueue, gotoEnd ) {
  149. var timers = jQuery.timers;
  150. if ( clearQueue ) {
  151. this.queue([]);
  152. }
  153. this.each(function() {
  154. // go in reverse order so anything added to the queue during the loop is ignored
  155. for ( var i = timers.length - 1; i >= 0; i-- ) {
  156. if ( timers[i].elem === this ) {
  157. if (gotoEnd) {
  158. // force the next step to be the last
  159. timers[i](true);
  160. }
  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. function genFx( type, num ) {
  173. var obj = {};
  174. jQuery.each( fxAttrs.concat.apply([], fxAttrs.slice(0,num)), function() {
  175. obj[ this ] = type;
  176. });
  177. return obj;
  178. }
  179. // Generate shortcuts for custom animations
  180. jQuery.each({
  181. slideDown: genFx("show", 1),
  182. slideUp: genFx("hide", 1),
  183. slideToggle: genFx("toggle", 1),
  184. fadeIn: { opacity: "show" },
  185. fadeOut: { opacity: "hide" }
  186. }, function( name, props ) {
  187. jQuery.fn[ name ] = function( speed, callback ) {
  188. return this.animate( props, speed, callback );
  189. };
  190. });
  191. jQuery.extend({
  192. speed: function( speed, easing, fn ) {
  193. var opt = speed && typeof speed === "object" ? speed : {
  194. complete: fn || !fn && easing ||
  195. jQuery.isFunction( speed ) && speed,
  196. duration: speed,
  197. easing: fn && easing || easing && !jQuery.isFunction(easing) && easing
  198. };
  199. opt.duration = jQuery.fx.off ? 0 : typeof opt.duration === "number" ? opt.duration :
  200. jQuery.fx.speeds[opt.duration] || jQuery.fx.speeds._default;
  201. // Queueing
  202. opt.old = opt.complete;
  203. opt.complete = function() {
  204. if ( opt.queue !== false ) {
  205. jQuery(this).dequeue();
  206. }
  207. if ( jQuery.isFunction( opt.old ) ) {
  208. opt.old.call( this );
  209. }
  210. };
  211. return opt;
  212. },
  213. easing: {
  214. linear: function( p, n, firstNum, diff ) {
  215. return firstNum + diff * p;
  216. },
  217. swing: function( p, n, firstNum, diff ) {
  218. return ((-Math.cos(p*Math.PI)/2) + 0.5) * diff + firstNum;
  219. }
  220. },
  221. timers: [],
  222. fx: function( elem, options, prop ) {
  223. this.options = options;
  224. this.elem = elem;
  225. this.prop = prop;
  226. if ( !options.orig ) {
  227. options.orig = {};
  228. }
  229. }
  230. });
  231. jQuery.fx.prototype = {
  232. // Simple function for setting a style value
  233. update: function() {
  234. if ( this.options.step ) {
  235. this.options.step.call( this.elem, this.now, this );
  236. }
  237. (jQuery.fx.step[this.prop] || jQuery.fx.step._default)( this );
  238. // Set display property to block for height/width animations
  239. if ( ( this.prop === "height" || this.prop === "width" ) && this.elem.style ) {
  240. this.elem.style.display = "block";
  241. }
  242. },
  243. // Get the current size
  244. cur: function( force ) {
  245. if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
  246. return this.elem[ this.prop ];
  247. }
  248. var r = parseFloat(jQuery.css(this.elem, this.prop, force));
  249. return r && r > -10000 ? r : parseFloat(jQuery.curCSS(this.elem, this.prop)) || 0;
  250. },
  251. // Start an animation from one number to another
  252. custom: function( from, to, unit ) {
  253. this.startTime = jQuery.now();
  254. this.start = from;
  255. this.end = to;
  256. this.unit = unit || this.unit || "px";
  257. this.now = this.start;
  258. this.pos = this.state = 0;
  259. var self = this;
  260. function t( gotoEnd ) {
  261. return self.step(gotoEnd);
  262. }
  263. t.elem = this.elem;
  264. if ( t() && jQuery.timers.push(t) && !timerId ) {
  265. timerId = setInterval(jQuery.fx.tick, 13);
  266. }
  267. },
  268. // Simple 'show' function
  269. show: function() {
  270. // Remember where we started, so that we can go back to it later
  271. this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
  272. this.options.show = true;
  273. // Begin the animation
  274. // Make sure that we start at a small width/height to avoid any
  275. // flash of content
  276. this.custom(this.prop === "width" || this.prop === "height" ? 1 : 0, this.cur());
  277. // Start by showing the element
  278. jQuery( this.elem ).show();
  279. },
  280. // Simple 'hide' function
  281. hide: function() {
  282. // Remember where we started, so that we can go back to it later
  283. this.options.orig[this.prop] = jQuery.style( this.elem, this.prop );
  284. this.options.hide = true;
  285. // Begin the animation
  286. this.custom(this.cur(), 0);
  287. },
  288. // Each step of an animation
  289. step: function( gotoEnd ) {
  290. var t = jQuery.now(), done = true;
  291. if ( gotoEnd || t >= this.options.duration + this.startTime ) {
  292. this.now = this.end;
  293. this.pos = this.state = 1;
  294. this.update();
  295. this.options.curAnim[ this.prop ] = true;
  296. for ( var i in this.options.curAnim ) {
  297. if ( this.options.curAnim[i] !== true ) {
  298. done = false;
  299. }
  300. }
  301. if ( done ) {
  302. if ( this.options.display != null ) {
  303. // Reset the overflow
  304. this.elem.style.overflow = this.options.overflow;
  305. // Reset the display
  306. var old = jQuery.data(this.elem, "olddisplay");
  307. this.elem.style.display = old ? old : this.options.display;
  308. if ( jQuery.css(this.elem, "display") === "none" ) {
  309. this.elem.style.display = "block";
  310. }
  311. }
  312. // Hide the element if the "hide" operation was done
  313. if ( this.options.hide ) {
  314. jQuery(this.elem).hide();
  315. }
  316. // Reset the properties, if the item has been hidden or shown
  317. if ( this.options.hide || this.options.show ) {
  318. for ( var p in this.options.curAnim ) {
  319. jQuery.style(this.elem, p, this.options.orig[p]);
  320. }
  321. }
  322. // Execute the complete function
  323. this.options.complete.call( this.elem );
  324. }
  325. return false;
  326. } else {
  327. var n = t - this.startTime;
  328. this.state = n / this.options.duration;
  329. // Perform the easing function, defaults to swing
  330. var specialEasing = this.options.specialEasing && this.options.specialEasing[this.prop];
  331. var defaultEasing = this.options.easing || (jQuery.easing.swing ? "swing" : "linear");
  332. this.pos = jQuery.easing[specialEasing || defaultEasing](this.state, n, 0, 1, this.options.duration);
  333. this.now = this.start + ((this.end - this.start) * this.pos);
  334. // Perform the next step of the animation
  335. this.update();
  336. }
  337. return true;
  338. }
  339. };
  340. jQuery.extend( jQuery.fx, {
  341. tick: function() {
  342. var timers = jQuery.timers;
  343. for ( var i = 0; i < timers.length; i++ ) {
  344. if ( !timers[i]() ) {
  345. timers.splice(i--, 1);
  346. }
  347. }
  348. if ( !timers.length ) {
  349. jQuery.fx.stop();
  350. }
  351. },
  352. stop: function() {
  353. clearInterval( timerId );
  354. timerId = null;
  355. },
  356. speeds: {
  357. slow: 600,
  358. fast: 200,
  359. // Default speed
  360. _default: 400
  361. },
  362. step: {
  363. opacity: function( fx ) {
  364. jQuery.style(fx.elem, "opacity", fx.now);
  365. },
  366. _default: function( fx ) {
  367. if ( fx.elem.style && fx.elem.style[ fx.prop ] != null ) {
  368. fx.elem.style[ fx.prop ] = (fx.prop === "width" || fx.prop === "height" ? Math.max(0, fx.now) : fx.now) + fx.unit;
  369. } else {
  370. fx.elem[ fx.prop ] = fx.now;
  371. }
  372. }
  373. }
  374. });
  375. if ( jQuery.expr && jQuery.expr.filters ) {
  376. jQuery.expr.filters.animated = function( elem ) {
  377. return jQuery.grep(jQuery.timers, function( fn ) {
  378. return elem === fn.elem;
  379. }).length;
  380. };
  381. }