0c1c7f3d798e339bbf072579353ce4db2a9d7766.svn-base 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /*
  7. * Copyright (c) 2007, Yahoo! Inc. All rights reserved. Code licensed under the
  8. * BSD License: http://developer.yahoo.net/yui/license.txt version: 2.2.0
  9. */
  10. /*
  11. * Copyright (c) 2006, Yahoo! Inc. All rights reserved. Code licensed under the
  12. * BSD License: http://developer.yahoo.net/yui/license.txt
  13. */
  14. /**
  15. * The animation module provides allows effects to be added to HTMLElements.
  16. *
  17. * @module animation
  18. * @requires yahoo, event, dom
  19. */
  20. /**
  21. *
  22. * Base animation class that provides the interface for building animated
  23. * effects.
  24. * <p>
  25. * Usage: var myAnim = new YAHOO.util.Anim(el, { width: { from: 10, to: 100 } },
  26. * 1, YAHOO.util.Easing.easeOut);
  27. * </p>
  28. *
  29. * @class Anim
  30. * @namespace YAHOO.util
  31. * @requires YAHOO.util.AnimMgr
  32. * @requires YAHOO.util.Easing
  33. * @requires YAHOO.util.Dom
  34. * @requires YAHOO.util.Event
  35. * @requires YAHOO.util.CustomEvent
  36. * @constructor
  37. * @param {String |
  38. * HTMLElement} el Reference to the element that will be animated
  39. * @param {Object}
  40. * attributes The attribute(s) to be animated. Each attribute is an
  41. * object with at minimum a "to" or "by" member defined. Additional
  42. * optional members are "from" (defaults to current value), "units"
  43. * (defaults to "px"). All attribute names use camelCase.
  44. * @param {Number}
  45. * duration (optional, defaults to 1 second) Length of animation
  46. * (frames or seconds), defaults to time-based
  47. * @param {Function}
  48. * method (optional, defaults to YAHOO.util.Easing.easeNone) Computes
  49. * the values that are applied to the attributes per frame (generally
  50. * a YAHOO.util.Easing method)
  51. */
  52. YAHOO.util.Anim = function(el, attributes, duration, method) {
  53. if (el) {
  54. this.init(el, attributes, duration, method);
  55. }
  56. };
  57. YAHOO.util.Anim.prototype = {
  58. /**
  59. * Provides a readable name for the Anim instance.
  60. *
  61. * @method toString
  62. * @return {String}
  63. */
  64. toString : function() {
  65. var el = this.getEl();
  66. var id = el.id || el.tagName;
  67. return ("Anim " + id);
  68. },
  69. patterns : { // cached for performance
  70. noNegatives : /width|height|opacity|padding/i, // keep at zero or above
  71. offsetAttribute : /^((width|height)|(top|left))$/, // use offsetValue
  72. // as default
  73. defaultUnit : /width|height|top$|bottom$|left$|right$/i, // use 'px'
  74. // by
  75. // default
  76. offsetUnit : /\d+(em|%|en|ex|pt|in|cm|mm|pc)$/i
  77. // IE may return these, so convert these to offset
  78. },
  79. /**
  80. * Returns the value computed by the animation's "method".
  81. *
  82. * @method doMethod
  83. * @param {String}
  84. * attr The name of the attribute.
  85. * @param {Number}
  86. * start The value this attribute should start from for this
  87. * animation.
  88. * @param {Number}
  89. * end The value this attribute should end at for this animation.
  90. * @return {Number} The Value to be applied to the attribute.
  91. */
  92. doMethod : function(attr, start, end) {
  93. return this.method(this.currentFrame, start, end - start,
  94. this.totalFrames);
  95. },
  96. /**
  97. * Applies a value to an attribute.
  98. *
  99. * @method setAttribute
  100. * @param {String}
  101. * attr The name of the attribute.
  102. * @param {Number}
  103. * val The value to be applied to the attribute.
  104. * @param {String}
  105. * unit The unit ('px', '%', etc.) of the value.
  106. */
  107. setAttribute : function(attr, val, unit) {
  108. if (this.patterns.noNegatives.test(attr)) {
  109. val = (val > 0) ? val : 0;
  110. }
  111. YAHOO.util.Dom.setStyle(this.getEl(), attr, val + unit);
  112. },
  113. /**
  114. * Returns current value of the attribute.
  115. *
  116. * @method getAttribute
  117. * @param {String}
  118. * attr The name of the attribute.
  119. * @return {Number} val The current value of the attribute.
  120. */
  121. getAttribute : function(attr) {
  122. var el = this.getEl();
  123. var val = YAHOO.util.Dom.getStyle(el, attr);
  124. if (val !== 'auto' && !this.patterns.offsetUnit.test(val)) {
  125. return parseFloat(val);
  126. }
  127. var a = this.patterns.offsetAttribute.exec(attr) || [];
  128. var pos = !!(a[3]); // top or left
  129. var box = !!(a[2]); // width or height
  130. // use offsets for width/height and abs pos top/left
  131. if (box
  132. || (YAHOO.util.Dom.getStyle(el, 'position') == 'absolute' && pos)) {
  133. val = el['offset' + a[0].charAt(0).toUpperCase() + a[0].substr(1)];
  134. } else { // default to zero for other 'auto'
  135. val = 0;
  136. }
  137. return val;
  138. },
  139. /**
  140. * Returns the unit to use when none is supplied.
  141. *
  142. * @method getDefaultUnit
  143. * @param {attr}
  144. * attr The name of the attribute.
  145. * @return {String} The default unit to be used.
  146. */
  147. getDefaultUnit : function(attr) {
  148. if (this.patterns.defaultUnit.test(attr)) {
  149. return 'px';
  150. }
  151. return '';
  152. },
  153. /**
  154. * Sets the actual values to be used during the animation. Should only be
  155. * needed for subclass use.
  156. *
  157. * @method setRuntimeAttribute
  158. * @param {Object}
  159. * attr The attribute object
  160. * @private
  161. */
  162. setRuntimeAttribute : function(attr) {
  163. var start;
  164. var end;
  165. var attributes = this.attributes;
  166. this.runtimeAttributes[attr] = {};
  167. var isset = function(prop) {
  168. return (typeof prop !== 'undefined');
  169. };
  170. if (!isset(attributes[attr]['to']) && !isset(attributes[attr]['by'])) {
  171. return false; // note return; nothing to animate to
  172. }
  173. start = (isset(attributes[attr]['from']))
  174. ? attributes[attr]['from']
  175. : this.getAttribute(attr);
  176. // To beats by, per SMIL 2.1 spec
  177. if (isset(attributes[attr]['to'])) {
  178. end = attributes[attr]['to'];
  179. } else if (isset(attributes[attr]['by'])) {
  180. if (start.constructor == Array) {
  181. end = [];
  182. for (var i = 0, len = start.length; i < len; ++i) {
  183. end[i] = start[i] + attributes[attr]['by'][i];
  184. }
  185. } else {
  186. end = start + attributes[attr]['by'];
  187. }
  188. }
  189. this.runtimeAttributes[attr].start = start;
  190. this.runtimeAttributes[attr].end = end;
  191. // set units if needed
  192. this.runtimeAttributes[attr].unit = (isset(attributes[attr].unit))
  193. ? attributes[attr]['unit']
  194. : this.getDefaultUnit(attr);
  195. },
  196. /**
  197. * Constructor for Anim instance.
  198. *
  199. * @method init
  200. * @param {String |
  201. * HTMLElement} el Reference to the element that will be animated
  202. * @param {Object}
  203. * attributes The attribute(s) to be animated. Each attribute is
  204. * an object with at minimum a "to" or "by" member defined.
  205. * Additional optional members are "from" (defaults to current
  206. * value), "units" (defaults to "px"). All attribute names use
  207. * camelCase.
  208. * @param {Number}
  209. * duration (optional, defaults to 1 second) Length of animation
  210. * (frames or seconds), defaults to time-based
  211. * @param {Function}
  212. * method (optional, defaults to YAHOO.util.Easing.easeNone)
  213. * Computes the values that are applied to the attributes per
  214. * frame (generally a YAHOO.util.Easing method)
  215. */
  216. init : function(el, attributes, duration, method) {
  217. /**
  218. * Whether or not the animation is running.
  219. *
  220. * @property isAnimated
  221. * @private
  222. * @type Boolean
  223. */
  224. var isAnimated = false;
  225. /**
  226. * A Date object that is created when the animation begins.
  227. *
  228. * @property startTime
  229. * @private
  230. * @type Date
  231. */
  232. var startTime = null;
  233. /**
  234. * The number of frames this animation was able to execute.
  235. *
  236. * @property actualFrames
  237. * @private
  238. * @type Int
  239. */
  240. var actualFrames = 0;
  241. /**
  242. * The element to be animated.
  243. *
  244. * @property el
  245. * @private
  246. * @type HTMLElement
  247. */
  248. el = YAHOO.util.Dom.get(el);
  249. /**
  250. * The collection of attributes to be animated. Each attribute must have
  251. * at least a "to" or "by" defined in order to animate. If "to" is
  252. * supplied, the animation will end with the attribute at that value. If
  253. * "by" is supplied, the animation will end at that value plus its
  254. * starting value. If both are supplied, "to" is used, and "by" is
  255. * ignored. Optional additional member include "from" (the value the
  256. * attribute should start animating from, defaults to current value),
  257. * and "unit" (the units to apply to the values).
  258. *
  259. * @property attributes
  260. * @type Object
  261. */
  262. this.attributes = attributes || {};
  263. /**
  264. * The length of the animation. Defaults to "1" (second).
  265. *
  266. * @property duration
  267. * @type Number
  268. */
  269. this.duration = duration || 1;
  270. /**
  271. * The method that will provide values to the attribute(s) during the
  272. * animation. Defaults to "YAHOO.util.Easing.easeNone".
  273. *
  274. * @property method
  275. * @type Function
  276. */
  277. this.method = method || YAHOO.util.Easing.easeNone;
  278. /**
  279. * Whether or not the duration should be treated as seconds. Defaults to
  280. * true.
  281. *
  282. * @property useSeconds
  283. * @type Boolean
  284. */
  285. this.useSeconds = true; // default to seconds
  286. /**
  287. * The location of the current animation on the timeline. In time-based
  288. * animations, this is used by AnimMgr to ensure the animation finishes
  289. * on time.
  290. *
  291. * @property currentFrame
  292. * @type Int
  293. */
  294. this.currentFrame = 0;
  295. /**
  296. * The total number of frames to be executed. In time-based animations,
  297. * this is used by AnimMgr to ensure the animation finishes on time.
  298. *
  299. * @property totalFrames
  300. * @type Int
  301. */
  302. this.totalFrames = YAHOO.util.AnimMgr.fps;
  303. /**
  304. * Returns a reference to the animated element.
  305. *
  306. * @method getEl
  307. * @return {HTMLElement}
  308. */
  309. this.getEl = function() {
  310. return el;
  311. };
  312. /**
  313. * Checks whether the element is currently animated.
  314. *
  315. * @method isAnimated
  316. * @return {Boolean} current value of isAnimated.
  317. */
  318. this.isAnimated = function() {
  319. return isAnimated;
  320. };
  321. /**
  322. * Returns the animation start time.
  323. *
  324. * @method getStartTime
  325. * @return {Date} current value of startTime.
  326. */
  327. this.getStartTime = function() {
  328. return startTime;
  329. };
  330. this.runtimeAttributes = {};
  331. /**
  332. * Starts the animation by registering it with the animation manager.
  333. *
  334. * @method animate
  335. */
  336. this.animate = function() {
  337. if (this.isAnimated()) {
  338. return false;
  339. }
  340. this.currentFrame = 0;
  341. this.totalFrames = (this.useSeconds)
  342. ? Math.ceil(YAHOO.util.AnimMgr.fps * this.duration)
  343. : this.duration;
  344. YAHOO.util.AnimMgr.registerElement(this);
  345. };
  346. /**
  347. * Stops the animation. Normally called by AnimMgr when animation
  348. * completes.
  349. *
  350. * @method stop
  351. * @param {Boolean}
  352. * finish (optional) If true, animation will jump to final
  353. * frame.
  354. */
  355. this.stop = function(finish) {
  356. if (finish) {
  357. this.currentFrame = this.totalFrames;
  358. this._onTween.fire();
  359. }
  360. YAHOO.util.AnimMgr.stop(this);
  361. };
  362. var onStart = function() {
  363. this.onStart.fire();
  364. this.runtimeAttributes = {};
  365. for (var attr in this.attributes) {
  366. this.setRuntimeAttribute(attr);
  367. }
  368. isAnimated = true;
  369. actualFrames = 0;
  370. startTime = allGetServerTime();
  371. };
  372. /**
  373. * Feeds the starting and ending values for each animated attribute to
  374. * doMethod once per frame, then applies the resulting value to the
  375. * attribute(s).
  376. *
  377. * @private
  378. */
  379. var onTween = function() {
  380. var data = {
  381. duration : allGetServerTime() - this.getStartTime(),
  382. currentFrame : this.currentFrame
  383. };
  384. data.toString = function() {
  385. return ('duration: ' + data.duration + ', currentFrame: ' + data.currentFrame);
  386. };
  387. this.onTween.fire(data);
  388. var runtimeAttributes = this.runtimeAttributes;
  389. for (var attr in runtimeAttributes) {
  390. this.setAttribute(attr, this.doMethod(attr,
  391. runtimeAttributes[attr].start,
  392. runtimeAttributes[attr].end),
  393. runtimeAttributes[attr].unit);
  394. }
  395. actualFrames += 1;
  396. };
  397. var onComplete = function() {
  398. var actual_duration = (allGetServerTime() - startTime) / 1000;
  399. var data = {
  400. duration : actual_duration,
  401. frames : actualFrames,
  402. fps : actualFrames / actual_duration
  403. };
  404. data.toString = function() {
  405. return ('duration: ' + data.duration + ', frames: '
  406. + data.frames + ', fps: ' + data.fps);
  407. };
  408. isAnimated = false;
  409. actualFrames = 0;
  410. this.onComplete.fire(data);
  411. };
  412. /**
  413. * Custom event that fires after onStart, useful in subclassing
  414. *
  415. * @private
  416. */
  417. this._onStart = new YAHOO.util.CustomEvent('_start', this, true);
  418. /**
  419. * Custom event that fires when animation begins Listen via subscribe
  420. * method (e.g. myAnim.onStart.subscribe(someFunction)
  421. *
  422. * @event onStart
  423. */
  424. this.onStart = new YAHOO.util.CustomEvent('start', this);
  425. /**
  426. * Custom event that fires between each frame Listen via subscribe
  427. * method (e.g. myAnim.onTween.subscribe(someFunction)
  428. *
  429. * @event onTween
  430. */
  431. this.onTween = new YAHOO.util.CustomEvent('tween', this);
  432. /**
  433. * Custom event that fires after onTween
  434. *
  435. * @private
  436. */
  437. this._onTween = new YAHOO.util.CustomEvent('_tween', this, true);
  438. /**
  439. * Custom event that fires when animation ends Listen via subscribe
  440. * method (e.g. myAnim.onComplete.subscribe(someFunction)
  441. *
  442. * @event onComplete
  443. */
  444. this.onComplete = new YAHOO.util.CustomEvent('complete', this);
  445. /**
  446. * Custom event that fires after onComplete
  447. *
  448. * @private
  449. */
  450. this._onComplete = new YAHOO.util.CustomEvent('_complete', this, true);
  451. this._onStart.subscribe(onStart);
  452. this._onTween.subscribe(onTween);
  453. this._onComplete.subscribe(onComplete);
  454. }
  455. };
  456. /**
  457. * Handles animation queueing and threading. Used by Anim and subclasses.
  458. *
  459. * @class AnimMgr
  460. * @namespace YAHOO.util
  461. */
  462. YAHOO.util.AnimMgr = new function() {
  463. /**
  464. * Reference to the animation Interval.
  465. *
  466. * @property thread
  467. * @private
  468. * @type Int
  469. */
  470. var thread = null;
  471. /**
  472. * The current queue of registered animation objects.
  473. *
  474. * @property queue
  475. * @private
  476. * @type Array
  477. */
  478. var queue = [];
  479. /**
  480. * The number of active animations.
  481. *
  482. * @property tweenCount
  483. * @private
  484. * @type Int
  485. */
  486. var tweenCount = 0;
  487. /**
  488. * Base frame rate (frames per second). Arbitrarily high for better
  489. * x-browser calibration (slower browsers drop more frames).
  490. *
  491. * @property fps
  492. * @type Int
  493. *
  494. */
  495. this.fps = 1000;
  496. /**
  497. * Interval delay in milliseconds, defaults to fastest possible.
  498. *
  499. * @property delay
  500. * @type Int
  501. *
  502. */
  503. this.delay = 1;
  504. /**
  505. * Adds an animation instance to the animation queue. All animation
  506. * instances must be registered in order to animate.
  507. *
  508. * @method registerElement
  509. * @param {object}
  510. * tween The Anim instance to be be registered
  511. */
  512. this.registerElement = function(tween) {
  513. queue[queue.length] = tween;
  514. tweenCount += 1;
  515. tween._onStart.fire();
  516. this.start();
  517. };
  518. /**
  519. * removes an animation instance from the animation queue. All animation
  520. * instances must be registered in order to animate.
  521. *
  522. * @method unRegister
  523. * @param {object}
  524. * tween The Anim instance to be be registered
  525. * @param {Int}
  526. * index The index of the Anim instance
  527. * @private
  528. */
  529. this.unRegister = function(tween, index) {
  530. tween._onComplete.fire();
  531. index = index || getIndex(tween);
  532. if (index != -1) {
  533. queue.splice(index, 1);
  534. }
  535. tweenCount -= 1;
  536. if (tweenCount <= 0) {
  537. this.stop();
  538. }
  539. };
  540. /**
  541. * Starts the animation thread. Only one thread can run at a time.
  542. *
  543. * @method start
  544. */
  545. this.start = function() {
  546. if (thread === null) {
  547. thread = setInterval(this.run, this.delay);
  548. }
  549. };
  550. /**
  551. * Stops the animation thread or a specific animation instance.
  552. *
  553. * @method stop
  554. * @param {object}
  555. * tween A specific Anim instance to stop (optional) If no
  556. * instance given, Manager stops thread and all animations.
  557. */
  558. this.stop = function(tween) {
  559. if (!tween) {
  560. clearInterval(thread);
  561. for (var i = 0, len = queue.length; i < len; ++i) {
  562. if (queue[0].isAnimated()) {
  563. this.unRegister(queue[0], 0);
  564. }
  565. }
  566. queue = [];
  567. thread = null;
  568. tweenCount = 0;
  569. } else {
  570. this.unRegister(tween);
  571. }
  572. };
  573. /**
  574. * Called per Interval to handle each animation frame.
  575. *
  576. * @method run
  577. */
  578. this.run = function() {
  579. for (var i = 0, len = queue.length; i < len; ++i) {
  580. var tween = queue[i];
  581. if (!tween || !tween.isAnimated()) {
  582. continue;
  583. }
  584. if (tween.currentFrame < tween.totalFrames
  585. || tween.totalFrames === null) {
  586. tween.currentFrame += 1;
  587. if (tween.useSeconds) {
  588. correctFrame(tween);
  589. }
  590. tween._onTween.fire();
  591. } else {
  592. YAHOO.util.AnimMgr.stop(tween, i);
  593. }
  594. }
  595. };
  596. var getIndex = function(anim) {
  597. for (var i = 0, len = queue.length; i < len; ++i) {
  598. if (queue[i] == anim) {
  599. return i; // note return;
  600. }
  601. }
  602. return -1;
  603. };
  604. /**
  605. * On the fly frame correction to keep animation on time.
  606. *
  607. * @method correctFrame
  608. * @private
  609. * @param {Object}
  610. * tween The Anim instance being corrected.
  611. */
  612. var correctFrame = function(tween) {
  613. var frames = tween.totalFrames;
  614. var frame = tween.currentFrame;
  615. var expected = (tween.currentFrame * tween.duration * 1000 / tween.totalFrames);
  616. var elapsed = (allGetServerTime() - tween.getStartTime());
  617. var tweak = 0;
  618. if (elapsed < tween.duration * 1000) { // check if falling behind
  619. tweak = Math.round((elapsed / expected - 1) * tween.currentFrame);
  620. } else { // went over duration, so jump to end
  621. tweak = frames - (frame + 1);
  622. }
  623. if (tweak > 0 && isFinite(tweak)) { // adjust if needed
  624. if (tween.currentFrame + tweak >= frames) {// dont go past last
  625. // frame
  626. tweak = frames - (frame + 1);
  627. }
  628. tween.currentFrame += tweak;
  629. }
  630. };
  631. };
  632. /**
  633. * Used to calculate Bezier splines for any number of control points.
  634. *
  635. * @class Bezier
  636. * @namespace YAHOO.util
  637. *
  638. */
  639. YAHOO.util.Bezier = new function() {
  640. /**
  641. * Get the current position of the animated element based on t. Each point
  642. * is an array of "x" and "y" values (0 = x, 1 = y) At least 2 points are
  643. * required (start and end). First point is start. Last point is end.
  644. * Additional control points are optional.
  645. *
  646. * @method getPosition
  647. * @param {Array}
  648. * points An array containing Bezier points
  649. * @param {Number}
  650. * t A number between 0 and 1 which is the basis for determining
  651. * current position
  652. * @return {Array} An array containing int x and y member data
  653. */
  654. this.getPosition = function(points, t) {
  655. var n = points.length;
  656. var tmp = [];
  657. for (var i = 0; i < n; ++i) {
  658. tmp[i] = [points[i][0], points[i][1]]; // save input
  659. }
  660. for (var j = 1; j < n; ++j) {
  661. for (i = 0; i < n - j; ++i) {
  662. tmp[i][0] = (1 - t) * tmp[i][0] + t
  663. * tmp[parseInt(i + 1, 10)][0];
  664. tmp[i][1] = (1 - t) * tmp[i][1] + t
  665. * tmp[parseInt(i + 1, 10)][1];
  666. }
  667. }
  668. return [tmp[0][0], tmp[0][1]];
  669. };
  670. };
  671. (function() {
  672. /**
  673. * Anim subclass for color transitions.
  674. * <p>
  675. * Usage:
  676. * <code>var myAnim = new Y.ColorAnim(el, { backgroundColor: { from: '#FF0000', to: '#FFFFFF' } }, 1, Y.Easing.easeOut);</code>
  677. * Color values can be specified with either 112233, #112233, [255,255,255],
  678. * or rgb(255,255,255)
  679. * </p>
  680. *
  681. * @class ColorAnim
  682. * @namespace YAHOO.util
  683. * @requires YAHOO.util.Anim
  684. * @requires YAHOO.util.AnimMgr
  685. * @requires YAHOO.util.Easing
  686. * @requires YAHOO.util.Bezier
  687. * @requires YAHOO.util.Dom
  688. * @requires YAHOO.util.Event
  689. * @constructor
  690. * @extends YAHOO.util.Anim
  691. * @param {HTMLElement |
  692. * String} el Reference to the element that will be animated
  693. * @param {Object}
  694. * attributes The attribute(s) to be animated. Each attribute is
  695. * an object with at minimum a "to" or "by" member defined.
  696. * Additional optional members are "from" (defaults to current
  697. * value), "units" (defaults to "px"). All attribute names use
  698. * camelCase.
  699. * @param {Number}
  700. * duration (optional, defaults to 1 second) Length of animation
  701. * (frames or seconds), defaults to time-based
  702. * @param {Function}
  703. * method (optional, defaults to YAHOO.util.Easing.easeNone)
  704. * Computes the values that are applied to the attributes per
  705. * frame (generally a YAHOO.util.Easing method)
  706. */
  707. YAHOO.util.ColorAnim = function(el, attributes, duration, method) {
  708. YAHOO.util.ColorAnim.superclass.constructor.call(this, el, attributes,
  709. duration, method);
  710. };
  711. YAHOO.extend(YAHOO.util.ColorAnim, YAHOO.util.Anim);
  712. // shorthand
  713. var Y = YAHOO.util;
  714. var superclass = Y.ColorAnim.superclass;
  715. var proto = Y.ColorAnim.prototype;
  716. proto.toString = function() {
  717. var el = this.getEl();
  718. var id = el.id || el.tagName;
  719. return ("ColorAnim " + id);
  720. };
  721. proto.patterns.color = /color$/i;
  722. proto.patterns.rgb = /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i;
  723. proto.patterns.hex = /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i;
  724. proto.patterns.hex3 = /^#?([0-9A-F]{1})([0-9A-F]{1})([0-9A-F]{1})$/i;
  725. proto.patterns.transparent = /^transparent|rgba\(0, 0, 0, 0\)$/; // need
  726. // rgba
  727. // for
  728. // safari
  729. /**
  730. * Attempts to parse the given string and return a 3-tuple.
  731. *
  732. * @method parseColor
  733. * @param {String}
  734. * s The string to parse.
  735. * @return {Array} The 3-tuple of rgb values.
  736. */
  737. proto.parseColor = function(s) {
  738. if (s.length == 3) {
  739. return s;
  740. }
  741. var c = this.patterns.hex.exec(s);
  742. if (c && c.length == 4) {
  743. return [parseInt(c[1], 16), parseInt(c[2], 16), parseInt(c[3], 16)];
  744. }
  745. c = this.patterns.rgb.exec(s);
  746. if (c && c.length == 4) {
  747. return [parseInt(c[1], 10), parseInt(c[2], 10), parseInt(c[3], 10)];
  748. }
  749. c = this.patterns.hex3.exec(s);
  750. if (c && c.length == 4) {
  751. return [parseInt(c[1] + c[1], 16), parseInt(c[2] + c[2], 16),
  752. parseInt(c[3] + c[3], 16)];
  753. }
  754. return null;
  755. };
  756. proto.getAttribute = function(attr) {
  757. var el = this.getEl();
  758. if (this.patterns.color.test(attr)) {
  759. var val = YAHOO.util.Dom.getStyle(el, attr);
  760. if (this.patterns.transparent.test(val)) { // bgcolor default
  761. var parent = el.parentNode; // try and get from an ancestor
  762. val = Y.Dom.getStyle(parent, attr);
  763. while (parent && this.patterns.transparent.test(val)) {
  764. parent = parent.parentNode;
  765. val = Y.Dom.getStyle(parent, attr);
  766. if (parent.tagName.toUpperCase() == 'HTML') {
  767. val = '#fff';
  768. }
  769. }
  770. }
  771. } else {
  772. val = superclass.getAttribute.call(this, attr);
  773. }
  774. return val;
  775. };
  776. proto.doMethod = function(attr, start, end) {
  777. var val;
  778. if (this.patterns.color.test(attr)) {
  779. val = [];
  780. for (var i = 0, len = start.length; i < len; ++i) {
  781. val[i] = superclass.doMethod.call(this, attr, start[i], end[i]);
  782. }
  783. val = 'rgb(' + Math.floor(val[0]) + ',' + Math.floor(val[1]) + ','
  784. + Math.floor(val[2]) + ')';
  785. } else {
  786. val = superclass.doMethod.call(this, attr, start, end);
  787. }
  788. return val;
  789. };
  790. proto.setRuntimeAttribute = function(attr) {
  791. superclass.setRuntimeAttribute.call(this, attr);
  792. if (this.patterns.color.test(attr)) {
  793. var attributes = this.attributes;
  794. var start = this.parseColor(this.runtimeAttributes[attr].start);
  795. var end = this.parseColor(this.runtimeAttributes[attr].end);
  796. // fix colors if going "by"
  797. if (typeof attributes[attr]['to'] === 'undefined'
  798. && typeof attributes[attr]['by'] !== 'undefined') {
  799. end = this.parseColor(attributes[attr].by);
  800. for (var i = 0, len = start.length; i < len; ++i) {
  801. end[i] = start[i] + end[i];
  802. }
  803. }
  804. this.runtimeAttributes[attr].start = start;
  805. this.runtimeAttributes[attr].end = end;
  806. }
  807. };
  808. })();
  809. /*
  810. * TERMS OF USE - EASING EQUATIONS Open source under the BSD License. Copyright
  811. * 2001 Robert Penner All rights reserved.
  812. *
  813. * Redistribution and use in source and binary forms, with or without
  814. * modification, are permitted provided that the following conditions are met:
  815. *
  816. * Redistributions of source code must retain the above copyright notice, this
  817. * list of conditions and the following disclaimer. Redistributions in binary
  818. * form must reproduce the above copyright notice, this list of conditions and
  819. * the following disclaimer in the documentation and/or other materials provided
  820. * with the distribution. Neither the name of the author nor the names of
  821. * contributors may be used to endorse or promote products derived from this
  822. * software without specific prior written permission.
  823. *
  824. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  825. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  826. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  827. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  828. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  829. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  830. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  831. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  832. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  833. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  834. * POSSIBILITY OF SUCH DAMAGE.
  835. */
  836. /**
  837. * Singleton that determines how an animation proceeds from start to end.
  838. *
  839. * @class Easing
  840. * @namespace YAHOO.util
  841. */
  842. YAHOO.util.Easing = {
  843. /**
  844. * Uniform speed between points.
  845. *
  846. * @method easeNone
  847. * @param {Number}
  848. * t Time value used to compute current value
  849. * @param {Number}
  850. * b Starting value
  851. * @param {Number}
  852. * c Delta between start and end values
  853. * @param {Number}
  854. * d Total length of animation
  855. * @return {Number} The computed value for the current animation frame
  856. */
  857. easeNone : function(t, b, c, d) {
  858. return c * t / d + b;
  859. },
  860. /**
  861. * Begins slowly and accelerates towards end. (quadratic)
  862. *
  863. * @method easeIn
  864. * @param {Number}
  865. * t Time value used to compute current value
  866. * @param {Number}
  867. * b Starting value
  868. * @param {Number}
  869. * c Delta between start and end values
  870. * @param {Number}
  871. * d Total length of animation
  872. * @return {Number} The computed value for the current animation frame
  873. */
  874. easeIn : function(t, b, c, d) {
  875. return c * (t /= d) * t + b;
  876. },
  877. /**
  878. * Begins quickly and decelerates towards end. (quadratic)
  879. *
  880. * @method easeOut
  881. * @param {Number}
  882. * t Time value used to compute current value
  883. * @param {Number}
  884. * b Starting value
  885. * @param {Number}
  886. * c Delta between start and end values
  887. * @param {Number}
  888. * d Total length of animation
  889. * @return {Number} The computed value for the current animation frame
  890. */
  891. easeOut : function(t, b, c, d) {
  892. return -c * (t /= d) * (t - 2) + b;
  893. },
  894. /**
  895. * Begins slowly and decelerates towards end. (quadratic)
  896. *
  897. * @method easeBoth
  898. * @param {Number}
  899. * t Time value used to compute current value
  900. * @param {Number}
  901. * b Starting value
  902. * @param {Number}
  903. * c Delta between start and end values
  904. * @param {Number}
  905. * d Total length of animation
  906. * @return {Number} The computed value for the current animation frame
  907. */
  908. easeBoth : function(t, b, c, d) {
  909. if ((t /= d / 2) < 1) {
  910. return c / 2 * t * t + b;
  911. }
  912. return -c / 2 * ((--t) * (t - 2) - 1) + b;
  913. },
  914. /**
  915. * Begins slowly and accelerates towards end. (quartic)
  916. *
  917. * @method easeInStrong
  918. * @param {Number}
  919. * t Time value used to compute current value
  920. * @param {Number}
  921. * b Starting value
  922. * @param {Number}
  923. * c Delta between start and end values
  924. * @param {Number}
  925. * d Total length of animation
  926. * @return {Number} The computed value for the current animation frame
  927. */
  928. easeInStrong : function(t, b, c, d) {
  929. return c * (t /= d) * t * t * t + b;
  930. },
  931. /**
  932. * Begins quickly and decelerates towards end. (quartic)
  933. *
  934. * @method easeOutStrong
  935. * @param {Number}
  936. * t Time value used to compute current value
  937. * @param {Number}
  938. * b Starting value
  939. * @param {Number}
  940. * c Delta between start and end values
  941. * @param {Number}
  942. * d Total length of animation
  943. * @return {Number} The computed value for the current animation frame
  944. */
  945. easeOutStrong : function(t, b, c, d) {
  946. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  947. },
  948. /**
  949. * Begins slowly and decelerates towards end. (quartic)
  950. *
  951. * @method easeBothStrong
  952. * @param {Number}
  953. * t Time value used to compute current value
  954. * @param {Number}
  955. * b Starting value
  956. * @param {Number}
  957. * c Delta between start and end values
  958. * @param {Number}
  959. * d Total length of animation
  960. * @return {Number} The computed value for the current animation frame
  961. */
  962. easeBothStrong : function(t, b, c, d) {
  963. if ((t /= d / 2) < 1) {
  964. return c / 2 * t * t * t * t + b;
  965. }
  966. return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
  967. },
  968. /**
  969. * Snap in elastic effect.
  970. *
  971. * @method elasticIn
  972. * @param {Number}
  973. * t Time value used to compute current value
  974. * @param {Number}
  975. * b Starting value
  976. * @param {Number}
  977. * c Delta between start and end values
  978. * @param {Number}
  979. * d Total length of animation
  980. * @param {Number}
  981. * a Amplitude (optional)
  982. * @param {Number}
  983. * p Period (optional)
  984. * @return {Number} The computed value for the current animation frame
  985. */
  986. elasticIn : function(t, b, c, d, a, p) {
  987. if (t == 0) {
  988. return b;
  989. }
  990. if ((t /= d) == 1) {
  991. return b + c;
  992. }
  993. if (!p) {
  994. p = d * .3;
  995. }
  996. if (!a || a < Math.abs(c)) {
  997. a = c;
  998. var s = p / 4;
  999. } else {
  1000. var s = p / (2 * Math.PI) * Math.asin(c / a);
  1001. }
  1002. return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s)
  1003. * (2 * Math.PI) / p))
  1004. + b;
  1005. },
  1006. /**
  1007. * Snap out elastic effect.
  1008. *
  1009. * @method elasticOut
  1010. * @param {Number}
  1011. * t Time value used to compute current value
  1012. * @param {Number}
  1013. * b Starting value
  1014. * @param {Number}
  1015. * c Delta between start and end values
  1016. * @param {Number}
  1017. * d Total length of animation
  1018. * @param {Number}
  1019. * a Amplitude (optional)
  1020. * @param {Number}
  1021. * p Period (optional)
  1022. * @return {Number} The computed value for the current animation frame
  1023. */
  1024. elasticOut : function(t, b, c, d, a, p) {
  1025. if (t == 0) {
  1026. return b;
  1027. }
  1028. if ((t /= d) == 1) {
  1029. return b + c;
  1030. }
  1031. if (!p) {
  1032. p = d * .3;
  1033. }
  1034. if (!a || a < Math.abs(c)) {
  1035. a = c;
  1036. var s = p / 4;
  1037. } else {
  1038. var s = p / (2 * Math.PI) * Math.asin(c / a);
  1039. }
  1040. return a * Math.pow(2, -10 * t)
  1041. * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
  1042. },
  1043. /**
  1044. * Snap both elastic effect.
  1045. *
  1046. * @method elasticBoth
  1047. * @param {Number}
  1048. * t Time value used to compute current value
  1049. * @param {Number}
  1050. * b Starting value
  1051. * @param {Number}
  1052. * c Delta between start and end values
  1053. * @param {Number}
  1054. * d Total length of animation
  1055. * @param {Number}
  1056. * a Amplitude (optional)
  1057. * @param {Number}
  1058. * p Period (optional)
  1059. * @return {Number} The computed value for the current animation frame
  1060. */
  1061. elasticBoth : function(t, b, c, d, a, p) {
  1062. if (t == 0) {
  1063. return b;
  1064. }
  1065. if ((t /= d / 2) == 2) {
  1066. return b + c;
  1067. }
  1068. if (!p) {
  1069. p = d * (.3 * 1.5);
  1070. }
  1071. if (!a || a < Math.abs(c)) {
  1072. a = c;
  1073. var s = p / 4;
  1074. } else {
  1075. var s = p / (2 * Math.PI) * Math.asin(c / a);
  1076. }
  1077. if (t < 1) {
  1078. return -.5
  1079. * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s)
  1080. * (2 * Math.PI) / p)) + b;
  1081. }
  1082. return a * Math.pow(2, -10 * (t -= 1))
  1083. * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
  1084. },
  1085. /**
  1086. * Backtracks slightly, then reverses direction and moves to end.
  1087. *
  1088. * @method backIn
  1089. * @param {Number}
  1090. * t Time value used to compute current value
  1091. * @param {Number}
  1092. * b Starting value
  1093. * @param {Number}
  1094. * c Delta between start and end values
  1095. * @param {Number}
  1096. * d Total length of animation
  1097. * @param {Number}
  1098. * s Overshoot (optional)
  1099. * @return {Number} The computed value for the current animation frame
  1100. */
  1101. backIn : function(t, b, c, d, s) {
  1102. if (typeof s == 'undefined') {
  1103. s = 1.70158;
  1104. }
  1105. return c * (t /= d) * t * ((s + 1) * t - s) + b;
  1106. },
  1107. /**
  1108. * Overshoots end, then reverses and comes back to end.
  1109. *
  1110. * @method backOut
  1111. * @param {Number}
  1112. * t Time value used to compute current value
  1113. * @param {Number}
  1114. * b Starting value
  1115. * @param {Number}
  1116. * c Delta between start and end values
  1117. * @param {Number}
  1118. * d Total length of animation
  1119. * @param {Number}
  1120. * s Overshoot (optional)
  1121. * @return {Number} The computed value for the current animation frame
  1122. */
  1123. backOut : function(t, b, c, d, s) {
  1124. if (typeof s == 'undefined') {
  1125. s = 1.70158;
  1126. }
  1127. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  1128. },
  1129. /**
  1130. * Backtracks slightly, then reverses direction, overshoots end, then
  1131. * reverses and comes back to end.
  1132. *
  1133. * @method backBoth
  1134. * @param {Number}
  1135. * t Time value used to compute current value
  1136. * @param {Number}
  1137. * b Starting value
  1138. * @param {Number}
  1139. * c Delta between start and end values
  1140. * @param {Number}
  1141. * d Total length of animation
  1142. * @param {Number}
  1143. * s Overshoot (optional)
  1144. * @return {Number} The computed value for the current animation frame
  1145. */
  1146. backBoth : function(t, b, c, d, s) {
  1147. if (typeof s == 'undefined') {
  1148. s = 1.70158;
  1149. }
  1150. if ((t /= d / 2) < 1) {
  1151. return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
  1152. }
  1153. return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
  1154. },
  1155. /**
  1156. * Bounce off of start.
  1157. *
  1158. * @method bounceIn
  1159. * @param {Number}
  1160. * t Time value used to compute current value
  1161. * @param {Number}
  1162. * b Starting value
  1163. * @param {Number}
  1164. * c Delta between start and end values
  1165. * @param {Number}
  1166. * d Total length of animation
  1167. * @return {Number} The computed value for the current animation frame
  1168. */
  1169. bounceIn : function(t, b, c, d) {
  1170. return c - YAHOO.util.Easing.bounceOut(d - t, 0, c, d) + b;
  1171. },
  1172. /**
  1173. * Bounces off end.
  1174. *
  1175. * @method bounceOut
  1176. * @param {Number}
  1177. * t Time value used to compute current value
  1178. * @param {Number}
  1179. * b Starting value
  1180. * @param {Number}
  1181. * c Delta between start and end values
  1182. * @param {Number}
  1183. * d Total length of animation
  1184. * @return {Number} The computed value for the current animation frame
  1185. */
  1186. bounceOut : function(t, b, c, d) {
  1187. if ((t /= d) < (1 / 2.75)) {
  1188. return c * (7.5625 * t * t) + b;
  1189. } else if (t < (2 / 2.75)) {
  1190. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
  1191. } else if (t < (2.5 / 2.75)) {
  1192. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
  1193. }
  1194. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
  1195. },
  1196. /**
  1197. * Bounces off start and end.
  1198. *
  1199. * @method bounceBoth
  1200. * @param {Number}
  1201. * t Time value used to compute current value
  1202. * @param {Number}
  1203. * b Starting value
  1204. * @param {Number}
  1205. * c Delta between start and end values
  1206. * @param {Number}
  1207. * d Total length of animation
  1208. * @return {Number} The computed value for the current animation frame
  1209. */
  1210. bounceBoth : function(t, b, c, d) {
  1211. if (t < d / 2) {
  1212. return YAHOO.util.Easing.bounceIn(t * 2, 0, c, d) * .5 + b;
  1213. }
  1214. return YAHOO.util.Easing.bounceOut(t * 2 - d, 0, c, d) * .5 + c * .5
  1215. + b;
  1216. }
  1217. };
  1218. (function() {
  1219. /**
  1220. * Anim subclass for moving elements along a path defined by the "points"
  1221. * member of "attributes". All "points" are arrays with x, y coordinates.
  1222. * <p>
  1223. * Usage:
  1224. * <code>var myAnim = new YAHOO.util.Motion(el, { points: { to: [800, 800] } }, 1, YAHOO.util.Easing.easeOut);</code>
  1225. * </p>
  1226. *
  1227. * @class Motion
  1228. * @namespace YAHOO.util
  1229. * @requires YAHOO.util.Anim
  1230. * @requires YAHOO.util.AnimMgr
  1231. * @requires YAHOO.util.Easing
  1232. * @requires YAHOO.util.Bezier
  1233. * @requires YAHOO.util.Dom
  1234. * @requires YAHOO.util.Event
  1235. * @requires YAHOO.util.CustomEvent
  1236. * @constructor
  1237. * @extends YAHOO.util.Anim
  1238. * @param {String |
  1239. * HTMLElement} el Reference to the element that will be animated
  1240. * @param {Object}
  1241. * attributes The attribute(s) to be animated. Each attribute is
  1242. * an object with at minimum a "to" or "by" member defined.
  1243. * Additional optional members are "from" (defaults to current
  1244. * value), "units" (defaults to "px"). All attribute names use
  1245. * camelCase.
  1246. * @param {Number}
  1247. * duration (optional, defaults to 1 second) Length of animation
  1248. * (frames or seconds), defaults to time-based
  1249. * @param {Function}
  1250. * method (optional, defaults to YAHOO.util.Easing.easeNone)
  1251. * Computes the values that are applied to the attributes per
  1252. * frame (generally a YAHOO.util.Easing method)
  1253. */
  1254. YAHOO.util.Motion = function(el, attributes, duration, method) {
  1255. if (el) { // dont break existing subclasses not using YAHOO.extend
  1256. YAHOO.util.Motion.superclass.constructor.call(this, el, attributes,
  1257. duration, method);
  1258. }
  1259. };
  1260. YAHOO.extend(YAHOO.util.Motion, YAHOO.util.ColorAnim);
  1261. // shorthand
  1262. var Y = YAHOO.util;
  1263. var superclass = Y.Motion.superclass;
  1264. var proto = Y.Motion.prototype;
  1265. proto.toString = function() {
  1266. var el = this.getEl();
  1267. var id = el.id || el.tagName;
  1268. return ("Motion " + id);
  1269. };
  1270. proto.patterns.points = /^points$/i;
  1271. proto.setAttribute = function(attr, val, unit) {
  1272. if (this.patterns.points.test(attr)) {
  1273. unit = unit || 'px';
  1274. superclass.setAttribute.call(this, 'left', val[0], unit);
  1275. superclass.setAttribute.call(this, 'top', val[1], unit);
  1276. } else {
  1277. superclass.setAttribute.call(this, attr, val, unit);
  1278. }
  1279. };
  1280. proto.getAttribute = function(attr) {
  1281. if (this.patterns.points.test(attr)) {
  1282. var val = [superclass.getAttribute.call(this, 'left'),
  1283. superclass.getAttribute.call(this, 'top')];
  1284. } else {
  1285. val = superclass.getAttribute.call(this, attr);
  1286. }
  1287. return val;
  1288. };
  1289. proto.doMethod = function(attr, start, end) {
  1290. var val = null;
  1291. if (this.patterns.points.test(attr)) {
  1292. var t = this.method(this.currentFrame, 0, 100, this.totalFrames)
  1293. / 100;
  1294. val = Y.Bezier.getPosition(this.runtimeAttributes[attr], t);
  1295. } else {
  1296. val = superclass.doMethod.call(this, attr, start, end);
  1297. }
  1298. return val;
  1299. };
  1300. proto.setRuntimeAttribute = function(attr) {
  1301. if (this.patterns.points.test(attr)) {
  1302. var el = this.getEl();
  1303. var attributes = this.attributes;
  1304. var start;
  1305. var control = attributes['points']['control'] || [];
  1306. var end;
  1307. var i, len;
  1308. if (control.length > 0 && !(control[0] instanceof Array)) { // could
  1309. // be
  1310. // single
  1311. // point
  1312. // or
  1313. // array
  1314. // of
  1315. // points
  1316. control = [control];
  1317. } else { // break reference to attributes.points.control
  1318. var tmp = [];
  1319. for (i = 0, len = control.length; i < len; ++i) {
  1320. tmp[i] = control[i];
  1321. }
  1322. control = tmp;
  1323. }
  1324. if (Y.Dom.getStyle(el, 'position') == 'static') { // default to
  1325. // relative
  1326. Y.Dom.setStyle(el, 'position', 'relative');
  1327. }
  1328. if (isset(attributes['points']['from'])) {
  1329. Y.Dom.setXY(el, attributes['points']['from']); // set position
  1330. // to from point
  1331. } else {
  1332. Y.Dom.setXY(el, Y.Dom.getXY(el));
  1333. } // set it to current position
  1334. start = this.getAttribute('points'); // get actual top & left
  1335. // TO beats BY, per SMIL 2.1 spec
  1336. if (isset(attributes['points']['to'])) {
  1337. end = translateValues.call(this, attributes['points']['to'],
  1338. start);
  1339. var pageXY = Y.Dom.getXY(this.getEl());
  1340. for (i = 0, len = control.length; i < len; ++i) {
  1341. control[i] = translateValues.call(this, control[i], start);
  1342. }
  1343. } else if (isset(attributes['points']['by'])) {
  1344. end = [start[0] + attributes['points']['by'][0],
  1345. start[1] + attributes['points']['by'][1]];
  1346. for (i = 0, len = control.length; i < len; ++i) {
  1347. control[i] = [start[0] + control[i][0],
  1348. start[1] + control[i][1]];
  1349. }
  1350. }
  1351. this.runtimeAttributes[attr] = [start];
  1352. if (control.length > 0) {
  1353. this.runtimeAttributes[attr] = this.runtimeAttributes[attr]
  1354. .concat(control);
  1355. }
  1356. this.runtimeAttributes[attr][this.runtimeAttributes[attr].length] = end;
  1357. } else {
  1358. superclass.setRuntimeAttribute.call(this, attr);
  1359. }
  1360. };
  1361. var translateValues = function(val, start) {
  1362. var pageXY = Y.Dom.getXY(this.getEl());
  1363. val = [val[0] - pageXY[0] + start[0], val[1] - pageXY[1] + start[1]];
  1364. return val;
  1365. };
  1366. var isset = function(prop) {
  1367. return (typeof prop !== 'undefined');
  1368. };
  1369. })();
  1370. (function() {
  1371. /**
  1372. * Anim subclass for scrolling elements to a position defined by the
  1373. * "scroll" member of "attributes". All "scroll" members are arrays with x,
  1374. * y scroll positions.
  1375. * <p>
  1376. * Usage:
  1377. * <code>var myAnim = new YAHOO.util.Scroll(el, { scroll: { to: [0, 800] } }, 1, YAHOO.util.Easing.easeOut);</code>
  1378. * </p>
  1379. *
  1380. * @class Scroll
  1381. * @namespace YAHOO.util
  1382. * @requires YAHOO.util.Anim
  1383. * @requires YAHOO.util.AnimMgr
  1384. * @requires YAHOO.util.Easing
  1385. * @requires YAHOO.util.Bezier
  1386. * @requires YAHOO.util.Dom
  1387. * @requires YAHOO.util.Event
  1388. * @requires YAHOO.util.CustomEvent
  1389. * @extends YAHOO.util.Anim
  1390. * @constructor
  1391. * @param {String
  1392. * or HTMLElement} el Reference to the element that will be
  1393. * animated
  1394. * @param {Object}
  1395. * attributes The attribute(s) to be animated. Each attribute is
  1396. * an object with at minimum a "to" or "by" member defined.
  1397. * Additional optional members are "from" (defaults to current
  1398. * value), "units" (defaults to "px"). All attribute names use
  1399. * camelCase.
  1400. * @param {Number}
  1401. * duration (optional, defaults to 1 second) Length of animation
  1402. * (frames or seconds), defaults to time-based
  1403. * @param {Function}
  1404. * method (optional, defaults to YAHOO.util.Easing.easeNone)
  1405. * Computes the values that are applied to the attributes per
  1406. * frame (generally a YAHOO.util.Easing method)
  1407. */
  1408. YAHOO.util.Scroll = function(el, attributes, duration, method) {
  1409. if (el) { // dont break existing subclasses not using YAHOO.extend
  1410. YAHOO.util.Scroll.superclass.constructor.call(this, el, attributes,
  1411. duration, method);
  1412. }
  1413. };
  1414. YAHOO.extend(YAHOO.util.Scroll, YAHOO.util.ColorAnim);
  1415. // shorthand
  1416. var Y = YAHOO.util;
  1417. var superclass = Y.Scroll.superclass;
  1418. var proto = Y.Scroll.prototype;
  1419. proto.toString = function() {
  1420. var el = this.getEl();
  1421. var id = el.id || el.tagName;
  1422. return ("Scroll " + id);
  1423. };
  1424. proto.doMethod = function(attr, start, end) {
  1425. var val = null;
  1426. if (attr == 'scroll') {
  1427. val = [
  1428. this.method(this.currentFrame, start[0], end[0] - start[0],
  1429. this.totalFrames),
  1430. this.method(this.currentFrame, start[1], end[1] - start[1],
  1431. this.totalFrames)];
  1432. } else {
  1433. val = superclass.doMethod.call(this, attr, start, end);
  1434. }
  1435. return val;
  1436. };
  1437. proto.getAttribute = function(attr) {
  1438. var val = null;
  1439. var el = this.getEl();
  1440. if (attr == 'scroll') {
  1441. val = [el.scrollLeft, el.scrollTop];
  1442. } else {
  1443. val = superclass.getAttribute.call(this, attr);
  1444. }
  1445. return val;
  1446. };
  1447. proto.setAttribute = function(attr, val, unit) {
  1448. var el = this.getEl();
  1449. if (attr == 'scroll') {
  1450. el.scrollLeft = val[0];
  1451. el.scrollTop = val[1];
  1452. } else {
  1453. superclass.setAttribute.call(this, attr, val, unit);
  1454. }
  1455. };
  1456. })();
  1457. YAHOO.register("animation", YAHOO.util.Anim, {
  1458. version : "2.2.0",
  1459. build : "127"
  1460. });