Novecane
2025-02-15 a12e5f63a90446525b2f322fb2bbd21d5285cd6d
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
<template>
    <view class="uni-indexed-list" ref="list" id="list">
        <!-- #ifdef APP-NVUE -->
        <list class="uni-indexed-list__scroll" scrollable="true" show-scrollbar="false">
            <cell v-for="(list, idx) in lists" :key="idx" :ref="'uni-indexed-list-' + idx">
                <!-- #endif -->
                <!-- #ifndef APP-NVUE -->
                <scroll-view :scroll-into-view="scrollViewId" class="uni-indexed-list__scroll" scroll-y>
                    <view v-for="(list, idx) in lists" :key="idx" :id="'uni-indexed-list-' + idx">
                        <!-- #endif -->
                        <indexed-list-item :list="list" :loaded="loaded" :idx="idx" :showSelect="showSelect"
                            @itemClick="onClick"></indexed-list-item>
                        <!-- #ifndef APP-NVUE -->
                    </view>
                </scroll-view>
                <!-- #endif -->
                <!-- #ifdef APP-NVUE -->
            </cell>
        </list>
        <!-- #endif -->
        <view class="uni-indexed-list__menu" @touchstart="touchStart" @touchmove.stop.prevent="touchMove"
            @touchend="touchEnd" @mousedown.stop="mousedown" @mousemove.stop.prevent="mousemove"
            @mouseleave.stop="mouseleave">
            <view v-for="(list, key) in lists" :key="key" class="uni-indexed-list__menu-item"
                :class="touchmoveIndex == key ? 'uni-indexed-list__menu--active' : ''">
                <text class="uni-indexed-list__menu-text"
                    :class="touchmoveIndex == key ? 'uni-indexed-list__menu-text--active' : ''">{{ list.key }}</text>
            </view>
        </view>
        <view v-if="touchmove" class="uni-indexed-list__alert-wrapper">
            <text class="uni-indexed-list__alert">{{ lists[touchmoveIndex].key }}</text>
        </view>
    </view>
