scroll-reveal.vue 4.42 KB
<template>
  <div class="sr-list">
    <div class="sr-col" v-for="(col, index) in colsList" :key="index" :style="`width:${100/cols}%`">
      <template v-if="col.length">
        <ArticleItem v-for="item in col" :key="item.articleId" class="sr-item" :data="item" @click="onClick"></ArticleItem>
      </template>
      <ArticleItem class="sr-temp" :temporary="true"></ArticleItem>
    </div>
  </div>
</template>

<script>

import ArticleItem from './author-article-item';
import {assign, get, forEach} from 'lodash';
import {getArticleImageSize} from 'utils/image-handler';

export default {
  data() {
    return {
      colsList: [],
      loadedIndex: 0,
      colsHeight: []
    };
  },
  props: {
    list: {
      type: Array,
      default: []
    },
    cols: {
      type: Number,
      default: 2
    },
    params: Object
  },
  created() {
    this.reset();
  },
  mounted() {
    this.$cols = this.$el.getElementsByClassName('sr-col');
    this.getCoverImgWidth();
  },
  watch: {
    list(newList, oldList) {
      if (oldList.length > newList.length ||
        get(oldList, '[0]._type') !== get(newList, '[0]._type')) {
        this.reset();
      }

      this.$nextTick(() => {
        this.calcListLayout();
      });
    }
  },
  methods: {
    getCoverImgWidth() {
      let imgWidth = this.$el.offsetWidth / this.cols;
      let $item = this.$el.getElementsByClassName('sr-temp');

      if ($item && $item.length) {
        let _w = $item[0].offsetWidth;
        let $img = $item[0].children;

        (_w > 0) && (imgWidth = _w);

        if ($img && $img.length) {
          _w = $img[0].offsetWidth;
          (_w > 0) && (imgWidth = _w);
        }
      }

      this.imgBlockWidth = imgWidth;
    },
    calcListLayout(single) {
      this.loadedIndex = this.loadedIndex || 0;

      let num = this.list.length - this.loadedIndex;

      if (num <= 0) {
        return;
      }

      forEach(this.$cols, (col, index) => {
        this.colsHeight[index] = col.offsetHeight;
      });

      let minHeight = Math.min.apply(null, this.colsHeight);
      let minIndex = this.colsHeight.indexOf(minHeight);

      let end = 1;

      if (!single) {
        end = Math.max.apply(null, [1, num - Math.floor(this.cols * 2)]);
      }

      for (let i = 0; i < end; i++) {
        let item = this.list[this.loadedIndex + i];

        if (item) {
          let index = (minIndex + i) % this.cols;

          this.colsList[index] = this.colsList[index] || [];
          this.colsList[index].push(this.clacCoverSize(item));
        }
      }

      this.colsList = [...this.colsList];
      this.loadedIndex += end;

      this.$nextTick(() => {
        this.calcListLayout(true);
      });
    },

    clacCoverSize(item) {
      let {width, height} = getArticleImageSize({
        width: item.imageWidth,
        height: item.imageHeight
      });

      return assign(item, {
        imageWidth: width,
        imageHeight: height,
        scale: height / width,
        blockWidth: this.imgBlockWidth
      });
    },

    reset() {
      this.loadedIndex = 0;

      let list = [];

      for (let i = 0; i < this.cols; i++) {
        list[i] = [];
      }

      this.colsList = list;
    },
    onClick({data, type}) {
      if (type === 'author') {
        this.toAuthor(data)
      } else {
        this.toArticle(data);
      }
    },
    toAuthor({authorUid, authorType}) {
      console.log(get(this.params, 'params.authorUid'), authorUid);
      if (get(this.params, 'params.authorUid') === authorUid) {
        return;
      }

      this.$router.push({
        name: 'author',
        params: {
          id: authorUid,
          type: authorType
        }
      });
    },
    toArticle({articleId}) {
      let router = {
        name: 'article.user',
        params: {
          id: articleId,
          ...this.params.params
        },
        query: this.params.query
      };

      if (!router.params.authorUid) {
        try {
          this.$sdk.getUser().then(res => {
            if (get(res, 'uid') > 0) {
              router.params.authorUid = res.uid;
              this.$router.push(router);
            }
          });
        } catch(e){
          console.log(e);
        }
      } else {
        this.$router.push(router);
      }
    }
  },
  components: {
    ArticleItem
  }
};
</script>

<style lang="css">
  .sr-list {
    padding: 0 20px;
    display: flex;
    align-items: flex-start;

    .sr-col {
      flex-grow: 1;
      position: relative;
    }
  }
</style>