Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from jQuery UI datepicker to flatpickr #2506

Merged
merged 2 commits into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app/assets/javascripts/spree/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//= require solidus_admin/backbone-nested-models
//= require solidus_admin/popover
//= require solidus_admin/bootstrap
//= require solidus_admin/flatpickr/flatpickr
//= require prism
//= require spree
//= require spree/backend/namespaces
Expand All @@ -33,6 +34,7 @@
//= require spree/backend/components/tabs
//= require spree/backend/components/tooltips
//= require spree/backend/components/editable_table
//= require spree/backend/datepicker
//= require spree/backend/flash
//= require spree/backend/gateway
//= require spree/backend/handlebars_extensions
Expand Down
22 changes: 0 additions & 22 deletions backend/app/assets/javascripts/spree/backend/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,7 @@ $.fn.radioControlsVisibilityOfElement = function(dependentElementSelector){
});
}

var handle_date_picker_fields = function(){
$('.datepicker').datepicker({
dateFormat: Spree.t('date_picker.js_format', { default: 'yy/mm/dd' }),
dayNames: Spree.t('abbr_day_names'),
dayNamesMin: Spree.t('abbr_day_names'),
firstDay: parseInt(Spree.t('date_picker.first_day', { default: '0' }), 10),
monthNames: Spree.t('month_names'),
prevText: Spree.t('previous'),
nextText: Spree.t('next'),
showOn: "focus"
});

// Correctly display range dates
$('.date-range-filter .datepicker-from').datepicker('option', 'onSelect', function(selectedDate) {
$(".date-range-filter .datepicker-to" ).datepicker( "option", "minDate", selectedDate );
});
$('.date-range-filter .datepicker-to').datepicker('option', 'onSelect', function(selectedDate) {
$(".date-range-filter .datepicker-from" ).datepicker( "option", "maxDate", selectedDate );
});
}

Spree.ready(function(){
handle_date_picker_fields();
var uniqueId = 1;
$('.spree_add_fields').click(function() {
var target = $(this).data("target");
Expand Down
55 changes: 55 additions & 0 deletions backend/app/assets/javascripts/spree/backend/datepicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

Spree.ready(function(){
flatpickr.localize({
weekdays: {
shorthand: Spree.t('abbr_day_names')
},
months: {
longhand: Spree.t('month_names')
}
});

$('.datepicker').flatpickr({
allowInput: true
});

// Handle range dates
if ($('.date-range-filter .datepicker-from, .date-range-filter .datepicker-to').length) {
// Returns a callback for flatpickr onChange event which updates the
// respective range extremity flatpickr instance in order to have
// the left range extremity lower or equal to the right range extremity.
// This is accomplished swapping the range extremities when they are in the
// wrong order.
var swapDates = function($other, otherInstance, compareDates) {
return function(selectedDates, dateStr, instance) {
var date = instance.parseDate(dateStr);
var otherDateStr = $other.val();
var otherDate = instance.parseDate(otherDateStr);

if (date && otherDate && compareDates(date, otherDate)) {
instance.setDate(otherDate);
otherInstance.setDate(date);
}
}
}

var $left = $('.date-range-filter .datepicker-from');
var $right = $('.date-range-filter .datepicker-to');
var leftInstance = $left[0]._flatpickr;
var rightInstance = $right[0]._flatpickr;
var leftSwapDates = swapDates($right, rightInstance, function(date, otherDate) {
return date > otherDate
})
var rightSwapDates = swapDates($left, leftInstance, function(date, otherDate) {
return date < otherDate
})

leftInstance.config.onChange.push(leftSwapDates);
rightInstance.config.onChange.push(rightSwapDates);

// Execute swap dates check in order to correct possible wrong order at page
// load
leftSwapDates(null, $left.val(), leftInstance);
}
});
1 change: 1 addition & 0 deletions backend/app/assets/stylesheets/spree/backend.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*= require jquery-ui/datepicker
*= require jquery-ui/autocomplete
*= require solidus_admin/select2
*= require solidus_admin/flatpickr/flatpickr
*= require prism

*= require spree/backend/spree_admin
Expand Down
Loading