diff options
| -rw-r--r-- | app/assets/javascripts/calendars.coffee | 35 | ||||
| -rw-r--r-- | app/assets/javascripts/smart_date.coffee | 17 | ||||
| -rw-r--r-- | app/views/calendars/_date_value_fields.html.slim | 2 | 
3 files changed, 18 insertions, 36 deletions
| diff --git a/app/assets/javascripts/calendars.coffee b/app/assets/javascripts/calendars.coffee deleted file mode 100644 index ac5330cc8..000000000 --- a/app/assets/javascripts/calendars.coffee +++ /dev/null @@ -1,35 +0,0 @@ - -isEmpty = (x) -> x == '' - -daySelector   = '#q_contains_date_3i' -monthSelector = '#q_contains_date_2i' -yearSelector  = '#q_contains_date_1i' - -dateSelectors = [ daySelector, monthSelector, yearSelector ] - -checkDate = (args...) -> -  vals = args.map((ele) -> ele.val()) -  return if vals.find( isEmpty ) == "" # no checking needed, no checking possible yet - -  dates = vals.map (x) -> parseInt(x) -  return if isLegalDate(dates) -  $('.alert').text('illegal date') -  $('.alert').show() - -legalDaysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - -isLeapYear = (year) -> -  (year % 4 == 0) && ((year % 100 != 0) || (year % 1000 == 0)) - -isLegalDate = (dates) -> -  [day, month, year] = dates -  return true if legalDaysPerMonth[month-1] >= day -  return true if day == 29 && isLeapYear(year) -  false - -defineChangeHandler = (selector) -> -  $(selector).on 'change', -> -    checkDate $(daySelector), $(monthSelector), $(yearSelector) - -$ -> -  defineChangeHandler selector for selector in dateSelectors diff --git a/app/assets/javascripts/smart_date.coffee b/app/assets/javascripts/smart_date.coffee new file mode 100644 index 000000000..1698bee06 --- /dev/null +++ b/app/assets/javascripts/smart_date.coffee @@ -0,0 +1,17 @@ +legalDaysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + +correctDay = (dateValues) -> +  [day, month, year] = dates +  return day if legalDaysPerMonth[month-1] >= day +  return 29 if day == 29 && isLeapYear(year) +  legalDaysPerMonth[month-1] + +smartCorrectDate = -> +  allSelectors = $(@).parent().children('select') # N'a pas un sibbling('select', include_self = true) ? +  allVals      = allSelectors.map (sel) -> paeseInt(sel.val()) +  correctedDay = correctDay allVals +  daySelector  = allSelectors.first() +  $(daySelector).val(correctedDay) + +$ -> +  $(smartDateSelectSelector).on 'change', smartCorrectDate diff --git a/app/views/calendars/_date_value_fields.html.slim b/app/views/calendars/_date_value_fields.html.slim index 2d072fa0a..7bde06a94 100644 --- a/app/views/calendars/_date_value_fields.html.slim +++ b/app/views/calendars/_date_value_fields.html.slim @@ -8,6 +8,6 @@    .wrapper      div -      = f.input :value, as: :date, label: false, wrapper_html: { class: 'date' } +      = f.input :value, as: :date, label: false, wrapper_html: { class: 'date smart_date' }      div        = link_to_remove_association '', f, class: 'fa fa-trash', data: { confirm: 'Etes-vous sûr(e) ?' }, title: t('actions.delete') | 
