]> BookStack Code Mirror - bookstack/blobdiff - resources/js/components/attachments.js
fix Sidebar scrolling at mid-range sceen
[bookstack] / resources / js / components / attachments.js
index 49ba8f38877609878cbf9991afdfdab3923c4b8e..f45b25e36d05f92d96d175e8b5207020bdcd4d14 100644 (file)
@@ -1,38 +1,69 @@
+import {showLoading} from '../services/dom';
+import {Component} from './component';
 
-/**
- * Attachments
- * @extends {Component}
- */
-class Attachments {
+export class Attachments extends Component {
 
     setup() {
         this.container = this.$el;
         this.pageId = this.$opts.pageId;
         this.editContainer = this.$refs.editContainer;
-        this.mainTabs = this.$refs.mainTabs;
-        this.list = this.$refs.list;
+        this.listContainer = this.$refs.listContainer;
+        this.linksContainer = this.$refs.linksContainer;
+        this.listPanel = this.$refs.listPanel;
+        this.attachLinkButton = this.$refs.attachLinkButton;
 
         this.setupListeners();
     }
 
     setupListeners() {
-        this.container.addEventListener('dropzone-success', event => {
-            this.mainTabs.components.tabs.show('items');
-            window.$http.get(`/attachments/get/page/${this.pageId}`).then(resp => {
-                this.list.innerHTML = resp.data;
-                window.components.init(this.list);
-            })
-        });
+        const reloadListBound = this.reloadList.bind(this);
+        this.container.addEventListener('dropzone-upload-success', reloadListBound);
+        this.container.addEventListener('ajax-form-success', reloadListBound);
 
         this.container.addEventListener('sortable-list-sort', event => {
             this.updateOrder(event.detail.ids);
         });
 
-        this.editContainer.addEventListener('keypress', event => {
-            if (event.key === 'Enter') {
-                // TODO - Update editing file
-            }
-        })
+        this.container.addEventListener('event-emit-select-edit', event => {
+            this.startEdit(event.detail.id);
+        });
+
+        this.container.addEventListener('event-emit-select-edit-back', () => {
+            this.stopEdit();
+        });
+
+        this.container.addEventListener('event-emit-select-insert', event => {
+            const insertContent = event.target.closest('[data-drag-content]').getAttribute('data-drag-content');
+            const contentTypes = JSON.parse(insertContent);
+            window.$events.emit('editor::insert', {
+                html: contentTypes['text/html'],
+                markdown: contentTypes['text/plain'],
+            });
+        });
+
+        this.attachLinkButton.addEventListener('click', () => {
+            this.showSection('links');
+        });
+    }
+
+    showSection(section) {
+        const sectionMap = {
+            links: this.linksContainer,
+            edit: this.editContainer,
+            list: this.listContainer,
+        };
+
+        for (const [name, elem] of Object.entries(sectionMap)) {
+            elem.toggleAttribute('hidden', name !== section);
+        }
+    }
+
+    reloadList() {
+        this.stopEdit();
+        window.$http.get(`/attachments/get/page/${this.pageId}`).then(resp => {
+            this.listPanel.innerHTML = resp.data;
+            window.$components.init(this.listPanel);
+        });
     }
 
     updateOrder(idOrder) {
@@ -41,6 +72,17 @@ class Attachments {
         });
     }
 
-}
+    async startEdit(id) {
+        this.showSection('edit');
 
-export default Attachments;
\ No newline at end of file
+        showLoading(this.editContainer);
+        const resp = await window.$http.get(`/attachments/edit/${id}`);
+        this.editContainer.innerHTML = resp.data;
+        window.$components.init(this.editContainer);
+    }
+
+    stopEdit() {
+        this.showSection('list');
+    }
+
+}