mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] add closeOnPopstate mixin (#3708)
This commit is contained in:
parent
3eb802a689
commit
2df51f5aef
@ -1,8 +1,9 @@
|
||||
import { createNamespace, isServer } from '../utils';
|
||||
import { createNamespace } from '../utils';
|
||||
import { range } from '../utils/format/number';
|
||||
import { preventDefault } from '../utils/dom/event';
|
||||
import { PopupMixin } from '../mixins/popup';
|
||||
import { TouchMixin } from '../mixins/touch';
|
||||
import { CloseOnPopstateMixin } from '../mixins/close-on-popstate';
|
||||
import Swipe from '../swipe';
|
||||
import SwipeItem from '../swipe-item';
|
||||
|
||||
@ -18,7 +19,11 @@ function getDistance(touches) {
|
||||
}
|
||||
|
||||
export default createComponent({
|
||||
mixins: [PopupMixin, TouchMixin],
|
||||
mixins: [
|
||||
PopupMixin,
|
||||
TouchMixin,
|
||||
CloseOnPopstateMixin
|
||||
],
|
||||
|
||||
props: {
|
||||
images: Array,
|
||||
@ -27,7 +32,6 @@ export default createComponent({
|
||||
asyncClose: Boolean,
|
||||
startPosition: Number,
|
||||
showIndicators: Boolean,
|
||||
closeOnPopstate: Boolean,
|
||||
loop: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
@ -94,30 +98,10 @@ export default createComponent({
|
||||
|
||||
startPosition(active) {
|
||||
this.active = active;
|
||||
},
|
||||
|
||||
closeOnPopstate: {
|
||||
handler(val) {
|
||||
this.handlePopstate(val);
|
||||
},
|
||||
immediate: true
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
handlePopstate(bind) {
|
||||
/* istanbul ignore if */
|
||||
if (isServer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.bindStatus !== bind) {
|
||||
this.bindStatus = bind;
|
||||
const action = bind ? 'add' : 'remove';
|
||||
window[`${action}EventListener`]('popstate', this.close);
|
||||
}
|
||||
},
|
||||
|
||||
onWrapperTouchStart() {
|
||||
this.touchStartTime = new Date();
|
||||
},
|
||||
|
@ -7,7 +7,9 @@ type BindEventMixinThis = {
|
||||
binded: boolean;
|
||||
};
|
||||
|
||||
export function BindEventMixin(handler: Function) {
|
||||
type BindEventHandler = (bind: Function, isBind: boolean) => void;
|
||||
|
||||
export function BindEventMixin(handler: BindEventHandler) {
|
||||
function bind(this: BindEventMixinThis) {
|
||||
if (!this.binded) {
|
||||
handler.call(this, on, true);
|
||||
|
40
src/mixins/close-on-popstate.ts
Normal file
40
src/mixins/close-on-popstate.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import Vue from 'vue';
|
||||
import { on, off } from '../utils/dom/event';
|
||||
import { BindEventMixin } from './bind-event';
|
||||
|
||||
export const CloseOnPopstateMixin = Vue.extend({
|
||||
mixins: [BindEventMixin(function (this: any, bind, isBind) {
|
||||
this.onPopstate(isBind);
|
||||
})],
|
||||
|
||||
props: {
|
||||
closeOnPopstate: Boolean
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
bindStatus: false
|
||||
};
|
||||
},
|
||||
|
||||
watch: {
|
||||
closeOnPopstate(val: boolean) {
|
||||
this.onPopstate(val);
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
onPopstate(bind: boolean) {
|
||||
/* istanbul ignore if */
|
||||
if (this.$isServer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.bindStatus !== bind) {
|
||||
this.bindStatus = bind;
|
||||
const action = bind ? on : off;
|
||||
action(window, 'popstate', (this as any).close);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
@ -20,7 +20,7 @@ if (!isServer) {
|
||||
}
|
||||
|
||||
export function on(
|
||||
target: HTMLElement | Document,
|
||||
target: HTMLElement | Document | Window,
|
||||
event: string,
|
||||
handler: EventHandler,
|
||||
passive = false
|
||||
@ -35,7 +35,7 @@ export function on(
|
||||
}
|
||||
|
||||
export function off(
|
||||
target: HTMLElement | Document,
|
||||
target: HTMLElement | Document | Window,
|
||||
event: string,
|
||||
handler: EventHandler
|
||||
) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user