tm-album.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="tm-album">
  3. <view class="tm-album-body flex flex-wrap" :class="[`ml--${gutter}`]" :style="{width:alb_wk_body_size+'rpx'}">
  4. <block v-for="(item,index) in listData" :key="index">
  5. <view v-if="index<max-1" :style="{width:alb_body_size+'rpx'}" class="tm-album-item">
  6. <view @click="clickAlbum(item,index)" :class="[`py-${item[textKey]?0:gutter}`,`px-${gutter}`]">
  7. <view :style="{width:alb_size+'rpx',height:height||alb_size+'rpx'}" class="relative flex-center">
  8. <tm-images :model="model" :round="round" :width="alb_size" :height="(height||alb_size)" :src="item[rangKey]"></tm-images>
  9. <view class="tm-album-action absolute fulled fulled-height t-0 l-0" :class="[
  10. actionsPos=='top'?'flex-top-end':'',
  11. actionsPos=='bottom'?'flex-end-right':'',
  12. ]">
  13. <slot name="actions" :item="{data:item,index:index}"></slot>
  14. </view>
  15. </view>
  16. <view v-if="item[textKey]" class="py-24 text-align-center text-overflow">
  17. <slot name="default" :text="{text:item[textKey]}">
  18. <text class="text-size-s ">{{item[textKey]}}</text>
  19. </slot>
  20. </view>
  21. </view>
  22. </view>
  23. </block>
  24. <view v-if="listData.length>max-1" :style="{width:alb_body_size+'rpx'}" class="tm-album-item " :class="[`round-${round}`,black_tmeme?'bk':'']">
  25. <view @click="more" :class="[`px-${gutter}`]">
  26. <view :style="{width:alb_size+'rpx',height:height||alb_size+'rpx'}" class="relative flex-center flex-col" :class="[`round-${round}`,color_tmeme]">
  27. <view><text class="iconfont icon-angle-right text-size-xl"></text></view>
  28. <view class="text-size-s mt-12">更多</view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. /**
  37. * 相册集
  38. * @property {Array} list = [] 数据列表,可以是对象数组,也可以是字符串数组,也可对象和字符串数组混合型。
  39. * @property {Number} grid = [] 默认:4,默认一行几个。
  40. * @property {Number} height = [] 默认:0,默认宽度和高度是相同的。指定相册集的高度。如果指定了高度将不使用计算的高度。
  41. * @property {Number} gutter = [] 默认:4,图片之间的间隙。
  42. * @property {Number} round = [] 默认:3,项目的圆角
  43. * @property {Number} max = [] 默认:999,最大可展示数量,超过不显示,以更多项目代替。
  44. * @property {String} rang-key = [] 默认:'src',如果list的项目是对象,则需要提供图片字段名称
  45. * @property {String} text-key = [] 默认:'text',如果list的项目是对象,则需要提供图片标题字段名称
  46. * @property {Boolean|String} preview = [] 默认:true,点击图片是否可以预览。
  47. * @property {Boolean|String} actions-pos = [bottom|top] 默认:bottom,可选bottom,top,内部压在图片上方的元素位置。内容可在插槽actions中布局。
  48. * @property {String} color = [] 默认:primary,主题色名称,显示更多项目的背景主题色。
  49. */
  50. import tmImages from '@/tm-vuetify/components/tm-images/tm-images.vue';
  51. export default {
  52. name:"tm-album",
  53. components: {
  54. tmImages
  55. },
  56. props: {
  57. list: {
  58. type: Array,
  59. default: ()=>[]
  60. },
  61. //指定相册集的高度。如果指定了高度将不使用计算的高度。
  62. height:{
  63. type:Number,
  64. default:0
  65. },
  66. grid:{
  67. type:Number,
  68. default:4
  69. },
  70. gutter:{
  71. type:Number,
  72. default:4
  73. },
  74. round:{
  75. type:Number,
  76. default:3
  77. },
  78. max:{
  79. type:Number,
  80. default:999
  81. },
  82. rangKey: {
  83. type: String,
  84. default: 'src'
  85. },
  86. textKey: {
  87. type: String,
  88. default: 'text'
  89. },
  90. preview:{
  91. type:Boolean,
  92. default:true
  93. },
  94. actionsPos:{
  95. type:String,
  96. default:'bottom',//bottom,top
  97. },
  98. black:{
  99. type:Boolean,
  100. default:null
  101. },
  102. // 跟随主题色的改变而改变。
  103. fllowTheme:{
  104. type:Boolean|String,
  105. default:true
  106. },
  107. color:{
  108. type:String,
  109. default:"primary"
  110. },
  111. model:{
  112. type:String,
  113. default:'scaleToFill',//bottom,top
  114. }
  115. },
  116. data() {
  117. return {
  118. alb_size:0,
  119. alb_body_size:0,
  120. alb_wk_body_size:0,
  121. listAlbum:[]
  122. };
  123. },
  124. watch:{
  125. list:{
  126. deep:true,
  127. handler(){
  128. this.listData = this.chuliList(this.list)
  129. }
  130. }
  131. },
  132. created(){
  133. this.listData = this.chuliList(this.list)
  134. },
  135. computed: {
  136. black_tmeme: function() {
  137. if (this.black !== null) return this.black;
  138. return this.$tm.vx.state().tmVuetify.black;
  139. },
  140. color_tmeme:function(){
  141. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  142. return this.$tm.vx.state().tmVuetify.color;
  143. }
  144. return this.color;
  145. },
  146. listData:{
  147. get:function () {
  148. return this.listAlbum;
  149. },
  150. set:function (val) {
  151. this.listAlbum = val;
  152. }
  153. }
  154. },
  155. async mounted() {
  156. await this.setInits();
  157. },
  158. methods: {
  159. //处理数据,以标准化。
  160. chuliList(){
  161. let list = [...this.list];
  162. let p = [];
  163. for (var i = 0; i < list.length; i++) {
  164. if(typeof list[i] == 'string'){
  165. let ls = {}
  166. ls[this.rangKey] = list[i];
  167. ls[this.textKey] = null;
  168. p.push(ls)
  169. }else if(typeof list[i] == 'object'){
  170. let ls = {}
  171. ls[this.rangKey] = '';
  172. ls[this.textKey] = null;
  173. ls={...ls,...list[i]}
  174. p.push(ls)
  175. }
  176. }
  177. return p;
  178. },
  179. async setInits(){
  180. this.$nextTick(async function() {
  181. let t = this;
  182. this.$tm.sleep(150).then(async function(){
  183. let p = await t.$Querey('.tm-album',t).catch(e =>{});
  184. if (!p[0]) return;
  185. let grid = t.grid || 1;
  186. let size = (p[0].width+t.gutter)/grid;
  187. let gutter = t.gutter*2
  188. let ratio = 750/uni.upx2px(750);
  189. let blv = size * ratio - gutter;
  190. t.alb_body_size = size * ratio;
  191. t.alb_wk_body_size = p[0].width*ratio+t.gutter*2+(grid*2);
  192. t.alb_size = blv;
  193. })
  194. });
  195. },
  196. clickAlbum(item,index){
  197. if(this.preview){
  198. uni.$tm.preview(item[this.rangKey],this.listData,this.rangKey);
  199. }
  200. this.$emit('click',{item,index})
  201. },
  202. more(){
  203. this.$emit('more')
  204. }
  205. },
  206. }
  207. </script>
  208. <style lang="scss" scoped>
  209. .tm-album{
  210. .tm-album-action{
  211. }
  212. }
  213. </style>