123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import { querySelector } from './query-node';
- const POSITION_LEFT = 'left';
- const POSITION_RIGHT = 'right';
- Component({
- externalClasses: ['custom-class'],
- properties: {
- loading: {
- type: Boolean,
- value: false,
- },
- isAllLoaded: {
- type: Boolean,
- value: false,
- },
- loadingText: {
- type: String,
- value: '正在加载中...'
- },
- allLoadedText: {
- type: String,
- value: '下面没有了哦 '
- },
- isUpdateFlow: {
- type: Number,
- value: 0,
- observer:function(newVal){
- this.setData({isUpdateFlow:newVal})
- this.childNumber = 0;
- wx.nextTick(() => {
- this.resetParam();
- const waterfallItems = this.getRelationNodes('./waterfall-item');
- waterfallItems.forEach((waterfallItem) => {
- this.childNumber += 1;
- waterfallItem.setWaterfallItemPosition();
- })
- })
- }
- },
- isEmpty: {
- type: Boolean,
- value: false,
- },
- emptyText: {
- type: String,
- value: '暂无数据'
- }
- },
- relations: {
- './waterfall-item': {
- type: 'child',
- linked(target) {
- if(this.childNumber) {
- this.childNumber += 1;
- } else {
- this.childNumber = 1;
- }
- }
- },
- },
- data: {
- waterfallHeight: 0,
- isShowAllLoadedTxt: false,
- },
- lifetimes: {
- ready() {
- this.initParams();
- this.setWatefallWidth();
- }
- },
- // observers: {
- // isUpdateFlow(value) {
- // if(value) {
- // this.childNumber = 0;
- // wx.nextTick(() => {
- // this.resetParam();
- // const waterfallItems = this.getRelationNodes('./waterfall-item');
- // waterfallItems.forEach((waterfallItem) => {
- // this.childNumber += 1;
- // waterfallItem.setWaterfallItemPosition();
- // })
- // })
- // }
- // }
- // },
- /**
- * 组件的方法列表
- */
- methods: {
- handleShowAllLoadedTxt(value) {
- if(this.data.isShowAllLoadedTxt === value) return;
- wx.nextTick(() => {
- setTimeout(() => {
- this.setData({
- isShowAllLoadedTxt: value
- })
- })
- })
- },
- initParams() {
- this.childCount = 0;
- this.itemGap = -1;
- this.waterfallWidth = -1;
- this.leftHeights = 0;
- this.rightHeights = 0;
- this.watefallItemIndex = 0;
- },
- resetParam() {
- this.childCount = 0;
- this.leftHeights = 0;
- this.rightHeights = 0;
- this.watefallItemIndex = 0;
- this.childNumber = 0;
- this.itemGap = -1;
- },
- /**
- * 设置 waterfall-item 的高度值
- * @param {Object} node waterfall-item 组件位置尺寸数据
- */
- async getWaterfallItemPostionInfo(node) {
- if(!node) return;
- let top = 0;
- const { height } = node;
- this.watefallItemIndex += 1;
- let position = POSITION_LEFT;
- const { itemGap } = this;
- if (this.leftHeights <= this.rightHeights) {
- top = this.leftHeights;
- if(this.leftHeights === 0) {
- this.leftHeights += height;
- } else {
- top += itemGap;
- this.leftHeights += height + itemGap;
- }
- } else {
- position = POSITION_RIGHT;
- top = this.rightHeights;
- if(this.rightHeights === 0) {
- this.rightHeights += height;
- } else {
- top += itemGap;
- this.rightHeights += height + itemGap;
- }
- }
- this.setWaterfallHeight(itemGap);
- return {
- top,
- position,
- }
- },
- async setWatefallWidth() {
- const info = await querySelector('.waterfall-inner', this);
- if(!info) {
- this.setWatefallWidth();
- return;
- }
- this.waterfallWidth = info.width;
- },
- async setWaterfallHeight(itemGap) {
- if(this.watefallItemIndex === this.childNumber) {
- let waterfallHeight = Math.ceil(Math.max(this.leftHeights, this.rightHeights) + itemGap);
- this.setData({
- waterfallHeight
- })
- const loadingNode = await querySelector('.placeholder', this);
- let nodeHeight = 0;
- if(loadingNode) {
- nodeHeight = loadingNode.height;
- }
- this.triggerEvent('finish', {
- waterfallHeight: nodeHeight + waterfallHeight
- })
- this.handleShowAllLoadedTxt(this.data.isAllLoaded)
- }
- }
- }
- })
|