X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/02dc3154e34775f44b4f38b525224a2354aa698c..refs/pull/3918/head:/resources/js/components/image-manager.js diff --git a/resources/js/components/image-manager.js b/resources/js/components/image-manager.js index 71bc55f2e..a44fffc1b 100644 --- a/resources/js/components/image-manager.js +++ b/resources/js/components/image-manager.js @@ -1,13 +1,9 @@ import {onChildEvent, onSelect, removeLoading, showLoading} from "../services/dom"; +import {Component} from "./component"; -/** - * ImageManager - * @extends {Component} - */ -class ImageManager { +export class ImageManager extends Component { setup() { - // Options this.uploadedTo = this.$opts.uploadedTo; @@ -27,8 +23,8 @@ class ImageManager { this.type = 'gallery'; this.lastSelected = {}; this.lastSelectedTime = 0; + this.callback = null; this.resetState = () => { - this.callback = null; this.hasData = false; this.page = 1; this.filter = 'all'; @@ -36,8 +32,6 @@ class ImageManager { this.resetState(); this.setupListeners(); - - window.ImageManager = this; } setupListeners() { @@ -74,6 +68,10 @@ class ImageManager { this.listContainer.addEventListener('event-emit-select-image', this.onImageSelectEvent.bind(this)); + this.listContainer.addEventListener('error', event => { + event.target.src = baseUrl('loading_error.png'); + }, true); + onSelect(this.selectButton, () => { if (this.callback) { this.callback(this.lastSelected); @@ -96,7 +94,7 @@ class ImageManager { this.callback = callback; this.type = type; - this.popupEl.components.popup.show(); + this.getPopup().show(); this.dropzoneContainer.classList.toggle('hidden', type !== 'gallery'); if (!this.hasData) { @@ -106,7 +104,14 @@ class ImageManager { } hide() { - this.popupEl.components.popup.hide(); + this.getPopup().hide(); + } + + /** + * @returns {Popup} + */ + getPopup() { + return window.$components.firstOnElement(this.popupEl, 'popup'); } async loadGallery() { @@ -118,6 +123,9 @@ class ImageManager { }; const {data: html} = await window.$http.get(`images/${this.type}`, params); + if (params.page === 1) { + this.listContainer.innerHTML = ''; + } this.addReturnedHtmlElementsToList(html); removeLoading(this.listContainer); } @@ -125,7 +133,7 @@ class ImageManager { addReturnedHtmlElementsToList(html) { const el = document.createElement('div'); el.innerHTML = html; - window.components.init(el); + window.$components.init(el); for (const child of [...el.children]) { this.listContainer.appendChild(child); } @@ -143,14 +151,19 @@ class ImageManager { this.resetState(); this.resetListView(); this.resetSearchView(); - this.formContainer.innerHTML = ''; + this.resetEditForm(); this.setActiveFilterTab('all'); + this.selectButton.classList.add('hidden'); } resetSearchView() { this.searchInput.value = ''; } + resetEditForm() { + this.formContainer.innerHTML = ''; + } + resetListView() { showLoading(this.listContainer); this.page = 1; @@ -173,6 +186,8 @@ class ImageManager { if (!alreadySelected) { event.target.classList.add('selected'); this.loadImageEditForm(image.id); + } else { + this.resetEditForm(); } this.selectButton.classList.toggle('hidden', alreadySelected); @@ -193,9 +208,7 @@ class ImageManager { const params = requestDelete ? {delete: true} : {}; const {data: formHtml} = await window.$http.get(`/images/edit/${imageId}`, params); this.formContainer.innerHTML = formHtml; - window.components.init(this.formContainer); + window.$components.init(this.formContainer); } -} - -export default ImageManager; \ No newline at end of file +} \ No newline at end of file