summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/member_expiration_date.js
blob: 0bd90c573967627b5d67211dbea1348a96993df5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* eslint-disable */
(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.
  //
  gl.MemberExpirationDate = function() {
    function toggleClearInput() {
      $(this).closest('.clearable-input').toggleClass('has-value', $(this).val() !== '');
    }

    var inputs = $('.js-access-expiration-date');

    inputs.datepicker({
      dateFormat: 'yy-mm-dd',
      minDate: 1,
      onSelect: function () {
        $(this).trigger('change');
        toggleClearInput.call(this);
      }
    });

    inputs.next('.js-clear-input').on('click', function(event) {
      event.preventDefault();

      var input = $(this).closest('.clearable-input').find('.js-access-expiration-date');
      input.datepicker('setDate', null)
        .trigger('change');
      toggleClearInput.call(input);
    });

    inputs.on('blur', toggleClearInput);

    inputs.each(toggleClearInput);
  };
}).call(this);