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.10.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.10 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.0 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.9 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.8 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.7 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.0.0.rc2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.10.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.11.0 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.10.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.9.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.8.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-2.10.1 app/assets/javascripts/spree/backend/datepicker.js