管灌系统农户端微信小程序(嘉峪关应用)
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
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 props from './props';
import config from '../common/config';
import pageScrollMixin from '../mixins/page-scroll';
import { getRect } from '../common/utils';
const { prefix } = config;
const name = `${prefix}-sticky`;
const ContainerClass = `.${name}`;
let Sticky = class Sticky extends SuperComponent {
    constructor() {
        super(...arguments);
        this.externalClasses = [`${prefix}-class`, `${prefix}-class-content`];
        this.properties = props;
        this.behaviors = [pageScrollMixin()];
        this.observers = {
            'offsetTop, disabled, container'() {
                this.onScroll();
            },
        };
        this.data = {
            prefix,
            classPrefix: name,
            containerStyle: '',
            contentStyle: '',
        };
        this.methods = {
            onScroll(event) {
                const { scrollTop } = event || {};
                const { container, offsetTop, disabled } = this.properties;
                if (disabled) {
                    this.setDataAfterDiff({
                        isFixed: false,
                        transform: 0,
                    });
                    return;
                }
                this.scrollTop = scrollTop || this.scrollTop;
                if (typeof container === 'function') {
                    Promise.all([getRect(this, ContainerClass), this.getContainerRect()]).then(([root, container]) => {
                        if (!root || !container)
                            return;
                        if (offsetTop + root.height > container.height + container.top) {
                            this.setDataAfterDiff({
                                isFixed: false,
                                transform: container.height - root.height,
                            });
                        }
                        else if (offsetTop >= root.top) {
                            this.setDataAfterDiff({
                                isFixed: true,
                                height: root.height,
                                transform: 0,
                            });
                        }
                        else {
                            this.setDataAfterDiff({ isFixed: false, transform: 0 });
                        }
                    });
                    return;
                }
                getRect(this, ContainerClass).then((root) => {
                    if (!root)
                        return;
                    if (offsetTop >= root.top) {
                        this.setDataAfterDiff({ isFixed: true, height: root.height });
                        this.transform = 0;
                    }
                    else {
                        this.setDataAfterDiff({ isFixed: false });
                    }
                });
            },
            setDataAfterDiff(data) {
                const { offsetTop } = this.properties;
                const { containerStyle: prevContainerStyle, contentStyle: prevContentStyle } = this.data;
                const { isFixed, height, transform } = data;
                wx.nextTick(() => {
                    let containerStyle = '';
                    let contentStyle = '';
                    if (isFixed) {
                        containerStyle += `height:${height}px;`;
                        contentStyle += `position:fixed;top:${offsetTop}px;left:0;right:0;`;
                    }
                    if (transform) {
                        const translate = `translate3d(0, ${transform}px, 0)`;
                        contentStyle += `-webkit-transform:${translate};transform:${translate};`;
                    }
                    if (prevContainerStyle !== containerStyle || prevContentStyle !== contentStyle) {
                        this.setData({ containerStyle, contentStyle });
                    }
                    this.triggerEvent('scroll', {
                        scrollTop: this.scrollTop,
                        isFixed,
                    });
                });
            },
            getContainerRect() {
                const nodesRef = this.properties.container();
                return new Promise((resolve) => nodesRef.boundingClientRect(resolve).exec());
            },
        };
    }
    ready() {
        this.onScroll();
    }
};
Sticky = __decorate([
    wxComponent()
], Sticky);
export default Sticky;