管灌系统农户端微信小程序(嘉峪关应用)
zuoxiao
2024-02-26 3f7ec892d68f71e996489d14d9918dc332281b1e
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
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, getRect } from '../common/utils';
const { prefix } = config;
const name = `${prefix}-rate`;
let Rate = class Rate extends SuperComponent {
    constructor() {
        super(...arguments);
        this.externalClasses = [`${prefix}-class`, `${prefix}-class-icon`, `${prefix}-class-text`];
        this.properties = props;
        this.controlledProps = [
            {
                key: 'value',
                event: 'change',
            },
        ];
        this.data = {
            prefix,
            classPrefix: name,
            defaultTexts: ['极差', '失望', '一般', '满意', '惊喜'],
            tipsVisible: false,
            tipsLeft: 0,
            actionType: '',
            scaleIndex: -1,
            isVisibleToScreenReader: false,
        };
        this.methods = {
            onTouch(e, eventType) {
                const { count, allowHalf, gap, value: currentValue, size } = this.properties;
                const [touch] = e.changedTouches;
                const margin = unitConvert(gap);
                getRect(this, `.${name}__wrapper`).then((rect) => {
                    const { width, left } = rect;
                    const starWidth = (width - (count - 1) * margin) / count;
                    const offsetX = touch.pageX - left;
                    const num = (offsetX + margin) / (starWidth + margin);
                    const remainder = num % 1;
                    const integral = num - remainder;
                    let value = remainder <= 0.5 && allowHalf ? integral + 0.5 : integral + 1;
                    if (value > count) {
                        value = count;
                    }
                    else if (value < 0) {
                        value = 0;
                    }
                    if (eventType === 'move' || (eventType === 'tap' && allowHalf)) {
                        const left = Math.ceil(value - 1) * (unitConvert(gap) + unitConvert(size)) + unitConvert(size) * 0.5;
                        this.setData({
                            tipsVisible: true,
                            actionType: eventType,
                            scaleIndex: Math.ceil(value),
                            tipsLeft: Math.max(left, 0),
                        });
                    }
                    if (value !== currentValue) {
                        this._trigger('change', { value });
                    }
                    if (this.touchEnd) {
                        this.hideTips();
                    }
                });
            },
            onTap(e) {
                this.onTouch(e, 'tap');
            },
            onTouchStart() {
                this.touchEnd = false;
            },
            onTouchMove(e) {
                this.onTouch(e, 'move');
                this.showAlertText();
            },
            onTouchEnd() {
                this.touchEnd = true;
                this.hideTips();
            },
            hideTips() {
                if (this.data.actionType === 'move') {
                    this.setData({ tipsVisible: false, scaleIndex: -1 });
                }
            },
            onSelect(e) {
                const { value } = e.currentTarget.dataset;
                const { actionType } = this.data;
                if (actionType === 'move')
                    return;
                this._trigger('change', { value });
                setTimeout(() => this.setData({ tipsVisible: false, scaleIndex: -1 }), 300);
            },
            showAlertText() {
                if (this.data.isVisibleToScreenReader === true)
                    return;
                this.setData({
                    isVisibleToScreenReader: true,
                });
                setTimeout(() => {
                    this.setData({
                        isVisibleToScreenReader: false,
                    });
                }, 2e3);
            },
        };
    }
};
Rate = __decorate([
    wxComponent()
], Rate);
export default Rate;