gitlab-org--gitlab-foss/app/assets/javascripts/member_expiration_date.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-01-04 13:16:14 +00:00
/* global dateFormat */
import Pikaday from 'pikaday';
(() => {
// 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.
//
window.gl = window.gl || {};
gl.MemberExpirationDate = (selector = '.js-access-expiration-date') => {
2016-08-19 00:41:18 +00:00
function toggleClearInput() {
$(this).closest('.clearable-input').toggleClass('has-value', $(this).val() !== '');
}
const inputs = $(selector);
2017-01-04 13:16:14 +00:00
inputs.each((i, el) => {
const $input = $(el);
const calendar = new Pikaday({
field: $input.get(0),
2017-04-21 15:05:38 +00:00
theme: 'gitlab-theme animate-picker',
2017-02-14 21:45:36 +00:00
format: 'yyyy-mm-dd',
2017-01-04 13:16:14 +00:00
minDate: new Date(),
2017-04-21 15:05:38 +00:00
container: $input.parent().get(0),
2017-01-06 14:43:21 +00:00
onSelect(dateText) {
2017-01-04 13:16:14 +00:00
$input.val(dateFormat(new Date(dateText), 'yyyy-mm-dd'));
$input.trigger('change');
2017-01-06 14:43:21 +00:00
toggleClearInput.call($input);
2017-01-04 13:16:14 +00:00
},
});
2017-02-14 21:45:36 +00:00
calendar.setDate(new Date($input.val()));
2017-01-04 13:16:14 +00:00
$input.data('pikaday', calendar);
2016-08-19 00:41:18 +00:00
});
inputs.next('.js-clear-input').on('click', function clicked(event) {
2016-08-19 00:41:18 +00:00
event.preventDefault();
const input = $(this).closest('.clearable-input').find(selector);
2017-01-04 13:16:14 +00:00
const calendar = input.data('pikaday');
calendar.setDate(null);
input.trigger('change');
2016-08-19 00:41:18 +00:00
toggleClearInput.call(input);
});
2016-08-19 00:41:18 +00:00
inputs.on('blur', toggleClearInput);
2016-08-19 00:41:18 +00:00
inputs.each(toggleClearInput);
};
}).call(window);