linwe
2024-08-08 3c738f4fe2762bba8087e5a22fc0dc06560eab0e
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// [z-paging]scroll相关模块
import u from '.././z-paging-utils'
import Enum from '.././z-paging-enum'
 
// #ifdef APP-NVUE
const weexDom = weex.requireModule('dom');
// #endif
 
export default {
    props: {
        //使用页面滚动,默认为否,当设置为是时则使用页面的滚动而非此组件内部的scroll-view的滚动,使用页面滚动时z-paging无需设置确定的高度且对于长列表展示性能更高,但配置会略微繁琐
        usePageScroll: {
            type: Boolean,
            default: u.gc('usePageScroll', false)
        },
        //是否可以滚动,使用内置scroll-view和nvue时有效,默认为是
        scrollable: {
            type: Boolean,
            default: u.gc('scrollable', true)
        },
        //控制是否出现滚动条,默认为是
        showScrollbar: {
            type: Boolean,
            default: u.gc('showScrollbar', true)
        },
        //是否允许横向滚动,默认为否
        scrollX: {
            type: Boolean,
            default: u.gc('scrollX', false)
        },
        //iOS设备上滚动到顶部时是否允许回弹效果,默认为否。关闭回弹效果后可使滚动到顶部与下拉刷新更连贯,但是有吸顶view时滚动到顶部时可能出现抖动。
        scrollToTopBounceEnabled: {
            type: Boolean,
            default: u.gc('scrollToTopBounceEnabled', false)
        },
        //iOS设备上滚动到底部时是否允许回弹效果,默认为是。
        scrollToBottomBounceEnabled: {
            type: Boolean,
            default: u.gc('scrollToBottomBounceEnabled', true)
        },
        //在设置滚动条位置时使用动画过渡,默认为否
        scrollWithAnimation: {
            type: Boolean,
            default: u.gc('scrollWithAnimation', false)
        },
        //值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
        scrollIntoView: {
            type: String,
            default: u.gc('scrollIntoView', '')
        },
    },
    data() {
        return {
            scrollTop: 0,
            oldScrollTop: 0,
            scrollViewStyle: {},
            scrollViewContainerStyle: {},
            scrollViewInStyle: {},
            pageScrollTop: -1,
            scrollEnable: true,
            privateScrollWithAnimation: -1,
            cacheScrollNodeHeight: -1
        }
    },
    watch: {
        oldScrollTop(newVal) {
            !this.usePageScroll && this._scrollTopChange(newVal,false);
        },
        pageScrollTop(newVal) {
            this.usePageScroll && this._scrollTopChange(newVal,true);
        },
        usePageScroll: {
            handler(newVal) {
                this.loaded && this.autoHeight && this._setAutoHeight(!newVal);
                // #ifdef H5
                if (newVal) {
                    this.$nextTick(() => {
                        const mainScrollRef = this.$refs['zp-scroll-view'].$refs.main;
                        if (mainScrollRef) {
                            mainScrollRef.style = {};
                        }
                    })
                }
                // #endif
            },
            immediate: true
        },
        finalScrollTop(newVal) {
            if (!this.useChatRecordMode) {
                this.renderPropScrollTop = newVal < 6 ? 0 : 10;
            }
        },
    },
    computed: {
        finalScrollWithAnimation() {
            if (this.privateScrollWithAnimation !== -1) {
                const scrollWithAnimation = this.privateScrollWithAnimation === 1;
                this.privateScrollWithAnimation = -1;
                return scrollWithAnimation;
            }
            return this.scrollWithAnimation;
        },
        finalScrollViewStyle() {
            if (this.superContentZIndex != 1) {
                this.scrollViewStyle['z-index'] = this.superContentZIndex;
                this.scrollViewStyle['position'] = 'relative';
            }
            return this.scrollViewStyle;
        },
        finalScrollTop() {
            return this.usePageScroll ? this.pageScrollTop : this.oldScrollTop;
        },
        finalIsOldWebView() {
            return this.isOldWebView && !this.usePageScroll;
        }
    },
    methods: {
        //滚动到顶部,animate为是否展示滚动动画,默认为是
        scrollToTop(animate, checkReverse = true) {
            // #ifdef APP-NVUE
            if (checkReverse && this.useChatRecordMode) {
                if (!this.nIsFirstPageAndNoMore) {
                    this.scrollToBottom(animate, false);
                    return;
                }
            }
            // #endif
            this.$nextTick(() => {
                this._scrollToTop(animate, false);
                // #ifdef APP-NVUE
                if (this.nvueFastScroll && animate) {
                    u.delay(() => {
                        this._scrollToTop(false, false);
                    });
                }
                // #endif
            })
        },
        //滚动到底部,animate为是否展示滚动动画,默认为是
        scrollToBottom(animate, checkReverse = true) {
            // #ifdef APP-NVUE
            if (checkReverse && this.useChatRecordMode) {
                if (!this.nIsFirstPageAndNoMore) {
                    this.scrollToTop(animate, false);
                    return;
                }
            }
            // #endif
            this.$nextTick(() => {
                this._scrollToBottom(animate);
                // #ifdef APP-NVUE
                if (this.nvueFastScroll && animate) {
                    u.delay(() => {
                        this._scrollToBottom(false);
                    });
                }
                // #endif
            })
        },
        //滚动到指定view(vue中有效)。sel为需要滚动的view的id值,不包含"#";offset为偏移量,单位为px;animate为是否展示滚动动画,默认为否
        scrollIntoViewById(sel, offset, animate) {
            this._scrollIntoView(sel, offset, animate);
        },
        //滚动到指定view(vue中有效)。nodeTop为需要滚动的view的top值(通过uni.createSelectorQuery()获取);offset为偏移量,单位为px;animate为是否展示滚动动画,默认为否
        scrollIntoViewByNodeTop(nodeTop, offset, animate) {
            this.scrollTop = this.oldScrollTop;
            this.$nextTick(() => {
                this._scrollIntoViewByNodeTop(nodeTop, offset, animate);
            })
        },
        //滚动到指定位置(vue中有效)。y为与顶部的距离,单位为px;offset为偏移量,单位为px;animate为是否展示滚动动画,默认为否
        scrollToY(y, offset, animate) {
            this.scrollTop = this.oldScrollTop;
            this.$nextTick(() => {
                this._scrollToY(y, offset, animate);
            })
        },
        //滚动到指定view(nvue中有效)。index为需要滚动的view的index(第几个);offset为偏移量,单位为px;animate为是否展示滚动动画,默认为否
        scrollIntoViewByIndex(index, offset, animate) {
            this._scrollIntoView(index, offset, animate);
        },
        //滚动到指定view(nvue中有效)。view为需要滚动的view(通过`this.$refs.xxx`获取),不包含"#";offset为偏移量,单位为px;animate为是否展示滚动动画,默认为否
        scrollIntoViewByView(view, offset, animate) {
            this._scrollIntoView(view, offset, animate);
        },
        //当使用页面滚动并且自定义下拉刷新时,请在页面的onPageScroll中调用此方法,告知z-paging当前的pageScrollTop,否则会导致在任意位置都可以下拉刷新
        updatePageScrollTop(value) {
            this.pageScrollTop = value;
        },
        //当使用页面滚动并且设置了slot="top"时,默认初次加载会自动获取其高度,并使内部容器下移,当slot="top"的view高度动态改变时,在其高度需要更新时调用此方法
        updatePageScrollTopHeight() {
            this._updatePageScrollTopOrBottomHeight('top');
        },
        //当使用页面滚动并且设置了slot="bottom"时,默认初次加载会自动获取其高度,并使内部容器下移,当slot="bottom"的view高度动态改变时,在其高度需要更新时调用此方法
        updatePageScrollBottomHeight() {
            this._updatePageScrollTopOrBottomHeight('bottom');
        },
        //更新slot="left"和slot="right"宽度,当slot="left"或slot="right"宽度动态改变时调用
        updateLeftAndRightWidth() {
            if (!this.finalIsOldWebView) return;
            this.$nextTick(() => this._updateLeftAndRightWidth(this.scrollViewContainerStyle, 'zp-page'));
        },
        //更新z-paging内置scroll-view的scrollTop
        updateScrollViewScrollTop(scrollTop, animate = true) {
            this.privateScrollWithAnimation = animate ? 1 : 0;
            this.scrollTop = this.oldScrollTop;
            this.$nextTick(() => {
                this.scrollTop = scrollTop;
                this.oldScrollTop = this.scrollTop;
            });
        },
        
        //当滚动到顶部时
        _onScrollToUpper() {
            this.$emit('scrolltoupper');
            this.$emit('scrollTopChange', 0);
            this.$nextTick(() => {
                this.oldScrollTop = 0;
            })
            this.useChatRecordMode && this.loadingStatus !== Enum.More.NoMore && this._onLoadingMore('click');
        },
        //当滚动到底部时
        _onScrollToLower(e) {
            (!e.detail || !e.detail.direction || e.detail.direction === 'bottom') && this._onLoadingMore('toBottom')
        },
        //滚动到顶部
        _scrollToTop(animate = true, isPrivate = true) {
            // #ifdef APP-NVUE
            const el = this.$refs['zp-n-list-top-tag'];
            if (this.usePageScroll) {
                this._getNodeClientRect('zp-page-scroll-top', false).then(node => {
                    const nodeHeight = node ? node[0].height : 0;
                    weexDom.scrollToElement(el, {
                        offset: -nodeHeight,
                        animated: animate
                    });
                });
            } else {
                if (!this.isIos && this.nvueListIs === 'scroller') {
                    this._getNodeClientRect('zp-n-refresh-container', false).then(node => {
                        const nodeHeight = node ? node[0].height : 0;
                        weexDom.scrollToElement(el, {
                            offset: -nodeHeight,
                            animated: animate
                        });
                    });
                } else {
                    weexDom.scrollToElement(el, {
                        offset: 0,
                        animated: animate
                    });
                }
            }
            return;
            // #endif
            if (this.usePageScroll) {
                this.$nextTick(() => {
                    uni.pageScrollTo({
                        scrollTop: 0,
                        duration: animate ? 100 : 0,
                    });
                });
                return;
            }
            this.privateScrollWithAnimation = animate ? 1 : 0;
            this.scrollTop = this.oldScrollTop;
            this.$nextTick(() => {
                this.scrollTop = 0;
                this.oldScrollTop = this.scrollTop;
            });
        },
        //滚动到底部
        async _scrollToBottom(animate = true) {
            // #ifdef APP-NVUE
            const el = this.$refs['zp-n-list-bottom-tag'];
            if (el) {
                weexDom.scrollToElement(el, {
                    offset: 0,
                    animated: animate
                });
            } else {
                u.consoleErr('滚动到底部失败,因为您设置了hideNvueBottomTag为true');
            }
            return;
            // #endif
            if (this.usePageScroll) {
                this.$nextTick(() => {
                    uni.pageScrollTo({
                        scrollTop: Number.MAX_VALUE,
                        duration: animate ? 100 : 0,
                    });
                });
                return;
            }
            try {
                this.privateScrollWithAnimation = animate ? 1 : 0;
                const pagingContainerNode = await this._getNodeClientRect('.zp-paging-container');
                const scrollViewNode = await this._getNodeClientRect('.zp-scroll-view');
                const pagingContainerH = pagingContainerNode ? pagingContainerNode[0].height : 0;
                const scrollViewH = scrollViewNode ? scrollViewNode[0].height : 0;
                if (pagingContainerH > scrollViewH) {
                    this.scrollTop = this.oldScrollTop;
                    this.$nextTick(() => {
                        this.scrollTop = pagingContainerH - scrollViewH + this.virtualPlaceholderTopHeight;
                        this.oldScrollTop = this.scrollTop;
                    });
                }
            } catch (e) {}
        },
        //滚动到指定view
        _scrollIntoView(sel, offset = 0, animate = false, finishCallback) {
            try {
                this.scrollTop = this.oldScrollTop;
                this.$nextTick(() => {
                    // #ifdef APP-NVUE
                    const refs = this.$parent.$refs;
                    if (!refs) return;
                    const dataType = Object.prototype.toString.call(sel);
                    let el = null;
                    if (dataType === '[object Number]') {
                        const els = refs[`z-paging-${sel}`];
                        el = els ? els[0] : null;
                    } else if (dataType === '[object Array]') {
                        el = sel[0];
                    } else {
                        el = sel;
                    }
                    if (el) {
                        weexDom.scrollToElement(el, {
                            offset: -offset,
                            animated: animate
                        });
                    } else {
                        u.consoleErr('在nvue中滚动到指定位置,cell必须设置 :ref="`z-paging-${index}`"');
                    }
                    return;
                    // #endif
                    this._getNodeClientRect('#' + sel.replace('#', ''), this.$parent).then((node) => {
                        if (node) {
                            let nodeTop = node[0].top;
                            this._scrollIntoViewByNodeTop(nodeTop, offset, animate);
                            finishCallback && finishCallback();
                        }
                    });
                });
            } catch (e) {}
        },
        //通过nodeTop滚动到指定view
        _scrollIntoViewByNodeTop(nodeTop, offset = 0, animate = false) {
            this._scrollToY(nodeTop, offset, animate, true);
        },
        //滚动到指定位置
        _scrollToY(y, offset = 0, animate = false, addScrollTop = false) {
            this.privateScrollWithAnimation = animate ? 1 : 0;
            if (this.usePageScroll) {
                uni.pageScrollTo({
                    scrollTop: y - offset,
                    duration: animate ? 100 : 0
                });
            } else {
                if(addScrollTop){
                   y += this.oldScrollTop; 
                }
                this.scrollTop = y - offset;
                this.oldScrollTop = this.scrollTop;
            }
        },
        //scroll-view滚动中
        _scroll(e) {
            this.$emit('scroll', e);
            const scrollTop = e.detail.scrollTop;
            // #ifndef APP-NVUE
            this.finalUseVirtualList && this._updateVirtualScroll(scrollTop, this.oldScrollTop - scrollTop);
            // #endif
            this.oldScrollTop = scrollTop;
            const scrollDiff = e.detail.scrollHeight - this.oldScrollTop;
            !this.isIos && this._checkScrolledToBottom(scrollDiff);
        },
        //检测scrollView是否要铺满屏幕
        _doCheckScrollViewShouldFullHeight(totalData) {
            if (this.autoFullHeight && this.usePageScroll && this.isTotalChangeFromAddData) {
                // #ifndef APP-NVUE
                this.$nextTick(() => {
                    this._checkScrollViewShouldFullHeight((scrollViewNode, pagingContainerNode) => {
                        this._preCheckShowNoMoreInside(totalData, scrollViewNode, pagingContainerNode)
                    });
                })
                // #endif
                // #ifdef APP-NVUE
                this._preCheckShowNoMoreInside(totalData)
                // #endif
            } else {
                this._preCheckShowNoMoreInside(totalData)
            } 
        },
        //检测z-paging是否要全屏覆盖(当使用页面滚动并且不满全屏时,默认z-paging需要铺满全屏,避免数据过少时内部的empty-view无法正确展示)
        async _checkScrollViewShouldFullHeight(callback) {
            try {
                const scrollViewNode = await this._getNodeClientRect('.zp-scroll-view');
                const pagingContainerNode = await this._getNodeClientRect('.zp-paging-container-content');
                if (!scrollViewNode || !pagingContainerNode) return;
                const scrollViewHeight = pagingContainerNode[0].height;
                const scrollViewTop = scrollViewNode[0].top;
                if (this.isAddedData && scrollViewHeight + scrollViewTop <= this.windowHeight) {
                    this._setAutoHeight(true, scrollViewNode);
                    callback(scrollViewNode, pagingContainerNode);
                } else {
                    this._setAutoHeight(false);
                    callback(null, null);
                }
            } catch (e) {
                callback(null, null);
            }
        },
        //scrollTop改变时触发
        _scrollTopChange(newVal, isPageScrollTop){
            this.$emit('scrollTopChange', newVal);
            this.$emit('update:scrollTop', newVal);
            this._checkShouldShowBackToTop(newVal);
            const scrollTop = this.isIos ? (newVal > 5 ? 6 : 0) : (newVal > 105 ? 106 : (newVal > 5 ? 6 : 0));
            if (isPageScrollTop && this.wxsPageScrollTop !== scrollTop) {
                this.wxsPageScrollTop = scrollTop;
            } else if (!isPageScrollTop && this.wxsScrollTop !== scrollTop) {
                this.wxsScrollTop = scrollTop;
                if (scrollTop > 6) {
                    this.scrollEnable = true;
                }
            }
        },
        //更新使用页面滚动时slot="top"或"bottom"插入view的高度
        _updatePageScrollTopOrBottomHeight(type) {
            // #ifndef APP-NVUE
            if (!this.usePageScroll) return;
            // #endif
            this._doCheckScrollViewShouldFullHeight(this.realTotalData);
            const node = `.zp-page-${type}`;
            const marginText = `margin${type.slice(0,1).toUpperCase() + type.slice(1)}`;
            let safeAreaInsetBottomAdd = this.safeAreaInsetBottom;
            this.$nextTick(() => {
                let delayTime = 0;
                // #ifdef MP-BAIDU || APP-NVUE
                delayTime = 50;
                // #endif
                u.delay(() => {
                    this._getNodeClientRect(node).then((res) => {
                        if (res) {
                            let pageScrollNodeHeight = res[0].height;
                            if (type === 'bottom') {
                                if (safeAreaInsetBottomAdd) {
                                    pageScrollNodeHeight += this.safeAreaBottom;
                                }
                            } else {
                                this.cacheTopHeight = pageScrollNodeHeight;
                            }
                            this.$set(this.scrollViewStyle, marginText, `${pageScrollNodeHeight}px`);
                        } else if (safeAreaInsetBottomAdd) {
                            this.$set(this.scrollViewStyle, marginText, `${this.safeAreaBottom}px`);
                        }
                    });
                }, delayTime)
            })
        },
    }
}