all files / src/timePicker/ clockMinutes.vue

0% Statements 0/46
0% Branches 0/10
0% Functions 0/1
0% Lines 0/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                                                                                                                                                                                   
<template>
<div class="mu-clock-minutes">
  <clock-pointer hasSelected :value="minutes.selected" :hasSelected="minutes.hasSelected" type="minute" />
  <clock-number v-for="minute in minutes.numbers" :selected="minute.isSelected" :key="minute.minute" type="minute" :value="minute.minute" />
  <div ref="mask" @mouseup="handleUp" @mousemove="handleMove" @touchmove="handleTouch" @touchend="handleTouch" class="mu-clock-minutes-mask"></div>
</div>
</template>
 
<script>
import clockNumber from './clockNumber'
import clockPointer from './clockPointer'
import {getTouchEventOffsetValues, rad2deg} from './timeUtils'
export default {
  props: {
    initialMinutes: {
      type: Number,
      default () {
        return new Date().getMinutes()
      }
    }
  },
  mounted () {
    const clockElement = this.$refs.mask
    this.center = {
      x: clockElement.offsetWidth / 2,
      y: clockElement.offsetHeight / 2
    }
    this.basePoint = {
      x: this.center.x,
      y: 0
    }
  },
  data () {
    return {
      minutes: null
    }
  },
  created () {
    this.minutes = this.getMinuteNumbers()
  },
  methods: {
    getMinuteNumbers () {
      const minutes = []
      for (let i = 0; i < 12; i++) {
        minutes.push(i * 5)
      }
      const selectedMinutes = this.initialMinutes
      let hasSelected = false
 
      const numbers = minutes.map((minute) => {
        const isSelected = selectedMinutes === minute
        if (isSelected) {
          hasSelected = true
        }
        return {
          minute,
          isSelected
        }
      })
      return {
        numbers: numbers,
        hasSelected: hasSelected,
        selected: selectedMinutes
      }
    },
    isMousePressed (event) {
      if (typeof event.buttons === 'undefined') {
        return event.nativeEvent.which
      }
      return event.buttons
    },
    handleUp (event) {
      event.preventDefault()
      this.setClock(event, true)
    },
    handleMove (event) {
      event.preventDefault()
      if (this.isMousePressed(event) !== 1) {
        return
      }
      this.setClock(event, false)
    },
    handleTouch (event) {
      event.preventDefault()
      this.setClock(event.changedTouches[0], false)
    },
    setClock (event, finish) {
      if (typeof event.offsetX === 'undefined') {
        const offset = getTouchEventOffsetValues(event)
 
        event.offsetX = offset.offsetX
        event.offsetY = offset.offsetY
      }
 
      const minutes = this.getMinutes(event.offsetX, event.offsetY)
 
      this.$emit('change', minutes, finish)
    },
    getMinutes (offsetX, offsetY) {
      const step = 6
      const x = offsetX - this.center.x
      const y = offsetY - this.center.y
      const cx = this.basePoint.x - this.center.x
      const cy = this.basePoint.y - this.center.y
 
      const atan = Math.atan2(cx, cy) - Math.atan2(x, y)
 
      let deg = rad2deg(atan)
      deg = Math.round(deg / step) * step
      deg %= 360
 
      const value = Math.floor(deg / step) || 0
 
      return value
    }
  },
  watch: {
    initialMinutes (val) {
      this.minutes = this.getMinuteNumbers()
    }
  },
  components: {
    'clock-number': clockNumber,
    'clock-pointer': clockPointer
  }
}
</script>
 
<style lang="less">
@import "../styles/import.less";
.mu-clock-minutes{
  height: 100%;
  width: 100%;
  border-radius: 100%;
  position: relative;
  pointer-events: none;
  box-sizing: border-box;
}
 
.mu-clock-minutes-mask{
  height: 100%;
  width: 100%;
  pointer-events: auto;
}
</style>