transition.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. exports.transition = void 0;
  4. var utils_1 = require('../common/utils');
  5. var getClassNames = function (name) {
  6. return {
  7. enter:
  8. 'van-' +
  9. name +
  10. '-enter van-' +
  11. name +
  12. '-enter-active enter-class enter-active-class',
  13. 'enter-to':
  14. 'van-' +
  15. name +
  16. '-enter-to van-' +
  17. name +
  18. '-enter-active enter-to-class enter-active-class',
  19. leave:
  20. 'van-' +
  21. name +
  22. '-leave van-' +
  23. name +
  24. '-leave-active leave-class leave-active-class',
  25. 'leave-to':
  26. 'van-' +
  27. name +
  28. '-leave-to van-' +
  29. name +
  30. '-leave-active leave-to-class leave-active-class',
  31. };
  32. };
  33. exports.transition = function (showDefaultValue) {
  34. return Behavior({
  35. properties: {
  36. customStyle: String,
  37. // @ts-ignore
  38. show: {
  39. type: Boolean,
  40. value: showDefaultValue,
  41. observer: 'observeShow',
  42. },
  43. // @ts-ignore
  44. duration: {
  45. type: null,
  46. value: 300,
  47. observer: 'observeDuration',
  48. },
  49. name: {
  50. type: String,
  51. value: 'fade',
  52. },
  53. },
  54. data: {
  55. type: '',
  56. inited: false,
  57. display: false,
  58. },
  59. methods: {
  60. observeShow: function (value, old) {
  61. if (value === old) {
  62. return;
  63. }
  64. value ? this.enter() : this.leave();
  65. },
  66. enter: function () {
  67. var _this = this;
  68. var _a = this.data,
  69. duration = _a.duration,
  70. name = _a.name;
  71. var classNames = getClassNames(name);
  72. var currentDuration = utils_1.isObj(duration)
  73. ? duration.enter
  74. : duration;
  75. this.status = 'enter';
  76. this.$emit('before-enter');
  77. utils_1.requestAnimationFrame(function () {
  78. _this.checkStatus('enter');
  79. _this.$emit('enter');
  80. _this.setData({
  81. inited: true,
  82. display: true,
  83. classes: classNames.enter,
  84. currentDuration: currentDuration,
  85. });
  86. utils_1.requestAnimationFrame(function () {
  87. _this.checkStatus('enter');
  88. _this.transitionEnded = false;
  89. _this.setData({ classes: classNames['enter-to'] });
  90. });
  91. });
  92. },
  93. leave: function () {
  94. var _this = this;
  95. if (!this.data.display) {
  96. return;
  97. }
  98. var _a = this.data,
  99. duration = _a.duration,
  100. name = _a.name;
  101. var classNames = getClassNames(name);
  102. var currentDuration = utils_1.isObj(duration)
  103. ? duration.leave
  104. : duration;
  105. this.status = 'leave';
  106. this.$emit('before-leave');
  107. utils_1.requestAnimationFrame(function () {
  108. _this.checkStatus('leave');
  109. _this.$emit('leave');
  110. _this.setData({
  111. classes: classNames.leave,
  112. currentDuration: currentDuration,
  113. });
  114. utils_1.requestAnimationFrame(function () {
  115. _this.checkStatus('leave');
  116. _this.transitionEnded = false;
  117. setTimeout(function () {
  118. return _this.onTransitionEnd();
  119. }, currentDuration);
  120. _this.setData({ classes: classNames['leave-to'] });
  121. });
  122. });
  123. },
  124. checkStatus: function (status) {
  125. if (status !== this.status) {
  126. throw new Error('incongruent status: ' + status);
  127. }
  128. },
  129. onTransitionEnd: function () {
  130. if (this.transitionEnded) {
  131. return;
  132. }
  133. this.transitionEnded = true;
  134. this.$emit('after-' + this.status);
  135. var _a = this.data,
  136. show = _a.show,
  137. display = _a.display;
  138. if (!show && display) {
  139. this.setData({ display: false });
  140. }
  141. },
  142. },
  143. });
  144. };