zhangwei
2025-01-17 2af6a1003a5a6870c5ad14c7b2b74994c136ca88
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// 本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号:9151   01  313  3  2 006 1 9     3K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。
var MIN_DISTANCE = 10
var app = false;
 
function isPC() {
    if (typeof navigator !== 'object') return false;
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length - 1; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}
var isH5 = false
if (typeof window === 'object') isH5 = true
 
function getDom(ins, ownerInstance) {
    var state = ins.getState()
    var rightDom = ownerInstance.selectComponent('.fui-swipe__action-right')
    if (!rightDom) {
        state.rightWidth = 0;
        return;
    }
    if (isH5 || isPC()) {
        if (rightDom['$el']) {
            state.rightWidth = rightDom['$el'].offsetWidth || 0
        } else {
            state.rightWidth = 0
        }
    } else {
        var rightStyles = rightDom.getBoundingClientRect()
        state.rightWidth = rightStyles.width || 0
    }
}
 
function getDirection(x, y) {
    if (x > y && x > MIN_DISTANCE) {
        return 'horizontal';
    }
    if (y > x && y > MIN_DISTANCE) {
        return 'vertical';
    }
    return '';
}
 
function stopTouchMove(event) {
    var instance = event.instance;
    var state = instance.getState();
    var touch = event.touches[0];
    if (isH5 && isPC()) {
        touch = event;
    }
    state.deltaX = touch.clientX - state.startX;
    state.deltaY = touch.clientY - state.startY;
    state.offsetY = Math.abs(state.deltaY);
    state.offsetX = Math.abs(state.deltaX);
    state.direction = state.direction || getDirection(state.offsetX, state.offsetY);
}
 
function stopTouchStart(event) {
    var instance = event.instance;
    var state = instance.getState();
    resetTouchStatus(instance);
    var touch = event.touches[0];
    if (isH5 && isPC()) {
        touch = event;
    }
    state.startX = touch.clientX;
    state.startY = touch.clientY;
}
 
function touchstart(e, ownerInstance) {
    var ins = e.instance;
    var state = ins.getState();
    var dataset = e.instance.getDataset()
    app = (+dataset.app) == 1 ? true : false;
    getDom(ins, ownerInstance)
    if (state.disabled) return
    // 开始触摸时移除动画类
    ins.requestAnimationFrame(function() {
        ins.removeClass('fui-swipe__action-ani');
        ownerInstance.callMethod('closeSwipe');
    })
 
    // 记录上次的位置
    state.x = state.left || 0
    // 计算滑动开始位置
    stopTouchStart(e, ownerInstance)
}
 
function move(value, ins, ownerInstance) {
    value = value || 0
    var state = ins.getState()
    var rightWidth = state.rightWidth
    // 获取可滑动范围
    state.left = Math.min(Math.max(value, -rightWidth), 0);
    ins.requestAnimationFrame(function() {
        ins.setStyle({
            transform: 'translateX(' + state.left + 'px)',
            '-webkit-transform': 'translateX(' + state.left + 'px)'
        })
    })
}
 
function touchmove(e, ownerInstance) {
    var ins = e.instance;
    var state = ins.getState()
    if (state.disabled) return;
    // 是否可以滑动页面
    stopTouchMove(e);
    if (state.direction !== 'horizontal') return;
 
    if (e.preventDefault) {
        e.preventDefault()
    }
    if (app && event && event.preventDefault) {
        event.preventDefault()
    }
 
    move(state.x + state.deltaX, ins, ownerInstance)
}
 
function openState(type, ins, ownerInstance) {
    var state = ins.getState()
    var leftWidth = state.leftWidth
    var rightWidth = state.rightWidth
    var left = ''
    state.open = state.open ? state.open : 'none'
    if (type === 'right' || type === true) {
        left = -rightWidth
    } else {
        left = 0
    }
    if (state.open !== type) {
        state.throttle = true
        ownerInstance.callMethod('change', {
            open: type
        })
    }
 
    state.open = type
    // 添加动画类
    ins.requestAnimationFrame(function() {
        ins.addClass('fui-swipe__action-ani');
        move(left, ins, ownerInstance)
    })
}
 
function moveDirection(left, ins, ownerInstance) {
    var state = ins.getState()
    var threshold = state.threshold || 30
    var position = state.position
    var open = state.open || 'none'
    var rightWidth = state.rightWidth
    if (state.deltaX === 0 && state.clickclose) {
        openState('none', ins, ownerInstance)
        return
    }
    if ((open === 'none' && rightWidth > 0 && -left > threshold) || (open !== 'none' && rightWidth > 0 &&
            rightWidth +
            left < threshold)) {
        openState('right', ins, ownerInstance)
    } else {
        // default
        openState('none', ins, ownerInstance)
    }
}
 
function touchend(e, ownerInstance) {
    var instance = e.instance;
    var state = instance.getState()
    if (state.disabled) return
    moveDirection(state.left, instance, ownerInstance)
}
 
function resetTouchStatus(instance) {
    var state = instance.getState();
    state.direction = '';
    state.deltaX = 0;
    state.deltaY = 0;
    state.offsetX = 0;
    state.offsetY = 0;
}
 
function showChange(newVal, oldVal, ownerInstance, instance) {
    var state = instance.getState()
    getDom(instance, ownerInstance)
    if (newVal && newVal !== 'none') {
        openState(newVal || 'right', instance, ownerInstance)
        return
    }
 
    if (state.left) {
        openState('none', instance, ownerInstance)
    }
    resetTouchStatus(instance)
}
 
var movable = false
 
function mousedown(e, ins) {
    if (!isH5 || !isPC()) return
    touchstart(e, ins)
    movable = true
}
 
function mousemove(e, ins) {
    if (!isH5 || !isPC() || !movable) return
    touchmove(e, ins)
}
 
function mouseup(e, ins) {
    if (!isH5 || !isPC()) return
    touchend(e, ins)
    movable = false
}
 
function mouseleave(e, ins) {
    if (!isH5 || !isPC()) return
    movable = false
}
 
function disabledChange(newVal, oldVal, ownerInstance, ins) {
    var st = ins.getState()
    st.disabled = newVal
}
 
function thresholdChange(newVal, oldVal, ownerInstance, ins) {
    var st = ins.getState()
    st.threshold = newVal || 30
}
 
function clickCloseChange(newVal, oldVal, ownerInstance, ins) {
    var st = ins.getState()
    if (newVal || newVal === null || newVal === undefined) {
        st.clickclose = true
    } else {
        st.clickclose = false
    }
}
 
 
module.exports = {
    touchstart: touchstart,
    touchmove: touchmove,
    touchend: touchend,
    mousedown: mousedown,
    mousemove: mousemove,
    mouseup: mouseup,
    mouseleave: mouseleave,
    disabledChange: disabledChange,
    thresholdChange: thresholdChange,
    showChange: showChange,
    clickCloseChange: clickCloseChange
}