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

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