+import {showLoading} from '../services/dom.ts';
+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) {
});
}
-}
+ 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');
+ }
+
+}