debian-mirror-gitlab/app/assets/javascripts/member_expiration_date.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import Pikaday from 'pikaday';
2018-12-13 13:39:08 +05:30
import { parsePikadayDate, pikadayToString } from './lib/utils/datetime_utility';
2018-03-17 18:26:18 +05:30
// 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-12-13 13:39:08 +05:30
$(this)
.closest('.clearable-input')
.toggleClass('has-value', $(this).val() !== '');
2018-03-17 18:26:18 +05:30
}
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),
parse: dateString => parsePikadayDate(dateString),
toString: date => pikadayToString(date),
onSelect(dateText) {
$input.val(calendar.toString(dateText));
$input.trigger('change');
toggleClearInput.call($input);
},
2019-03-02 22:35:43 +05:30
firstDay: gon.first_day_of_week,
2016-09-13 17:45:13 +05:30
});
2018-03-17 18:26:18 +05:30
calendar.setDate(parsePikadayDate($input.val()));
$input.data('pikaday', calendar);
});
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
inputs.next('.js-clear-input').on('click', function clicked(event) {
event.preventDefault();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
const input = $(this)
.closest('.clearable-input')
.find(selector);
2018-03-17 18:26:18 +05:30
const calendar = input.data('pikaday');
calendar.setDate(null);
input.trigger('change');
toggleClearInput.call(input);
});
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
inputs.on('blur', toggleClearInput);
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
inputs.each(toggleClearInput);
}