mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-24 02:16:12 +08:00
[improvement] add bind event mixin
This commit is contained in:
parent
884f624a16
commit
616db0114e
@ -1,14 +1,24 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import { ParentMixin } from '../mixins/relation';
|
import { ParentMixin } from '../mixins/relation';
|
||||||
|
import { BindEventMixin } from '../mixins/bind-event';
|
||||||
import { GREEN } from '../utils/color';
|
import { GREEN } from '../utils/color';
|
||||||
import { on, off } from '../utils/event';
|
|
||||||
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/scroll';
|
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/scroll';
|
||||||
|
|
||||||
const [sfc, bem] = use('index-bar');
|
const [sfc, bem] = use('index-bar');
|
||||||
|
|
||||||
export default sfc({
|
export default sfc({
|
||||||
mixins: [TouchMixin, ParentMixin('vanIndexBar')],
|
mixins: [
|
||||||
|
TouchMixin,
|
||||||
|
ParentMixin('vanIndexBar'),
|
||||||
|
BindEventMixin(function (bind) {
|
||||||
|
if (!this.scroller) {
|
||||||
|
this.scroller = getScrollEventTarget(this.$el);
|
||||||
|
}
|
||||||
|
|
||||||
|
bind(this.scroller, 'scroll', this.onScroll);
|
||||||
|
})
|
||||||
|
],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
sticky: {
|
sticky: {
|
||||||
@ -56,32 +66,7 @@ export default sfc({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
|
||||||
this.scroller = getScrollEventTarget(this.$el);
|
|
||||||
this.handler(true);
|
|
||||||
},
|
|
||||||
|
|
||||||
destroyed() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
activated() {
|
|
||||||
this.handler(true);
|
|
||||||
},
|
|
||||||
|
|
||||||
deactivated() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
handler(bind) {
|
|
||||||
/* istanbul ignore else */
|
|
||||||
if (this.binded !== bind) {
|
|
||||||
this.binded = bind;
|
|
||||||
(bind ? on : off)(this.scroller, 'scroll', this.onScroll);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onScroll() {
|
onScroll() {
|
||||||
if (!this.sticky) {
|
if (!this.sticky) {
|
||||||
return;
|
return;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import Loading from '../loading';
|
import Loading from '../loading';
|
||||||
import { on, off } from '../utils/event';
|
import { BindEventMixin } from '../mixins/bind-event';
|
||||||
import {
|
import {
|
||||||
getScrollTop,
|
getScrollTop,
|
||||||
getElementTop,
|
getElementTop,
|
||||||
@ -11,6 +11,16 @@ import {
|
|||||||
const [sfc, bem, t] = use('list');
|
const [sfc, bem, t] = use('list');
|
||||||
|
|
||||||
export default sfc({
|
export default sfc({
|
||||||
|
mixins: [
|
||||||
|
BindEventMixin(function (bind) {
|
||||||
|
if (!this.scroller) {
|
||||||
|
this.scroller = getScrollEventTarget(this.$el);
|
||||||
|
}
|
||||||
|
|
||||||
|
bind(this.scroller, 'scroll', this.check);
|
||||||
|
})
|
||||||
|
],
|
||||||
|
|
||||||
model: {
|
model: {
|
||||||
prop: 'loading'
|
prop: 'loading'
|
||||||
},
|
},
|
||||||
@ -37,26 +47,11 @@ export default sfc({
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
this.scroller = getScrollEventTarget(this.$el);
|
|
||||||
this.handler(true);
|
|
||||||
|
|
||||||
if (this.immediateCheck) {
|
if (this.immediateCheck) {
|
||||||
this.$nextTick(this.check);
|
this.$nextTick(this.check);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
destroyed() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
activated() {
|
|
||||||
this.handler(true);
|
|
||||||
},
|
|
||||||
|
|
||||||
deactivated() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
loading() {
|
loading() {
|
||||||
this.$nextTick(this.check);
|
this.$nextTick(this.check);
|
||||||
@ -117,14 +112,6 @@ export default sfc({
|
|||||||
clickErrorText() {
|
clickErrorText() {
|
||||||
this.$emit('update:error', false);
|
this.$emit('update:error', false);
|
||||||
this.$nextTick(this.check);
|
this.$nextTick(this.check);
|
||||||
},
|
|
||||||
|
|
||||||
handler(bind) {
|
|
||||||
/* istanbul ignore else */
|
|
||||||
if (this.binded !== bind) {
|
|
||||||
this.binded = bind;
|
|
||||||
(bind ? on : off)(this.scroller, 'scroll', this.check);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
24
packages/mixins/bind-event.js
Normal file
24
packages/mixins/bind-event.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { on, off } from '../utils/event';
|
||||||
|
|
||||||
|
export function BindEventMixin(handler) {
|
||||||
|
function bind() {
|
||||||
|
if (!this.binded) {
|
||||||
|
handler.call(this, on);
|
||||||
|
this.binded = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function unbind() {
|
||||||
|
if (this.binded) {
|
||||||
|
handler.call(this, off);
|
||||||
|
this.binded = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
mounted: bind,
|
||||||
|
activated: bind,
|
||||||
|
destroyed: unbind,
|
||||||
|
deactivated: unbind
|
||||||
|
};
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { stopPropagation } from '../utils/event';
|
import { stopPropagation } from '../utils/event';
|
||||||
|
import { BindEventMixin } from '../mixins/bind-event';
|
||||||
import Key from './Key';
|
import Key from './Key';
|
||||||
|
|
||||||
const [sfc, bem, t] = use('number-keyboard');
|
const [sfc, bem, t] = use('number-keyboard');
|
||||||
@ -7,6 +8,14 @@ const CLOSE_KEY_TYPE = ['blue', 'big'];
|
|||||||
const DELETE_KEY_TYPE = ['delete', 'big', 'gray'];
|
const DELETE_KEY_TYPE = ['delete', 'big', 'gray'];
|
||||||
|
|
||||||
export default sfc({
|
export default sfc({
|
||||||
|
mixins: [
|
||||||
|
BindEventMixin(function (bind) {
|
||||||
|
if (this.hideOnClickOutside) {
|
||||||
|
bind(document.body, 'touchstart', this.onBlur);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
show: Boolean,
|
show: Boolean,
|
||||||
title: String,
|
title: String,
|
||||||
@ -39,22 +48,6 @@ export default sfc({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
|
||||||
this.handler(true);
|
|
||||||
},
|
|
||||||
|
|
||||||
destroyed() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
activated() {
|
|
||||||
this.handler(true);
|
|
||||||
},
|
|
||||||
|
|
||||||
deactivated() {
|
|
||||||
this.handler(false);
|
|
||||||
},
|
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
show() {
|
show() {
|
||||||
if (!this.transition) {
|
if (!this.transition) {
|
||||||
@ -92,21 +85,6 @@ export default sfc({
|
|||||||
},
|
},
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
handler(action) {
|
|
||||||
/* istanbul ignore if */
|
|
||||||
if (this.$isServer) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (action !== this.handlerStatus && this.hideOnClickOutside) {
|
|
||||||
this.handlerStatus = action;
|
|
||||||
document.body[(action ? 'add' : 'remove') + 'EventListener'](
|
|
||||||
'touchstart',
|
|
||||||
this.onBlur
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
onBlur() {
|
onBlur() {
|
||||||
this.$emit('blur');
|
this.$emit('blur');
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user