@@ -24,6 +24,16 @@ angular.module('g1b.datetime-range', []).
2424 // Get current date
2525 scope . current = moment ( ) ;
2626
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+
2737 // Set selected date
2838 scope . selectDate = function ( date ) {
2939 if ( scope . selected === date ) {
@@ -43,7 +53,13 @@ angular.module('g1b.datetime-range', []).
4353 if ( ( scope . selected . clone ( ) . startOf ( 'week' ) . month ( ) !== scope . calendar . month ( ) && scope . selected . clone ( ) . endOf ( 'week' ) . month ( ) !== scope . calendar . month ( ) ) || calendar_update ) {
4454 scope . calendar = scope . selected . clone ( ) ;
4555 }
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 ( ) ;
4763 } else {
4864 scope . warning = ( scope . selected === scope . start ) ? 'end' : 'start' ;
4965 $timeout ( function ( ) {
@@ -54,43 +70,55 @@ angular.module('g1b.datetime-range', []).
5470
5571 // Set start and end datetime objects to the selected preset
5672 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 ( ) ;
6183 }
62- scope . start = preset . start ;
63- scope . end = preset . end ;
64- scope . presetsActive = false ;
6584
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 ( ) ;
6993 } ;
7094
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+ } ) ;
75101 }
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+ } ) ;
78110 }
111+ } ;
112+
113+ // Callbacks fired on change of start and/or end datetime objects
114+ scope . callbackAll = function ( ) {
79115 if ( ! ! scope . onChange ) {
80- scope . onChange ( ) ;
116+ $timeout ( function ( ) {
117+ scope . onChange ( ) ;
118+ } ) ;
81119 }
82120 } ;
83121
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-
94122 // Close edit popover
95123 scope . close = function ( ) {
96124 scope . selected = '' ;
0 commit comments