123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <template>
- <view class="tn-waterfall-class tn-waterfall">
- <view id="tn-waterfall-left" class="tn-waterfall__column"><slot name="left" :leftList="leftList"></slot></view>
- <view id="tn-waterfall-right" class="tn-waterfall__column"><slot name="right" :rightList="rightList"></slot></view>
- </view>
- </template>
- <script>
- export default {
- name: 'tn-waterfall',
- props: {
-
- value: {
- type: Array,
- default() {
- return []
- }
- },
-
-
- idKey: {
- type: String,
- default: 'id'
- },
-
-
- addTime: {
- type: Number,
- default: 200
- }
- },
- computed: {
-
- copyValue() {
- return this.cloneData(this.value)
- }
- },
- watch: {
- copyValue(nVal, oVal) {
-
- let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0
-
- this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)))
- this.splitData()
- }
- },
- data() {
- return {
-
- leftList: [],
-
- rightList: [],
-
- tempList: []
- }
- },
- mounted() {
- this.tempList = this.cloneData(this.copyValue)
- this.splitData()
- },
- methods: {
-
- async splitData() {
- if (!this.tempList.length) return
-
- let leftRect = await this._tGetRect('#tn-waterfall-left')
- let rightRect = await this._tGetRect('#tn-waterfall-right')
-
- let item = this.tempList[0]
-
-
- if (!item) return
-
-
- if (leftRect.height < rightRect.height) {
- this.leftList.push(item)
- } else if (leftRect.height > rightRect.height) {
- this.rightList.push(item)
- } else {
-
- if (this.leftList.length <= this.rightList.length) {
- this.leftList.push(item)
- } else {
- this.rightList.push(item)
- }
- }
-
-
- this.tempList.splice(0, 1)
-
- if (this.tempList.length) {
- setTimeout(() => {
- this.splitData()
- }, this.addTime)
- } else {
- this.$emit('finish')
- }
- },
-
- cloneData(data) {
- return JSON.parse(JSON.stringify(data))
- },
-
- clear() {
- this.leftList = []
- this.rightList = []
- this.$emit('input', [])
- this.tempList = []
- },
-
- remove(id) {
-
- let index = -1
- index = this.leftList.findIndex(val => val[this.idKey] == id)
- if (index != -1) {
-
- this.leftList.splice(index, 1)
- } else {
-
- index = this.rightList.findIndex(val => val[this.idKey] == id)
- if (index != -1) this.rightList.splice(index, 1)
- }
-
- index = this.value.findIndex(val => val[this.idKey] == id)
- if (index != -1) this.$emit('input', this.value.splice(index, 1))
- },
-
- modify(id, key, value) {
-
- let index = -1
- index = this.leftList.findIndex(val => val[this.idKey] == id)
- if (index != -1) {
-
- this.leftList[index][key] = value
- } else {
-
- index = this.rightList.findIndex(val => val[this.idKey] == id)
- if (index != -1) this.rightList[index][key] = value
- }
-
- index = this.value.findIndex(val => val[this.idKey] == id)
- if(index != -1) {
- let data = this.cloneData(this.value)
- data[index][key] = value
- this.$emit('input', data)
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .tn-waterfall {
- display: flex;
- flex-direction: row;
- align-items: flex-start;
-
- &__column {
- display: flex;
- flex-direction: column;
- flex: 1;
- height: auto;
- }
- }
- </style>
|