@@ -17,6 +17,7 @@ class RecurringSelectDialog {
1717 this . daysChanged = this . daysChanged . bind ( this ) ;
1818 this . dateOfMonthChanged = this . dateOfMonthChanged . bind ( this ) ;
1919 this . weekOfMonthChanged = this . weekOfMonthChanged . bind ( this ) ;
20+ this . terminationChanged = this . terminationChanged . bind ( this ) ;
2021 this . recurring_selector = recurring_selector ;
2122 this . current_rule = this . recurring_selector . recurring_select ( 'current_rule' ) ;
2223 this . initDialogBox ( ) ;
@@ -41,9 +42,12 @@ class RecurringSelectDialog {
4142
4243 this . mainEventInit ( ) ;
4344 this . freqInit ( ) ;
45+ this . terminationInit ( ) ;
4446 this . summaryInit ( ) ;
47+
4548 trigger ( this . outer_holder , "recurring_select:dialog_opened" ) ;
4649 this . freq_select . focus ( ) ;
50+
4751 }
4852
4953 cancel ( ) {
@@ -134,9 +138,9 @@ class RecurringSelectDialog {
134138 interval_input . value = this . current_rule . hash . interval
135139 on ( interval_input , "change keyup" , this . intervalChanged )
136140
137- if ( ! this . current_rule . hash . validations ) { this . current_rule . hash . validations = { } } ;
138- if ( ! this . current_rule . hash . validations . day_of_month ) { this . current_rule . hash . validations . day_of_month = [ ] } ;
139- if ( ! this . current_rule . hash . validations . day_of_week ) { this . current_rule . hash . validations . day_of_week = { } } ;
141+ if ( ! this . current_rule . hash . validations ) { this . current_rule . hash . validations = { } ; }
142+ if ( ! this . current_rule . hash . validations . day_of_month ) { this . current_rule . hash . validations . day_of_month = [ ] ; }
143+ if ( ! this . current_rule . hash . validations . day_of_week ) { this . current_rule . hash . validations . day_of_week = { } ; }
140144 this . init_calendar_days ( section ) ;
141145 this . init_calendar_weeks ( section ) ;
142146
@@ -156,6 +160,40 @@ class RecurringSelectDialog {
156160 section . style . display = 'block'
157161 }
158162
163+ terminationInit ( ) {
164+ const section = this . outer_holder . querySelector ( ".rs_termination_section" ) ;
165+ this . until_date = section . querySelector ( "#rs_until_date" ) ;
166+ this . until_date . flatpickr ( {
167+ enableTime : true ,
168+ dateFormat : "Y-m-d H:i" ,
169+ altInput : true ,
170+ altFormat : "F j, Y h:i K" ,
171+ } ) ;
172+
173+ if ( this . current_rule . hash && this . current_rule . hash . count ) {
174+ this . count_option = section . querySelector ( "input[name=rs_termination][value=count]" ) ;
175+ this . count_option . checked = true ;
176+ this . occurence_count = section . querySelector ( "#rs_occurrence_count" ) ;
177+ this . occurence_count . value = this . current_rule . hash . count ;
178+ } else if ( this . current_rule . hash && this . current_rule . hash . until ) {
179+ this . until_option = section . querySelector ( "input[name=rs_termination][value=until]" ) ;
180+ this . until_option . checked = true ;
181+ // IceCube::TimeUtil will serialize a TimeWithZone into a hash, such as:
182+ // {time: Thu, 04 Sep 2014 06:59:59 +0000, zone: "Pacific Time (US & Canada)"}
183+ // If we're initializing from an unsaved rule, until will be a string
184+ if ( this . current_rule . hash . until . time ) {
185+ this . until_val = new Date ( this . current_rule . hash . until . time ) ;
186+ this . until_date . value = ( this . until_val . getFullYear ( ) + "-" + ( this . until_val . getMonth ( ) + 1 ) + "-" + this . until_val . getDate ( ) + " " + this . until_val . getHours ( ) + ":" + this . until_val . getMinutes ( ) ) ;
187+ } else {
188+ this . until_date . value = this . current_rule . hash . until ;
189+ }
190+ } else {
191+ this . never_option = section . querySelector ( "input[name=rs_termination][value=never]" ) ;
192+ this . never_option . checked = true ;
193+ }
194+
195+ section . addEventListener ( "change" , this . terminationChanged . bind ( this ) ) ;
196+ }
159197
160198 summaryInit ( ) {
161199 this . summary = this . outer_holder . querySelector ( ".rs_summary" ) ;
@@ -166,6 +204,7 @@ class RecurringSelectDialog {
166204
167205 summaryUpdate ( new_string ) {
168206 // this.summary.style.width = `${this.content.getBoundingClientRect().width}px`;
207+ console . log ( "Updating summary" , this . current_rule . hash , this . current_rule . str ) ;
169208 if ( ( this . current_rule . hash != null ) && ( this . current_rule . str != null ) ) {
170209 this . summary . classList . remove ( "fetching" ) ;
171210 this . save_button . classList . remove ( "disabled" ) ;
@@ -212,7 +251,7 @@ class RecurringSelectDialog {
212251 if ( Array . from ( this . current_rule . hash . validations . day_of_month ) . includes ( num ) ) {
213252 day_link . classList . add ( "selected" ) ;
214253 }
215- } ;
254+ }
216255
217256 // add last day of month button
218257 const end_of_month_link = document . createElement ( "a" )
@@ -248,9 +287,9 @@ class RecurringSelectDialog {
248287 day_link . setAttribute ( "day" , day_of_week ) ;
249288 day_link . setAttribute ( "instance" , num ) ;
250289 monthly_calendar . appendChild ( day_link ) ;
251- } ;
290+ }
252291 }
253- } ;
292+ }
254293
255294 Object . entries ( this . current_rule . hash . validations . day_of_week ) . forEach ( ( [ key , value ] ) => {
256295 Array . from ( value ) . forEach ( ( instance , index ) => {
@@ -278,10 +317,9 @@ class RecurringSelectDialog {
278317 freqChanged ( ) {
279318 if ( ! isPlainObject ( this . current_rule . hash ) ) { this . current_rule . hash = null ; } // for custom values
280319
281- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
320+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } }
321+ this . current_rule . str = null ;
282322 this . current_rule . hash . interval = 1 ;
283- this . current_rule . hash . until = null ;
284- this . current_rule . hash . count = null ;
285323 this . current_rule . hash . validations = null ;
286324 this . content . querySelectorAll ( ".freq_option_section" ) . forEach ( el => el . style . display = 'none' )
287325 this . content . querySelector ( "input[type=radio], input[type=checkbox]" ) . checked = false
@@ -305,13 +343,13 @@ class RecurringSelectDialog {
305343 this . current_rule . hash . rule_type = "IceCube::DailyRule" ;
306344 this . current_rule . str = this . config . texts [ "daily" ] ;
307345 this . initDailyOptions ( ) ;
308- } ;
309- this . summaryUpdate ( ) ;
346+ }
347+ this . summaryFetch ( ) ;
310348 }
311349
312350 intervalChanged ( event ) {
313351 this . current_rule . str = null ;
314- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
352+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
315353 this . current_rule . hash . interval = parseInt ( event . currentTarget . value ) ;
316354 if ( ( this . current_rule . hash . interval < 1 ) || isNaN ( this . current_rule . hash . interval ) ) {
317355 this . current_rule . hash . interval = 1 ;
@@ -322,7 +360,7 @@ class RecurringSelectDialog {
322360 daysChanged ( event ) {
323361 event . target . classList . toggle ( "selected" ) ;
324362 this . current_rule . str = null ;
325- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
363+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
326364 this . current_rule . hash . validations = { } ;
327365 const raw_days = Array . from ( this . content . querySelectorAll ( ".day_holder a.selected" ) )
328366 . map ( el => parseInt ( el . dataset . value ) )
@@ -334,7 +372,7 @@ class RecurringSelectDialog {
334372 dateOfMonthChanged ( event ) {
335373 event . target . classList . toggle ( "selected" ) ;
336374 this . current_rule . str = null ;
337- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
375+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
338376 this . current_rule . hash . validations = { } ;
339377 const raw_days = Array . from ( this . content . querySelectorAll ( ".monthly_options .rs_calendar_day a.selected" ) )
340378 . map ( el => {
@@ -349,21 +387,44 @@ class RecurringSelectDialog {
349387 weekOfMonthChanged ( event ) {
350388 event . target . classList . toggle ( "selected" ) ;
351389 this . current_rule . str = null ;
352- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
390+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
353391 this . current_rule . hash . validations = { } ;
354392 this . current_rule . hash . validations . day_of_month = [ ] ;
355393 this . current_rule . hash . validations . day_of_week = { } ;
356394 this . content . querySelectorAll ( ".monthly_options .rs_calendar_week a.selected" )
357395 . forEach ( ( elm , index ) => {
358396 const day = parseInt ( elm . getAttribute ( "day" ) ) ;
359397 const instance = parseInt ( elm . getAttribute ( "instance" ) ) ;
360- if ( ! this . current_rule . hash . validations . day_of_week [ day ] ) { this . current_rule . hash . validations . day_of_week [ day ] = [ ] } ;
398+ if ( ! this . current_rule . hash . validations . day_of_week [ day ] ) { this . current_rule . hash . validations . day_of_week [ day ] = [ ] ; }
361399 return this . current_rule . hash . validations . day_of_week [ day ] . push ( instance ) ;
362400 } )
363401 this . summaryUpdate ( ) ;
364402 return false ;
365403 }
366404
405+ terminationChanged ( ) {
406+ this . selected_termination_type = this . outer_holder . querySelector ( ".rs_termination_section input[type='radio']:checked" ) ;
407+ if ( ! this . selected_termination_type ) { return ; }
408+ this . current_rule . str = null ;
409+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
410+ switch ( this . selected_termination_type . value ) {
411+ case "count" :
412+ this . current_rule . hash . count = parseInt ( this . occurence_count ? this . occurence_count . value : this . outer_holder . querySelector ( "#rs_occurrence_count" ) . value ) ;
413+ if ( ( this . current_rule . hash . count < 1 ) || isNaN ( this . current_rule . hash . count ) ) {
414+ this . current_rule . hash . count = 1 ;
415+ }
416+ this . current_rule . hash . until = null ;
417+ break
418+ case "until" :
419+ this . current_rule . hash . until = this . until_date ? this . until_date . value : this . outer_holder . querySelector ( "#rs_until_date" ) . value ;
420+ this . current_rule . hash . count = null ;
421+ break
422+ default :
423+ this . current_rule . hash . count = null ;
424+ this . current_rule . hash . until = null ;
425+ }
426+ this . summaryUpdate ( ) ;
427+ }
367428// ========================= Change callbacks ===============================
368429
369430 template ( ) {
@@ -381,7 +442,6 @@ class RecurringSelectDialog {
381442 <option value='Yearly'>${ this . config . texts [ "yearly" ] } </option> \
382443 </select> \
383444 </p> \
384- \
385445 <div class='daily_options freq_option_section'> \
386446 <p> \
387447 ${ this . config . texts [ "every" ] } \
@@ -400,7 +460,7 @@ class RecurringSelectDialog {
400460 for ( let i = this . config . texts [ "first_day_of_week" ] , day_of_week = i , end = 7 + this . config . texts [ "first_day_of_week" ] , asc = this . config . texts [ "first_day_of_week" ] <= end ; asc ? i < end : i > end ; asc ? i ++ : i -- , day_of_week = i ) {
401461 day_of_week = day_of_week % 7 ;
402462 str += `<a href='#' data-value='${ day_of_week } '>${ this . config . texts [ "days_first_letter" ] [ day_of_week ] } </a>` ;
403- } ;
463+ }
404464
405465 str += `\
406466 </div> \
@@ -426,6 +486,31 @@ class RecurringSelectDialog {
426486 ${ this . config . texts [ "years" ] } \
427487 </p> \
428488 </div> \
489+ <div class='rs_termination_section'>
490+ <table>
491+ <tr>
492+ <td>
493+ <label class='rs_termination_label'>${ this . config . texts [ "ends" ] } :</label>
494+ </td>
495+ <td>
496+ <label>
497+ <input type='radio' name='rs_termination' value='never' />
498+ ${ this . config . texts [ "never" ] }
499+ </label><br>
500+ <label>
501+ <input type='radio' name='rs_termination' value='count' />
502+ ${ this . config . texts [ "after" ] }
503+ <input type='text' data-wrapper-class='ui-recurring-select' id='rs_occurrence_count' class='rs_count' value='10' size='2' />
504+ ${ this . config . texts [ "occurrences" ] }
505+ </label><br>
506+ <label>
507+ <input type='radio' name='rs_termination' value='until' />
508+ ${ this . config . texts [ "on" ] }
509+ <input type='text' data-wrapper-class='ui-recurring-select' class='rs_datepicker' id='rs_until_date' />
510+ </label>
511+ </td>
512+ </table>
513+ </div>
429514 <p class='rs_summary'> \
430515 <span></span> \
431516 </p> \
0 commit comments