@@ -103,7 +103,13 @@ $(function () {
103103 return additional_items_html += '<div><span class="btn_date_range_' + i + '">' + el [ 'title' ] + '</span></div>' ;
104104 } ) ;
105105
106- $ ( 'body' ) . append ( '<div style="min-width: ' + e . target . offsetWidth + 'px; top: ' + ( e . target . offsetTop ) + 'px; left: ' + ( e . target . offsetLeft ) + 'px" class="block_timerange">' +
106+ // Bind in the same tick the popup is appended. jQuery 3 reimplemented
107+ // .ready() on top of a Deferred, whose .then schedules through
108+ // setTimeout, so handlers attached inside it landed a macrotask late
109+ // and the popup was on screen but inert until then. jQuery 4 keeps
110+ // that implementation, so waiting buys nothing there either, and
111+ // .ready() on a non-document collection has been deprecated since 3.0.
112+ let container = $ ( '<div style="min-width: ' + e . target . offsetWidth + 'px; top: ' + ( e . target . offsetTop ) + 'px; left: ' + ( e . target . offsetLeft ) + 'px" class="block_timerange">' +
107113 '<div><span class="btn_today">Today</span></div>' +
108114 '<div><span class="btn_yesterday">Yesterday</span></div>' +
109115 '<div><span class="btn_week">This Week</span></div>' +
@@ -112,78 +118,76 @@ $(function () {
112118 '<div><span class="btn_last_month">Last Month</span></div>' +
113119 additional_items_html +
114120 '</div>'
115- ) . ready ( function ( ) {
116- let container = $ ( this ) . find ( '.block_timerange' ) ;
117-
118- // additional ranges
119- opts . add_range . forEach ( function ( el , i ) {
120- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_date_range_' + i , function ( e ) {
121- unbindClickEventBlockTimerange ( ) ;
122- let start = new Date ( el [ 'start' ] . getFullYear ( ) , el [ 'start' ] . getMonth ( ) , el [ 'start' ] . getDate ( ) ) ;
123- let end = new Date ( el [ 'end' ] . getFullYear ( ) , el [ 'end' ] . getMonth ( ) , el [ 'end' ] . getDate ( ) ) ;
124- fillInputs ( start , end )
125- } ) ;
126- } ) ;
121+ ) . appendTo ( 'body' ) ;
127122
128- // Today
129- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_today' , function ( e ) {
123+ // additional ranges
124+ opts . add_range . forEach ( function ( el , i ) {
125+ container . on ( 'click.CalendarRangeSet' , '.btn_date_range_' + i , function ( e ) {
130126 unbindClickEventBlockTimerange ( ) ;
131- let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) ) ;
132- let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) + 1 ) ;
133- fillInputs ( start , end ) ;
127+ let start = new Date ( el [ 'start' ] . getFullYear ( ) , el [ 'start' ] . getMonth ( ) , el [ 'start' ] . getDate ( ) ) ;
128+ let end = new Date ( el [ 'end' ] . getFullYear ( ) , el [ 'end' ] . getMonth ( ) , el [ 'end' ] . getDate ( ) ) ;
129+ fillInputs ( start , end )
134130 } ) ;
131+ } ) ;
135132
136- // Yesterday
137- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_yesterday ' , function ( e ) {
138- unbindClickEventBlockTimerange ( ) ;
139- let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - 1 ) ;
140- let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) ) ;
141- fillInputs ( start , end ) ;
142- } ) ;
133+ // Today
134+ container . on ( 'click.CalendarRangeSet' , '.btn_today ' , function ( e ) {
135+ unbindClickEventBlockTimerange ( ) ;
136+ let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) ) ;
137+ let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) + 1 ) ;
138+ fillInputs ( start , end ) ;
139+ } ) ;
143140
144- // Week
145- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_week ' , function ( e ) {
146- unbindClickEventBlockTimerange ( ) ;
147- let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - days_since_monday ( datetime ) ) ;
148- let end = new Date ( start . getFullYear ( ) , start . getMonth ( ) , start . getDate ( ) + 7 ) ;
149- fillInputs ( start , end ) ;
150- } ) ;
141+ // Yesterday
142+ container . on ( 'click.CalendarRangeSet' , '.btn_yesterday ' , function ( e ) {
143+ unbindClickEventBlockTimerange ( ) ;
144+ let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - 1 ) ;
145+ let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) ) ;
146+ fillInputs ( start , end ) ;
147+ } ) ;
151148
152- // Month
153- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_month ' , function ( e ) {
154- unbindClickEventBlockTimerange ( ) ;
155- let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , 1 ) ;
156- let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) + 1 , 1 ) ;
157- fillInputs ( start , end ) ;
158- } ) ;
149+ // Week
150+ container . on ( 'click.CalendarRangeSet' , '.btn_week ' , function ( e ) {
151+ unbindClickEventBlockTimerange ( ) ;
152+ let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - days_since_monday ( datetime ) ) ;
153+ let end = new Date ( start . getFullYear ( ) , start . getMonth ( ) , start . getDate ( ) + 7 ) ;
154+ fillInputs ( start , end ) ;
155+ } ) ;
159156
160- // Last Week
161- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_last_week ' , function ( e ) {
162- unbindClickEventBlockTimerange ( ) ;
163- let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - days_since_monday ( datetime ) ) ;
164- let start = new Date ( end . getFullYear ( ) , end . getMonth ( ) , end . getDate ( ) - 7 ) ;
165- fillInputs ( start , end ) ;
166- } ) ;
157+ // Month
158+ container . on ( 'click.CalendarRangeSet' , '.btn_month ' , function ( e ) {
159+ unbindClickEventBlockTimerange ( ) ;
160+ let start = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , 1 ) ;
161+ let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) + 1 , 1 ) ;
162+ fillInputs ( start , end ) ;
163+ } ) ;
167164
168- // Last Month
169- $ ( container ) . on ( 'click.CalendarRangeSet' , '.btn_last_month ' , function ( e ) {
170- unbindClickEventBlockTimerange ( ) ;
171- let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , 1 ) ;
172- let start = new Date ( end . getFullYear ( ) , end . getMonth ( ) - 1 , 1 ) ;
173- fillInputs ( start , end ) ;
174- } ) ;
165+ // Last Week
166+ container . on ( 'click.CalendarRangeSet' , '.btn_last_week ' , function ( e ) {
167+ unbindClickEventBlockTimerange ( ) ;
168+ let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , datetime . getDate ( ) - days_since_monday ( datetime ) ) ;
169+ let start = new Date ( end . getFullYear ( ) , end . getMonth ( ) , end . getDate ( ) - 7 ) ;
170+ fillInputs ( start , end ) ;
171+ } ) ;
175172
176- // Outer
177- // No stopPropagation here: this listener only watches for a click
178- // that should close the popup. Active Admin delegates Clear Filters
179- // and the has_many buttons from <document>, which is upstream of
180- // <body>, so swallowing the event would break them for as long as
181- // the popup is open.
182- $ ( 'body' ) . on ( 'click.CalendarRangeSet' , function ( e ) {
183- if ( $ ( e . target ) . closest ( '.block_timerange' ) . length == 0 ) {
184- unbindClickEventBlockTimerange ( ) ;
185- }
186- } ) ;
173+ // Last Month
174+ container . on ( 'click.CalendarRangeSet' , '.btn_last_month' , function ( e ) {
175+ unbindClickEventBlockTimerange ( ) ;
176+ let end = new Date ( datetime . getFullYear ( ) , datetime . getMonth ( ) , 1 ) ;
177+ let start = new Date ( end . getFullYear ( ) , end . getMonth ( ) - 1 , 1 ) ;
178+ fillInputs ( start , end ) ;
179+ } ) ;
180+
181+ // Outer
182+ // No stopPropagation here: this listener only watches for a click
183+ // that should close the popup. Active Admin delegates Clear Filters
184+ // and the has_many buttons from <document>, which is upstream of
185+ // <body>, so swallowing the event would break them for as long as
186+ // the popup is open.
187+ $ ( 'body' ) . on ( 'click.CalendarRangeSet' , function ( e ) {
188+ if ( $ ( e . target ) . closest ( '.block_timerange' ) . length == 0 ) {
189+ unbindClickEventBlockTimerange ( ) ;
190+ }
187191 } ) ;
188192 } ) ;
189193 } ) ;
0 commit comments