Sha256: e5536e6f78651887527ed75d5023eba263802520cc64fdd6b9897b463651c5f4

Contents?: true

Size: 1.8 KB

Versions: 107

Compression:

Stored size: 1.8 KB

Contents

'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);
  }
});

Version data entries

107 entries across 107 versions & 2 rubygems

Version Path
solidus_backend-2.11.17 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.7 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.16 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.15 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.14 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.13 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.12 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.1.0 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.11 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.2 app/assets/javascripts/spree/backend/datepicker.js