沙盘演示系统应用的微信小程序
zuoxiao
2024-08-28 eb3dbfdcb126beeb1d08f3306ac8f5bbc466e133
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
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { SuperComponent, wxComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
import { unitConvert } from '../common/utils';
const { prefix } = config;
const name = `${prefix}-pull-down-refresh`;
let PullDownRefresh = class PullDownRefresh extends SuperComponent {
    constructor() {
        super(...arguments);
        this.pixelRatio = 1;
        this.startPoint = null;
        this.isPulling = false;
        this.loadingBarHeight = 100;
        this.maxRefreshAnimateTimeFlag = 0;
        this.closingAnimateTimeFlag = 0;
        this.externalClasses = [`${prefix}-class`, `${prefix}-class-loading`, `${prefix}-class-text`, `${prefix}-class-indicator`];
        this.options = {
            multipleSlots: true,
        };
        this.relations = {
            '../back-top/back-top': {
                type: 'descendant',
            },
        };
        this.properties = props;
        this.data = {
            prefix,
            classPrefix: name,
            barHeight: 0,
            refreshStatus: -1,
            loosing: false,
            enableToRefresh: true,
            scrollTop: 0,
        };
        this.lifetimes = {
            attached() {
                const { screenWidth } = wx.getSystemInfoSync();
                const { loadingBarHeight, loadingTexts } = this.properties;
                this.setData({
                    loadingTexts: Array.isArray(loadingTexts) && loadingTexts.length >= 4
                        ? loadingTexts
                        : ['下拉刷新', '松手刷新', '正在刷新', '刷新完成'],
                });
                this.pixelRatio = 750 / screenWidth;
                Object.defineProperty(this, 'maxBarHeight', {
                    get() {
                        return unitConvert(this.data.maxBarHeight);
                    },
                });
                Object.defineProperty(this, 'loadingBarHeight', {
                    get() {
                        return unitConvert(this.data.loadingBarHeight);
                    },
                });
                if (loadingBarHeight) {
                    this.setData({
                        computedLoadingBarHeight: unitConvert(loadingBarHeight),
                    });
                }
            },
            detached() {
                clearTimeout(this.maxRefreshAnimateTimeFlag);
                clearTimeout(this.closingAnimateTimeFlag);
            },
        };
        this.observers = {
            value(val) {
                if (!val) {
                    clearTimeout(this.maxRefreshAnimateTimeFlag);
                    if (this.data.refreshStatus > 0) {
                        this.setData({
                            refreshStatus: 3,
                        });
                    }
                    this.setData({ barHeight: 0 });
                }
                else {
                    this.doRefresh();
                }
            },
        };
        this.methods = {
            onScrollToBottom() {
                this.triggerEvent('scrolltolower');
            },
            onScrollToTop() {
                this.setData({
                    enableToRefresh: true,
                });
            },
            onScroll(e) {
                const { scrollTop } = e.detail;
                this.setData({
                    enableToRefresh: scrollTop === 0,
                });
                this.triggerEvent('scroll', { scrollTop });
            },
            onTouchStart(e) {
                if (this.isPulling || !this.data.enableToRefresh)
                    return;
                const { touches } = e;
                if (touches.length !== 1)
                    return;
                const { pageX, pageY } = touches[0];
                this.setData({ loosing: false });
                this.startPoint = { pageX, pageY };
                this.isPulling = true;
            },
            onTouchMove(e) {
                if (!this.startPoint)
                    return;
                const { touches } = e;
                if (touches.length !== 1)
                    return;
                const { pageY } = touches[0];
                const offset = pageY - this.startPoint.pageY;
                if (offset > 0) {
                    this.setRefreshBarHeight(offset);
                }
            },
            onTouchEnd(e) {
                if (!this.startPoint)
                    return;
                const { changedTouches } = e;
                if (changedTouches.length !== 1)
                    return;
                const { pageY } = changedTouches[0];
                const barHeight = pageY - this.startPoint.pageY;
                this.startPoint = null;
                this.isPulling = false;
                this.setData({ loosing: true });
                if (barHeight > this.loadingBarHeight) {
                    this._trigger('change', { value: true });
                    this.triggerEvent('refresh');
                }
                else {
                    this.setData({ barHeight: 0 });
                }
            },
            onDragStart(e) {
                const { scrollTop, scrollLeft } = e.detail;
                this.triggerEvent('dragstart', { scrollTop, scrollLeft });
            },
            onDragging(e) {
                const { scrollTop, scrollLeft } = e.detail;
                this.triggerEvent('dragging', { scrollTop, scrollLeft });
            },
            onDragEnd(e) {
                const { scrollTop, scrollLeft } = e.detail;
                this.triggerEvent('dragend', { scrollTop, scrollLeft });
            },
            doRefresh() {
                this.setData({
                    barHeight: this.loadingBarHeight,
                    refreshStatus: 2,
                    loosing: true,
                });
                this.maxRefreshAnimateTimeFlag = setTimeout(() => {
                    this.maxRefreshAnimateTimeFlag = null;
                    if (this.data.refreshStatus === 2) {
                        this.triggerEvent('timeout');
                        this._trigger('change', { value: false });
                    }
                }, this.properties.refreshTimeout);
            },
            setRefreshBarHeight(value) {
                const barHeight = Math.min(value, this.maxBarHeight);
                const data = { barHeight };
                if (barHeight >= this.loadingBarHeight) {
                    data.refreshStatus = 1;
                }
                else {
                    data.refreshStatus = 0;
                }
                return new Promise((resolve) => {
                    this.setData(data, () => resolve(barHeight));
                });
            },
            setScrollTop(scrollTop) {
                this.setData({ scrollTop });
            },
            scrollToTop() {
                this.setScrollTop(0);
            },
        };
    }
};
PullDownRefresh = __decorate([
    wxComponent()
], PullDownRefresh);
export default PullDownRefresh;