123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import { requestAnimationFrame } from '../common/utils';
- import { isObj } from '../common/validator';
- const getClassNames = (name) => ({
- enter: `van-${name}-enter van-${name}-enter-active enter-class enter-active-class`,
- 'enter-to': `van-${name}-enter-to van-${name}-enter-active enter-to-class enter-active-class`,
- leave: `van-${name}-leave van-${name}-leave-active leave-class leave-active-class`,
- 'leave-to': `van-${name}-leave-to van-${name}-leave-active leave-to-class leave-active-class`,
- });
- export function transition(showDefaultValue) {
- return Behavior({
- properties: {
- customStyle: String,
-
- show: {
- type: Boolean,
- value: showDefaultValue,
- observer: 'observeShow',
- },
-
- duration: {
- type: null,
- value: 300,
- },
- name: {
- type: String,
- value: 'fade',
- },
- },
- data: {
- type: '',
- inited: false,
- display: false,
- },
- ready() {
- if (this.data.show === true) {
- this.observeShow(true, false);
- }
- },
- methods: {
- observeShow(value, old) {
- if (value === old) {
- return;
- }
- value ? this.enter() : this.leave();
- },
- enter() {
- if (this.enterFinishedPromise)
- return;
- this.enterFinishedPromise = new Promise((resolve) => {
- const { duration, name } = this.data;
- const classNames = getClassNames(name);
- const currentDuration = isObj(duration) ? duration.enter : duration;
- if (this.status === 'enter') {
- return;
- }
- this.status = 'enter';
- this.$emit('before-enter');
- requestAnimationFrame(() => {
- if (this.status !== 'enter') {
- return;
- }
- this.$emit('enter');
- this.setData({
- inited: true,
- display: true,
- classes: classNames.enter,
- currentDuration,
- });
- requestAnimationFrame(() => {
- if (this.status !== 'enter') {
- return;
- }
- this.transitionEnded = false;
- this.setData({ classes: classNames['enter-to'] });
- resolve();
- });
- });
- });
- },
- leave() {
- if (!this.enterFinishedPromise)
- return;
- this.enterFinishedPromise.then(() => {
- if (!this.data.display) {
- return;
- }
- const { duration, name } = this.data;
- const classNames = getClassNames(name);
- const currentDuration = isObj(duration) ? duration.leave : duration;
- this.status = 'leave';
- this.$emit('before-leave');
- requestAnimationFrame(() => {
- if (this.status !== 'leave') {
- return;
- }
- this.$emit('leave');
- this.setData({
- classes: classNames.leave,
- currentDuration,
- });
- requestAnimationFrame(() => {
- if (this.status !== 'leave') {
- return;
- }
- this.transitionEnded = false;
- setTimeout(() => {
- this.onTransitionEnd();
- this.enterFinishedPromise = null;
- }, currentDuration);
- this.setData({ classes: classNames['leave-to'] });
- });
- });
- });
- },
- onTransitionEnd() {
- if (this.transitionEnded) {
- return;
- }
- this.transitionEnded = true;
- this.$emit(`after-${this.status}`);
- const { show, display } = this.data;
- if (!show && display) {
- this.setData({ display: false });
- }
- },
- },
- });
- }
|