2023-04-18 17:20:02 -04:00
|
|
|
import {
|
|
|
|
onChildEvent, onSelect, removeLoading, showLoading,
|
|
|
|
} from '../services/dom';
|
|
|
|
import {Component} from './component';
|
2020-07-24 19:20:58 -04:00
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
export class ImageManager extends Component {
|
2020-07-24 19:20:58 -04:00
|
|
|
|
|
|
|
setup() {
|
|
|
|
// Options
|
|
|
|
this.uploadedTo = this.$opts.uploadedTo;
|
|
|
|
|
|
|
|
// Element References
|
|
|
|
this.container = this.$el;
|
|
|
|
this.popupEl = this.$refs.popup;
|
|
|
|
this.searchForm = this.$refs.searchForm;
|
|
|
|
this.searchInput = this.$refs.searchInput;
|
|
|
|
this.cancelSearch = this.$refs.cancelSearch;
|
|
|
|
this.listContainer = this.$refs.listContainer;
|
|
|
|
this.filterTabs = this.$manyRefs.filterTabs;
|
|
|
|
this.selectButton = this.$refs.selectButton;
|
2023-04-26 09:23:28 -04:00
|
|
|
this.uploadButton = this.$refs.uploadButton;
|
|
|
|
this.uploadHint = this.$refs.uploadHint;
|
2020-07-24 19:20:58 -04:00
|
|
|
this.formContainer = this.$refs.formContainer;
|
2023-04-25 08:10:25 -04:00
|
|
|
this.formContainerPlaceholder = this.$refs.formContainerPlaceholder;
|
2020-07-24 19:20:58 -04:00
|
|
|
this.dropzoneContainer = this.$refs.dropzoneContainer;
|
|
|
|
|
|
|
|
// Instance data
|
|
|
|
this.type = 'gallery';
|
|
|
|
this.lastSelected = {};
|
|
|
|
this.lastSelectedTime = 0;
|
2020-07-25 06:47:12 -04:00
|
|
|
this.callback = null;
|
2020-07-24 19:20:58 -04:00
|
|
|
this.resetState = () => {
|
|
|
|
this.hasData = false;
|
|
|
|
this.page = 1;
|
|
|
|
this.filter = 'all';
|
|
|
|
};
|
|
|
|
this.resetState();
|
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
setupListeners() {
|
|
|
|
onSelect(this.filterTabs, e => {
|
|
|
|
this.resetAll();
|
|
|
|
this.filter = e.target.dataset.filter;
|
|
|
|
this.setActiveFilterTab(this.filter);
|
|
|
|
this.loadGallery();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.searchForm.addEventListener('submit', event => {
|
|
|
|
this.resetListView();
|
|
|
|
this.loadGallery();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
onSelect(this.cancelSearch, () => {
|
2020-07-24 19:20:58 -04:00
|
|
|
this.resetListView();
|
|
|
|
this.resetSearchView();
|
|
|
|
this.loadGallery();
|
|
|
|
});
|
|
|
|
|
2023-04-26 09:23:28 -04:00
|
|
|
onChildEvent(this.listContainer, '.load-more button', 'click', async event => {
|
|
|
|
const wrapper = event.target.closest('.load-more');
|
|
|
|
showLoading(wrapper);
|
2023-04-19 10:20:04 -04:00
|
|
|
this.page += 1;
|
2020-07-24 19:20:58 -04:00
|
|
|
await this.loadGallery();
|
2023-04-26 09:23:28 -04:00
|
|
|
wrapper.remove();
|
2020-07-24 19:20:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.listContainer.addEventListener('event-emit-select-image', this.onImageSelectEvent.bind(this));
|
|
|
|
|
2022-01-10 13:12:11 -05:00
|
|
|
this.listContainer.addEventListener('error', event => {
|
2023-04-19 10:20:04 -04:00
|
|
|
event.target.src = window.baseUrl('loading_error.png');
|
2022-01-10 13:12:11 -05:00
|
|
|
}, true);
|
|
|
|
|
2020-07-24 19:20:58 -04:00
|
|
|
onSelect(this.selectButton, () => {
|
|
|
|
if (this.callback) {
|
|
|
|
this.callback(this.lastSelected);
|
|
|
|
}
|
|
|
|
this.hide();
|
|
|
|
});
|
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
onChildEvent(this.formContainer, '#image-manager-delete', 'click', () => {
|
2020-07-24 19:20:58 -04:00
|
|
|
if (this.lastSelected) {
|
|
|
|
this.loadImageEditForm(this.lastSelected.id, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-25 08:10:25 -04:00
|
|
|
this.formContainer.addEventListener('ajax-form-success', () => {
|
|
|
|
this.refreshGallery();
|
|
|
|
this.resetEditForm();
|
|
|
|
});
|
|
|
|
this.container.addEventListener('dropzone-upload-success', this.refreshGallery.bind(this));
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
show(callback, type = 'gallery') {
|
|
|
|
this.resetAll();
|
|
|
|
|
|
|
|
this.callback = callback;
|
|
|
|
this.type = type;
|
2022-11-16 10:46:41 -05:00
|
|
|
this.getPopup().show();
|
2023-04-26 09:23:28 -04:00
|
|
|
|
|
|
|
const hideUploads = type !== 'gallery';
|
|
|
|
this.dropzoneContainer.classList.toggle('hidden', hideUploads);
|
|
|
|
this.uploadButton.classList.toggle('hidden', hideUploads);
|
|
|
|
this.uploadHint.classList.toggle('hidden', hideUploads);
|
|
|
|
|
|
|
|
/** @var {Dropzone} * */
|
|
|
|
const dropzone = window.$components.firstOnElement(this.container, 'dropzone');
|
|
|
|
dropzone.toggleActive(!hideUploads);
|
2020-07-24 19:20:58 -04:00
|
|
|
|
|
|
|
if (!this.hasData) {
|
|
|
|
this.loadGallery();
|
|
|
|
this.hasData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
2022-11-16 10:46:41 -05:00
|
|
|
this.getPopup().hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Popup}
|
|
|
|
*/
|
|
|
|
getPopup() {
|
|
|
|
return window.$components.firstOnElement(this.popupEl, 'popup');
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadGallery() {
|
|
|
|
const params = {
|
|
|
|
page: this.page,
|
|
|
|
search: this.searchInput.value || null,
|
|
|
|
uploaded_to: this.uploadedTo,
|
|
|
|
filter_type: this.filter === 'all' ? null : this.filter,
|
|
|
|
};
|
|
|
|
|
|
|
|
const {data: html} = await window.$http.get(`images/${this.type}`, params);
|
2022-01-24 16:38:11 -05:00
|
|
|
if (params.page === 1) {
|
|
|
|
this.listContainer.innerHTML = '';
|
|
|
|
}
|
2020-07-24 19:20:58 -04:00
|
|
|
this.addReturnedHtmlElementsToList(html);
|
|
|
|
removeLoading(this.listContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
addReturnedHtmlElementsToList(html) {
|
|
|
|
const el = document.createElement('div');
|
|
|
|
el.innerHTML = html;
|
2022-11-14 18:19:02 -05:00
|
|
|
window.$components.init(el);
|
2020-07-24 19:20:58 -04:00
|
|
|
for (const child of [...el.children]) {
|
|
|
|
this.listContainer.appendChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setActiveFilterTab(filterName) {
|
2023-01-28 07:50:51 -05:00
|
|
|
for (const tab of this.filterTabs) {
|
|
|
|
const selected = tab.dataset.filter === filterName;
|
|
|
|
tab.setAttribute('aria-selected', selected ? 'true' : 'false');
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetAll() {
|
|
|
|
this.resetState();
|
|
|
|
this.resetListView();
|
|
|
|
this.resetSearchView();
|
2020-07-25 06:47:12 -04:00
|
|
|
this.resetEditForm();
|
2020-07-24 19:20:58 -04:00
|
|
|
this.setActiveFilterTab('all');
|
2020-07-25 06:47:12 -04:00
|
|
|
this.selectButton.classList.add('hidden');
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resetSearchView() {
|
|
|
|
this.searchInput.value = '';
|
|
|
|
}
|
|
|
|
|
2020-07-25 06:47:12 -04:00
|
|
|
resetEditForm() {
|
|
|
|
this.formContainer.innerHTML = '';
|
2023-04-25 08:10:25 -04:00
|
|
|
this.formContainerPlaceholder.removeAttribute('hidden');
|
2020-07-25 06:47:12 -04:00
|
|
|
}
|
|
|
|
|
2020-07-24 19:20:58 -04:00
|
|
|
resetListView() {
|
|
|
|
showLoading(this.listContainer);
|
|
|
|
this.page = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshGallery() {
|
|
|
|
this.resetListView();
|
|
|
|
this.loadGallery();
|
|
|
|
}
|
|
|
|
|
|
|
|
onImageSelectEvent(event) {
|
|
|
|
const image = JSON.parse(event.detail.data);
|
|
|
|
const isDblClick = ((image && image.id === this.lastSelected.id)
|
|
|
|
&& Date.now() - this.lastSelectedTime < 400);
|
|
|
|
const alreadySelected = event.target.classList.contains('selected');
|
|
|
|
[...this.listContainer.querySelectorAll('.selected')].forEach(el => {
|
|
|
|
el.classList.remove('selected');
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!alreadySelected) {
|
|
|
|
event.target.classList.add('selected');
|
|
|
|
this.loadImageEditForm(image.id);
|
2020-07-25 06:47:12 -04:00
|
|
|
} else {
|
|
|
|
this.resetEditForm();
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
this.selectButton.classList.toggle('hidden', alreadySelected);
|
|
|
|
|
|
|
|
if (isDblClick && this.callback) {
|
|
|
|
this.callback(image);
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastSelected = image;
|
|
|
|
this.lastSelectedTime = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadImageEditForm(imageId, requestDelete = false) {
|
|
|
|
if (!requestDelete) {
|
|
|
|
this.formContainer.innerHTML = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = requestDelete ? {delete: true} : {};
|
|
|
|
const {data: formHtml} = await window.$http.get(`/images/edit/${imageId}`, params);
|
|
|
|
this.formContainer.innerHTML = formHtml;
|
2023-04-25 08:10:25 -04:00
|
|
|
this.formContainerPlaceholder.setAttribute('hidden', '');
|
2022-11-14 18:19:02 -05:00
|
|
|
window.$components.init(this.formContainer);
|
2020-07-24 19:20:58 -04:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|