2019-10-05 02:09:52 +02:00
|
|
|
/**
|
|
|
|
* Gallery rearrangement.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { arraysEqual } from './utils/array';
|
2024-06-08 17:58:11 +02:00
|
|
|
import { assertNotNull, assertNotUndefined } from './utils/assert';
|
2019-10-05 02:09:52 +02:00
|
|
|
import { $, $$ } from './utils/dom';
|
|
|
|
import { initDraggables } from './utils/draggable';
|
|
|
|
import { fetchJson } from './utils/requests';
|
|
|
|
|
|
|
|
export function setupGalleryEditing() {
|
|
|
|
if (!$('.rearrange-button')) return;
|
|
|
|
|
2024-06-08 17:58:11 +02:00
|
|
|
const [ rearrangeEl, saveEl ] = $$<HTMLElement>('.rearrange-button');
|
|
|
|
const sortableEl = assertNotNull($<HTMLDivElement>('#sortable'));
|
|
|
|
const containerEl = assertNotNull($<HTMLDivElement>('.js-resizable-media-container'));
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// Copy array
|
2024-06-08 17:58:11 +02:00
|
|
|
const galleryImages = assertNotUndefined(window.booru.galleryImages);
|
|
|
|
let oldImages = galleryImages.slice();
|
|
|
|
let newImages = galleryImages.slice();
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
initDraggables();
|
|
|
|
|
2024-06-08 17:58:11 +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-08 17:58:11 +02:00
|
|
|
newImages = $$<HTMLDivElement>('.image-container', containerEl)
|
|
|
|
.map(i => parseInt(assertNotUndefined(i.dataset.imageId), 10));
|
2019-10-05 02:09:52 +02:00
|
|
|
|
|
|
|
// If nothing changed, don't bother.
|
|
|
|
if (arraysEqual(newImages, oldImages)) return;
|
|
|
|
|
2024-06-08 17:58:11 +02:00
|
|
|
const reorderPath = assertNotUndefined(saveEl.dataset.reorderPath);
|
|
|
|
|
|
|
|
fetchJson('PATCH', reorderPath, {
|
2019-10-05 02:09:52 +02:00
|
|
|
image_ids: newImages,
|
|
|
|
|
|
|
|
// copy the array again so that we have the newly updated set
|
|
|
|
}).then(() => oldImages = newImages.slice());
|
|
|
|
});
|
|
|
|
}
|