gx
queenwuli
2021-01-28 b83ba3cc4687f21d744e9866e10e30e91229e8a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
export default {
    data() {
        return {
            position: [],
            button: {},
            btn: "[]"
        }
    },
    // computed: {
    //     pos() {
    //         return JSON.stringify(this.position)
    //     },
    //     btn() {
    //         return JSON.stringify(this.button)
    //     }
    // },
    watch: {
        button: {
            handler(newVal) {
                this.btn = JSON.stringify(newVal)
            },
            deep: true
        },
        show(newVal) {
            if (this.autoClose) return
            if (!this.button) {
                this.init()
                return
            }
            this.button.show = newVal
        },
        leftOptions() {
            this.init()
        },
        rightOptions() {
            this.init()
        }
    },
    created() {
        if (this.swipeaction.children !== undefined) {
            this.swipeaction.children.push(this)
        }
    },
    mounted() {
        this.init()
    },
    beforeDestroy() {
        this.swipeaction.children.forEach((item, index) => {
            if (item === this) {
                this.swipeaction.children.splice(index, 1)
            }
        })
    },
    methods: {
        init() {
            clearTimeout(this.swipetimer)
            this.swipetimer = setTimeout(() => {
                this.getButtonSize()
            }, 50)
        },
        closeSwipe(e) {
            if (!this.autoClose) return
            this.swipeaction.closeOther(this)
        },
 
        change(e) {
            this.$emit('change', e.open)
            let show = this.button.show
            if (show !== e.open) {
                this.button.show = e.open
            }
 
        },
 
        appTouchStart(e) {
            const {
                clientX
            } = e.changedTouches[0]
            this.clientX = clientX
            this.timestamp = new Date().getTime()
        },
        appTouchEnd(e, index, item, position) {
            const {
                clientX
            } = e.changedTouches[0]
            // fixed by xxxx 模拟点击事件,解决 ios 13 点击区域错位的问题
            let diff = Math.abs(this.clientX - clientX)
            let time = (new Date().getTime()) - this.timestamp
            if (diff < 40 && time < 300) {
                this.$emit('click', {
                    content: item,
                    index,
                    position
                })
            }
        },
        getButtonSize() {
            const views = uni.createSelectorQuery().in(this)
            views
                .selectAll('.uni-swipe_button-group')
                .boundingClientRect(data => {
                    let show = 'none'
                    if (this.autoClose) {
                        show = 'none'
                    } else {
                        show = this.show
                    }
                    this.button = {
                        data,
                        show
                    }
                })
                .exec()
        }
    }
}