You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.5 KiB

3 years ago
// @ts-nocheck
import { requestAnimationFrame } from '../common/utils';
import { isObj } from '../common/validator';
3 years ago
const getClassNames = name => ({
3 years ago
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`,
3 years ago
'leave-to': `van-${name}-leave-to van-${name}-leave-active leave-to-class leave-active-class`
3 years ago
});
3 years ago
export default function () {
return {
data() {
return {
type: '',
inited: false,
display: false
};
},
props: {
3 years ago
customStyle: String,
// @ts-ignore
show: {
type: Boolean,
3 years ago
default: showDefaultValue
3 years ago
},
// @ts-ignore
duration: {
type: null,
3 years ago
default: 300
3 years ago
},
name: {
type: String,
3 years ago
default: 'fade'
}
3 years ago
},
3 years ago
watch: {
show: {
handler: 'observeShow',
immediate: true
},
duration: {
handler: 'observeDuration',
immediate: true
}
3 years ago
},
3 years ago
mounted() {
if (this.show === true) {
3 years ago
this.observeShow(true, false);
}
},
3 years ago
3 years ago
methods: {
observeShow(value, old) {
if (value === old) {
return;
}
3 years ago
3 years ago
value ? this.enter() : this.leave();
},
3 years ago
3 years ago
enter() {
3 years ago
const {
duration,
name
} = this;
3 years ago
const classNames = getClassNames(name);
const currentDuration = isObj(duration) ? duration.enter : duration;
this.status = 'enter';
this.$emit('before-enter');
requestAnimationFrame(() => {
if (this.status !== 'enter') {
return;
}
3 years ago
3 years ago
this.$emit('enter');
this.setData({
inited: true,
display: true,
classes: classNames.enter,
3 years ago
currentDuration
3 years ago
});
requestAnimationFrame(() => {
if (this.status !== 'enter') {
return;
}
3 years ago
3 years ago
this.transitionEnded = false;
3 years ago
this.setData({
classes: classNames['enter-to']
});
3 years ago
});
});
},
3 years ago
3 years ago
leave() {
3 years ago
if (!this.display) {
3 years ago
return;
}
3 years ago
const {
duration,
name
} = this;
3 years ago
const classNames = getClassNames(name);
const currentDuration = isObj(duration) ? duration.leave : duration;
this.status = 'leave';
this.$emit('before-leave');
requestAnimationFrame(() => {
if (this.status !== 'leave') {
return;
}
3 years ago
3 years ago
this.$emit('leave');
this.setData({
classes: classNames.leave,
3 years ago
currentDuration
3 years ago
});
requestAnimationFrame(() => {
if (this.status !== 'leave') {
return;
}
3 years ago
3 years ago
this.transitionEnded = false;
setTimeout(() => this.onTransitionEnd(), currentDuration);
3 years ago
this.setData({
classes: classNames['leave-to']
});
3 years ago
});
});
},
3 years ago
3 years ago
onTransitionEnd() {
if (this.transitionEnded) {
return;
}
3 years ago
3 years ago
this.transitionEnded = true;
this.$emit(`after-${this.status}`);
3 years ago
const {
show,
display
} = this;
3 years ago
if (!show && display) {
3 years ago
this.setData({
display: false
});
3 years ago
}
3 years ago
}
}
};
}