|
1 | 1 | <template lang="pug"> |
2 | 2 | div |
3 | 3 | div |
4 | | - table |
5 | | - tr |
6 | | - th.pr-2 |
7 | | - label(for="mode") Interval mode: |
8 | | - td |
9 | | - select(id="mode" v-model="mode") |
10 | | - option(value='last_duration') Last duration |
11 | | - option(value='range') Date range |
12 | | - tr(v-if="mode == 'last_duration'") |
13 | | - th.pr-2 |
14 | | - label(for="duration") Show last: |
15 | | - td |
16 | | - select(id="duration" :value="duration", @change="valueChanged") |
17 | | - option(:value="15*60") 15min |
18 | | - option(:value="30*60") 30min |
19 | | - option(:value="60*60") 1h |
20 | | - option(:value="2*60*60") 2h |
21 | | - option(:value="4*60*60") 4h |
22 | | - option(:value="6*60*60") 6h |
23 | | - option(:value="12*60*60") 12h |
24 | | - option(:value="24*60*60") 24h |
25 | | - tr(v-if="mode == 'range'") |
26 | | - th.pr-2 Range: |
27 | | - td |
28 | | - input(type="date", v-model="start", @input="valueChanged") |
29 | | - input(type="date", v-model="end", @input="valueChanged") |
| 4 | + div(class="settings-bar") |
| 5 | + div |
| 6 | + label(for="mode") Interval mode: |
| 7 | + select(id="mode" v-model="mode") |
| 8 | + option(value='last_duration') Last duration |
| 9 | + option(value='range') Date range |
| 10 | + div(v-if="mode == 'last_duration'") |
| 11 | + label(for="duration") Show last: |
| 12 | + select(id="duration" :value="duration" @change="changeDuration") |
| 13 | + option(:value="15*60") 15min |
| 14 | + option(:value="30*60") 30min |
| 15 | + option(:value="60*60") 1h |
| 16 | + option(:value="2*60*60") 2h |
| 17 | + option(:value="4*60*60") 4h |
| 18 | + option(:value="6*60*60") 6h |
| 19 | + option(:value="12*60*60") 12h |
| 20 | + option(:value="24*60*60") 24h |
| 21 | + div(v-if="mode == 'range'") |
| 22 | + | Range: |
| 23 | + input(type="date", v-model="start") |
| 24 | + input(type="date", v-model="end") |
| 25 | + div |
| 26 | + button.btn.btn-outline-dark( |
| 27 | + type="button", |
| 28 | + :disabled="invalidDaterange || emptyDaterange", |
| 29 | + @click="showTimeline" |
| 30 | + ) Show Timeline |
| 31 | + div(:style="{ color: 'red', visibility: invalidDaterange ? 'visible' : 'hidden' }") |
| 32 | + | The selected date range is invalid. |
30 | 33 |
|
31 | 34 | </template> |
32 | 35 |
|
33 | | -<style scoped lang="scss"></style> |
| 36 | +<style scoped lang="scss"> |
| 37 | +.settings-bar { |
| 38 | + display: flex; |
| 39 | + justify-content: space-between; |
| 40 | +} |
| 41 | +</style> |
34 | 42 |
|
35 | 43 | <script> |
36 | 44 | import moment from 'moment'; |
37 | 45 |
|
38 | 46 | export default { |
39 | 47 | name: 'input-timeinterval', |
40 | 48 | props: { |
41 | | - duration: { |
| 49 | + defaultDuration: { |
42 | 50 | type: Number, |
43 | 51 | default: 60 * 60, |
44 | 52 | }, |
45 | 53 | }, |
46 | 54 | data: () => { |
47 | 55 | return { |
48 | | - now: moment(), |
49 | 56 | mode: 'last_duration', |
| 57 | + duration: null, |
50 | 58 | start: null, |
51 | 59 | end: null, |
52 | 60 | }; |
| 61 | + }, |
| 62 | + mounted: function() { |
| 63 | + this.duration = this.defaultDuration; |
53 | 64 | }, |
54 | 65 | computed: { |
55 | | - value: { |
| 66 | + daterange: { |
56 | 67 | get() { |
57 | 68 | if (this.mode == 'range' && this.start && this.end) { |
58 | 69 | return [moment(this.start), moment(this.end)]; |
59 | 70 | } else { |
60 | | - return [moment(this.now).subtract(this.duration, 'seconds'), moment(this.now)]; |
61 | | - } |
62 | | - }, |
63 | | - set(newValue) { |
64 | | - if (!isNaN(newValue)) { |
65 | | - // Set new now and duration |
66 | | - this.now = moment(); |
67 | | - this.duration = newValue; |
68 | | - } else { |
69 | | - // Not required for mode='range', start and end set directly through v-model |
| 71 | + return [moment().subtract(this.duration, 'seconds'), moment()]; |
70 | 72 | } |
71 | 73 | }, |
72 | 74 | }, |
| 75 | + emptyDaterange() { |
| 76 | + return this.mode == 'range' && !(this.start && this.end); |
| 77 | + }, |
| 78 | + invalidDaterange() { |
| 79 | + return this.mode == 'range' && moment(this.start) >= moment(this.end); |
| 80 | + }, |
73 | 81 | }, |
74 | 82 | methods: { |
75 | | - valueChanged(e) { |
76 | | - this.value = e.target.value; |
77 | | - this.$emit('input', this.value); |
| 83 | + showTimeline() { |
| 84 | + this.$emit('update-timeline', this.daterange); |
| 85 | + }, |
| 86 | + changeDuration(e) { |
| 87 | + // This is only for last_duration, range mode changes daterange automatically |
| 88 | + this.duration = e.target.value; |
78 | 89 | }, |
79 | 90 | }, |
80 | 91 | }; |
|
0 commit comments