aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/smart_date.coffee
diff options
context:
space:
mode:
authorXinhui2017-06-28 17:20:23 +0200
committerXinhui2017-06-28 17:20:23 +0200
commitbed9795be0bb85d7c8c311fe4ee0fb12e46832b4 (patch)
tree18459aa9dcb81821a88f2acdf9ba72cee9be5616 /app/assets/javascripts/smart_date.coffee
parent641b1458236d2718a76ffaf0c04a5998623276bf (diff)
parentb260c832c8e129dbeacfe065d01bd0732dd80701 (diff)
downloadchouette-core-bed9795be0bb85d7c8c311fe4ee0fb12e46832b4.tar.bz2
Merge branch 'master' into staging
Diffstat (limited to 'app/assets/javascripts/smart_date.coffee')
-rw-r--r--app/assets/javascripts/smart_date.coffee26
1 files changed, 26 insertions, 0 deletions
diff --git a/app/assets/javascripts/smart_date.coffee b/app/assets/javascripts/smart_date.coffee
new file mode 100644
index 000000000..afc0c7ddf
--- /dev/null
+++ b/app/assets/javascripts/smart_date.coffee
@@ -0,0 +1,26 @@
+window.legalDaysPerMonth =
+ false: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
+ true: [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+
+window.legal
+window.correctDay = (dateValues) ->
+ [day, month, year] = dateValues
+ return day unless day > 0 && month > 0 && year > 0
+
+ legallyMaximumDay = legalDaysPerMonth[isLeapYear(year)][month - 1]
+ Math.min day, legallyMaximumDay
+
+window.isLeapYear = (year) ->
+ (year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0))
+
+window.smartCorrectDate = ->
+ allSelectors = $(@).parent().children('select')
+ allVals = allSelectors.map (index, sel) ->
+ parseInt($(sel).val())
+ correctedDay = correctDay allVals
+ daySelector = allSelectors.first()
+ $(daySelector).val(correctedDay)
+
+$ ->
+ $(document).on 'change', '.smart_date select', smartCorrectDate