Sha256: 9f5891354459fdd03bb976ee742513345b978a6f461ad4ab43001ffc88fff612

Contents?: true

Size: 1.85 KB

Versions: 30

Compression:

Stored size: 1.85 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,
    dateFormat: Spree.t('date_picker.js_format')
  });

  // 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

30 entries across 30 versions & 1 rubygems

Version Path
solidus_backend-4.4.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.4.0 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.3.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.2.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.1.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.3.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.3.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.1.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.3.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.3.0 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.2.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.1.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.0.4 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.4.6 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.0.3 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.1.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.2.2 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-3.4.5 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.2.1 app/assets/javascripts/spree/backend/datepicker.js
solidus_backend-4.2.0 app/assets/javascripts/spree/backend/datepicker.js