Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transition-timer-debug.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('transition-timer', function(Y) {
  9. /*
  10. * The Transition Utility provides an API for creating advanced transitions.
  11. * @module transition
  12. */
  13. /*
  14. * Provides the base Transition class, for animating numeric properties.
  15. *
  16. * @module transition
  17. * @submodule transition-timer
  18. */
  19. var Transition = Y.Transition;
  20. Y.mix(Transition.prototype, {
  21. _start: function() {
  22. if (Transition.useNative) {
  23. this._runNative();
  24. } else {
  25. this._runTimer();
  26. }
  27. },
  28. _runTimer: function() {
  29. var anim = this;
  30. anim._initAttrs();
  31. Transition._running[Y.stamp(anim)] = anim;
  32. anim._startTime = new Date();
  33. Transition._startTimer();
  34. },
  35. _endTimer: function() {
  36. var anim = this;
  37. delete Transition._running[Y.stamp(anim)];
  38. anim._startTime = null;
  39. },
  40. _runFrame: function() {
  41. var t = new Date() - this._startTime;
  42. this._runAttrs(t);
  43. },
  44. _runAttrs: function(time) {
  45. var anim = this,
  46. node = anim._node,
  47. config = anim._config,
  48. uid = Y.stamp(node),
  49. attrs = Transition._nodeAttrs[uid],
  50. customAttr = Transition.behaviors,
  51. done = false,
  52. allDone = false,
  53. data,
  54. name,
  55. attribute,
  56. setter,
  57. elapsed,
  58. delay,
  59. d,
  60. t,
  61. i;
  62. for (name in attrs) {
  63. if ((attribute = attrs[name]) && attribute.transition === anim) {
  64. d = attribute.duration;
  65. delay = attribute.delay;
  66. elapsed = (time - delay) / 1000;
  67. t = time;
  68. data = {
  69. type: 'propertyEnd',
  70. propertyName: name,
  71. config: config,
  72. elapsedTime: elapsed
  73. };
  74. setter = (i in customAttr && 'set' in customAttr[i]) ?
  75. customAttr[i].set : Transition.DEFAULT_SETTER;
  76. done = (t >= d);
  77. if (t > d) {
  78. t = d;
  79. }
  80. if (!delay || time >= delay) {
  81. setter(anim, name, attribute.from, attribute.to, t - delay, d - delay,
  82. attribute.easing, attribute.unit);
  83. if (done) {
  84. delete attrs[name];
  85. anim._count--;
  86. if (config[name] && config[name].on && config[name].on.end) {
  87. config[name].on.end.call(Y.one(node), data);
  88. }
  89. //node.fire('transition:propertyEnd', data);
  90. if (!allDone && anim._count <= 0) {
  91. allDone = true;
  92. anim._end(elapsed);
  93. anim._endTimer();
  94. }
  95. }
  96. }
  97. }
  98. }
  99. },
  100. _initAttrs: function() {
  101. var anim = this,
  102. customAttr = Transition.behaviors,
  103. uid = Y.stamp(anim._node),
  104. attrs = Transition._nodeAttrs[uid],
  105. attribute,
  106. duration,
  107. delay,
  108. easing,
  109. val,
  110. name,
  111. mTo,
  112. mFrom,
  113. unit, begin, end;
  114. for (name in attrs) {
  115. if ((attribute = attrs[name]) && attribute.transition === anim) {
  116. duration = attribute.duration * 1000;
  117. delay = attribute.delay * 1000;
  118. easing = attribute.easing;
  119. val = attribute.value;
  120. // only allow supported properties
  121. if (name in anim._node.style || name in Y.DOM.CUSTOM_STYLES) {
  122. begin = (name in customAttr && 'get' in customAttr[name]) ?
  123. customAttr[name].get(anim, name) : Transition.DEFAULT_GETTER(anim, name);
  124. mFrom = Transition.RE_UNITS.exec(begin);
  125. mTo = Transition.RE_UNITS.exec(val);
  126. begin = mFrom ? mFrom[1] : begin;
  127. end = mTo ? mTo[1] : val;
  128. unit = mTo ? mTo[2] : mFrom ? mFrom[2] : ''; // one might be zero TODO: mixed units
  129. if (!unit && Transition.RE_DEFAULT_UNIT.test(name)) {
  130. unit = Transition.DEFAULT_UNIT;
  131. }
  132. if (typeof easing === 'string') {
  133. if (easing.indexOf('cubic-bezier') > -1) {
  134. easing = easing.substring(13, easing.length - 1).split(',');
  135. } else if (Transition.easings[easing]) {
  136. easing = Transition.easings[easing];
  137. }
  138. }
  139. attribute.from = Number(begin);
  140. attribute.to = Number(end);
  141. attribute.unit = unit;
  142. attribute.easing = easing;
  143. attribute.duration = duration + delay;
  144. attribute.delay = delay;
  145. } else {
  146. delete attrs[name];
  147. anim._count--;
  148. }
  149. }
  150. }
  151. },
  152. destroy: function() {
  153. this.detachAll();
  154. this._node = null;
  155. }
  156. }, true);
  157. Y.mix(Y.Transition, {
  158. _runtimeAttrs: {},
  159. /*
  160. * Regex of properties that should use the default unit.
  161. *
  162. * @property RE_DEFAULT_UNIT
  163. * @static
  164. */
  165. RE_DEFAULT_UNIT: /^width|height|top|right|bottom|left|margin.*|padding.*|border.*$/i,
  166. /*
  167. * The default unit to use with properties that pass the RE_DEFAULT_UNIT test.
  168. *
  169. * @property DEFAULT_UNIT
  170. * @static
  171. */
  172. DEFAULT_UNIT: 'px',
  173. /*
  174. * Time in milliseconds passed to setInterval for frame processing
  175. *
  176. * @property intervalTime
  177. * @default 20
  178. * @static
  179. */
  180. intervalTime: 20,
  181. /*
  182. * Bucket for custom getters and setters
  183. *
  184. * @property behaviors
  185. * @static
  186. */
  187. behaviors: {
  188. left: {
  189. get: function(anim, attr) {
  190. return Y.DOM._getAttrOffset(anim._node, attr);
  191. }
  192. }
  193. },
  194. /*
  195. * The default setter to use when setting object properties.
  196. *
  197. * @property DEFAULT_SETTER
  198. * @static
  199. */
  200. DEFAULT_SETTER: function(anim, att, from, to, elapsed, duration, fn, unit) {
  201. from = Number(from);
  202. to = Number(to);
  203. var node = anim._node,
  204. val = Transition.cubicBezier(fn, elapsed / duration);
  205. val = from + val[0] * (to - from);
  206. if (node) {
  207. if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  208. unit = unit || '';
  209. Y.DOM.setStyle(node, att, val + unit);
  210. }
  211. } else {
  212. anim._end();
  213. }
  214. },
  215. /*
  216. * The default getter to use when getting object properties.
  217. *
  218. * @property DEFAULT_GETTER
  219. * @static
  220. */
  221. DEFAULT_GETTER: function(anim, att) {
  222. var node = anim._node,
  223. val = '';
  224. if (att in node.style || att in Y.DOM.CUSTOM_STYLES) {
  225. val = Y.DOM.getComputedStyle(node, att);
  226. }
  227. return val;
  228. },
  229. _startTimer: function() {
  230. if (!Transition._timer) {
  231. Transition._timer = setInterval(Transition._runFrame, Transition.intervalTime);
  232. }
  233. },
  234. _stopTimer: function() {
  235. clearInterval(Transition._timer);
  236. Transition._timer = null;
  237. },
  238. /*
  239. * Called per Interval to handle each animation frame.
  240. * @method _runFrame
  241. * @private
  242. * @static
  243. */
  244. _runFrame: function() {
  245. var done = true,
  246. anim;
  247. for (anim in Transition._running) {
  248. if (Transition._running[anim]._runFrame) {
  249. done = false;
  250. Transition._running[anim]._runFrame();
  251. }
  252. }
  253. if (done) {
  254. Transition._stopTimer();
  255. }
  256. },
  257. cubicBezier: function(p, t) {
  258. var x0 = 0,
  259. y0 = 0,
  260. x1 = p[0],
  261. y1 = p[1],
  262. x2 = p[2],
  263. y2 = p[3],
  264. x3 = 1,
  265. y3 = 0,
  266. A = x3 - 3 * x2 + 3 * x1 - x0,
  267. B = 3 * x2 - 6 * x1 + 3 * x0,
  268. C = 3 * x1 - 3 * x0,
  269. D = x0,
  270. E = y3 - 3 * y2 + 3 * y1 - y0,
  271. F = 3 * y2 - 6 * y1 + 3 * y0,
  272. G = 3 * y1 - 3 * y0,
  273. H = y0,
  274. x = (((A*t) + B)*t + C)*t + D,
  275. y = (((E*t) + F)*t + G)*t + H;
  276. return [x, y];
  277. },
  278. easings: {
  279. ease: [0.25, 0, 1, 0.25],
  280. linear: [0, 0, 1, 1],
  281. 'ease-in': [0.42, 0, 1, 1],
  282. 'ease-out': [0, 0, 0.58, 1],
  283. 'ease-in-out': [0.42, 0, 0.58, 1]
  284. },
  285. _running: {},
  286. _timer: null,
  287. RE_UNITS: /^(-?\d*\.?\d*){1}(em|ex|px|in|cm|mm|pt|pc|%)*$/
  288. }, true);
  289. Transition.behaviors.top = Transition.behaviors.bottom = Transition.behaviors.right = Transition.behaviors.left;
  290. Y.Transition = Transition;
  291. }, '3.4.0' ,{requires:['transition']});