[improvement] add closeOnPopstate mixin (#3708)

This commit is contained in:
neverland 2019-07-01 16:29:47 +08:00 committed by GitHub
parent 3eb802a689
commit 2df51f5aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 26 deletions

View File

@ -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();
},

View File

@ -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);

View 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);
}
}
}
});

View File

@ -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
) {