transition.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. var nextTick = function () {
  34. return new Promise(function (resolve) {
  35. return setTimeout(resolve, 1000 / 30);
  36. });
  37. };
  38. exports.transition = function (showDefaultValue) {
  39. return Behavior({
  40. properties: {
  41. customStyle: String,
  42. // @ts-ignore
  43. show: {
  44. type: Boolean,
  45. value: showDefaultValue,
  46. observer: 'observeShow',
  47. },
  48. // @ts-ignore
  49. duration: {
  50. type: null,
  51. value: 300,
  52. observer: 'observeDuration',
  53. },
  54. name: {
  55. type: String,
  56. value: 'fade',
  57. },
  58. },
  59. data: {
  60. type: '',
  61. inited: false,
  62. display: false,
  63. },
  64. methods: {
  65. observeShow: function (value, old) {
  66. if (value === old) {
  67. return;
  68. }
  69. value ? this.enter() : this.leave();
  70. },
  71. enter: function () {
  72. var _this = this;
  73. var _a = this.data,
  74. duration = _a.duration,
  75. name = _a.name;
  76. var classNames = getClassNames(name);
  77. var currentDuration = utils_1.isObj(duration)
  78. ? duration.enter
  79. : duration;
  80. this.status = 'enter';
  81. this.$emit('before-enter');
  82. Promise.resolve()
  83. .then(nextTick)
  84. .then(function () {
  85. _this.checkStatus('enter');
  86. _this.$emit('enter');
  87. _this.setData({
  88. inited: true,
  89. display: true,
  90. classes: classNames.enter,
  91. currentDuration: currentDuration,
  92. });
  93. })
  94. .then(nextTick)
  95. .then(function () {
  96. _this.checkStatus('enter');
  97. _this.transitionEnded = false;
  98. _this.setData({
  99. classes: classNames['enter-to'],
  100. });
  101. })
  102. .catch(function () {});
  103. },
  104. leave: function () {
  105. var _this = this;
  106. if (!this.data.display) {
  107. return;
  108. }
  109. var _a = this.data,
  110. duration = _a.duration,
  111. name = _a.name;
  112. var classNames = getClassNames(name);
  113. var currentDuration = utils_1.isObj(duration)
  114. ? duration.leave
  115. : duration;
  116. this.status = 'leave';
  117. this.$emit('before-leave');
  118. Promise.resolve()
  119. .then(nextTick)
  120. .then(function () {
  121. _this.checkStatus('leave');
  122. _this.$emit('leave');
  123. _this.setData({
  124. classes: classNames.leave,
  125. currentDuration: currentDuration,
  126. });
  127. })
  128. .then(nextTick)
  129. .then(function () {
  130. _this.checkStatus('leave');
  131. _this.transitionEnded = false;
  132. setTimeout(function () {
  133. return _this.onTransitionEnd();
  134. }, currentDuration);
  135. _this.setData({
  136. classes: classNames['leave-to'],
  137. });
  138. })
  139. .catch(function () {});
  140. },
  141. checkStatus: function (status) {
  142. if (status !== this.status) {
  143. throw new Error('incongruent status: ' + status);
  144. }
  145. },
  146. onTransitionEnd: function () {
  147. if (this.transitionEnded) {
  148. return;
  149. }
  150. this.transitionEnded = true;
  151. this.$emit('after-' + this.status);
  152. var _a = this.data,
  153. show = _a.show,
  154. display = _a.display;
  155. if (!show && display) {
  156. this.setData({ display: false });
  157. }
  158. },
  159. },
  160. });
  161. };