transition.js 5.5 KB

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