</template>
<script>
    import indexedListItem from './uni-indexed-list-item.vue'
    // #ifdef APP-NVUE
    const dom = weex.requireModule('dom');
    // #endif
    // #ifdef APP-PLUS
    function throttle(func, delay) {
        var prev = Date.now();
        return function() {
            var context = this;
            var args = arguments;
            var now = Date.now();
            if (now - prev >= delay) {
                func.apply(context, args);
                prev = Date.now();
            }
        }
    }
 
    function touchMove(e) {
        let pageY = e.touches[0].pageY
        let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
        if (this.touchmoveIndex === index) {
            return false
        }
        let item = this.lists[index]
        if (item) {
            // #ifndef APP-NVUE
            this.scrollViewId = 'uni-indexed-list-' + index
            this.touchmoveIndex = index
            // #endif
            // #ifdef APP-NVUE
            dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
                animated: false
            })
            this.touchmoveIndex = index
            // #endif
        }
    }
    const throttleTouchMove = throttle(touchMove, 40)
    // #endif
 
    /**
     * IndexedList 索引列表
     * @description 用于展示索引列表
     * @tutorial https://ext.dcloud.net.cn/plugin?id=375
     * @property {Boolean} showSelect = [true|false] 展示模式
     *     @value true 展示模式
     *     @value false 选择模式
     * @property {Object} options 索引列表需要的数据对象
     * @event {Function} click 点击列表事件 ,返回当前选择项的事件对象
     * @example <uni-indexed-list options="" showSelect="false" @click=""></uni-indexed-list>
     */
    export default {
        name: 'UniIndexedList',
        components: {
            indexedListItem
        },
        emits: ['click'],
        props: {
            options: {
                type: Array,
                default () {
                    return []
                }
            },
            showSelect: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                lists: [],
                winHeight: 0,
                itemHeight: 0,
                winOffsetY: 0,
                touchmove: false,
                touchmoveIndex: -1,
                scrollViewId: '',
                touchmovable: true,
                loaded: false,
                isPC: false
            }
        },
        watch: {
            options: {
                handler: function() {
                    this.setList()
                },
                deep: true
            }
        },
        mounted() {
            // #ifdef H5
            this.isPC = this.IsPC()
            // #endif
            setTimeout(() => {
                this.setList()
            }, 50)
            setTimeout(() => {
                this.loaded = true
            }, 300);
        },
        methods: {
            setList() {
                let index = 0;
                this.lists = []
                this.options.forEach((value, index) => {
                    if (value.data.length === 0) {
                        return
                    }
                    let indexBefore = index
                    let items = value.data.map(item => {
                        let obj = {}
                        obj['key'] = value.letter
                        obj['name'] = item
                        obj['itemIndex'] = index
                        index++
                        obj.checked = item.checked ? item.checked : false
                        return obj
                    })
                    this.lists.push({
                        title: value.letter,
                        key: value.letter,
                        items: items,
                        itemIndex: indexBefore
                    })
                })
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    .in(this)
                    .select('#list')
                    .boundingClientRect()
                    .exec(ret => {
                        this.winOffsetY = ret[0].top
                        this.winHeight = ret[0].height
                        this.itemHeight = this.winHeight / this.lists.length
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['list'], (res) => {
                    this.winOffsetY = res.size.top
                    this.winHeight = res.size.height
                    this.itemHeight = this.winHeight / this.lists.length
                })
                // #endif
            },
            touchStart(e) {
                this.touchmove = true
                let pageY = this.isPC ? e.pageY : e.touches[0].pageY
                let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
                let item = this.lists[index]
                if (item) {
                    this.scrollViewId = 'uni-indexed-list-' + index
                    this.touchmoveIndex = index
                    // #ifdef APP-NVUE
                    dom.scrollToElement(this.$refs['uni-indexed-list-' + index][0], {
                        animated: false
                    })
                    // #endif
                }
            },
            touchMove(e) {
                // #ifndef APP-PLUS
                let pageY = this.isPC ? e.pageY : e.touches[0].pageY
                let index = Math.floor((pageY - this.winOffsetY) / this.itemHeight)
                if (this.touchmoveIndex === index) {
                    return false
                }
                let item = this.lists[index]
                if (item) {
                    this.scrollViewId = 'uni-indexed-list-' + index
                    this.touchmoveIndex = index
                }
                // #endif
                // #ifdef APP-PLUS
                throttleTouchMove.call(this, e)
                // #endif
            },
            touchEnd() {
                this.touchmove = false
                // this.touchmoveIndex = -1
            },
 
            /**
             * 兼容 PC @tian
             */
 
            mousedown(e) {
                if (!this.isPC) return
                this.touchStart(e)
            },
            mousemove(e) {
                if (!this.isPC) return
                this.touchMove(e)
            },
            mouseleave(e) {
                if (!this.isPC) return
                this.touchEnd(e)
            },
 
            // #ifdef H5
            IsPC() {
                var userAgentInfo = navigator.userAgent;
                var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
                var flag = true;
                for (let v = 0; v < Agents.length - 1; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                        flag = false;
                        break;
                    }
                }
                return flag;
            },
            // #endif
 
 
            onClick(e) {
                let {
                    idx,
                    index
                } = e
                let obj = {}
                for (let key in this.lists[idx].items[index]) {
                    obj[key] = this.lists[idx].items[index][key]
                }
                let select = []
                if (this.showSelect) {
                    this.lists[idx].items[index].checked = !this.lists[idx].items[index].checked
                    this.lists.forEach((value, idx) => {
                        value.items.forEach((item, index) => {
                            if (item.checked) {
                                let obj = {}
                                for (let key in this.lists[idx].items[index]) {
                                    obj[key] = this.lists[idx].items[index][key]
                                }
                                select.push(obj)
                            }
                        })
                    })
                }
                this.$emit('click', {
                    item: obj,
                    select: select
                })
            }
        }
    }
</script>
<style lang="scss" scoped>
    .uni-indexed-list {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
    }
 
    .uni-indexed-list__scroll {
        flex: 1;
    }
 
    .uni-indexed-list__menu {
        width: 24px;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: column;
    }
 
    .uni-indexed-list__menu-item {
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex: 1;
        align-items: center;
        justify-content: center;
        /* #ifdef H5 */
        cursor: pointer;
        /* #endif */
    }
 
    .uni-indexed-list__menu-text {
        font-size: 12px;
        text-align: center;
        color: #aaa;
    }
 
    .uni-indexed-list__menu--active {
        // background-color: rgb(200, 200, 200);
    }
 
    .uni-indexed-list__menu--active {}
 
    .uni-indexed-list__menu-text--active {
        border-radius: 16px;
        width: 16px;
        height: 16px;
        line-height: 16px;
        background-color: #007aff;
        color: #fff;
    }
 
    .uni-indexed-list__alert-wrapper {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        /* #ifndef APP-NVUE */
        display: flex;
        /* #endif */
        flex-direction: row;
        align-items: center;
        justify-content: center;
    }
 
    .uni-indexed-list__alert {
        width: 80px;
        height: 80px;
        border-radius: 80px;
        text-align: center;
        line-height: 80px;
        font-size: 35px;
        color: #fff;
        background-color: rgba(0, 0, 0, 0.5);
    }
</style>