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

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-09-30 21:07:59 +05:30
import Sortable from 'sortablejs';
import { s__ } from '~/locale';
import createFlash from '~/flash';
import {
getBoardSortableDefaultOptions,
sortableStart,
} from '~/boards/mixins/sortable_default_options';
import axios from '~/lib/utils/axios_utils';
const updateIssue = (url, issueList, { move_before_id, move_after_id }) =>
axios
.put(`${url}/reorder`, {
move_before_id,
move_after_id,
group_full_path: issueList.dataset.groupFullPath,
})
.catch(() => {
createFlash(s__("ManualOrdering|Couldn't save the order of the issues"));
});
2019-12-26 22:10:19 +05:30
const initManualOrdering = (draggableSelector = 'li.issue') => {
2019-09-30 21:07:59 +05:30
const issueList = document.querySelector('.manual-ordering');
2019-10-12 21:52:04 +05:30
if (!issueList || !(gon.current_user_id > 0)) {
2019-09-30 21:07:59 +05:30
return;
}
Sortable.create(
issueList,
getBoardSortableDefaultOptions({
scroll: true,
2020-03-13 15:44:24 +05:30
fallbackTolerance: 1,
2019-09-30 21:07:59 +05:30
dataIdAttr: 'data-id',
fallbackOnBody: false,
group: {
name: 'issues',
},
2019-12-26 22:10:19 +05:30
draggable: draggableSelector,
2019-09-30 21:07:59 +05:30
onStart: () => {
sortableStart();
},
onUpdate: event => {
const el = event.item;
2019-12-26 22:10:19 +05:30
const url = el.getAttribute('url') || el.dataset.url;
2019-09-30 21:07:59 +05:30
const prev = el.previousElementSibling;
const next = el.nextElementSibling;
const beforeId = prev && parseInt(prev.dataset.id, 10);
const afterId = next && parseInt(next.dataset.id, 10);
updateIssue(url, issueList, { move_after_id: afterId, move_before_id: beforeId });
},
}),
);
};
export default initManualOrdering;