member_expiration_date.js 1.02 KB
Newer Older
1 2 3 4 5 6
(function() {
  // Add datepickers to all `js-access-expiration-date` elements. If those elements are
  // children of an element with the `clearable-input` class, and have a sibling
  // `js-clear-input` element, then show that element when there is a value in the
  // datepicker, and make clicking on that element clear the field.
  //
7
  gl.MemberExpirationDate = function() {
Douwe Maan's avatar
Douwe Maan committed
8 9 10
    function toggleClearInput() {
      $(this).closest('.clearable-input').toggleClass('has-value', $(this).val() !== '');
    }
11

Douwe Maan's avatar
Douwe Maan committed
12
    var inputs = $('.js-access-expiration-date');
13

Douwe Maan's avatar
Douwe Maan committed
14 15 16 17 18
    inputs.datepicker({
      dateFormat: 'yy-mm-dd',
      minDate: 1,
      onSelect: toggleClearInput
    });
19

Douwe Maan's avatar
Douwe Maan committed
20 21
    inputs.next('.js-clear-input').on('click', function(event) {
      event.preventDefault();
22

Douwe Maan's avatar
Douwe Maan committed
23 24 25 26
      var input = $(this).closest('.clearable-input').find('.js-access-expiration-date');
      input.datepicker('setDate', null);
      toggleClearInput.call(input);
    });
27

Douwe Maan's avatar
Douwe Maan committed
28
    inputs.on('blur', toggleClearInput);
29

Douwe Maan's avatar
Douwe Maan committed
30
    inputs.each(toggleClearInput);
31 32
  };
}).call(this);