entrance.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Durandal 2.1.0 Copyright (c) 2012 Blue Spire Consulting, Inc. All Rights Reserved.
  3. * Available via the MIT license.
  4. * see: http://durandaljs.com or https://github.com/BlueSpire/Durandal for details.
  5. */
  6. /**
  7. * The entrance transition module.
  8. * @module entrance
  9. * @requires system
  10. * @requires composition
  11. * @requires jquery
  12. */
  13. define(['durandal/system', 'durandal/composition', 'jquery'], function(system, composition, $) {
  14. var fadeOutDuration = 100;
  15. var endValues = {
  16. left: '0px',
  17. opacity: 1
  18. };
  19. var clearValues = {
  20. left: '',
  21. top: '',
  22. right: '',
  23. bottom:'',
  24. position:'',
  25. opacity: ''
  26. };
  27. var isIE = navigator.userAgent.match(/Trident/) || navigator.userAgent.match(/MSIE/);
  28. var animation = false,
  29. domPrefixes = 'Webkit Moz O ms Khtml'.split(' '),
  30. elm = document.createElement('div');
  31. if(elm.style.animationName !== undefined) {
  32. animation = true;
  33. }
  34. if(!animation) {
  35. for(var i = 0; i < domPrefixes.length; i++) {
  36. if(elm.style[domPrefixes[i] + 'AnimationName'] !== undefined) {
  37. animation = true;
  38. break;
  39. }
  40. }
  41. }
  42. if(animation) {
  43. if(isIE){
  44. system.log('Using CSS3/jQuery mixed animations.');
  45. }else{
  46. system.log('Using CSS3 animations.');
  47. }
  48. } else {
  49. system.log('Using jQuery animations.');
  50. }
  51. function removeAnimationClasses(ele, fadeOnly){
  52. ele.classList.remove(fadeOnly ? 'entrance-in-fade' : 'entrance-in');
  53. ele.classList.remove('entrance-out');
  54. }
  55. /**
  56. * @class EntranceModule
  57. * @constructor
  58. */
  59. var entrance = function(context) {
  60. return system.defer(function(dfd) {
  61. function endTransition() {
  62. dfd.resolve();
  63. }
  64. function scrollIfNeeded() {
  65. if (!context.keepScrollPosition) {
  66. $(document).scrollTop(0);
  67. }
  68. }
  69. if (!context.child) {
  70. $(context.activeView).fadeOut(fadeOutDuration, endTransition);
  71. } else {
  72. var duration = context.duration || 500;
  73. var $child = $(context.child);
  74. var fadeOnly = !!context.fadeOnly;
  75. var startValues = {
  76. display: 'block',
  77. opacity: 0,
  78. position: 'absolute',
  79. left: fadeOnly || animation ? '0px' : '20px',
  80. right: 0,
  81. top: 0,
  82. bottom: 0
  83. };
  84. function startTransition() {
  85. scrollIfNeeded();
  86. context.triggerAttach();
  87. if (animation) {
  88. removeAnimationClasses(context.child, fadeOnly);
  89. context.child.classList.add(fadeOnly ? 'entrance-in-fade' : 'entrance-in');
  90. setTimeout(function () {
  91. removeAnimationClasses(context.child, fadeOnly);
  92. if(context.activeView){
  93. removeAnimationClasses(context.activeView, fadeOnly);
  94. }
  95. $child.css(clearValues);
  96. endTransition();
  97. }, duration);
  98. } else {
  99. $child.animate(endValues, {
  100. duration: duration,
  101. easing: 'swing',
  102. always: function() {
  103. $child.css(clearValues);
  104. endTransition();
  105. }
  106. });
  107. }
  108. }
  109. $child.css(startValues);
  110. if(context.activeView) {
  111. if (animation && !isIE) {
  112. removeAnimationClasses(context.activeView, fadeOnly);
  113. context.activeView.classList.add('entrance-out');
  114. setTimeout(startTransition, fadeOutDuration);
  115. } else {
  116. $(context.activeView).fadeOut({ duration: fadeOutDuration, always: startTransition });
  117. }
  118. } else {
  119. startTransition();
  120. }
  121. }
  122. }).promise();
  123. };
  124. return entrance;
  125. });