Blame view

uni_modules/uview-plus/components/u-list-item/u-list-item.vue 2.7 KB
a2702f6d   刘淇   巡查计划
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
  <template>
  	<!-- #ifdef APP-NVUE -->
  	<cell>
  		<!-- #endif -->
  		<view
  			class="u-list-item"
  			:ref="`u-list-item-${anchor}`"
  			:anchor="`u-list-item-${anchor}`"
  			:class="[`u-list-item-${anchor}`]"
  		>
  			<slot />
  		</view>
  		<!-- #ifdef APP-NVUE -->
  	</cell>
  	<!-- #endif -->
  </template>
  
  <script>
  	import { props } from './props';
  	import { mpMixin } from '../../libs/mixin/mpMixin';
  	import { mixin } from '../../libs/mixin/mixin';
  	import { getWindowInfo } from '../../libs/function/index';
  	// #ifdef APP-NVUE
  	const dom = uni.requireNativePlugin('dom')
  	// #endif
  	/**
  	 * List 列表
  	 * @description 该组件为高性能列表组件
  	 * @tutorial https://uview-plus.jiangruyi.com/components/list.html
  	 * @property {String | Number}	anchor	用于滚动到指定item
  	 * @example <u-list-ite v-for="(item, index) in indexList" :key="index" ></u-list-item>
  	 */
  	export default {
  		name: 'u-list-item',
  		mixins: [mpMixin, mixin, props],
  		data() {
  			return {
  				// 节点信息
  				rect: {},
  				index: 0,
  				show: true,
  				sys: getWindowInfo()
  			}
  		},
  		computed: {
  
  		},
  		inject: ['uList'],
  		watch: {
  			// #ifndef APP-NVUE
  			'uList.innerScrollTop'(n) {
  				const preLoadScreen = this.uList.preLoadScreen
  				const windowHeight = this.sys.windowHeight
  				if(n <= windowHeight * preLoadScreen) {
  					this.parent.updateOffsetFromChild(0)
  				} else if (this.rect.top <= n - windowHeight * preLoadScreen) {
  					this.parent.updateOffsetFromChild(this.rect.top)
  				}
  			}
  			// #endif
  		},
  		created() {
  			this.parent = {}
  		},
  		mounted() {
  			this.init()
  		},
  		methods: {
  			init() {
  				// 初始化数据
  				this.updateParentData()
  				this.index = this.parent.children.indexOf(this)
  				this.resize()
  			},
  			updateParentData() {
  				// 此方法在mixin中
  				this.getParentData('u-list')
  			},
  			resize() {
  				this.queryRect(`u-list-item-${this.anchor}`).then(size => {
  					const lastChild = this.parent.children[this.index - 1]
  					this.rect = size
  					const preLoadScreen = this.uList.preLoadScreen
  					const windowHeight = this.sys.windowHeight
  					// #ifndef APP-NVUE
  					if (lastChild) {
  						this.rect.top = lastChild.rect.top + lastChild.rect.height
  					}
  					if (size.top >= this.uList.innerScrollTop + (1 + preLoadScreen) * windowHeight) this.show =
  						false
  					// #endif
  				})
  			},
  			// 查询元素尺寸
  			queryRect(el) {
  				return new Promise(resolve => {
  					// #ifndef APP-NVUE
  					this.$uGetRect(`.${el}`).then(size => {
  						resolve(size)
  					})
  					// #endif
  
  					// #ifdef APP-NVUE
  					const ref = this.$refs[el]
  					dom.getComponentRect(ref, res => {
  						resolve(res.size)
  					})
  					// #endif
  				})
  			}
  		}
  	}
  </script>
  
  <style lang="scss" scoped>
  
  	.u-list-item {}
  </style>