11<template lang="pug">
22div
33 div
4+ b-alert( v-if ="mode == 'range' && invalidDaterange" , variant ="warning" , show )
5+ | The selected date range is invalid. The second date must be greater than the first date.
6+ b-alert( v-if ="mode == 'range' && daterangeTooLong" , variant ="warning" , show )
7+ | The selected date range is too long. The maximum is {{ maxDuration/(24*60*60) }} days.
8+
49 table
510 tr
611 th.pr-2
712 label( for ="mode" ) Interval mode:
813 td
9- select( id ="mode" v-model ="mode" )
14+ select( id ="mode" , v-model ="mode" )
1015 option( value ='last_duration' ) Last duration
1116 option( value ='range' ) Date range
1217 tr( v-if ="mode == 'last_duration'" )
1318 th.pr-2
1419 label( for ="duration" ) Show last:
1520 td
16- select( id ="duration" : value = "duration" , @change ="valueChanged" )
21+ select( id ="duration" , v-model ="duration" , @change ="valueChanged" )
1722 option( :value ="15*60" ) 15min
1823 option( :value ="30*60" ) 30min
1924 option( :value ="60*60" ) 1h
2530 tr( v-if ="mode == 'range'" )
2631 th.pr-2 Range:
2732 td
28- input( type ="date" , v-model ="start" , @input ="valueChanged" )
29- input( type ="date" , v-model ="end" , @input ="valueChanged" )
33+ input( type ="date" , v-model ="start" )
34+ input( type ="date" , v-model ="end" )
35+ button(
36+ class ="btn btn-outline-dark btn-sm" ,
37+ type ="button" ,
38+ :disabled ="mode == 'range' && (invalidDaterange || emptyDaterange || daterangeTooLong)" ,
39+ @click ="valueChanged"
40+ ) Update
3041
3142</template >
3243
3344<style scoped lang="scss"></style >
3445
3546<script >
3647import moment from ' moment' ;
37-
3848export default {
3949 name: ' input-timeinterval' ,
4050 props: {
41- duration : {
51+ defaultDuration : {
4252 type: Number ,
4353 default: 60 * 60 ,
4454 },
55+ maxDuration: {
56+ type: Number ,
57+ default: null ,
58+ },
4559 },
46- data : () => {
60+ data () {
4761 return {
48- now : moment (),
62+ duration : JSON . parse ( JSON . stringify ( this . defaultDuration )), // Make a copy of defaultDuration
4963 mode: ' last_duration' ,
5064 start: null ,
5165 end: null ,
@@ -57,24 +71,31 @@ export default {
5771 if (this .mode == ' range' && this .start && this .end ) {
5872 return [moment (this .start ), moment (this .end )];
5973 } 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
74+ return [moment ().subtract (this .duration , ' seconds' ), moment ()];
7075 }
7176 },
7277 },
78+ emptyDaterange () {
79+ return ! (this .start && this .end );
80+ },
81+ invalidDaterange () {
82+ return moment (this .start ) >= moment (this .end );
83+ },
84+ daterangeTooLong () {
85+ return moment (this .start ).add (this .maxDuration , ' seconds' ).isBefore (moment (this .end ));
86+ },
87+ },
88+ mounted () {
89+ this .valueChanged ();
7390 },
7491 methods: {
75- valueChanged (e ) {
76- this .value = e .target .value ;
77- this .$emit (' input' , this .value );
92+ valueChanged () {
93+ if (
94+ this .mode == ' last_duration' ||
95+ (! this .emptyDaterange && ! this .invalidDaterange && ! this .daterangeTooLong )
96+ ) {
97+ this .$emit (' input' , this .value );
98+ }
7899 },
79100 },
80101};
0 commit comments