tm-weekbar.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="tm-weekbar">
  3. <tm-sheet :color="color" :black="black_tmeme" :round="8" :shadow="0" :margin="[0,0]" :padding="[0,0]">
  4. <view class="flex-between ">
  5. <tm-button :fllowTheme="false" @click="timeNextPre(false)" :black="black_tmeme" :theme="btnColor" :font-color="fontColor" icon="icon-angle-left" block width="44" :round="10" :shadow="0"></tm-button>
  6. <view class="flex-shrink flex-start" style="width: calc(100% - 88rpx);">
  7. <view @click="weekClick(index,item.dateFull)" v-for="(item,index) in timeList" :key="index" class="flex-center flex-col px-5" style="width:14.28%">
  8. <view :class="[fontColor?(d_time==item.dateFull?`text-${activeColor_tmeme}`:(black_tmeme?'text-grey':`text-${fontColor}`)):color,d_time==item.dateFull?'text-weight-b':'',black_tmeme?'bk':'']" class=" text-size-s text-weight-b">{{item.weekCN}}</view>
  9. <view :class="[dateColor?(d_time==item.dateFull?`text-${activeColor_tmeme}`:(black_tmeme?'text-grey':`text-${dateColor}`)):color,d_time==item.dateFull?'text-weight-b':'',black_tmeme?'bk':'']" class=" text-size-xs">{{item.date}}</view>
  10. </view>
  11. </view>
  12. <tm-button :fllowTheme="false" :black="black_tmeme" @click="timeNextPre(true)" :theme="btnColor" :font-color="fontColor" icon="icon-angle-right" block width="44" :round="10" :shadow="0"></tm-button>
  13. </view>
  14. </tm-sheet>
  15. </view>
  16. </template>
  17. <script>
  18. /**
  19. * 时间周切换组件
  20. * @property {String} default-value = [] ,默认:'',建议使用.sync双向绑定,默认显示的日期,格式为"2021-9-21"。
  21. * @property {String} color = [] ,默认:'white',背景色
  22. * @property {String} font-color = [] ,默认:'black',默认的标题字体颜色
  23. * @property {String} date-color = [] ,默认:'grey',默认的日期字体颜色
  24. * @property {String} active-color = [] ,默认:'primary',激活后的文字颜色
  25. * @property {String} btn-color = [] ,默认:'grey-lighten-4',两按钮的背景主题色
  26. * @property {Function} change 点击时间时触发,返回当前选中的日期。
  27. */
  28. import tmSheet from "@/tm-vuetify/components/tm-sheet/tm-sheet.vue"
  29. import tmButton from "@/tm-vuetify/components/tm-button/tm-button.vue"
  30. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  31. export default {
  32. components:{tmSheet,tmButton,tmIcons},
  33. name:"tm-weekbar",
  34. props:{
  35. defaultValue:{
  36. type:String,
  37. default:''
  38. },
  39. color:{
  40. type:String,
  41. default:'white'
  42. },
  43. fontColor:{
  44. type:String,
  45. default:'black'
  46. },
  47. dateColor:{
  48. type:String,
  49. default:'grey'
  50. },
  51. activeColor:{
  52. type:String,
  53. default:'primary'
  54. },
  55. btnColor:{
  56. type:String,
  57. default:'grey-lighten-4'
  58. },
  59. black:{
  60. type:Boolean,
  61. default:null
  62. },
  63. // 跟随主题色的改变而改变。
  64. fllowTheme:{
  65. type:Boolean|String,
  66. default:true
  67. }
  68. },
  69. data() {
  70. return {
  71. nowDate:"",
  72. timeList:[],
  73. };
  74. },
  75. watch:{
  76. defaultValue:function(nval,oval){
  77. if(nval!=oval){
  78. let p = nval;
  79. if(typeof p==='string'){
  80. p = p.replace(/-/g,'/')
  81. }
  82. this.d_time = new Date(p).toLocaleDateString()
  83. }
  84. }
  85. },
  86. computed:{
  87. black_tmeme: function() {
  88. if (this.black !== null) return this.black;
  89. return this.$tm.vx.state().tmVuetify.black;
  90. },
  91. activeColor_tmeme:function(){
  92. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  93. return this.$tm.vx.state().tmVuetify.color;
  94. }
  95. return this.activeColor;
  96. },
  97. d_time:{
  98. get:function(){
  99. return this.nowDate;
  100. },
  101. set:function(val){
  102. this.nowDate = val;
  103. this.$emit("update:defaultValue",val)
  104. this.$emit("change",val)
  105. },
  106. }
  107. },
  108. mounted() {
  109. let nDate = new Date();
  110. const dte = nDate.getFullYear()+"/"+ (nDate.getMonth()+1) + "/" + nDate.getDate();
  111. let p = this.defaultValue;
  112. if(typeof p==='string'){
  113. p = p.replace(/-/g,'/')
  114. }
  115. this.nowDate = new Date(p||dte).toLocaleDateString();
  116. this.getTimeList(this.d_time)
  117. },
  118. methods: {
  119. weekClick(eindex,nowdefault){
  120. this.d_time = nowdefault
  121. },
  122. getTimeList(nwd){
  123. if(!nwd){
  124. nwd = this.nowDate;
  125. }
  126. let nwod = new Date(nwd);
  127. let year = nwod.getFullYear();
  128. let month = nwod.getMonth();
  129. let day = nwod.getDate();
  130. let week = nwod.getDay();
  131. let new_weekDay = [nwod];
  132. let timcha = Math.abs(week-7);
  133. let zcha = Math.abs(7-timcha);
  134. let weekCn=["周日","周一","周二","周三","周四","周五","周六"];
  135. for(let i=1;i<zcha;i++){
  136. let d = new Date(year,month,day-i);
  137. new_weekDay.push(d);
  138. }
  139. for(let i=1;i<=timcha;i++){
  140. let d = new Date(year,month,day+i);
  141. new_weekDay.push(d);
  142. }
  143. let weekDay = [];
  144. for(let i=0;i<new_weekDay.length;i++){
  145. let ide = new_weekDay[i];
  146. let sc = {
  147. week:ide.getDay(),
  148. dateFull:ide.getFullYear()+"/"+ (ide.getMonth()+1) + "/" + ide.getDate(),
  149. date:(ide.getMonth()+1) + "-" + ide.getDate(),
  150. index:i,
  151. weekCN:weekCn[ide.getDay()]
  152. }
  153. weekDay.push(sc);
  154. }
  155. weekDay.sort(function(a, b){return a.week - b.week});
  156. weekDay.push(weekDay[0])
  157. weekDay.splice(0,1)
  158. let t= this;
  159. this.timeList = [];
  160. this.$nextTick(function(){
  161. this.timeList = weekDay;
  162. })
  163. },
  164. timeNextPre(typeV){
  165. if(typeV){
  166. let nd = this.timeList[this.timeList.length-1];
  167. let enx = new Date(nd.dateFull.replace(/-/g,'/'));
  168. enx.setDate(enx.getDate()+1)
  169. this.getTimeList(enx.getFullYear()+"/"+ (enx.getMonth()+1) + "/" + enx.getDate())
  170. }else{
  171. let nd = this.timeList[0];
  172. let enx = new Date(nd.dateFull.replace(/-/g,'/'));
  173. enx.setDate(enx.getDate()-7)
  174. this.getTimeList(enx.getFullYear()+"/"+ (enx.getMonth()+1) + "/" + enx.getDate())
  175. }
  176. },
  177. },
  178. }
  179. </script>
  180. <style lang="scss">
  181. </style>