-
zhangwei
2024-08-22 d5eb1ecff5d0236359c744b334cf30fcfdad1132
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号: 915 1 01 313     320 0  61 9  3  K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view>
        <!-- #ifdef APP-VUE || H5 -->
        <view class="fui-movable__view" :class="{'fui-movable__cursor':direction!=='none'}" :data-direction="direction"
            :data-width="maxWidth" :data-height="maxHeight" :data-left="eLeft" :data-top="eTop" :data-app="isApp"
            @touchstart="handler.touchstart" @touchmove="handler.touchmove" @mousedown="handler.mousedown"
            :style="getStyles">
            <slot></slot>
        </view>
        <!-- #endif -->
 
        <!-- #ifdef MP-WEIXIN -->
        <view class="fui-movable__view" :class="{'fui-movable__cursor':direction!=='none'}" :data-direction="direction"
            :data-width="maxWidth" :data-height="maxHeight" :data-left="eLeft" :data-top="eTop" :data-app="isApp"
            @touchstart="handler.touchstart" :catchtouchmove="handler.touchmove" :style="getStyles">
            <slot></slot>
        </view>
        <!-- #endif -->
 
        <!-- #ifdef APP-NVUE -->
        <view class="fui-movable__view" @touchstart="touchstart" @touchmove.stop.prevent="touchmove"
            ref="fui-movable__view" :style="getStyles">
            <slot></slot>
        </view>
        <!-- #endif -->
 
        <!-- #ifndef APP-PLUS || MP-WEIXIN || H5 -->
        <view class="fui-movable__view" @touchstart="touchstart" @touchmove.stop.prevent="touchmove" :style="getStyles">
            <slot></slot>
        </view>
        <!-- #endif -->
    </view>
</template>
<!-- #ifdef APP-VUE || MP-WEIXIN || H5-->
<script src="./index.wxs" module="handler" lang="wxs"></script>
<!-- #endif -->
 
<script>
    // #ifdef APP-NVUE
    const animation = uni.requireNativePlugin('animation');
    const dom = uni.requireNativePlugin('dom');
    // #endif
    import mpjs from './mpjs.js'
    import bindingx from './bindingx.js'
    export default {
        name: "fui-movable-view",
        emits: ['change'],
        mixins: [mpjs, bindingx],
        props: {
            //left值,设置大于-1的值则right失效
            left: {
                type: [Number, String],
                default: -1
            },
            right: {
                type: [Number, String],
                default: 80
            },
            //top值,设置大于-1的值则bottom失效
            top: {
                type: [Number, String],
                default: -1
            },
            bottom: {
                type: [Number, String],
                default: 120
            },
            zIndex: {
                type: [Number, String],
                default: 10
            },
            //移动方向,属性值有all、vertical、horizontal、none
            direction: {
                type: String,
                default: 'all'
            }
        },
        data() {
            let isApp = 0;
            // #ifdef APP
            isApp = 1;
            // #endif
            return {
                isApp,
                maxWidth: 0,
                maxHeight: 0,
                eLeft: 0,
                eTop: 0,
            };
        },
        watch: {
            position(val) {
                this.$nextTick(() => {
                    setTimeout(() => {
                        this._getSize()
                    }, 50);
                })
            }
        },
        mounted() {
            this.$nextTick(() => {
                setTimeout(() => {
                    this._getSize()
                }, 50);
            })
        },
        computed: {
            position() {
                return `${this.left}_${this.right}_${this.top}_${this.bottom}`
            },
            getStyles() {
                let styles = `z-index:${this.zIndex};`
                if (this.left != -1) {
                    styles += `left:${this.left}rpx;`
                } else {
                    styles += `right:${this.right}rpx;`
                }
                if (this.top != -1) {
                    styles += `top:${this.top}rpx;`
                } else {
                    styles += `bottom:${this.bottom}rpx;`
                }
                // #ifndef APP-PLUS || MP-WEIXIN || H5
                if (this.direction !== 'none') {
                    styles += `transform:${this.transform};`
                }
                // #endif
                return styles;
            }
        },
        methods: {
            _getSize() {
                const sys = uni.getSystemInfoSync()
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select('.fui-movable__view')
                    .boundingClientRect()
                    .exec(ret => {
                        if (ret) {
                            this.maxWidth = sys.windowWidth - ret[0].width - ret[0].left;
                            this.maxHeight = sys.windowHeight - ret[0].height - ret[0].top;
                            this.eLeft = ret[0].left || 0;
                            this.eTop = ret[0].top || 0;
                            this.change({
                                left: 0,
                                top: 0
                            })
                        }
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui-movable__view'], (ret) => {
                    const size = ret.size
                    if (size) {
                        this.maxWidth = sys.windowWidth - size.width - size.left;
                        this.maxHeight = sys.windowHeight - size.height - size.top;
                        this.eLeft = size.left || 0;
                        this.eTop = size.top || 0;
                        this.change({
                            left: 0,
                            top: 0
                        })
                    }
                })
                // #endif
            },
            reset() {
                setTimeout(() => {
                    this._getSize()
                }, 50);
            },
            change(e) {
                this.$emit('change', {
                    x: e.left + this.eLeft,
                    y: e.top + this.eTop
                })
            }
        }
    }
</script>
 
<style scoped>
    .fui-movable__view {
        position: fixed;
    }
 
    /* #ifdef H5 */
    .fui-movable__cursor {
        cursor: grab;
    }
 
    /* #endif */
</style>