@@ -24,6 +24,16 @@ angular.module('g1b.datetime-range', []).
24
24
// Get current date
25
25
scope . current = moment ( ) ;
26
26
27
+ // Convert start datetime to moment.js if its not a moment object yet
28
+ if ( scope . start && ! scope . start . _isAMomentObject ) {
29
+ scope . start = moment ( scope . start ) ;
30
+ }
31
+
32
+ // Convert end datetime to moment.js if its not a moment object yet
33
+ if ( scope . end && ! scope . end . _isAMomentObject ) {
34
+ scope . end = moment ( scope . end ) ;
35
+ }
36
+
27
37
// Set selected date
28
38
scope . selectDate = function ( date ) {
29
39
if ( scope . selected === date ) {
@@ -43,7 +53,13 @@ angular.module('g1b.datetime-range', []).
43
53
if ( ( scope . selected . clone ( ) . startOf ( 'week' ) . month ( ) !== scope . calendar . month ( ) && scope . selected . clone ( ) . endOf ( 'week' ) . month ( ) !== scope . calendar . month ( ) ) || calendar_update ) {
44
54
scope . calendar = scope . selected . clone ( ) ;
45
55
}
46
- scope . callback ( ) ;
56
+ if ( scope . selected === scope . start ) {
57
+ scope . callbackStart ( ) ;
58
+ }
59
+ if ( scope . selected === scope . end ) {
60
+ scope . callbackEnd ( ) ;
61
+ }
62
+ scope . callbackAll ( ) ;
47
63
} else {
48
64
scope . warning = ( scope . selected === scope . start ) ? 'end' : 'start' ;
49
65
$timeout ( function ( ) {
@@ -54,43 +70,55 @@ angular.module('g1b.datetime-range', []).
54
70
55
71
// Set start and end datetime objects to the selected preset
56
72
scope . selectPreset = function ( preset ) {
57
- if ( ! ! scope . selected && scope . selected === scope . start ) {
58
- scope . selected = preset . start ;
59
- } else if ( ! ! scope . selected && scope . selected === scope . end ) {
60
- scope . selected = preset . end ;
73
+ // Hide presets menu on select
74
+ scope . close ( ) ;
75
+
76
+ // Don't do anything if nothing is changed
77
+ if ( scope . start . isSame ( preset . start ) && scope . end . isSame ( preset . end ) ) { return ; }
78
+
79
+ // Update start datetime object if changed
80
+ if ( ! scope . start . isSame ( preset . start ) ) {
81
+ scope . start = preset . start . clone ( ) ;
82
+ scope . callbackStart ( ) ;
61
83
}
62
- scope . start = preset . start ;
63
- scope . end = preset . end ;
64
- scope . presetsActive = false ;
65
84
66
- $timeout ( function ( ) {
67
- scope . callback ( true ) ;
68
- } ) ;
85
+ // Update end datetime object if changed
86
+ if ( ! scope . end . isSame ( preset . end ) ) {
87
+ scope . end = preset . end . clone ( ) ;
88
+ scope . callbackEnd ( ) ;
89
+ }
90
+
91
+ // Something has definitely changed, fire ambiguous callback
92
+ scope . callbackAll ( ) ;
69
93
} ;
70
94
71
- // Callbacks fired on change of start and/or end datetime objects
72
- scope . callback = function ( allChanged ) {
73
- if ( ! ! scope . onChangeStart && ( allChanged || scope . selected === scope . start ) ) {
74
- scope . onChangeStart ( ) ;
95
+ // Callbacks fired on change of start datetime object
96
+ scope . callbackStart = function ( ) {
97
+ if ( ! ! scope . onChangeStart ) {
98
+ $timeout ( function ( ) {
99
+ scope . onChangeStart ( ) ;
100
+ } ) ;
75
101
}
76
- if ( ! ! scope . onChangeEnd && ( allChanged || scope . selected === scope . end ) ) {
77
- scope . onChangeEnd ( ) ;
102
+ } ;
103
+
104
+ // Callbacks fired on change of end datetime object
105
+ scope . callbackEnd = function ( ) {
106
+ if ( ! ! scope . onChangeEnd ) {
107
+ $timeout ( function ( ) {
108
+ scope . onChangeEnd ( ) ;
109
+ } ) ;
78
110
}
111
+ } ;
112
+
113
+ // Callbacks fired on change of start and/or end datetime objects
114
+ scope . callbackAll = function ( ) {
79
115
if ( ! ! scope . onChange ) {
80
- scope . onChange ( ) ;
116
+ $timeout ( function ( ) {
117
+ scope . onChange ( ) ;
118
+ } ) ;
81
119
}
82
120
} ;
83
121
84
- // Convert start datetime to moment.js if its not a moment object yet
85
- if ( scope . start && ! scope . start . _isAMomentObject ) {
86
- scope . start = moment ( scope . start ) ;
87
- }
88
-
89
- // Convert end datetime to moment.js if its not a moment object yet
90
- if ( scope . end && ! scope . end . _isAMomentObject ) {
91
- scope . end = moment ( scope . end ) ;
92
- }
93
-
94
122
// Close edit popover
95
123
scope . close = function ( ) {
96
124
scope . selected = '' ;
0 commit comments