@@ -70,30 +70,52 @@ angular.module('g1b.datetime-range', []).
7070
7171 // Set start and end datetime objects to the selected preset
7272 scope . selectPreset = function ( preset ) {
73- if ( ! ! scope . selected && scope . selected === scope . start ) {
74- scope . selected = preset . start ;
75- } else if ( ! ! scope . selected && scope . selected === scope . end ) {
76- scope . selected = preset . end ;
77- }
78- scope . start = preset . start ;
79- scope . end = preset . end ;
73+ // Hide presets menu on select
8074 scope . presetsActive = false ;
8175
82- $timeout ( function ( ) {
83- scope . callback ( true ) ;
84- } ) ;
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 ( ) ;
83+ }
84+
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 ( ) ;
8593 } ;
8694
87- // Callbacks fired on change of start and/or end datetime objects
88- scope . callback = function ( allChanged ) {
89- if ( ! ! scope . onChangeStart && ( allChanged || scope . selected === scope . start ) ) {
90- 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+ } ) ;
91101 }
92- if ( ! ! scope . onChangeEnd && ( allChanged || scope . selected === scope . end ) ) {
93- 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+ } ) ;
94110 }
111+ } ;
112+
113+ // Callbacks fired on change of start and/or end datetime objects
114+ scope . callbackAll = function ( ) {
95115 if ( ! ! scope . onChange ) {
96- scope . onChange ( ) ;
116+ $timeout ( function ( ) {
117+ scope . onChange ( ) ;
118+ } ) ;
97119 }
98120 } ;
99121
0 commit comments