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

203 lines
6.2 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* global dateFormat */
2017-09-10 17:25:29 +05:30
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2017-09-10 17:25:29 +05:30
import Pikaday from 'pikaday';
2018-03-17 18:26:18 +05:30
import axios from './lib/utils/axios_utils';
import { parsePikadayDate, pikadayToString } from './lib/utils/datefix';
2017-08-17 22:00:37 +05:30
class DueDateSelect {
constructor({ $dropdown, $loading } = {}) {
const $dropdownParent = $dropdown.closest('.dropdown');
const $block = $dropdown.closest('.block');
this.$loading = $loading;
this.$dropdown = $dropdown;
this.$dropdownParent = $dropdownParent;
this.$datePicker = $dropdownParent.find('.js-due-date-calendar');
this.$block = $block;
this.$selectbox = $dropdown.closest('.selectbox');
this.$value = $block.find('.value');
this.$valueContent = $block.find('.value-content');
this.$sidebarValue = $('.js-due-date-sidebar-value', $block);
2018-03-27 19:54:05 +05:30
this.fieldName = $dropdown.data('fieldName');
this.abilityName = $dropdown.data('abilityName');
this.issueUpdateURL = $dropdown.data('issueUpdate');
2017-08-17 22:00:37 +05:30
this.rawSelectedDate = null;
this.displayedDate = null;
this.datePayload = null;
this.initGlDropdown();
this.initRemoveDueDate();
this.initDatePicker();
}
initGlDropdown() {
this.$dropdown.glDropdown({
opened: () => {
const calendar = this.$datePicker.data('pikaday');
calendar.show();
},
hidden: () => {
this.$selectbox.hide();
this.$value.css('display', '');
2018-03-17 18:26:18 +05:30
},
2017-08-17 22:00:37 +05:30
});
}
initDatePicker() {
const $dueDateInput = $(`input[name='${this.fieldName}']`);
const calendar = new Pikaday({
field: $dueDateInput.get(0),
theme: 'gitlab-theme',
format: 'yyyy-mm-dd',
2018-03-17 18:26:18 +05:30
parse: dateString => parsePikadayDate(dateString),
toString: date => pikadayToString(date),
2017-08-17 22:00:37 +05:30
onSelect: (dateText) => {
2018-03-17 18:26:18 +05:30
$dueDateInput.val(calendar.toString(dateText));
2017-08-17 22:00:37 +05:30
if (this.$dropdown.hasClass('js-issue-boards-due-date')) {
gl.issueBoards.BoardsStore.detail.issue.dueDate = $dueDateInput.val();
this.updateIssueBoardIssue();
} else {
this.saveDueDate(true);
}
2018-03-17 18:26:18 +05:30
},
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
calendar.setDate(parsePikadayDate($dueDateInput.val()));
2017-08-17 22:00:37 +05:30
this.$datePicker.append(calendar.el);
this.$datePicker.data('pikaday', calendar);
}
initRemoveDueDate() {
this.$block.on('click', '.js-remove-due-date', (e) => {
const calendar = this.$datePicker.data('pikaday');
e.preventDefault();
calendar.setDate(null);
if (this.$dropdown.hasClass('js-issue-boards-due-date')) {
gl.issueBoards.BoardsStore.detail.issue.dueDate = '';
this.updateIssueBoardIssue();
} else {
2018-03-17 18:26:18 +05:30
$(`input[name='${this.fieldName}']`).val('');
this.saveDueDate(false);
2017-08-17 22:00:37 +05:30
}
});
}
saveDueDate(isDropdown) {
this.parseSelectedDate();
this.prepSelectedDate();
this.submitSelectedDate(isDropdown);
}
parseSelectedDate() {
this.rawSelectedDate = $(`input[name='${this.fieldName}']`).val();
if (this.rawSelectedDate.length) {
// Construct Date object manually to avoid buggy dateString support within Date constructor
const dateArray = this.rawSelectedDate.split('-').map(v => parseInt(v, 10));
const dateObj = new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);
this.displayedDate = dateFormat(dateObj, 'mmm d, yyyy');
} else {
this.displayedDate = 'No due date';
}
}
prepSelectedDate() {
const datePayload = {};
datePayload[this.abilityName] = {};
datePayload[this.abilityName].due_date = this.rawSelectedDate;
this.datePayload = datePayload;
}
2018-03-17 18:26:18 +05:30
updateIssueBoardIssue() {
2017-08-17 22:00:37 +05:30
this.$loading.fadeIn();
this.$dropdown.trigger('loading.gl.dropdown');
this.$selectbox.hide();
this.$value.css('display', '');
const fadeOutLoader = () => {
this.$loading.fadeOut();
};
gl.issueBoards.BoardsStore.detail.issue.update(this.$dropdown.attr('data-issue-update'))
.then(fadeOutLoader)
.catch(fadeOutLoader);
}
submitSelectedDate(isDropdown) {
2018-03-17 18:26:18 +05:30
const selectedDateValue = this.datePayload[this.abilityName].due_date;
const displayedDateStyle = this.displayedDate !== 'No due date' ? 'bold' : 'no-value';
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$loading.removeClass('hidden').fadeIn();
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
if (isDropdown) {
this.$dropdown.trigger('loading.gl.dropdown');
this.$selectbox.hide();
}
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
this.$value.css('display', '');
this.$valueContent.html(`<span class='${displayedDateStyle}'>${this.displayedDate}</span>`);
this.$sidebarValue.html(this.displayedDate);
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
$('.js-remove-due-date-holder').toggleClass('hidden', selectedDateValue.length);
return axios.put(this.issueUpdateURL, this.datePayload)
.then(() => {
if (isDropdown) {
this.$dropdown.trigger('loaded.gl.dropdown');
this.$dropdown.dropdown('toggle');
}
return this.$loading.fadeOut();
});
2017-08-17 22:00:37 +05:30
}
}
2018-03-17 18:26:18 +05:30
export default class DueDateSelectors {
2017-08-17 22:00:37 +05:30
constructor() {
this.initMilestoneDatePicker();
this.initIssuableSelect();
}
2018-03-17 18:26:18 +05:30
// eslint-disable-next-line class-methods-use-this
2017-08-17 22:00:37 +05:30
initMilestoneDatePicker() {
2018-03-17 18:26:18 +05:30
$('.datepicker').each(function initPikadayMilestone() {
2017-08-17 22:00:37 +05:30
const $datePicker = $(this);
const calendar = new Pikaday({
field: $datePicker.get(0),
theme: 'gitlab-theme animate-picker',
format: 'yyyy-mm-dd',
container: $datePicker.parent().get(0),
2018-03-17 18:26:18 +05:30
parse: dateString => parsePikadayDate(dateString),
toString: date => pikadayToString(date),
2017-08-17 22:00:37 +05:30
onSelect(dateText) {
2018-03-17 18:26:18 +05:30
$datePicker.val(calendar.toString(dateText));
},
2017-08-17 22:00:37 +05:30
});
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
calendar.setDate(parsePikadayDate($datePicker.val()));
2017-08-17 22:00:37 +05:30
$datePicker.data('pikaday', calendar);
});
$('.js-clear-due-date,.js-clear-start-date').on('click', (e) => {
e.preventDefault();
const calendar = $(e.target).siblings('.datepicker').data('pikaday');
calendar.setDate(null);
});
}
2018-03-17 18:26:18 +05:30
// eslint-disable-next-line class-methods-use-this
2017-08-17 22:00:37 +05:30
initIssuableSelect() {
const $loading = $('.js-issuable-update .due_date').find('.block-loading').hide();
$('.js-due-date-select').each((i, dropdown) => {
const $dropdown = $(dropdown);
2018-03-17 18:26:18 +05:30
// eslint-disable-next-line no-new
2017-08-17 22:00:37 +05:30
new DueDateSelect({
$dropdown,
2018-03-17 18:26:18 +05:30
$loading,
2017-08-17 22:00:37 +05:30
});
});
}
}