1
0

tm-flowLayout-custom.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <view class="tm-flowLayout relative">
  3. <view class="tm-flowLayout-hidden absolute fulled">
  4. <view v-for="(item, index) in list2" :key="index" style="width: 350rpx;" :class="'tm-flowLayout-hidden-'+index">
  5. <tm-images @error="loadimg($event,false,index)" @load="loadimg($event,true,index)" :src="item.spec.url"></tm-images>
  6. </view>
  7. </view>
  8. <view class="tm-flowLayout-cm flex-between">
  9. <view class="tm-flowLayout-left" style="width: 48.6%;">
  10. <block v-for="(item, index) in dataList[0]" :key="index">
  11. <view @click.stop="onclick(index,0)" class="tm-flowLayout-item mb-20 fulled" :class="['tm-flowLayout-item_'+index]">
  12. <tm-translate animation-name="fadeUp">
  13. <view class="card round-3 overflow white">
  14. <tm-images :previmage="false" :src="item.spec.url"></tm-images>
  15. </view>
  16. </tm-translate>
  17. </view>
  18. </block>
  19. </view>
  20. <view class="tm-flowLayout-right" style="width: 48.6%;">
  21. <block v-for="(item, index) in dataList[1]" :key="index">
  22. <view @click.stop="onclick(index,1)" class="tm-flowLayout-item mb-20 fulled" :class="['tm-flowLayout-item_'+index]">
  23. <tm-translate animation-name="fadeUp">
  24. <view class="card round-3 overflow white">
  25. <tm-images :previmage="false" :src="item.spec.url"></tm-images>
  26. </view>
  27. </tm-translate>
  28. </view>
  29. </block>
  30. </view>
  31. </view>
  32. <view v-if="isLoading" class="flex-shrink fulled">
  33. <tm-loadding></tm-loadding>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. /**
  39. * 瀑布流组件
  40. * @property {Function} click 点击项目时触发
  41. * @property {Function} load 当前列表加载完成发出的事件。
  42. * @property {String} model = [rank|desc] desc下正时排序,所有加载完成再进行显示,rank随机,哪个先加载哪个先排前面。
  43. */
  44. import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
  45. import tmLoadding from "@/tm-vuetify/components/tm-loadding/tm-loadding.vue"
  46. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  47. import tmTranslate from "@/tm-vuetify/components/tm-translate/tm-translate.vue"
  48. export default {
  49. components: {tmImages, tmLoadding, tmIcons, tmTranslate},
  50. name: 'tm-flowLayout',
  51. props: {
  52. model: {
  53. type: String,
  54. default: 'rank' //desc下正时排序,所有加载完成再进行显示,rank随机,哪个先加载哪个先排前面。
  55. }
  56. },
  57. data() {
  58. return {
  59. list2: [],
  60. dataList: [
  61. [], []
  62. ],
  63. colHeight: [0, 0],
  64. minWidth: 0,
  65. isLoading: true,
  66. };
  67. },
  68. async mounted() {
  69. let p = await this.$Querey('.tm-flowLayout-left').catch(e => {
  70. });
  71. this.minWidth = p[0].width;
  72. },
  73. methods: {
  74. onclick(index, dirIndex) {
  75. this.$emit('click',
  76. {
  77. childrenIndex: index,
  78. dirIndex: dirIndex,
  79. item: this.dataList[dirIndex][index]
  80. });
  81. },
  82. //修改或者替换列表数据
  83. changeItemData(dirIndex, childrenIndex, item) {
  84. this.dataList[dirIndex].splice(childrenIndex, 1, item);
  85. },
  86. //删除一项列表数据
  87. delItemData(dirIndex, childrenIndex) {
  88. this.colHeight.splice(dirIndex, 1, this.colHeight[dirIndex] - this.dataList[dirIndex][childrenIndex].height)
  89. this.dataList[dirIndex].splice(childrenIndex, 1);
  90. this.list2.splice(this.dataList[dirIndex][childrenIndex].index, 1);
  91. this.$nextTick(function () {
  92. this.sucessRank();
  93. })
  94. },
  95. //向列表添加数据
  96. pushData(list) {
  97. let prIdx_i = this.list2.length;
  98. if (!Array.isArray(list) || typeof list == 'undefined') {
  99. return false;
  100. }
  101. for (let i = 0; i < list.length; i++) {
  102. this.list2.push({
  103. index: i + prIdx_i,
  104. isLoad: false,
  105. image: "",
  106. width: 0,
  107. height: 0,
  108. ...list[i]
  109. })
  110. }
  111. },
  112. //获取内部列表数据
  113. getDataList() {
  114. return this.dataList;
  115. },
  116. async loadimg(event, isLoad, index) {
  117. this.isLoading = true;
  118. let ps = this.list2[index];
  119. ps.isLoad = true;
  120. if (isLoad == false) {
  121. ps.width = this.minWidth;
  122. ps.height = this.minWidth;
  123. } else {
  124. ps.width = this.minWidth;
  125. ps.height = ps.height + event.height;
  126. }
  127. if (this.list2.length == 0 && this.dataList[0].length == 0 && this.dataList[1].length == 0) {
  128. this.isLoading = false;
  129. return;
  130. }
  131. this.list2.splice(index, 1, ps);
  132. if (this.model == 'desc') {
  133. this.sucessRank();
  134. } else if (this.model == 'rank') {
  135. let indexCol = this.colHeight[0] <= this.colHeight[1] ? 0 : 1;
  136. this.dataList[indexCol].push(this.list2[index]);
  137. this.colHeight.splice(indexCol, 1, this.colHeight[indexCol] + this.list2[index].height);
  138. if (this.isAllLoading() === false) return;
  139. this.isLoading = false;
  140. }
  141. },
  142. isAllLoading() {
  143. let isAllload = true;
  144. for (let i = 0; i < this.list2.length; i++) {
  145. if (this.list2[i].isLoad === false) {
  146. isAllload = false;
  147. break;
  148. }
  149. }
  150. if (isAllload === true) {
  151. // 当前列表加载完成发出的事件。
  152. this.$emit('load', this.dataList)
  153. }
  154. return isAllload;
  155. },
  156. sucessRank() {
  157. if (this.isAllLoading() === false) return;
  158. this.isLoading = false;
  159. for (let i = 0; i < this.list2.length; i++) {
  160. // let p = await this.getLeftRightHeight();
  161. let index = this.colHeight[0] <= this.colHeight[1] ? 0 : 1;
  162. this.dataList[index].push(this.list2[i]);
  163. this.colHeight.splice(index, 1, this.colHeight[index] + this.list2[i].height)
  164. }
  165. },
  166. //清空瀑布流数据 。
  167. clear() {
  168. this.list2 = [];
  169. this.dataList = [[], []];
  170. this.isLoading = false;
  171. },
  172. getLeftRightHeight(index) {
  173. let t = this;
  174. return new Promise((resolve, rejecvt) => {
  175. let q = uni.createSelectorQuery().in(t);
  176. q.select(".tm-flowLayout-left").boundingClientRect();
  177. q.select(".tm-flowLayout-right").boundingClientRect();
  178. q.exec((res) => {
  179. resolve([res[0].height, res[1].height])
  180. })
  181. })
  182. }
  183. },
  184. };
  185. </script>
  186. <style lang="scss">
  187. .tm-flowLayout {
  188. .tm-flowLayout-hidden {
  189. width: 100%;
  190. height: 100px;
  191. overflow-y: auto;
  192. left: -300%;
  193. top: 0;
  194. }
  195. .tm-flowLayout-cm {
  196. align-items: flex-start;
  197. }
  198. .card {
  199. box-shadow: 0rpx 4rpx 24rpx rgba(0, 0, 0, 0.03);
  200. }
  201. }
  202. </style>