]> BookStack Code Mirror - bookstack/blobdiff - resources/js/components/image-manager.js
Updated code view block line highlighting to only show on focus
[bookstack] / resources / js / components / image-manager.js
index c974ab1b0abd818d7be81c81d08ae56cf134cd6c..b817823643506b4c34a8a253ee6ffae9ec9e0749 100644 (file)
@@ -1,13 +1,11 @@
-import {onChildEvent, onSelect, removeLoading, showLoading} from "../services/dom";
+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;
 
@@ -20,7 +18,10 @@ class ImageManager {
         this.listContainer = this.$refs.listContainer;
         this.filterTabs = this.$manyRefs.filterTabs;
         this.selectButton = this.$refs.selectButton;
+        this.uploadButton = this.$refs.uploadButton;
+        this.uploadHint = this.$refs.uploadHint;
         this.formContainer = this.$refs.formContainer;
+        this.formContainerPlaceholder = this.$refs.formContainerPlaceholder;
         this.dropzoneContainer = this.$refs.dropzoneContainer;
 
         // Instance data
@@ -36,8 +37,6 @@ class ImageManager {
         this.resetState();
 
         this.setupListeners();
-
-        window.ImageManager = this;
     }
 
     setupListeners() {
@@ -54,26 +53,26 @@ class ImageManager {
             event.preventDefault();
         });
 
-        onSelect(this.cancelSearch, event => {
+        onSelect(this.cancelSearch, () => {
             this.resetListView();
             this.resetSearchView();
             this.loadGallery();
-            this.cancelSearch.classList.remove('active');
-        });
-
-        this.searchInput.addEventListener('input', event => {
-            this.cancelSearch.classList.toggle('active', this.searchInput.value.trim());
         });
 
-        onChildEvent(this.listContainer, '.load-more', 'click', async event => {
-            showLoading(event.target);
-            this.page++;
+        onChildEvent(this.listContainer, '.load-more button', 'click', async event => {
+            const wrapper = event.target.closest('.load-more');
+            showLoading(wrapper);
+            this.page += 1;
             await this.loadGallery();
-            event.target.remove();
+            wrapper.remove();
         });
 
         this.listContainer.addEventListener('event-emit-select-image', this.onImageSelectEvent.bind(this));
 
+        this.listContainer.addEventListener('error', event => {
+            event.target.src = window.baseUrl('loading_error.png');
+        }, true);
+
         onSelect(this.selectButton, () => {
             if (this.callback) {
                 this.callback(this.lastSelected);
@@ -81,14 +80,17 @@ class ImageManager {
             this.hide();
         });
 
-        onChildEvent(this.formContainer, '#image-manager-delete', 'click', event => {
+        onChildEvent(this.formContainer, '#image-manager-delete', 'click', () => {
             if (this.lastSelected) {
                 this.loadImageEditForm(this.lastSelected.id, true);
             }
         });
 
-        this.formContainer.addEventListener('ajax-form-success', this.refreshGallery.bind(this));
-        this.container.addEventListener('dropzone-success', this.refreshGallery.bind(this));
+        this.formContainer.addEventListener('ajax-form-success', () => {
+            this.refreshGallery();
+            this.resetEditForm();
+        });
+        this.container.addEventListener('dropzone-upload-success', this.refreshGallery.bind(this));
     }
 
     show(callback, type = 'gallery') {
@@ -96,8 +98,16 @@ class ImageManager {
 
         this.callback = callback;
         this.type = type;
-        this.popupEl.components.popup.show();
-        this.dropzoneContainer.classList.toggle('hidden', type !== 'gallery');
+        this.getPopup().show();
+
+        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);
 
         if (!this.hasData) {
             this.loadGallery();
@@ -106,7 +116,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 +135,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,17 +145,16 @@ 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);
         }
     }
 
     setActiveFilterTab(filterName) {
-        this.filterTabs.forEach(t => t.classList.remove('selected'));
-        const activeTab = this.filterTabs.find(t => t.dataset.filter === filterName);
-        if (activeTab) {
-            activeTab.classList.add('selected');
+        for (const tab of this.filterTabs) {
+            const selected = tab.dataset.filter === filterName;
+            tab.setAttribute('aria-selected', selected ? 'true' : 'false');
         }
     }
 
@@ -154,6 +173,7 @@ class ImageManager {
 
     resetEditForm() {
         this.formContainer.innerHTML = '';
+        this.formContainerPlaceholder.removeAttribute('hidden');
     }
 
     resetListView() {
@@ -200,9 +220,8 @@ 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);
+        this.formContainerPlaceholder.setAttribute('hidden', '');
+        window.$components.init(this.formContainer);
     }
 
 }
-
-export default ImageManager;
\ No newline at end of file