2 onChildEvent, onSelect, removeLoading, showLoading,
3 } from '../services/dom';
4 import {Component} from './component';
6 export class ImageManager extends Component {
10 this.uploadedTo = this.$opts.uploadedTo;
13 this.container = this.$el;
14 this.popupEl = this.$refs.popup;
15 this.searchForm = this.$refs.searchForm;
16 this.searchInput = this.$refs.searchInput;
17 this.cancelSearch = this.$refs.cancelSearch;
18 this.listContainer = this.$refs.listContainer;
19 this.filterTabs = this.$manyRefs.filterTabs;
20 this.selectButton = this.$refs.selectButton;
21 this.uploadButton = this.$refs.uploadButton;
22 this.uploadHint = this.$refs.uploadHint;
23 this.formContainer = this.$refs.formContainer;
24 this.formContainerPlaceholder = this.$refs.formContainerPlaceholder;
25 this.dropzoneContainer = this.$refs.dropzoneContainer;
28 this.type = 'gallery';
29 this.lastSelected = {};
30 this.lastSelectedTime = 0;
32 this.resetState = () => {
39 this.setupListeners();
43 onSelect(this.filterTabs, e => {
45 this.filter = e.target.dataset.filter;
46 this.setActiveFilterTab(this.filter);
50 this.searchForm.addEventListener('submit', event => {
53 event.preventDefault();
56 onSelect(this.cancelSearch, () => {
58 this.resetSearchView();
62 onChildEvent(this.listContainer, '.load-more button', 'click', async event => {
63 const wrapper = event.target.closest('.load-more');
66 await this.loadGallery();
70 this.listContainer.addEventListener('event-emit-select-image', this.onImageSelectEvent.bind(this));
72 this.listContainer.addEventListener('error', event => {
73 event.target.src = window.baseUrl('loading_error.png');
76 onSelect(this.selectButton, () => {
78 this.callback(this.lastSelected);
83 onChildEvent(this.formContainer, '#image-manager-delete', 'click', () => {
84 if (this.lastSelected) {
85 this.loadImageEditForm(this.lastSelected.id, true);
89 this.formContainer.addEventListener('ajax-form-success', () => {
90 this.refreshGallery();
93 this.container.addEventListener('dropzone-upload-success', this.refreshGallery.bind(this));
96 show(callback, type = 'gallery') {
99 this.callback = callback;
101 this.getPopup().show();
103 const hideUploads = type !== 'gallery';
104 this.dropzoneContainer.classList.toggle('hidden', hideUploads);
105 this.uploadButton.classList.toggle('hidden', hideUploads);
106 this.uploadHint.classList.toggle('hidden', hideUploads);
108 /** @var {Dropzone} * */
109 const dropzone = window.$components.firstOnElement(this.container, 'dropzone');
110 dropzone.toggleActive(!hideUploads);
119 this.getPopup().hide();
126 return window.$components.firstOnElement(this.popupEl, 'popup');
129 async loadGallery() {
132 search: this.searchInput.value || null,
133 uploaded_to: this.uploadedTo,
134 filter_type: this.filter === 'all' ? null : this.filter,
137 const {data: html} = await window.$http.get(`images/${this.type}`, params);
138 if (params.page === 1) {
139 this.listContainer.innerHTML = '';
141 this.addReturnedHtmlElementsToList(html);
142 removeLoading(this.listContainer);
145 addReturnedHtmlElementsToList(html) {
146 const el = document.createElement('div');
148 window.$components.init(el);
149 for (const child of [...el.children]) {
150 this.listContainer.appendChild(child);
154 setActiveFilterTab(filterName) {
155 for (const tab of this.filterTabs) {
156 const selected = tab.dataset.filter === filterName;
157 tab.setAttribute('aria-selected', selected ? 'true' : 'false');
163 this.resetListView();
164 this.resetSearchView();
165 this.resetEditForm();
166 this.setActiveFilterTab('all');
167 this.selectButton.classList.add('hidden');
171 this.searchInput.value = '';
175 this.formContainer.innerHTML = '';
176 this.formContainerPlaceholder.removeAttribute('hidden');
180 showLoading(this.listContainer);
185 this.resetListView();
189 onImageSelectEvent(event) {
190 const image = JSON.parse(event.detail.data);
191 const isDblClick = ((image && image.id === this.lastSelected.id)
192 && Date.now() - this.lastSelectedTime < 400);
193 const alreadySelected = event.target.classList.contains('selected');
194 [...this.listContainer.querySelectorAll('.selected')].forEach(el => {
195 el.classList.remove('selected');
198 if (!alreadySelected) {
199 event.target.classList.add('selected');
200 this.loadImageEditForm(image.id);
202 this.resetEditForm();
204 this.selectButton.classList.toggle('hidden', alreadySelected);
206 if (isDblClick && this.callback) {
207 this.callback(image);
211 this.lastSelected = image;
212 this.lastSelectedTime = Date.now();
215 async loadImageEditForm(imageId, requestDelete = false) {
216 if (!requestDelete) {
217 this.formContainer.innerHTML = '';
220 const params = requestDelete ? {delete: true} : {};
221 const {data: formHtml} = await window.$http.get(`/images/edit/${imageId}`, params);
222 this.formContainer.innerHTML = formHtml;
223 this.formContainerPlaceholder.setAttribute('hidden', '');
224 window.$components.init(this.formContainer);