duration-picker.vue 2.66 KB
Newer Older
Nick's avatar
Nick committed
1
<template lang='pug'>
2
  v-toolbar.radius-7(flat, :color='$vuetify.theme.dark ? "grey darken-4-l3" : "grey lighten-3"')
3
    .body-2.mr-3 {{$t('common:duration.every')}}
Nick's avatar
Nick committed
4 5 6 7 8 9
    v-text-field(
      solo
      hide-details
      flat
      reverse
      v-model='minutes'
Nick's avatar
Nick committed
10
      style='flex: 1 1 70px;'
Nick's avatar
Nick committed
11
    )
12
    .body-2.mx-3 {{$t('common:duration.minutes')}}
Nick's avatar
Nick committed
13
    v-divider.mr-3
Nick's avatar
Nick committed
14 15 16 17 18 19
    v-text-field(
      solo
      hide-details
      flat
      reverse
      v-model='hours'
Nick's avatar
Nick committed
20
      style='flex: 1 1 70px;'
Nick's avatar
Nick committed
21
    )
22
    .body-2.mx-3 {{$t('common:duration.hours')}}
Nick's avatar
Nick committed
23
    v-divider.mr-3
Nick's avatar
Nick committed
24 25 26 27 28 29
    v-text-field(
      solo
      hide-details
      flat
      reverse
      v-model='days'
Nick's avatar
Nick committed
30
      style='flex: 1 1 70px;'
Nick's avatar
Nick committed
31
    )
32
    .body-2.mx-3 {{$t('common:duration.days')}}
Nick's avatar
Nick committed
33
    v-divider.mr-3
Nick's avatar
Nick committed
34 35 36 37 38 39
    v-text-field(
      solo
      hide-details
      flat
      reverse
      v-model='months'
Nick's avatar
Nick committed
40
      style='flex: 1 1 70px;'
Nick's avatar
Nick committed
41
    )
42
    .body-2.mx-3 {{$t('common:duration.months')}}
Nick's avatar
Nick committed
43
    v-divider.mr-3
Nick's avatar
Nick committed
44 45 46 47 48 49
    v-text-field(
      solo
      hide-details
      flat
      reverse
      v-model='years'
Nick's avatar
Nick committed
50
      style='flex: 1 1 70px;'
Nick's avatar
Nick committed
51
    )
52
    .body-2.mx-3 {{$t('common:duration.years')}}
Nick's avatar
Nick committed
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
</template>

<script>
import _ from 'lodash'
import moment from 'moment'

export default {
  props: {
    value: {
      type: String,
      default: 'PT5M'
    }
  },
  data() {
    return {
      duration: moment.duration(0)
    }
  },
  computed: {
    years: {
      get() { return this.duration.years() || 0 },
      set(val) { this.rebuild(_.toNumber(val), 'years') }
    },
    months: {
      get() { return this.duration.months() || 0 },
      set(val) { this.rebuild(_.toNumber(val), 'months') }
    },
    days: {
      get() { return this.duration.days() || 0 },
      set(val) { this.rebuild(_.toNumber(val), 'days') }
    },
    hours: {
      get() { return this.duration.hours() || 0 },
      set(val) { this.rebuild(_.toNumber(val), 'hours') }
    },
    minutes: {
      get() { return this.duration.minutes() || 0 },
      set(val) { this.rebuild(_.toNumber(val), 'minutes') }
    }
  },
  watch: {
    value(newValue, oldValue) {
      this.duration = moment.duration(newValue)
    }
  },
  methods: {
    rebuild(val, unit) {
      if (!_.isFinite(val) || val < 0) {
        val = 0
      }
      const newDuration = {
        minutes: this.duration.minutes(),
        hours: this.duration.hours(),
        days: this.duration.days(),
        months: this.duration.months(),
        years: this.duration.years()
      }
      _.set(newDuration, unit, val)
      this.duration = moment.duration(newDuration)
      this.$emit('input', this.duration.toISOString())
    }
  },
  mounted() {
    this.duration = moment.duration(this.value)
  }
}
</script>