transition.js 3.9 KB

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