[improvement] Swipe: optimize swipe event binding (#3652)

This commit is contained in:
neverland 2019-06-26 22:06:09 +08:00 committed by GitHub
parent e109b6d9c9
commit c0e82773ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,11 +1,23 @@
import { use } from '../utils'; import { use } from '../utils';
import { on, off, preventDefault } from '../utils/dom/event'; import { preventDefault } from '../utils/dom/event';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { BindEventMixin } from '../mixins/bind-event';
const [sfc, bem] = use('swipe'); const [sfc, bem] = use('swipe');
export default sfc({ export default sfc({
mixins: [TouchMixin], mixins: [
TouchMixin,
BindEventMixin(function (bind, isBind) {
bind(window, 'resize', this.onResize, true);
if (isBind) {
this.initialize();
} else {
this.clear();
}
})
],
props: { props: {
width: Number, width: Number,
@ -45,30 +57,6 @@ export default sfc({
}; };
}, },
mounted() {
this.initialize();
if (!this.$isServer) {
on(window, 'resize', this.onResize, true);
}
},
activated() {
if (this.rendered) {
this.initialize();
}
this.rendered = true;
},
destroyed() {
this.clear();
if (!this.$isServer) {
off(window, 'resize', this.onResize, true);
}
},
watch: { watch: {
swipes() { swipes() {
this.initialize(); this.initialize();
@ -265,15 +253,18 @@ export default sfc({
}, 30); }, 30);
}, autoplay); }, autoplay);
} }
}
}, },
render(h) { renderIndicator() {
const { count, activeIndicator } = this; const { count, activeIndicator } = this;
const slot = this.slots('indicator');
const Indicator = if (slot) {
this.slots('indicator') || return slot;
(this.showIndicators && count > 1 && ( }
if (this.showIndicators && count > 1) {
return (
<div class={bem('indicators', { vertical: this.vertical })}> <div class={bem('indicators', { vertical: this.vertical })}>
{Array(...Array(count)).map((empty, index) => ( {Array(...Array(count)).map((empty, index) => (
<i <i
@ -282,8 +273,12 @@ export default sfc({
/> />
))} ))}
</div> </div>
)); );
}
}
},
render(h) {
return ( return (
<div class={bem()}> <div class={bem()}>
<div <div
@ -297,7 +292,7 @@ export default sfc({
> >
{this.slots()} {this.slots()}
</div> </div>
{Indicator} {this.renderIndicator()}
</div> </div>
); );
} }