zhaojs
2023-09-15 fc13938ff90213060532d99a600dea4a84456885
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// #ifdef APP-NVUE
const dom = weex.requireModule('dom');
// #endif
export default {
    data() {
        return {
            uniShow: false,
            left: 0
        }
    },
    computed: {
        moveLeft() {
            return `translateX(${this.left}px)`
        }
    },
    watch: {
        show(newVal) {
            if (!this.position || JSON.stringify(this.position) === '{}') return;
            if (this.autoClose) return
            if (newVal) {
                this.$emit('change', true)
                this.open()
            } else {
                this.$emit('change', false)
                this.close()
            }
        }
    },
    mounted() {
        this.position = {}
        if (this.swipeaction.children !== undefined) {
            this.swipeaction.children.push(this)
        }
        setTimeout(() => {
            this.getSelectorQuery()
        }, 100)
    },
    beforeDestoy() {
        this.swipeaction.children.forEach((item, index) => {
            if (item === this) {
                this.swipeaction.children.splice(index, 1)
            }
        })
    },
    methods: {
        onClick(index, item) {
            this.$emit('click', {
                content: item,
                index
            })
            this.close()
        },
        touchstart(e) {
            const {
                pageX
            } = e.touches[0]
            if (this.disabled) return
            const left = this.position.content.left
            if (this.autoClose) {
                this.swipeaction.closeOther(this)
            }
            this.width = pageX - left
            if (this.isopen) return
            if (this.uniShow) {
                this.uniShow = false
                this.isopen = true
                this.openleft = this.left + this.position.button.width
            }
        },
        touchmove(e, index) {
            if (this.disabled) return
            const {
                pageX
            } = e.touches[0]
            this.setPosition(pageX)
        },
        touchend() {
            if (this.disabled) return
            if (this.isopen) {
                this.move(this.openleft, 0)
                return
            }
            this.move(this.left, -40)
        },
        setPosition(x, y) {
            if (!this.position.button.width) {
                return
            }
            // this.left = x - this.width
            this.setValue(x - this.width)
        },
        setValue(value) {
            // 设置最大最小值
            this.left = Math.max(-this.position.button.width, Math.min(parseInt(value), 0))
            this.position.content.left = this.left
            if (this.isopen) {
                this.openleft = this.left + this.position.button.width
            }
        },
        move(left, value) {
            if (left >= value) {
                this.$emit('change', false)
                this.close()
            } else {
                this.$emit('change', true)
                this.open()
            }
        },
        open() {
            this.uniShow = true
            this.left = -this.position.button.width
            this.setValue(-this.position.button.width)
        },
        close() {
            this.uniShow = true
            this.setValue(0)
            setTimeout(() => {
                this.uniShow = false
                this.isopen = false
            }, 300)
        },
        getSelectorQuery() {
            // #ifndef APP-NVUE
            const views = uni.createSelectorQuery()
                .in(this)
            views
                .selectAll('.selector-query-hock')
                .boundingClientRect(data => {
                    this.position.content = data[1]
                    this.position.button = data[0]
                    if (this.autoClose) return
                    if (this.show) {
                        this.open()
                    } else {
                        this.close()
                    }
                })
                .exec()
            // #endif
            // #ifdef APP-NVUE
            dom.getComponentRect(this.$refs['selector-content-hock'], (data) => {
                if (this.position.content) return
                this.position.content = data.size
            })
            dom.getComponentRect(this.$refs['selector-button-hock'], (data) => {
                if (this.position.button) return
                this.position.button = data.size
                if (this.autoClose) return
                if (this.show) {
                    this.open()
                } else {
                    this.close()
                }
            })
            // #endif
        }
    }
}