2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Gallery rearrangement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { arraysEqual } from './utils/array';
|
|
|
|
import { $, $$ } from './utils/dom';
|
|
|
|
import { initDraggables } from './utils/draggable';
|
|
|
|
import { fetchJson } from './utils/requests';
|
|
|
|
|
|
|
|
export function setupGalleryEditing() {
|
2024-06-06 18:48:16 +02:00
|
|
|
if (!$<HTMLElement>('.rearrange-button')) return;
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-06-06 18:48:16 +02:00
|
|
|
const [ rearrangeEl, saveEl ] = $$<HTMLElement>('.rearrange-button');
|
|
|
|
const sortableEl = $<HTMLDivElement>('#sortable');
|
|
|
|
const containerEl = $<HTMLDivElement>('.media-list');
|
|
|
|
|
|
|
|
if (!sortableEl || !containerEl || !saveEl || !rearrangeEl) { return; }
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Copy array
|
|
|
|
let oldImages = window.booru.galleryImages.slice();
|
|
|
|
let newImages = window.booru.galleryImages.slice();
|
|
|
|
|
|
|
|
initDraggables();
|
|
|
|
|
2024-06-06 18:48:16 +02:00
|
|
|
$$<HTMLDivElement>('.media-box', containerEl).forEach(i => i.draggable = true);
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
rearrangeEl.addEventListener('click', () => {
|
|
|
|
sortableEl.classList.add('editing');
|
|
|
|
containerEl.classList.add('drag-container');
|
|
|
|
});
|
|
|
|
|
|
|
|
saveEl.addEventListener('click', () => {
|
|
|
|
sortableEl.classList.remove('editing');
|
|
|
|
containerEl.classList.remove('drag-container');
|
|
|
|
|
2024-06-06 18:48:16 +02:00
|
|
|
newImages = $$<HTMLDivElement>('.image-container', containerEl).map(i => parseInt(i.dataset.imageId || '-1', 10));
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// If nothing changed, don't bother.
|
|
|
|
if (arraysEqual(newImages, oldImages)) return;
|
|
|
|
|
2024-06-06 18:48:16 +02:00
|
|
|
if (saveEl.dataset.reorderPath) {
|
|
|
|
fetchJson('PATCH', saveEl.dataset.reorderPath, {
|
|
|
|
image_ids: newImages,
|
2019-10-05 02:09:52 +02:00
|
|
|
|
2024-06-06 18:48:16 +02:00
|
|
|
// copy the array again so that we have the newly updated set
|
|
|
|
}).then(() => oldImages = newImages.slice());
|
|
|
|
}
|
2019-10-05 02:09:52 +02:00
|
|
|
});
|
|
|
|
}
|