app/assets/javascripts/integral/support/date_picker.coffee in integral-1.1.0 vs app/assets/javascripts/integral/support/date_picker.coffee in integral-1.2.0

- old
+ new

@@ -1,83 +1,78 @@ -# Represents Date Picker (PickADate) +# Represents Date Picker - requires jQuery UI Datepicker - https://jqueryui.com/datepicker/ class this.DatePicker + @getCurrentDate: (element, dateFormat='yy-mm-dd') -> + date = null + try + date = $.datepicker.parseDate( dateFormat, element.value ) + catch error + date + @initDateRanges: -> $('[data-date-picker-end-element]').each (index, dpStartElement) -> $dpStartElement = $(dpStartElement) - dpStart = $dpStartElement.pickadate('picker') - $dpEndElement = $('#' + $dpStartElement.data().datePickerEndElement) - dpEnd = $dpEndElement.pickadate('picker') + endSelector = "[id='" + $dpStartElement.data().datePickerEndElement + "']" + $dpEndElement = $(endSelector) - # Check if there’s a “from” or “to” date to start with. - if dpStart.get('value') - minimumDate = new Date(dpStart.get('select').obj.valueOf() + 86400000) - dpEnd.set('min', minimumDate) - if dpEnd.get('value') - maximumDate = new Date(dpEnd.get('select').obj.valueOf() - 86400000) - dpStart.set('max', maximumDate) + # Passing these options is doesn't seem to be working? + dpStart = $dpStartElement.datepicker( + firstDay: 1 + showAnim: "" + ) + dpEnd = $dpEndElement.datepicker( + firstDay: 1 + showAnim: "" + ) + dpStart.datepicker("option", "firstDay", 1) + dpEnd.datepicker("option", "firstDay", 1) + dpStart.datepicker("option", "showAnim", '') + dpEnd.datepicker("option", "showAnim", '') + dpStart.on "change", (ev) => + dateMinimum = DatePicker.getCurrentDate(ev.currentTarget) + dateMinimum.setDate(dateMinimum.getDate() + 1) + dpEnd.datepicker("option", "minDate", dateMinimum) + dpEnd.on "change", (ev) => + dateMaximum = DatePicker.getCurrentDate(ev.currentTarget) + dateMaximum.setDate(dateMaximum.getDate() - 1) + dpStart.datepicker("option", "maxDate", dateMaximum) - # When something is selected, update the “from” and “to” limits. - dpStart.on 'set', (event) => - if event.select - minimumDate = new Date(dpStart.get('select').obj.valueOf() + 86400000) - dpEnd.set('min', minimumDate) - - dpEnd.on 'set', (event) => - if event.select - maximumDate = new Date(dpEnd.get('select').obj.valueOf() - 86400000) - dpStart.set('max', maximumDate) - else if 'clear' of event - dpStart.set('max', false) - constructor: (selector, opts={}) -> @selector = selector @opts = opts - @_setLanguage() @_initializeDatePicker() _initializeDatePicker: -> $(@selector).each (i, dp) => dp = $(dp) minRaw = dp[0].min maxRaw = dp[0].max minDate = new Date minRaw if minRaw maxDate = new Date maxRaw if maxRaw + disabledDates = dp.data('disabled-dates') + showButtonPanel = if dp.data('date-picker-button-panel') == true then true else false + closeText = if dp.data('date-picker-close-text') then dp.data('date-picker-close-text') else 'Close' - dp.pickadate - min: minDate - max: maxDate - disable: @getDisabledDates(dp) - container: @getContainer(dp) - closeOnSelect: true - format: "yyyy-mm-dd" - onOpen: @opts.onOpen - onClose: @opts.onClose + dp.datepicker + dateFormat: "yy-mm-dd" + minDate: minDate + maxDate: maxDate + beforeShow: @opts.onOpen + onClose: (dateText, inst) => + if dp.data('date-picker-clear') == true && inst.dpDiv.find('button.clicked').length > 0 + inst.input.val('') + inst.input.change() + else + @opts.onClose() if @opts.close + showButtonPanel: showButtonPanel + closeText: closeText + # Return false to disable a specific date + beforeShowDay: (currentDate) => + return [true] if not disabledDates - getContainer: (dp) -> - dp.data('date-picker-container') + for dateStr in disabledDates.split(',') + parsedDate = new Date(dateStr) - getDisabledDates: (dp) -> - data = dp.data('disabled-dates') - return [] if not data + return [false] if (parsedDate.getFullYear() == currentDate.getFullYear() and parsedDate.getDate() == currentDate.getDate() and parsedDate.getMonth() == currentDate.getMonth()) - dates = [] - for date in data.split(',') - dates.push new Date(date) - - dates - - # TODO: Change this to I18n - _setLanguage: -> - $.extend($.fn.pickadate.defaults, { - monthsFull: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ], - monthsShort: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ], - weekdaysFull: [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ], - weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ], - weekdaysLetter: [ 'S', 'M', 'T', 'W', 'T', 'F', 'S' ], - today: 'Today', - clear: 'Clear', - close: 'Close', - firstDay: 1, - }) - + [true]