transition.js 5.4 KB

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