nvue.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // [z-paging]nvue独有部分模块
  2. import u from '.././z-paging-utils'
  3. import c from '.././z-paging-constant'
  4. import Enum from '.././z-paging-enum'
  5. // #ifdef APP-NVUE
  6. const weexAnimation = weex.requireModule('animation');
  7. // #endif
  8. export default {
  9. props: {
  10. // #ifdef APP-NVUE
  11. // nvue中修改列表类型,可选值有list、waterfall和scroller,默认为list
  12. nvueListIs: {
  13. type: String,
  14. default: u.gc('nvueListIs', 'list')
  15. },
  16. // nvue waterfall配置,仅在nvue中且nvueListIs=waterfall时有效,配置参数详情参见:https://uniapp.dcloud.io/component/waterfall
  17. nvueWaterfallConfig: {
  18. type: Object,
  19. default: u.gc('nvueWaterfallConfig', {})
  20. },
  21. // nvue 控制是否回弹效果,iOS不支持动态修改
  22. nvueBounce: {
  23. type: Boolean,
  24. default: u.gc('nvueBounce', true)
  25. },
  26. // nvue中通过代码滚动到顶部/底部时,是否加快动画效果(无滚动动画时无效),默认为否
  27. nvueFastScroll: {
  28. type: Boolean,
  29. default: u.gc('nvueFastScroll', false)
  30. },
  31. // nvue中list的id
  32. nvueListId: {
  33. type: String,
  34. default: u.gc('nvueListId', '')
  35. },
  36. // nvue中refresh组件的样式
  37. nvueRefresherStyle: {
  38. type: Object,
  39. default: u.gc('nvueRefresherStyle', {})
  40. },
  41. // nvue中是否按分页模式(类似竖向swiper)显示List,默认为false
  42. nvuePagingEnabled: {
  43. type: Boolean,
  44. default: u.gc('nvuePagingEnabled', false)
  45. },
  46. // 是否隐藏nvue列表底部的tagView,此view用于标识滚动到底部位置,若隐藏则滚动到底部功能将失效,在nvue中实现吸顶+swiper功能时需将最外层z-paging的此属性设置为true。默认为否
  47. hideNvueBottomTag: {
  48. type: Boolean,
  49. default: u.gc('hideNvueBottomTag', false)
  50. },
  51. // nvue中控制onscroll事件触发的频率:表示两次onscroll事件之间列表至少滚动了10px。注意,将该值设置为较小的数值会提高滚动事件采样的精度,但同时也会降低页面的性能
  52. offsetAccuracy: {
  53. type: Number,
  54. default: u.gc('offsetAccuracy', 10)
  55. },
  56. // #endif
  57. },
  58. data() {
  59. return {
  60. nRefresherLoading: false,
  61. nListIsDragging: false,
  62. nShowBottom: true,
  63. nFixFreezing: false,
  64. nShowRefresherReveal: false,
  65. nLoadingMoreFixedHeight: false,
  66. nShowRefresherRevealHeight: 0,
  67. nOldShowRefresherRevealHeight: -1,
  68. nRefresherWidth: u.rpx2px(750),
  69. nListHeight: 0,
  70. nF2Opacity: 0
  71. }
  72. },
  73. computed: {
  74. // #ifdef APP-NVUE
  75. nScopedSlots() {
  76. // #ifdef VUE2
  77. return this.$scopedSlots;
  78. // #endif
  79. // #ifdef VUE3
  80. return null;
  81. // #endif
  82. },
  83. nWaterfallColumnCount() {
  84. if (this.finalNvueListIs !== 'waterfall') return 0;
  85. return this._nGetWaterfallConfig('column-count', 2);
  86. },
  87. nWaterfallColumnWidth() {
  88. return this._nGetWaterfallConfig('column-width', 'auto');
  89. },
  90. nWaterfallColumnGap() {
  91. return this._nGetWaterfallConfig('column-gap', 'normal');
  92. },
  93. nWaterfallLeftGap() {
  94. return this._nGetWaterfallConfig('left-gap', 0);
  95. },
  96. nWaterfallRightGap() {
  97. return this._nGetWaterfallConfig('right-gap', 0);
  98. },
  99. nViewIs() {
  100. const is = this.finalNvueListIs;
  101. return is === 'scroller' || is === 'view' ? 'view' : is === 'waterfall' ? 'header' : 'cell';
  102. },
  103. nSafeAreaBottomHeight() {
  104. return this.safeAreaInsetBottom ? this.safeAreaBottom : 0;
  105. },
  106. finalNvueListIs() {
  107. if (this.usePageScroll) return 'view';
  108. const nvueListIsLowerCase = this.nvueListIs.toLowerCase();
  109. if (['list','waterfall','scroller'].indexOf(nvueListIsLowerCase) !== -1) return nvueListIsLowerCase;
  110. return 'list';
  111. },
  112. finalNvueSuperListIs() {
  113. return this.usePageScroll ? 'view' : 'scroller';
  114. },
  115. finalNvueRefresherEnabled() {
  116. return this.finalNvueListIs !== 'view' && this.finalRefresherEnabled && !this.nShowRefresherReveal && !this.useChatRecordMode;
  117. },
  118. // #endif
  119. },
  120. mounted(){
  121. // #ifdef APP-NVUE
  122. //旋转屏幕时更新宽度
  123. uni.onWindowResize((res) => {
  124. // this._nUpdateRefresherWidth();
  125. })
  126. // #endif
  127. },
  128. methods: {
  129. // #ifdef APP-NVUE
  130. // 列表滚动时触发
  131. _nOnScroll(e) {
  132. this.$emit('scroll', e);
  133. const scrollTop = -e.contentOffset.y;
  134. const scrollHeight = e.contentSize.height;
  135. if (this.watchScrollDirectionChange) {
  136. // 计算scroll-view滚动方向,正常情况下上次滚动的oldScrollTop大于当前scrollTop即为向上滚动,反之为向下滚动
  137. let direction = this.oldScrollTop > scrollTop ? 'top' : 'bottom';
  138. // 此处为解决在iOS中,滚动到顶部因bounce的影响回弹导致滚动方向为bottom的问题:如果滚动到顶部了并且scrollTop小于顶部滚动区域,则强制设置direction为top
  139. if (scrollTop <= 0) {
  140. direction = 'top';
  141. }
  142. // 此处为解决在iOS中,滚动到底部因bounce的影响回弹导致滚动方向为top的问题:如果滚动到底部了并且scrollTop超过底部滚动区域,则强制设置direction为bottom
  143. if (scrollTop > this.lastScrollHeight - this.nListHeight - 1) {
  144. direction = 'bottom';
  145. }
  146. // emit 列表滚动方向改变事件
  147. if (direction !== this.lastScrollDirection) {
  148. this.$emit('scrollDirectionChange', direction);
  149. this.lastScrollDirection = direction;
  150. }
  151. // 当scrollHeight变化时,需要延迟100毫秒设置lastScrollHeight,如果直接根据scrollHeight的话,因为此时数据还未改变,会导致滚动方向从bottom变为top
  152. if (this.lastScrollHeight !== scrollHeight && !this.setContentHeightPending) {
  153. // 因此处会多次触发,因此加个标识确保在延时期间仅触发一次
  154. this.setContentHeightPending = true;
  155. u.delay(() => {
  156. this.lastScrollHeight = scrollHeight;
  157. this.setContentHeightPending = false;
  158. })
  159. }
  160. }
  161. this.oldScrollTop = scrollTop;
  162. this.nListIsDragging = e.isDragging;
  163. this._checkShouldShowBackToTop(scrollTop, scrollTop - 1);
  164. },
  165. // 列表滚动结束
  166. _nOnScrollend(e) {
  167. this.$emit('scrollend', e);
  168. // 判断是否滚动到顶部了
  169. if (e?.contentOffset?.y >= 0) {
  170. this._emitScrollEvent('scrolltoupper');
  171. }
  172. // 判断是否滚动到底部了
  173. this._getNodeClientRect('.zp-n-list').then(node => {
  174. if (node) {
  175. this.nListHeight = node[0].height;
  176. if (e?.contentSize?.height + e?.contentOffset?.y <= node[0].height) {
  177. this._emitScrollEvent('scrolltolower');
  178. }
  179. }
  180. })
  181. },
  182. // 下拉刷新刷新中
  183. _nOnRrefresh() {
  184. if (this.nShowRefresherReveal) return;
  185. // 进入刷新状态
  186. this.nRefresherLoading = true;
  187. if (this.refresherStatus === Enum.Refresher.GoF2) {
  188. this._handleGoF2();
  189. this.$nextTick(() => {
  190. this._nRefresherEnd();
  191. })
  192. } else {
  193. this.refresherStatus = Enum.Refresher.Loading;
  194. this._doRefresherLoad();
  195. }
  196. },
  197. // 下拉刷新下拉中
  198. _nOnPullingdown(e) {
  199. if (this.refresherStatus === Enum.Refresher.Loading || (this.isIos && !this.nListIsDragging)) return;
  200. this._emitTouchmove(e);
  201. let { viewHeight, pullingDistance } = e;
  202. // 更新下拉刷新状态
  203. // 下拉刷新距离超过阈值
  204. if (pullingDistance >= viewHeight) {
  205. // 如果开启了下拉进入二楼并且下拉刷新距离超过进入二楼阈值,则当前下拉刷新状态为松手进入二楼,否则为松手立即刷新
  206. // (pullingDistance - viewHeight) + this.finalRefresherThreshold 不等同于pullingDistance,此处是为了兼容不同平台下拉相同距离pullingDistance不一致的问题,pullingDistance仅与viewHeight互相关联
  207. this.refresherStatus = this.refresherF2Enabled && (pullingDistance - viewHeight) + this.finalRefresherThreshold >= this.finalRefresherF2Threshold ? Enum.Refresher.GoF2 : Enum.Refresher.ReleaseToRefresh;
  208. } else {
  209. // 下拉刷新距离未超过阈值,显示默认状态
  210. this.refresherStatus = Enum.Refresher.Default;
  211. }
  212. },
  213. // 下拉刷新结束
  214. _nRefresherEnd(doEnd = true) {
  215. if (doEnd) {
  216. this._nDoRefresherEndAnimation(0, -this.nShowRefresherRevealHeight);
  217. !this.usePageScroll && this.$refs['zp-n-list'].resetLoadmore();
  218. this.nRefresherLoading = false;
  219. }
  220. },
  221. // 执行主动触发下拉刷新动画
  222. _nDoRefresherEndAnimation(height, translateY, animate = true, checkStack = true) {
  223. // 清除下拉刷新相关timeout
  224. this._cleanRefresherCompleteTimeout();
  225. this._cleanRefresherEndTimeout();
  226. if (!this.finalShowRefresherWhenReload) {
  227. // 如果reload不需要自动展示下拉刷新view,则在complete duration结束后再把下拉刷新状态设置回默认
  228. this.refresherEndTimeout = u.delay(() => {
  229. this.refresherStatus = Enum.Refresher.Default;
  230. }, this.refresherCompleteDuration);
  231. return;
  232. }
  233. // 用户处理用户在短时间内多次调用reload的情况,此时下拉刷新view不需要重复显示,只需要保证最后一次reload对应的请求结束后收回下拉刷新view即可
  234. const stackCount = this.refresherRevealStackCount;
  235. if (height === 0 && checkStack) {
  236. this.refresherRevealStackCount --;
  237. if (stackCount > 1) return;
  238. this.refresherEndTimeout = u.delay(() => {
  239. this.refresherStatus = Enum.Refresher.Default;
  240. }, this.refresherCompleteDuration);
  241. }
  242. if (stackCount > 1) {
  243. this.refresherStatus = Enum.Refresher.Loading;
  244. }
  245. const duration = animate ? 200 : 0;
  246. if (this.nOldShowRefresherRevealHeight !== height) {
  247. if (height > 0) {
  248. this.nShowRefresherReveal = true;
  249. }
  250. // 展示下拉刷新view
  251. weexAnimation.transition(this.$refs['zp-n-list-refresher-reveal'], {
  252. styles: {
  253. height: `${height}px`,
  254. transform: `translateY(${translateY}px)`,
  255. },
  256. duration,
  257. timingFunction: 'linear',
  258. needLayout: true,
  259. delay: 0
  260. })
  261. }
  262. u.delay(() => {
  263. if (animate) {
  264. this.nShowRefresherReveal = height > 0;
  265. }
  266. }, duration > 0 ? duration - 60 : 0);
  267. this.nOldShowRefresherRevealHeight = height;
  268. },
  269. // 滚动到底部加载更多
  270. _nOnLoadmore() {
  271. if (this.nShowRefresherReveal || !this.totalData.length) return;
  272. this.useChatRecordMode ? this.doChatRecordLoadMore() : this._onLoadingMore('toBottom');
  273. },
  274. // 获取nvue waterfall单项配置
  275. _nGetWaterfallConfig(key, defaultValue) {
  276. return this.nvueWaterfallConfig[key] || defaultValue;
  277. },
  278. // 更新nvue 下拉刷新view容器的宽度
  279. _nUpdateRefresherWidth() {
  280. u.delay(() => {
  281. this.$nextTick(()=>{
  282. this._getNodeClientRect('.zp-n-list').then(node => {
  283. if (node) {
  284. this.nRefresherWidth = node[0].width || this.nRefresherWidth;
  285. }
  286. })
  287. })
  288. })
  289. }
  290. // #endif
  291. }
  292. }