沙盘演示系统应用的微信小程序
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
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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { SuperComponent, wxComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
import { getRect } from '../common/utils';
const { prefix } = config;
const name = `${prefix}-cascader`;
let Cascader = class Cascader extends SuperComponent {
    constructor() {
        super(...arguments);
        this.externalClasses = [`${prefix}-class`];
        this.options = {
            multipleSlots: true,
        };
        this.properties = props;
        this.controlledProps = [
            {
                key: 'value',
                event: 'change',
            },
        ];
        this.data = {
            prefix,
            name,
            stepIndex: 0,
            selectedIndexes: [],
            selectedValue: [],
            scrollTopList: [],
            steps: [],
        };
        this.observers = {
            visible(v) {
                if (v) {
                    const $tabs = this.selectComponent('#tabs');
                    $tabs === null || $tabs === void 0 ? void 0 : $tabs.setTrack();
                    this.updateScrollTop();
                    this.initWithValue();
                }
            },
            value() {
                this.initWithValue();
            },
            'selectedIndexes, options'() {
                var _a, _b, _c, _d;
                const { options, selectedIndexes, keys, placeholder } = this.data;
                const selectedValue = [];
                const steps = [];
                const items = [options];
                if (options.length > 0) {
                    for (let i = 0, size = selectedIndexes.length; i < size; i += 1) {
                        const index = selectedIndexes[i];
                        const next = items[i][index];
                        selectedValue.push(next[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value']);
                        steps.push(next[(_b = keys === null || keys === void 0 ? void 0 : keys.label) !== null && _b !== void 0 ? _b : 'label']);
                        if (next[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children']) {
                            items.push(next[(_d = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _d !== void 0 ? _d : 'children']);
                        }
                    }
                }
                if (steps.length < items.length) {
                    steps.push(placeholder);
                }
                this.setData({
                    steps,
                    items,
                    selectedValue,
                    stepIndex: items.length - 1,
                });
            },
            stepIndex() {
                return __awaiter(this, void 0, void 0, function* () {
                    const { visible } = this.data;
                    if (visible) {
                        this.updateScrollTop();
                    }
                });
            },
        };
        this.methods = {
            initWithValue() {
                if (this.data.value != null && this.data.value !== '') {
                    const selectedIndexes = this.getIndexesByValue(this.data.options, this.data.value);
                    if (selectedIndexes) {
                        this.setData({ selectedIndexes });
                    }
                }
                else {
                    this.setData({ selectedIndexes: [] });
                }
            },
            getIndexesByValue(options, value) {
                var _a, _b, _c;
                const { keys } = this.data;
                for (let i = 0, size = options.length; i < size; i += 1) {
                    const opt = options[i];
                    if (opt[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value) {
                        return [i];
                    }
                    if (opt[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) {
                        const res = this.getIndexesByValue(opt[(_c = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _c !== void 0 ? _c : 'children'], value);
                        if (res) {
                            return [i, ...res];
                        }
                    }
                }
            },
            updateScrollTop() {
                const { visible, items, selectedIndexes, stepIndex } = this.data;
                if (visible) {
                    getRect(this, '.cascader-radio-group-0').then((rect) => {
                        var _a;
                        const eachRadioHeight = rect.height / ((_a = items[0]) === null || _a === void 0 ? void 0 : _a.length);
                        this.setData({
                            [`scrollTopList[${stepIndex}]`]: eachRadioHeight * selectedIndexes[stepIndex],
                        });
                    });
                }
            },
            hide(trigger) {
                this.setData({ visible: false });
                this.triggerEvent('close', { trigger: trigger });
            },
            onVisibleChange() {
                this.hide('overlay');
            },
            onClose() {
                this.hide('close-btn');
            },
            onStepClick(e) {
                const { index } = e.currentTarget.dataset;
                this.setData({ stepIndex: index });
            },
            onTabChange(e) {
                const { value } = e.detail;
                this.setData({
                    stepIndex: value,
                });
            },
            handleSelect(e) {
                var _a, _b, _c;
                const { level } = e.target.dataset;
                const { value } = e.detail;
                const { selectedIndexes, items, keys } = this.data;
                const index = items[level].findIndex((item) => { var _a; return item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'] === value; });
                const item = items[level][index];
                if (item.disabled) {
                    return;
                }
                selectedIndexes[level] = index;
                selectedIndexes.length = level + 1;
                this.triggerEvent('pick', { value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'], index, level });
                if ((_c = item === null || item === void 0 ? void 0 : item[(_b = keys === null || keys === void 0 ? void 0 : keys.children) !== null && _b !== void 0 ? _b : 'children']) === null || _c === void 0 ? void 0 : _c.length) {
                    this.setData({ selectedIndexes });
                }
                else {
                    this.setData({ selectedIndexes }, () => {
                        var _a;
                        const { items } = this.data;
                        this._trigger('change', {
                            value: item[(_a = keys === null || keys === void 0 ? void 0 : keys.value) !== null && _a !== void 0 ? _a : 'value'],
                            selectedOptions: items.map((item, index) => item[selectedIndexes[index]]),
                        });
                    });
                    this.hide('finish');
                }
            },
        };
    }
};
Cascader = __decorate([
    wxComponent()
], Cascader);
export default Cascader;