RMCalendarCell.ets 3.77 KB
import { RMCalendarBean } from './RMCalendarBean';
import font from '@ohos.font';

@Component
export struct RMCalenderCell {
  private selectOpacity: number = 0.15
  itemFontSize: Length = 0
  itemFontColor: ResourceColor = {} as ResourceColor
  itemFontWeight: FontWeight = FontWeight.Normal
  selectDayFontColor: ResourceColor = {} as ResourceColor
  selectFontColor: ResourceColor = {} as ResourceColor
  selectItemBgColor: ResourceColor = {} as ResourceColor
  disabledFontColor: ResourceColor = {} as ResourceColor
  nowFontColor: ResourceColor = {} as ResourceColor
  // 今日时间戳
  selectDay: number = 0
  @Link selectItem: RMCalendarBean
  @Link selectedDates: Array<RMCalendarBean>
  @Prop hasPre: boolean
  @Prop hasNext: boolean
  @ObjectLink item: RMCalendarBean
  // 自定义每一项布局
  cellLayout?: (item: RMCalendarBean) => void
  selectDayLayout?: (item: RMCalendarBean) => void
  cellClick?: (item: RMCalendarBean) => void
  disableClick?: (item: RMCalendarBean) => void

  /**
   * 检测是否包含在数组中
   */
  checkInArrays() {
    for (let index = 0; index < this.selectedDates.length; index++) {
      if (this.item.time == this.selectedDates[index].time) {
        return index
      }
    }
    return -1
  }

  getItemColor() {
    if (!this.isShowSelectBg() && this.item.isNow) {
      return this.nowFontColor
    } else if (this.item.isPre) {
      return this.disabledFontColor
    } else if (this.item.isNext) {
      return this.disabledFontColor
    } else if (this.selectItem && this.selectItem.time == this.item.time) {
      return this.selectFontColor
    }
    return this.itemFontColor
  }

  getBorder() {
    if (this.selectItem && this.selectItem.time == this.item.time) {
      return this.border({
        width: 1,
        color: this.getItemColor()
      })
    } else {
      return this.border({
        width: 0,
      }
      )
    }
  }

  /**
   * 获取选中项背景颜色
   */
  getSelectItemBg() {
    return this.selectItemBgColor
  }

  /**
   * 获取选中项背景透明度
   * @returns
   */
  getSelectItemBgOpa() {
    return this.item.isPre || this.item.isNext ? this.selectOpacity : 1
  }

  /**
   * 是否需要显示选项背景
   */
  isShowSelectBg() {
    return this.selectItem && this.selectItem.time == this.item.time
  }

  build() {
    Column() {
      Stack() {
        if (!this.item.isPre) {
          if (this.isShowSelectBg()) {
            Column()// .position({ x: "10%", y: "10%" })
              .height("80%")
              .aspectRatio(1)// .borderRadius(999)
              .backgroundColor(this.getSelectItemBg())
              .opacity(this.getSelectItemBgOpa())
              .borderRadius(2)
              .transition({ type: TransitionType.Insert, opacity: 1 })
              .transition({ type: TransitionType.Delete, opacity: 0 })
          }
          Text(this.item.date + '')
            .fontSize(this.itemFontSize)
            .fontColor(this.getItemColor())
            .fontWeight(this.itemFontWeight)
            .fontFamily('BebasNeueBold')
        }
      }
      // .justifyContent(FlexAlign.Center)
      .aspectRatio(1)
      // .border(this.getBorder())
      .width("80%")
    }
    .justifyContent(FlexAlign.Center)
    // .width("100%")
    // .aspectRatio(1)
    .onClick(() => {
      // animateTo({ duration: 200 }, () => {
      //
      // })

      if (this.item.isNext) {
        if (!this.hasNext) {
          if (this.disableClick) {
            this.disableClick(this.item)
          }
          return
        }
      }
      if (this.item.isPre) {
        // if (!this.hasPre) {
        if (this.disableClick) {
          this.disableClick(this.item)
        }
        return
        // }
      }
      this.selectItem = this.item
      if (this.cellClick) {
        this.cellClick(this.item)
      }
    })
  }

}