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.

831 lines
26 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*
  2. * # Semantic - Transition
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Copyright 2014 Contributor
  7. * Released under the MIT license
  8. * http://opensource.org/licenses/MIT
  9. *
  10. */
  11. ;(function ( $, window, document, undefined ) {
  12. "use strict";
  13. $.fn.transition = function() {
  14. var
  15. $allModules = $(this),
  16. moduleSelector = $allModules.selector || '',
  17. time = new Date().getTime(),
  18. performance = [],
  19. moduleArguments = arguments,
  20. query = moduleArguments[0],
  21. queryArguments = [].slice.call(arguments, 1),
  22. methodInvoked = (typeof query === 'string'),
  23. requestAnimationFrame = window.requestAnimationFrame
  24. || window.mozRequestAnimationFrame
  25. || window.webkitRequestAnimationFrame
  26. || window.msRequestAnimationFrame
  27. || function(callback) { setTimeout(callback, 0); },
  28. returnedValue
  29. ;
  30. $allModules
  31. .each(function() {
  32. var
  33. $module = $(this),
  34. element = this,
  35. // set at run time
  36. settings,
  37. instance,
  38. error,
  39. className,
  40. metadata,
  41. animationEnd,
  42. animationName,
  43. namespace,
  44. moduleNamespace,
  45. module
  46. ;
  47. module = {
  48. initialize: function() {
  49. // get settings
  50. settings = module.get.settings.apply(element, moduleArguments);
  51. module.verbose('Converted arguments into settings object', settings);
  52. // set shortcuts
  53. error = settings.error;
  54. className = settings.className;
  55. namespace = settings.namespace;
  56. metadata = settings.metadata;
  57. moduleNamespace = 'module-' + namespace;
  58. animationEnd = module.get.animationEvent();
  59. animationName = module.get.animationName();
  60. instance = $module.data(moduleNamespace) || module;
  61. if(methodInvoked) {
  62. methodInvoked = module.invoke(query);
  63. }
  64. // no internal method was found matching query or query not made
  65. if(methodInvoked === false) {
  66. module.animate();
  67. module.instantiate();
  68. }
  69. },
  70. instantiate: function() {
  71. module.verbose('Storing instance of module', module);
  72. $module
  73. .data(moduleNamespace, instance)
  74. ;
  75. },
  76. destroy: function() {
  77. module.verbose('Destroying previous module for', element);
  78. $module
  79. .removeData(moduleNamespace)
  80. ;
  81. },
  82. refresh: function() {
  83. module.verbose('Refreshing display type on next animation');
  84. delete instance.displayType;
  85. },
  86. forceRepaint: function() {
  87. module.verbose('Forcing element repaint');
  88. var
  89. $parentElement = $module.parent(),
  90. $nextElement = $module.next()
  91. ;
  92. if($nextElement.size() === 0) {
  93. $module.detach().appendTo($parentElement);
  94. }
  95. else {
  96. $module.detach().insertBefore($nextElement);
  97. }
  98. },
  99. repaint: function() {
  100. module.verbose('Repainting element');
  101. var
  102. fakeAssignment = element.offsetWidth
  103. ;
  104. },
  105. animate: function(overrideSettings) {
  106. settings = overrideSettings || settings;
  107. if(!module.is.supported()) {
  108. module.error(error.support);
  109. return false;
  110. }
  111. module.debug('Preparing animation', settings.animation);
  112. if(module.is.animating() && settings.queue) {
  113. if(!settings.allowRepeats && module.has.direction() && module.is.occuring() && instance.queuing !== true) {
  114. module.error(error.repeated);
  115. }
  116. else {
  117. module.queue(settings.animation);
  118. }
  119. return false;
  120. }
  121. if(module.can.animate) {
  122. module.set.animating(settings.animation);
  123. }
  124. else {
  125. module.error(error.noAnimation, settings.animation);
  126. }
  127. },
  128. reset: function() {
  129. module.debug('Resetting animation to beginning conditions');
  130. $module.off(animationEnd);
  131. module.restore.conditions();
  132. module.hide();
  133. module.remove.animating();
  134. },
  135. queue: function(animation) {
  136. module.debug('Queueing animation of', animation);
  137. instance.queuing = true;
  138. $module
  139. .one(animationEnd, function() {
  140. instance.queuing = false;
  141. module.repaint();
  142. module.animate.apply(this, settings);
  143. })
  144. ;
  145. },
  146. complete: function () {
  147. module.verbose('CSS animation complete', settings.animation);
  148. if(!module.is.looping()) {
  149. if( module.is.outward() ) {
  150. module.verbose('Animation is outward, hiding element');
  151. module.restore.conditions();
  152. module.remove.display();
  153. module.hide();
  154. $.proxy(settings.onHide, this)();
  155. }
  156. else if( module.is.inward() ) {
  157. module.verbose('Animation is outward, showing element');
  158. module.restore.conditions();
  159. module.show();
  160. $.proxy(settings.onShow, this)();
  161. }
  162. else {
  163. module.restore.conditions();
  164. }
  165. module.remove.duration();
  166. module.remove.animating();
  167. }
  168. $.proxy(settings.complete, this)();
  169. },
  170. has: {
  171. direction: function(animation) {
  172. animation = animation || settings.animation;
  173. if( animation.search(className.inward) !== -1 || animation.search(className.outward) !== -1) {
  174. module.debug('Direction already set in animation');
  175. return true;
  176. }
  177. return false;
  178. }
  179. },
  180. set: {
  181. animating: function(animation) {
  182. animation = animation || settings.animation;
  183. module.save.conditions();
  184. if(module.can.transition() && !module.has.direction()) {
  185. module.set.direction();
  186. }
  187. module.remove.hidden();
  188. module.set.display();
  189. $module
  190. .addClass(className.animating)
  191. .addClass(className.transition)
  192. .addClass(animation)
  193. .one(animationEnd, module.complete)
  194. ;
  195. module.set.duration(settings.duration);
  196. module.debug('Starting tween', settings.animation, $module.attr('class'));
  197. },
  198. display: function() {
  199. var
  200. style = $module.css('style'),
  201. displayType = module.get.displayType(),
  202. overrideStyle = style + ';display: ' + displayType + ' !important;'
  203. ;
  204. if(displayType !== 'block') {
  205. module.verbose('Setting final visibility to', displayType);
  206. $module
  207. .attr('style', overrideStyle)
  208. ;
  209. }
  210. },
  211. direction: function() {
  212. if($module.is(':visible')) {
  213. module.debug('Automatically determining the direction of animation', 'Outward');
  214. $module
  215. .removeClass(className.inward)
  216. .addClass(className.outward)
  217. ;
  218. }
  219. else {
  220. module.debug('Automatically determining the direction of animation', 'Inward');
  221. $module
  222. .removeClass(className.outward)
  223. .addClass(className.inward)
  224. ;
  225. }
  226. },
  227. looping: function() {
  228. module.debug('Transition set to loop');
  229. $module
  230. .addClass(className.looping)
  231. ;
  232. },
  233. duration: function(duration) {
  234. duration = duration || settings.duration;
  235. duration = (typeof duration == 'number')
  236. ? duration + 'ms'
  237. : duration
  238. ;
  239. module.verbose('Setting animation duration', duration);
  240. $module
  241. .css({
  242. '-webkit-animation-duration': duration,
  243. '-moz-animation-duration': duration,
  244. '-ms-animation-duration': duration,
  245. '-o-animation-duration': duration,
  246. 'animation-duration': duration
  247. })
  248. ;
  249. },
  250. hidden: function() {
  251. $module
  252. .addClass(className.transition)
  253. .addClass(className.hidden)
  254. ;
  255. requestAnimationFrame(function() {
  256. if($module.css('display') !== 'none') {
  257. module.verbose('Overriding default display to hide element');
  258. $module
  259. .css('display', 'none')
  260. ;
  261. }
  262. });
  263. },
  264. visible: function() {
  265. $module
  266. .addClass(className.transition)
  267. .addClass(className.visible)
  268. ;
  269. }
  270. },
  271. save: {
  272. displayType: function(displayType) {
  273. instance.displayType = displayType;
  274. },
  275. transitionExists: function(animation, exists) {
  276. $.fn.transition.exists[animation] = exists;
  277. module.verbose('Saving existence of transition', animation, exists);
  278. },
  279. conditions: function() {
  280. instance.cache = {
  281. className : $module.attr('class'),
  282. style : $module.attr('style')
  283. };
  284. module.verbose('Saving original attributes', instance.cache);
  285. }
  286. },
  287. restore: {
  288. conditions: function() {
  289. if(instance.cache === undefined) {
  290. return false;
  291. }
  292. if(instance.cache.className) {
  293. $module.attr('class', instance.cache.className);
  294. }
  295. else {
  296. $module.removeAttr('class');
  297. }
  298. if(instance.cache.style) {
  299. $module.attr('style', instance.cache.style);
  300. }
  301. else {
  302. if(module.get.displayType() === 'block') {
  303. $module.removeAttr('style');
  304. }
  305. }
  306. if(module.is.looping()) {
  307. module.remove.looping();
  308. }
  309. module.verbose('Restoring original attributes', instance.cache);
  310. }
  311. },
  312. remove: {
  313. animating: function() {
  314. $module.removeClass(className.animating);
  315. },
  316. display: function() {
  317. if(instance.displayType !== undefined) {
  318. $module.css('display', '');
  319. }
  320. },
  321. duration: function() {
  322. $module
  323. .css({
  324. '-webkit-animation-duration' : '',
  325. '-moz-animation-duration' : '',
  326. '-ms-animation-duration' : '',
  327. '-o-animation-duration' : '',
  328. 'animation-duration' : ''
  329. })
  330. ;
  331. },
  332. hidden: function() {
  333. $module.removeClass(className.hidden);
  334. },
  335. visible: function() {
  336. $module.removeClass(className.visible);
  337. },
  338. looping: function() {
  339. module.debug('Transitions are no longer looping');
  340. $module
  341. .removeClass(className.looping)
  342. ;
  343. module.forceRepaint();
  344. },
  345. transition: function() {
  346. $module
  347. .removeClass(className.visible)
  348. .removeClass(className.hidden)
  349. ;
  350. }
  351. },
  352. get: {
  353. settings: function(animation, duration, complete) {
  354. // single settings object
  355. if(typeof animation == 'object') {
  356. return $.extend(true, {}, $.fn.transition.settings, animation);
  357. }
  358. // all arguments provided
  359. else if(typeof complete == 'function') {
  360. return $.extend({}, $.fn.transition.settings, {
  361. animation : animation,
  362. complete : complete,
  363. duration : duration
  364. });
  365. }
  366. // only duration provided
  367. else if(typeof duration == 'string' || typeof duration == 'number') {
  368. return $.extend({}, $.fn.transition.settings, {
  369. animation : animation,
  370. duration : duration
  371. });
  372. }
  373. // duration is actually settings object
  374. else if(typeof duration == 'object') {
  375. return $.extend({}, $.fn.transition.settings, duration, {
  376. animation : animation
  377. });
  378. }
  379. // duration is actually callback
  380. else if(typeof duration == 'function') {
  381. return $.extend({}, $.fn.transition.settings, {
  382. animation : animation,
  383. complete : duration
  384. });
  385. }
  386. // only animation provided
  387. else {
  388. return $.extend({}, $.fn.transition.settings, {
  389. animation : animation
  390. });
  391. }
  392. return $.fn.transition.settings;
  393. },
  394. displayType: function() {
  395. if(instance.displayType === undefined) {
  396. // create fake element to determine display state
  397. module.can.transition();
  398. }
  399. return instance.displayType;
  400. },
  401. transitionExists: function(animation) {
  402. return $.fn.transition.exists[animation];
  403. },
  404. animationName: function() {
  405. var
  406. element = document.createElement('div'),
  407. animations = {
  408. 'animation' :'animationName',
  409. 'OAnimation' :'oAnimationName',
  410. 'MozAnimation' :'mozAnimationName',
  411. 'WebkitAnimation' :'webkitAnimationName'
  412. },
  413. animation
  414. ;
  415. for(animation in animations){
  416. if( element.style[animation] !== undefined ){
  417. module.verbose('Determined animation vendor name property', animations[animation]);
  418. return animations[animation];
  419. }
  420. }
  421. return false;
  422. },
  423. animationEvent: function() {
  424. var
  425. element = document.createElement('div'),
  426. animations = {
  427. 'animation' :'animationend',
  428. 'OAnimation' :'oAnimationEnd',
  429. 'MozAnimation' :'mozAnimationEnd',
  430. 'WebkitAnimation' :'webkitAnimationEnd'
  431. },
  432. animation
  433. ;
  434. for(animation in animations){
  435. if( element.style[animation] !== undefined ){
  436. module.verbose('Determined animation vendor end event', animations[animation]);
  437. return animations[animation];
  438. }
  439. }
  440. return false;
  441. }
  442. },
  443. can: {
  444. animate: function() {
  445. if($module.css(settings.animation) !== 'none') {
  446. module.debug('CSS definition found', $module.css(settings.animation));
  447. return true;
  448. }
  449. else {
  450. module.debug('Unable to find css definition', $module.attr('class'));
  451. return false;
  452. }
  453. },
  454. transition: function() {
  455. var
  456. elementClass = $module.attr('class'),
  457. animation = settings.animation,
  458. transitionExists = module.get.transitionExists(settings.animation),
  459. $clone,
  460. currentAnimation,
  461. inAnimation,
  462. displayType
  463. ;
  464. if( transitionExists === undefined || instance.displayType === undefined) {
  465. module.verbose('Determining whether animation exists');
  466. $clone = $('<div>').addClass( elementClass ).appendTo($('body'));
  467. currentAnimation = $clone
  468. .removeClass(className.inward)
  469. .removeClass(className.outward)
  470. .addClass(className.animating)
  471. .addClass(className.transition)
  472. .addClass(animation)
  473. .css(animationName)
  474. ;
  475. inAnimation = $clone
  476. .addClass(className.inward)
  477. .css(animationName)
  478. ;
  479. displayType = $clone
  480. .attr('class', elementClass)
  481. .show()
  482. .css('display')
  483. ;
  484. module.verbose('Determining final display state', displayType);
  485. if(currentAnimation != inAnimation) {
  486. module.debug('Transition exists for animation', animation);
  487. transitionExists = true;
  488. }
  489. else {
  490. module.debug('Static animation found', animation, displayType);
  491. transitionExists = false;
  492. }
  493. $clone.remove();
  494. module.save.displayType(displayType);
  495. module.save.transitionExists(animation, transitionExists);
  496. }
  497. return transitionExists;
  498. }
  499. },
  500. is: {
  501. animating: function() {
  502. return $module.hasClass(className.animating);
  503. },
  504. inward: function() {
  505. return $module.hasClass(className.inward);
  506. },
  507. outward: function() {
  508. return $module.hasClass(className.outward);
  509. },
  510. looping: function() {
  511. return $module.hasClass(className.looping);
  512. },
  513. occuring: function(animation) {
  514. animation = animation || settings.animation;
  515. return ( $module.hasClass(animation) );
  516. },
  517. visible: function() {
  518. return $module.is(':visible');
  519. },
  520. supported: function() {
  521. return(animationName !== false && animationEnd !== false);
  522. }
  523. },
  524. hide: function() {
  525. module.verbose('Hiding element');
  526. module.remove.visible();
  527. module.set.hidden();
  528. module.repaint();
  529. },
  530. show: function(display) {
  531. module.verbose('Showing element', display);
  532. module.remove.hidden();
  533. module.set.visible();
  534. module.repaint();
  535. },
  536. start: function() {
  537. module.verbose('Starting animation');
  538. $module.removeClass(className.disabled);
  539. },
  540. stop: function() {
  541. module.debug('Stopping animation');
  542. $module.addClass(className.disabled);
  543. },
  544. toggle: function() {
  545. module.debug('Toggling play status');
  546. $module.toggleClass(className.disabled);
  547. },
  548. setting: function(name, value) {
  549. module.debug('Changing setting', name, value);
  550. if( $.isPlainObject(name) ) {
  551. $.extend(true, settings, name);
  552. }
  553. else if(value !== undefined) {
  554. settings[name] = value;
  555. }
  556. else {
  557. return settings[name];
  558. }
  559. },
  560. internal: function(name, value) {
  561. if( $.isPlainObject(name) ) {
  562. $.extend(true, module, name);
  563. }
  564. else if(value !== undefined) {
  565. module[name] = value;
  566. }
  567. else {
  568. return module[name];
  569. }
  570. },
  571. debug: function() {
  572. if(settings.debug) {
  573. if(settings.performance) {
  574. module.performance.log(arguments);
  575. }
  576. else {
  577. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  578. module.debug.apply(console, arguments);
  579. }
  580. }
  581. },
  582. verbose: function() {
  583. if(settings.verbose && settings.debug) {
  584. if(settings.performance) {
  585. module.performance.log(arguments);
  586. }
  587. else {
  588. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  589. module.verbose.apply(console, arguments);
  590. }
  591. }
  592. },
  593. error: function() {
  594. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  595. module.error.apply(console, arguments);
  596. },
  597. performance: {
  598. log: function(message) {
  599. var
  600. currentTime,
  601. executionTime,
  602. previousTime
  603. ;
  604. if(settings.performance) {
  605. currentTime = new Date().getTime();
  606. previousTime = time || currentTime;
  607. executionTime = currentTime - previousTime;
  608. time = currentTime;
  609. performance.push({
  610. 'Element' : element,
  611. 'Name' : message[0],
  612. 'Arguments' : [].slice.call(message, 1) || '',
  613. 'Execution Time' : executionTime
  614. });
  615. }
  616. clearTimeout(module.performance.timer);
  617. module.performance.timer = setTimeout(module.performance.display, 100);
  618. },
  619. display: function() {
  620. var
  621. title = settings.name + ':',
  622. totalTime = 0
  623. ;
  624. time = false;
  625. clearTimeout(module.performance.timer);
  626. $.each(performance, function(index, data) {
  627. totalTime += data['Execution Time'];
  628. });
  629. title += ' ' + totalTime + 'ms';
  630. if(moduleSelector) {
  631. title += ' \'' + moduleSelector + '\'';
  632. }
  633. if($allModules.size() > 1) {
  634. title += ' ' + '(' + $allModules.size() + ')';
  635. }
  636. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  637. console.groupCollapsed(title);
  638. if(console.table) {
  639. console.table(performance);
  640. }
  641. else {
  642. $.each(performance, function(index, data) {
  643. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  644. });
  645. }
  646. console.groupEnd();
  647. }
  648. performance = [];
  649. }
  650. },
  651. invoke: function(query, passedArguments, context) {
  652. var
  653. object = instance,
  654. maxDepth,
  655. found,
  656. response
  657. ;
  658. passedArguments = passedArguments || queryArguments;
  659. context = element || context;
  660. if(typeof query == 'string' && object !== undefined) {
  661. query = query.split(/[\. ]/);
  662. maxDepth = query.length - 1;
  663. $.each(query, function(depth, value) {
  664. var camelCaseValue = (depth != maxDepth)
  665. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  666. : query
  667. ;
  668. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  669. object = object[camelCaseValue];
  670. }
  671. else if( object[camelCaseValue] !== undefined ) {
  672. found = object[camelCaseValue];
  673. return false;
  674. }
  675. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  676. object = object[value];
  677. }
  678. else if( object[value] !== undefined ) {
  679. found = object[value];
  680. return false;
  681. }
  682. else {
  683. return false;
  684. }
  685. });
  686. }
  687. if ( $.isFunction( found ) ) {
  688. response = found.apply(context, passedArguments);
  689. }
  690. else if(found !== undefined) {
  691. response = found;
  692. }
  693. if($.isArray(returnedValue)) {
  694. returnedValue.push(response);
  695. }
  696. else if(returnedValue !== undefined) {
  697. returnedValue = [returnedValue, response];
  698. }
  699. else if(response !== undefined) {
  700. returnedValue = response;
  701. }
  702. return found || false;
  703. }
  704. };
  705. module.initialize();
  706. })
  707. ;
  708. return (returnedValue !== undefined)
  709. ? returnedValue
  710. : this
  711. ;
  712. };
  713. $.fn.transition.exists = {};
  714. $.fn.transition.settings = {
  715. // module info
  716. name : 'Transition',
  717. // debug content outputted to console
  718. debug : false,
  719. // verbose debug output
  720. verbose : true,
  721. // performance data output
  722. performance : true,
  723. // event namespace
  724. namespace : 'transition',
  725. // animation complete event
  726. complete : function() {},
  727. onShow : function() {},
  728. onHide : function() {},
  729. // whether animation can occur twice in a row
  730. allowRepeats : false,
  731. // animation duration
  732. animation : 'fade',
  733. duration : '500ms',
  734. // new animations will occur after previous ones
  735. queue : true,
  736. className : {
  737. animating : 'animating',
  738. disabled : 'disabled',
  739. hidden : 'hidden',
  740. inward : 'in',
  741. loading : 'loading',
  742. looping : 'looping',
  743. outward : 'out',
  744. transition : 'transition',
  745. visible : 'visible'
  746. },
  747. // possible errors
  748. error: {
  749. noAnimation : 'There is no css animation matching the one you specified.',
  750. repeated : 'That animation is already occurring, cancelling repeated animation',
  751. method : 'The method you called is not defined',
  752. support : 'This browser does not support CSS animations'
  753. }
  754. };
  755. })( jQuery, window , document );