@@ -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,38 @@ 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+ } ) ;
170+
171+ if ( this . current_rule . hash && this . current_rule . hash . count ) {
172+ this . count_option = section . querySelector ( "input[name=rs_termination][value=count]" ) ;
173+ this . count_option . checked = true ;
174+ this . occurence_count = section . querySelector ( "#rs_occurrence_count" ) ;
175+ this . occurence_count . value = this . current_rule . hash . count ;
176+ } else if ( this . current_rule . hash && this . current_rule . hash . until ) {
177+ this . until_option = section . querySelector ( "input[name=rs_termination][value=until]" ) ;
178+ this . until_option . checked = true ;
179+ // IceCube::TimeUtil will serialize a TimeWithZone into a hash, such as:
180+ // {time: Thu, 04 Sep 2014 06:59:59 +0000, zone: "Pacific Time (US & Canada)"}
181+ // If we're initializing from an unsaved rule, until will be a string
182+ if ( this . current_rule . hash . until . time ) {
183+ this . until_val = new Date ( this . current_rule . hash . until . time ) ;
184+ 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 ( ) ) ;
185+ } else {
186+ this . until_date . value = this . current_rule . hash . until ;
187+ }
188+ } else {
189+ this . never_option = section . querySelector ( "input[name=rs_termination][value=never]" ) ;
190+ this . never_option . checked = true ;
191+ }
192+
193+ section . addEventListener ( "change" , this . terminationChanged . bind ( this ) ) ;
194+ }
159195
160196 summaryInit ( ) {
161197 this . summary = this . outer_holder . querySelector ( ".rs_summary" ) ;
@@ -212,7 +248,7 @@ class RecurringSelectDialog {
212248 if ( Array . from ( this . current_rule . hash . validations . day_of_month ) . includes ( num ) ) {
213249 day_link . classList . add ( "selected" ) ;
214250 }
215- } ;
251+ }
216252
217253 // add last day of month button
218254 const end_of_month_link = document . createElement ( "a" )
@@ -248,9 +284,9 @@ class RecurringSelectDialog {
248284 day_link . setAttribute ( "day" , day_of_week ) ;
249285 day_link . setAttribute ( "instance" , num ) ;
250286 monthly_calendar . appendChild ( day_link ) ;
251- } ;
287+ }
252288 }
253- } ;
289+ }
254290
255291 Object . entries ( this . current_rule . hash . validations . day_of_week ) . forEach ( ( [ key , value ] ) => {
256292 Array . from ( value ) . forEach ( ( instance , index ) => {
@@ -278,7 +314,8 @@ class RecurringSelectDialog {
278314 freqChanged ( ) {
279315 if ( ! isPlainObject ( this . current_rule . hash ) ) { this . current_rule . hash = null ; } // for custom values
280316
281- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
317+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } }
318+ this . current_rule . str = null ;
282319 this . current_rule . hash . interval = 1 ;
283320 this . current_rule . hash . until = null ;
284321 this . current_rule . hash . count = null ;
@@ -305,13 +342,13 @@ class RecurringSelectDialog {
305342 this . current_rule . hash . rule_type = "IceCube::DailyRule" ;
306343 this . current_rule . str = this . config . texts [ "daily" ] ;
307344 this . initDailyOptions ( ) ;
308- } ;
345+ }
309346 this . summaryUpdate ( ) ;
310347 }
311348
312349 intervalChanged ( event ) {
313350 this . current_rule . str = null ;
314- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
351+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
315352 this . current_rule . hash . interval = parseInt ( event . currentTarget . value ) ;
316353 if ( ( this . current_rule . hash . interval < 1 ) || isNaN ( this . current_rule . hash . interval ) ) {
317354 this . current_rule . hash . interval = 1 ;
@@ -322,7 +359,7 @@ class RecurringSelectDialog {
322359 daysChanged ( event ) {
323360 event . target . classList . toggle ( "selected" ) ;
324361 this . current_rule . str = null ;
325- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
362+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
326363 this . current_rule . hash . validations = { } ;
327364 const raw_days = Array . from ( this . content . querySelectorAll ( ".day_holder a.selected" ) )
328365 . map ( el => parseInt ( el . dataset . value ) )
@@ -334,7 +371,7 @@ class RecurringSelectDialog {
334371 dateOfMonthChanged ( event ) {
335372 event . target . classList . toggle ( "selected" ) ;
336373 this . current_rule . str = null ;
337- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
374+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
338375 this . current_rule . hash . validations = { } ;
339376 const raw_days = Array . from ( this . content . querySelectorAll ( ".monthly_options .rs_calendar_day a.selected" ) )
340377 . map ( el => {
@@ -349,21 +386,44 @@ class RecurringSelectDialog {
349386 weekOfMonthChanged ( event ) {
350387 event . target . classList . toggle ( "selected" ) ;
351388 this . current_rule . str = null ;
352- if ( ! this . current_rule . hash ) { this . current_rule . hash = { } } ;
389+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
353390 this . current_rule . hash . validations = { } ;
354391 this . current_rule . hash . validations . day_of_month = [ ] ;
355392 this . current_rule . hash . validations . day_of_week = { } ;
356393 this . content . querySelectorAll ( ".monthly_options .rs_calendar_week a.selected" )
357394 . forEach ( ( elm , index ) => {
358395 const day = parseInt ( elm . getAttribute ( "day" ) ) ;
359396 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 ] = [ ] } ;
397+ if ( ! this . current_rule . hash . validations . day_of_week [ day ] ) { this . current_rule . hash . validations . day_of_week [ day ] = [ ] ; }
361398 return this . current_rule . hash . validations . day_of_week [ day ] . push ( instance ) ;
362399 } )
363400 this . summaryUpdate ( ) ;
364401 return false ;
365402 }
366403
404+ terminationChanged ( ) {
405+ this . selected_termination_type = this . outer_holder . querySelector ( ".rs_termination_section input[type='radio']:checked" ) ;
406+ if ( ! this . selected_termination_type ) { return ; }
407+ this . current_rule . str = null ;
408+ if ( ! this . current_rule . hash ) { this . current_rule . hash = { } ; }
409+ switch ( this . selected_termination_type . value ) {
410+ case "count" :
411+ this . current_rule . hash . count = parseInt ( this . occurence_count ? this . occurence_count . value : this . outer_holder . querySelector ( "#rs_occurrence_count" ) . value ) ;
412+ if ( ( this . current_rule . hash . count < 1 ) || isNaN ( this . current_rule . hash . count ) ) {
413+ this . current_rule . hash . count = 1 ;
414+ }
415+ this . current_rule . hash . until = null ;
416+ break
417+ case "until" :
418+ this . current_rule . hash . until = this . until_date ? this . until_date . value : this . outer_holder . querySelector ( "#rs_until_date" ) . value ;
419+ this . current_rule . hash . count = null ;
420+ break
421+ default :
422+ this . current_rule . hash . count = null ;
423+ this . current_rule . hash . until = null ;
424+ }
425+ this . summaryUpdate ( ) ;
426+ }
367427// ========================= Change callbacks ===============================
368428
369429 template ( ) {
@@ -381,7 +441,6 @@ class RecurringSelectDialog {
381441 <option value='Yearly'>${ this . config . texts [ "yearly" ] } </option> \
382442 </select> \
383443 </p> \
384- \
385444 <div class='daily_options freq_option_section'> \
386445 <p> \
387446 ${ this . config . texts [ "every" ] } \
@@ -400,7 +459,7 @@ class RecurringSelectDialog {
400459 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 ) {
401460 day_of_week = day_of_week % 7 ;
402461 str += `<a href='#' data-value='${ day_of_week } '>${ this . config . texts [ "days_first_letter" ] [ day_of_week ] } </a>` ;
403- } ;
462+ }
404463
405464 str += `\
406465 </div> \
@@ -426,6 +485,31 @@ class RecurringSelectDialog {
426485 ${ this . config . texts [ "years" ] } \
427486 </p> \
428487 </div> \
488+ <div class='rs_termination_section'>
489+ <table>
490+ <tr>
491+ <td>
492+ <label class='rs_termination_label'>${ this . config . texts [ "ends" ] } :</label>
493+ </td>
494+ <td>
495+ <label>
496+ <input type='radio' name='rs_termination' value='never' />
497+ ${ this . config . texts [ "never" ] }
498+ </label><br>
499+ <label>
500+ <input type='radio' name='rs_termination' value='count' />
501+ ${ this . config . texts [ "after" ] }
502+ <input type='text' data-wrapper-class='ui-recurring-select' id='rs_occurrence_count' class='rs_count' value='10' size='2' />
503+ ${ this . config . texts [ "occurrences" ] }
504+ </label><br>
505+ <label>
506+ <input type='radio' name='rs_termination' value='until' />
507+ ${ this . config . texts [ "on" ] }
508+ <input type='text' data-wrapper-class='ui-recurring-select' class='rs_datepicker' id='rs_until_date' />
509+ </label>
510+ </td>
511+ </table>
512+ </div>
429513 <p class='rs_summary'> \
430514 <span></span> \
431515 </p> \
0 commit comments