transition.js 4.6 KB

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