Created
January 26, 2018 00:17
-
-
Save juanpablob/5e535fc65c6ba3029de452f567e0610f to your computer and use it in GitHub Desktop.
date handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dateHandler: { | |
self: this, | |
pickers: {}, | |
dates: { | |
form: null, | |
to: null, | |
disabled: [] | |
}, | |
objects: { | |
regular: 'input[data-date-picker="regular"]', | |
inline: 'input[data-date-picker="inline"]' | |
}, | |
onSelect: function(formattedDate, date, instance) { | |
// debugger; | |
if (instance.$el.attr('name') == 'fecha_inicio') { | |
app.dateHandler.dates.from = date; // Set the date | |
// Tweak date mask if picker is inline | |
if (instance.$el.attr('data-date-picker') == 'inline') { | |
$('.c-search__date-placeholder').hide(); | |
$('.c-search__date-date, .c-search__date-separator').show(); | |
$('.c-search__date-from').html(moment(formattedDate, helper.dateFormat()).format(helper.dateFormat('short'))); | |
} | |
// Set focus on the next picker | |
instance.$el.parents('form').find('input[name="fecha_termino"]').focus(); | |
if (moment.isDate(date)) { | |
// Cross check date integrity | |
if (moment.isDate(app.dateHandler.dates.to)) { | |
if (moment(app.dateHandler.dates.to).diff(app.dateHandler.dates.from, 'days') >= 7) { | |
instance.hide(); | |
} | |
else { | |
instance.clear(); | |
alert('Las fechas seleccionadas no son válidas. Recuerda que la reserva mínima son 7 días.'); | |
} | |
} | |
} | |
} | |
else if (instance.$el.attr('name') == 'fecha_termino') { | |
app.dateHandler.dates.to = date; // Set the date | |
// Tweak date mask if picker is inline | |
if (instance.$el.attr('data-date-picker') == 'inline') { | |
$('.c-search__date-to').html(moment(formattedDate, helper.dateFormat()).format(helper.dateFormat('short'))); | |
} | |
if (moment.isDate(date)) { | |
// Cross check date integrity | |
if (moment.isDate(app.dateHandler.dates.from)) { | |
if (moment(app.dateHandler.dates.to).diff(app.dateHandler.dates.from, 'days') >= 7) { | |
instance.hide(); | |
} | |
else { | |
instance.clear(); | |
alert('Las fechas seleccionadas no son válidas. Recuerda que la reserva mínima son 7 días.'); | |
} | |
} | |
} | |
} | |
}, | |
onShow: function(instance, animationCompleted) { | |
var pickerValue = moment(instance.$el.val(), helper.dateFormat()); | |
if (animationCompleted) { | |
if (pickerValue.isValid()) { | |
instance.date = pickerValue.toDate(); // Set given date on value attr | |
} | |
} | |
}, | |
onRenderCell: function(date, cellType) { | |
// Get booked dates | |
if ($('input[name="reservas"]').length) { | |
app.dateHandler.dates.disabled = $('input[name="reservas"]').val().split(',').map(function(bookedDates) { | |
return moment(bookedDates).format(); | |
}); | |
} | |
else { | |
self.disabledDates = []; | |
} | |
}, | |
init: function() { | |
// Initialize regular (via input) | |
app.dateHandler.pickers.regular = $(document).find(app.dateHandler.objects.regular).datepicker({ | |
language: settings.lang, | |
minDate: new Date(), | |
onRenderCell: function(date, cellType) { | |
app.dateHandler.onRenderCell(date, cellType); | |
}, | |
onShow: function(instance, animationCompleted) { | |
app.dateHandler.onShow(instance, animationCompleted); | |
}, | |
onSelect: function(formattedDate, date, instance) { | |
app.dateHandler.onSelect(formattedDate, date, instance); | |
} | |
}).data('datepicker'); | |
// Intialize inline | |
app.dateHandler.pickers.inline = $(document).find(app.dateHandler.objects.inline).datepicker({ | |
inline: true, | |
language: settings.lang, | |
minDate: new Date(), | |
onShow: function(instance, animationCompleted) { | |
app.dateHandler.onShow(instance, animationCompleted); | |
}, | |
onSelect: function(formattedDate, date, instance) { | |
app.dateHandler.onSelect(formattedDate, date, instance); | |
} | |
}).data('datepicker'); | |
// Check if element exists in the view | |
if ($('form[name="search"] [data-date-picker="inline"]').length) { | |
// Preselect dates passed via get params | |
if (moment(app.dateHandler.dates.from).isValid() && moment(app.dateHandler.dates.to).isValid()) { | |
// self.inlineFrom.selectDate(moment(self.dateFrom).toDate()); | |
// self.inlineTo.selectDate(moment(self.dateTo).toDate()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment