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.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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', function(Y) {
  9. /**
  10. * Provides the transition method for Node.
  11. * Transition has no API of its own, but adds the transition method to Node.
  12. *
  13. * @module transition
  14. * @requires node-style
  15. */
  16. var CAMEL_VENDOR_PREFIX = '',
  17. VENDOR_PREFIX = '',
  18. DOCUMENT = Y.config.doc,
  19. DOCUMENT_ELEMENT = 'documentElement',
  20. TRANSITION = 'transition',
  21. TRANSITION_CAMEL = 'Transition',
  22. TRANSITION_PROPERTY_CAMEL,
  23. TRANSITION_PROPERTY,
  24. TRANSITION_DURATION,
  25. TRANSITION_TIMING_FUNCTION,
  26. TRANSITION_DELAY,
  27. TRANSITION_END,
  28. ON_TRANSITION_END,
  29. TRANSFORM_CAMEL,
  30. EMPTY_OBJ = {},
  31. VENDORS = [
  32. 'Webkit',
  33. 'Moz'
  34. ],
  35. VENDOR_TRANSITION_END = {
  36. Webkit: 'webkitTransitionEnd'
  37. },
  38. /**
  39. * A class for constructing transition instances.
  40. * Adds the "transition" method to Node.
  41. * @class Transition
  42. * @constructor
  43. */
  44. Transition = function() {
  45. this.init.apply(this, arguments);
  46. };
  47. Transition._toCamel = function(property) {
  48. property = property.replace(/-([a-z])/gi, function(m0, m1) {
  49. return m1.toUpperCase();
  50. });
  51. return property;
  52. };
  53. Transition._toHyphen = function(property) {
  54. property = property.replace(/([A-Z]?)([a-z]+)([A-Z]?)/g, function(m0, m1, m2, m3) {
  55. var str = ((m1) ? '-' + m1.toLowerCase() : '') + m2;
  56. if (m3) {
  57. str += '-' + m3.toLowerCase();
  58. }
  59. return str;
  60. });
  61. return property;
  62. };
  63. Transition.SHOW_TRANSITION = 'fadeIn';
  64. Transition.HIDE_TRANSITION = 'fadeOut';
  65. Transition.useNative = false;
  66. Y.Array.each(VENDORS, function(val) { // then vendor specific
  67. var property = val + TRANSITION_CAMEL;
  68. if (property in DOCUMENT[DOCUMENT_ELEMENT].style) {
  69. CAMEL_VENDOR_PREFIX = val;
  70. VENDOR_PREFIX = Transition._toHyphen(val) + '-';
  71. Transition.useNative = true;
  72. Transition.supported = true; // TODO: remove
  73. Transition._VENDOR_PREFIX = val;
  74. }
  75. });
  76. TRANSITION_CAMEL = CAMEL_VENDOR_PREFIX + TRANSITION_CAMEL;
  77. TRANSITION_PROPERTY_CAMEL = CAMEL_VENDOR_PREFIX + 'TransitionProperty';
  78. TRANSITION_PROPERTY = VENDOR_PREFIX + 'transition-property';
  79. TRANSITION_DURATION = VENDOR_PREFIX + 'transition-duration';
  80. TRANSITION_TIMING_FUNCTION = VENDOR_PREFIX + 'transition-timing-function';
  81. TRANSITION_DELAY = VENDOR_PREFIX + 'transition-delay';
  82. TRANSITION_END = 'transitionend';
  83. ON_TRANSITION_END = 'on' + CAMEL_VENDOR_PREFIX.toLowerCase() + 'transitionend';
  84. TRANSITION_END = VENDOR_TRANSITION_END[CAMEL_VENDOR_PREFIX] || TRANSITION_END;
  85. TRANSFORM_CAMEL = CAMEL_VENDOR_PREFIX + 'Transform';
  86. Transition.fx = {};
  87. Transition.toggles = {};
  88. Transition._hasEnd = {};
  89. Transition._reKeywords = /^(?:node|duration|iterations|easing|delay|on|onstart|onend)$/i;
  90. Y.Node.DOM_EVENTS[TRANSITION_END] = 1;
  91. Transition.NAME = 'transition';
  92. Transition.DEFAULT_EASING = 'ease';
  93. Transition.DEFAULT_DURATION = 0.5;
  94. Transition.DEFAULT_DELAY = 0;
  95. Transition._nodeAttrs = {};
  96. Transition.prototype = {
  97. constructor: Transition,
  98. init: function(node, config) {
  99. var anim = this;
  100. anim._node = node;
  101. if (!anim._running && config) {
  102. anim._config = config;
  103. node._transition = anim; // cache for reuse
  104. anim._duration = ('duration' in config) ?
  105. config.duration: anim.constructor.DEFAULT_DURATION;
  106. anim._delay = ('delay' in config) ?
  107. config.delay: anim.constructor.DEFAULT_DELAY;
  108. anim._easing = config.easing || anim.constructor.DEFAULT_EASING;
  109. anim._count = 0; // track number of animated properties
  110. anim._running = false;
  111. }
  112. return anim;
  113. },
  114. addProperty: function(prop, config) {
  115. var anim = this,
  116. node = this._node,
  117. uid = Y.stamp(node),
  118. nodeInstance = Y.one(node),
  119. attrs = Transition._nodeAttrs[uid],
  120. computed,
  121. compareVal,
  122. dur,
  123. attr,
  124. val;
  125. if (!attrs) {
  126. attrs = Transition._nodeAttrs[uid] = {};
  127. }
  128. attr = attrs[prop];
  129. // might just be a value
  130. if (config && config.value !== undefined) {
  131. val = config.value;
  132. } else if (config !== undefined) {
  133. val = config;
  134. config = EMPTY_OBJ;
  135. }
  136. if (typeof val === 'function') {
  137. val = val.call(nodeInstance, nodeInstance);
  138. }
  139. if (attr && attr.transition) {
  140. // take control if another transition owns this property
  141. if (attr.transition !== anim) {
  142. attr.transition._count--; // remapping attr to this transition
  143. }
  144. }
  145. anim._count++; // properties per transition
  146. // make 0 async and fire events
  147. dur = ((typeof config.duration != 'undefined') ? config.duration :
  148. anim._duration) || 0.0001;
  149. attrs[prop] = {
  150. value: val,
  151. duration: dur,
  152. delay: (typeof config.delay != 'undefined') ? config.delay :
  153. anim._delay,
  154. easing: config.easing || anim._easing,
  155. transition: anim
  156. };
  157. // native end event doesnt fire when setting to same value
  158. // supplementing with timer
  159. // val may be a string or number (height: 0, etc), but computedStyle is always string
  160. computed = Y.DOM.getComputedStyle(node, prop);
  161. compareVal = (typeof val === 'string') ? computed : parseFloat(computed);
  162. if (Transition.useNative && compareVal === val) {
  163. setTimeout(function() {
  164. anim._onNativeEnd.call(node, {
  165. propertyName: prop,
  166. elapsedTime: dur
  167. });
  168. }, dur * 1000);
  169. }
  170. },
  171. removeProperty: function(prop) {
  172. var anim = this,
  173. attrs = Transition._nodeAttrs[Y.stamp(anim._node)];
  174. if (attrs && attrs[prop]) {
  175. delete attrs[prop];
  176. anim._count--;
  177. }
  178. },
  179. initAttrs: function(config) {
  180. var attr,
  181. node = this._node;
  182. if (config.transform && !config[TRANSFORM_CAMEL]) {
  183. config[TRANSFORM_CAMEL] = config.transform;
  184. delete config.transform; // TODO: copy
  185. }
  186. for (attr in config) {
  187. if (config.hasOwnProperty(attr) && !Transition._reKeywords.test(attr)) {
  188. this.addProperty(attr, config[attr]);
  189. // when size is auto or % webkit starts from zero instead of computed
  190. // (https://bugs.webkit.org/show_bug.cgi?id=16020)
  191. // TODO: selective set
  192. if (node.style[attr] === '') {
  193. Y.DOM.setStyle(node, attr, Y.DOM.getComputedStyle(node, attr));
  194. }
  195. }
  196. }
  197. },
  198. /**
  199. * Starts or an animation.
  200. * @method run
  201. * @chainable
  202. * @private
  203. */
  204. run: function(callback) {
  205. var anim = this,
  206. node = anim._node,
  207. config = anim._config,
  208. data = {
  209. type: 'transition:start',
  210. config: config
  211. };
  212. if (!anim._running) {
  213. anim._running = true;
  214. //anim._node.fire('transition:start', data);
  215. if (config.on && config.on.start) {
  216. config.on.start.call(Y.one(node), data);
  217. }
  218. anim.initAttrs(anim._config);
  219. anim._callback = callback;
  220. anim._start();
  221. }
  222. return anim;
  223. },
  224. _start: function() {
  225. this._runNative();
  226. },
  227. _prepDur: function(dur) {
  228. dur = parseFloat(dur);
  229. return dur + 's';
  230. },
  231. _runNative: function(time) {
  232. var anim = this,
  233. node = anim._node,
  234. uid = Y.stamp(node),
  235. style = node.style,
  236. computed = getComputedStyle(node),
  237. attrs = Transition._nodeAttrs[uid],
  238. cssText = '',
  239. cssTransition = computed[Transition._toCamel(TRANSITION_PROPERTY)],
  240. transitionText = TRANSITION_PROPERTY + ': ',
  241. duration = TRANSITION_DURATION + ': ',
  242. easing = TRANSITION_TIMING_FUNCTION + ': ',
  243. delay = TRANSITION_DELAY + ': ',
  244. hyphy,
  245. attr,
  246. name;
  247. // preserve existing transitions
  248. if (cssTransition !== 'all') {
  249. transitionText += cssTransition + ',';
  250. duration += computed[Transition._toCamel(TRANSITION_DURATION)] + ',';
  251. easing += computed[Transition._toCamel(TRANSITION_TIMING_FUNCTION)] + ',';
  252. delay += computed[Transition._toCamel(TRANSITION_DELAY)] + ',';
  253. }
  254. // run transitions mapped to this instance
  255. for (name in attrs) {
  256. hyphy = Transition._toHyphen(name);
  257. attr = attrs[name];
  258. if ((attr = attrs[name]) && attr.transition === anim) {
  259. if (name in node.style) { // only native styles allowed
  260. duration += anim._prepDur(attr.duration) + ',';
  261. delay += anim._prepDur(attr.delay) + ',';
  262. easing += (attr.easing) + ',';
  263. transitionText += hyphy + ',';
  264. cssText += hyphy + ': ' + attr.value + '; ';
  265. } else {
  266. this.removeProperty(name);
  267. }
  268. }
  269. }
  270. transitionText = transitionText.replace(/,$/, ';');
  271. duration = duration.replace(/,$/, ';');
  272. easing = easing.replace(/,$/, ';');
  273. delay = delay.replace(/,$/, ';');
  274. // only one native end event per node
  275. if (!Transition._hasEnd[uid]) {
  276. //anim._detach = Y.on(TRANSITION_END, anim._onNativeEnd, node);
  277. //node[ON_TRANSITION_END] = anim._onNativeEnd;
  278. node.addEventListener(TRANSITION_END, anim._onNativeEnd, '');
  279. Transition._hasEnd[uid] = true;
  280. }
  281. //setTimeout(function() { // allow updates to apply (size fix, onstart, etc)
  282. style.cssText += transitionText + duration + easing + delay + cssText;
  283. //}, 1);
  284. },
  285. _end: function(elapsed) {
  286. var anim = this,
  287. node = anim._node,
  288. callback = anim._callback,
  289. config = anim._config,
  290. data = {
  291. type: 'transition:end',
  292. config: config,
  293. elapsedTime: elapsed
  294. },
  295. nodeInstance = Y.one(node);
  296. anim._running = false;
  297. anim._callback = null;
  298. if (node) {
  299. if (config.on && config.on.end) {
  300. setTimeout(function() { // IE: allow previous update to finish
  301. config.on.end.call(nodeInstance, data);
  302. // nested to ensure proper fire order
  303. if (callback) {
  304. callback.call(nodeInstance, data);
  305. }
  306. }, 1);
  307. } else if (callback) {
  308. setTimeout(function() { // IE: allow previous update to finish
  309. callback.call(nodeInstance, data);
  310. }, 1);
  311. }
  312. //node.fire('transition:end', data);
  313. }
  314. },
  315. _endNative: function(name) {
  316. var node = this._node,
  317. value = node.ownerDocument.defaultView.getComputedStyle(node, '')[Transition._toCamel(TRANSITION_PROPERTY)];
  318. if (typeof value === 'string') {
  319. value = value.replace(new RegExp('(?:^|,\\s)' + name + ',?'), ',');
  320. value = value.replace(/^,|,$/, '');
  321. node.style[TRANSITION_CAMEL] = value;
  322. }
  323. },
  324. _onNativeEnd: function(e) {
  325. var node = this,
  326. uid = Y.stamp(node),
  327. event = e,//e._event,
  328. name = Transition._toCamel(event.propertyName),
  329. elapsed = event.elapsedTime,
  330. attrs = Transition._nodeAttrs[uid],
  331. attr = attrs[name],
  332. anim = (attr) ? attr.transition : null,
  333. data,
  334. config;
  335. if (anim) {
  336. anim.removeProperty(name);
  337. anim._endNative(name);
  338. config = anim._config[name];
  339. data = {
  340. type: 'propertyEnd',
  341. propertyName: name,
  342. elapsedTime: elapsed,
  343. config: config
  344. };
  345. if (config && config.on && config.on.end) {
  346. config.on.end.call(Y.one(node), data);
  347. }
  348. //node.fire('transition:propertyEnd', data);
  349. if (anim._count <= 0) { // after propertyEnd fires
  350. anim._end(elapsed);
  351. }
  352. }
  353. },
  354. destroy: function() {
  355. var anim = this,
  356. node = anim._node;
  357. /*
  358. if (anim._detach) {
  359. anim._detach.detach();
  360. }
  361. */
  362. //anim._node[ON_TRANSITION_END] = null;
  363. if (node) {
  364. node.removeEventListener(TRANSITION_END, anim._onNativeEnd, false);
  365. anim._node = null;
  366. }
  367. }
  368. };
  369. Y.Transition = Transition;
  370. Y.TransitionNative = Transition; // TODO: remove
  371. /**
  372. * Animate one or more css properties to a given value. Requires the "transition" module.
  373. * <pre>example usage:
  374. * Y.one('#demo').transition({
  375. * duration: 1, // in seconds, default is 0.5
  376. * easing: 'ease-out', // default is 'ease'
  377. * delay: '1', // delay start for 1 second, default is 0
  378. *
  379. * height: '10px',
  380. * width: '10px',
  381. *
  382. * opacity: { // per property
  383. * value: 0,
  384. * duration: 2,
  385. * delay: 2,
  386. * easing: 'ease-in'
  387. * }
  388. * });
  389. * </pre>
  390. * @for Node
  391. * @method transition
  392. * @param {Object} config An object containing one or more style properties, a duration and an easing.
  393. * @param {Function} callback A function to run after the transition has completed.
  394. * @chainable
  395. */
  396. Y.Node.prototype.transition = function(name, config, callback) {
  397. var
  398. transitionAttrs = Transition._nodeAttrs[Y.stamp(this._node)],
  399. anim = (transitionAttrs) ? transitionAttrs.transition || null : null,
  400. fxConfig,
  401. prop;
  402. if (typeof name === 'string') { // named effect, pull config from registry
  403. if (typeof config === 'function') {
  404. callback = config;
  405. config = null;
  406. }
  407. fxConfig = Transition.fx[name];
  408. if (config && typeof config !== 'boolean') {
  409. config = Y.clone(config);
  410. for (prop in fxConfig) {
  411. if (fxConfig.hasOwnProperty(prop)) {
  412. if (! (prop in config)) {
  413. config[prop] = fxConfig[prop];
  414. }
  415. }
  416. }
  417. } else {
  418. config = fxConfig;
  419. }
  420. } else { // name is a config, config is a callback or undefined
  421. callback = config;
  422. config = name;
  423. }
  424. if (anim && !anim._running) {
  425. anim.init(this, config);
  426. } else {
  427. anim = new Transition(this._node, config);
  428. }
  429. anim.run(callback);
  430. return this;
  431. };
  432. Y.Node.prototype.show = function(name, config, callback) {
  433. this._show(); // show prior to transition
  434. if (name && Y.Transition) {
  435. if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
  436. if (typeof config === 'function') {
  437. callback = config;
  438. config = name;
  439. }
  440. name = Transition.SHOW_TRANSITION;
  441. }
  442. this.transition(name, config, callback);
  443. }
  444. return this;
  445. };
  446. var _wrapCallBack = function(anim, fn, callback) {
  447. return function() {
  448. if (fn) {
  449. fn.call(anim);
  450. }
  451. if (callback) {
  452. callback.apply(anim._node, arguments);
  453. }
  454. };
  455. };
  456. Y.Node.prototype.hide = function(name, config, callback) {
  457. if (name && Y.Transition) {
  458. if (typeof config === 'function') {
  459. callback = config;
  460. config = null;
  461. }
  462. callback = _wrapCallBack(this, this._hide, callback); // wrap with existing callback
  463. if (typeof name !== 'string' && !name.push) { // named effect or array of effects supercedes default
  464. if (typeof config === 'function') {
  465. callback = config;
  466. config = name;
  467. }
  468. name = Transition.HIDE_TRANSITION;
  469. }
  470. this.transition(name, config, callback);
  471. } else {
  472. this._hide();
  473. }
  474. return this;
  475. };
  476. /**
  477. * Animate one or more css properties to a given value. Requires the "transition" module.
  478. * <pre>example usage:
  479. * Y.all('.demo').transition({
  480. * duration: 1, // in seconds, default is 0.5
  481. * easing: 'ease-out', // default is 'ease'
  482. * delay: '1', // delay start for 1 second, default is 0
  483. *
  484. * height: '10px',
  485. * width: '10px',
  486. *
  487. * opacity: { // per property
  488. * value: 0,
  489. * duration: 2,
  490. * delay: 2,
  491. * easing: 'ease-in'
  492. * }
  493. * });
  494. * </pre>
  495. * @for NodeList
  496. * @method transition
  497. * @param {Object} config An object containing one or more style properties, a duration and an easing.
  498. * @param {Function} callback A function to run after the transition has completed. The callback fires
  499. * once per item in the NodeList.
  500. * @chainable
  501. */
  502. Y.NodeList.prototype.transition = function(config, callback) {
  503. var nodes = this._nodes,
  504. i = 0,
  505. node;
  506. while ((node = nodes[i++])) {
  507. Y.one(node).transition(config, callback);
  508. }
  509. return this;
  510. };
  511. Y.Node.prototype.toggleView = function(name, on, callback) {
  512. this._toggles = this._toggles || [];
  513. callback = arguments[arguments.length - 1];
  514. if (typeof name == 'boolean') { // no transition, just toggle
  515. on = name;
  516. name = null;
  517. }
  518. name = name || Y.Transition.DEFAULT_TOGGLE;
  519. if (typeof on == 'undefined' && name in this._toggles) { // reverse current toggle
  520. on = ! this._toggles[name];
  521. }
  522. on = (on) ? 1 : 0;
  523. if (on) {
  524. this._show();
  525. } else {
  526. callback = _wrapCallBack(this, this._hide, callback);
  527. }
  528. this._toggles[name] = on;
  529. this.transition(Y.Transition.toggles[name][on], callback);
  530. return this;
  531. };
  532. Y.NodeList.prototype.toggleView = function(name, on, callback) {
  533. var nodes = this._nodes,
  534. i = 0,
  535. node;
  536. while ((node = nodes[i++])) {
  537. Y.one(node).toggleView(name, on, callback);
  538. }
  539. return this;
  540. };
  541. Y.mix(Transition.fx, {
  542. fadeOut: {
  543. opacity: 0,
  544. duration: 0.5,
  545. easing: 'ease-out'
  546. },
  547. fadeIn: {
  548. opacity: 1,
  549. duration: 0.5,
  550. easing: 'ease-in'
  551. },
  552. sizeOut: {
  553. height: 0,
  554. width: 0,
  555. duration: 0.75,
  556. easing: 'ease-out'
  557. },
  558. sizeIn: {
  559. height: function(node) {
  560. return node.get('scrollHeight') + 'px';
  561. },
  562. width: function(node) {
  563. return node.get('scrollWidth') + 'px';
  564. },
  565. duration: 0.5,
  566. easing: 'ease-in',
  567. on: {
  568. start: function() {
  569. var overflow = this.getStyle('overflow');
  570. if (overflow !== 'hidden') { // enable scrollHeight/Width
  571. this.setStyle('overflow', 'hidden');
  572. this._transitionOverflow = overflow;
  573. }
  574. },
  575. end: function() {
  576. if (this._transitionOverflow) { // revert overridden value
  577. this.setStyle('overflow', this._transitionOverflow);
  578. delete this._transitionOverflow;
  579. }
  580. }
  581. }
  582. }
  583. });
  584. Y.mix(Transition.toggles, {
  585. size: ['sizeOut', 'sizeIn'],
  586. fade: ['fadeOut', 'fadeIn']
  587. });
  588. Transition.DEFAULT_TOGGLE = 'fade';
  589. }, '3.4.0' ,{requires:['node-style']});