2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-08-09 07:57:45 -04:00
|
|
|
import Pikaday from 'pikaday';
|
2018-10-24 06:51:33 -04:00
|
|
|
import { parsePikadayDate, pikadayToString } from './lib/utils/datetime_utility';
|
2017-08-09 07:57:45 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
export default function memberExpirationDate(selector = '.js-access-expiration-date') {
|
|
|
|
function toggleClearInput() {
|
2018-10-10 03:13:34 -04:00
|
|
|
$(this)
|
|
|
|
.closest('.clearable-input')
|
|
|
|
.toggleClass('has-value', $(this).val() !== '');
|
2017-10-12 05:42:13 -04:00
|
|
|
}
|
|
|
|
const inputs = $(selector);
|
|
|
|
|
|
|
|
inputs.each((i, el) => {
|
|
|
|
const $input = $(el);
|
|
|
|
|
|
|
|
const calendar = new Pikaday({
|
|
|
|
field: $input.get(0),
|
|
|
|
theme: 'gitlab-theme animate-picker',
|
|
|
|
format: 'yyyy-mm-dd',
|
|
|
|
minDate: new Date(),
|
|
|
|
container: $input.parent().get(0),
|
2020-12-23 16:10:24 -05:00
|
|
|
parse: (dateString) => parsePikadayDate(dateString),
|
|
|
|
toString: (date) => pikadayToString(date),
|
2017-10-12 05:42:13 -04:00
|
|
|
onSelect(dateText) {
|
2017-10-18 07:31:01 -04:00
|
|
|
$input.val(calendar.toString(dateText));
|
2017-10-12 05:42:13 -04:00
|
|
|
|
|
|
|
toggleClearInput.call($input);
|
|
|
|
},
|
2018-11-06 16:16:49 -05:00
|
|
|
firstDay: gon.first_day_of_week,
|
2016-08-18 20:41:18 -04:00
|
|
|
});
|
2016-08-18 09:12:48 -04:00
|
|
|
|
2017-10-18 07:31:01 -04:00
|
|
|
calendar.setDate(parsePikadayDate($input.val()));
|
2017-10-12 05:42:13 -04:00
|
|
|
$input.data('pikaday', calendar);
|
|
|
|
});
|
2016-08-18 09:12:48 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
inputs.next('.js-clear-input').on('click', function clicked(event) {
|
|
|
|
event.preventDefault();
|
2017-01-04 08:16:14 -05:00
|
|
|
|
2020-12-23 07:10:26 -05:00
|
|
|
const input = $(this).closest('.clearable-input').find(selector);
|
2017-10-12 05:42:13 -04:00
|
|
|
const calendar = input.data('pikaday');
|
|
|
|
|
|
|
|
calendar.setDate(null);
|
|
|
|
toggleClearInput.call(input);
|
|
|
|
});
|
2016-08-18 09:12:48 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
inputs.on('blur', toggleClearInput);
|
2016-08-18 09:12:48 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
inputs.each(toggleClearInput);
|
|
|
|
}
|