]> BookStack Code Mirror - bookstack/blob - resources/js/components/image-manager.js
Fixed linting and failing test issues from dropzone work
[bookstack] / resources / js / components / image-manager.js
1 import {
2     onChildEvent, onSelect, removeLoading, showLoading,
3 } from '../services/dom';
4 import {Component} from './component';
5
6 export class ImageManager extends Component {
7
8     setup() {
9         // Options
10         this.uploadedTo = this.$opts.uploadedTo;
11
12         // Element References
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;
26
27         // Instance data
28         this.type = 'gallery';
29         this.lastSelected = {};
30         this.lastSelectedTime = 0;
31         this.callback = null;
32         this.resetState = () => {
33             this.hasData = false;
34             this.page = 1;
35             this.filter = 'all';
36         };
37         this.resetState();
38
39         this.setupListeners();
40     }
41
42     setupListeners() {
43         onSelect(this.filterTabs, e => {
44             this.resetAll();
45             this.filter = e.target.dataset.filter;
46             this.setActiveFilterTab(this.filter);
47             this.loadGallery();
48         });
49
50         this.searchForm.addEventListener('submit', event => {
51             this.resetListView();
52             this.loadGallery();
53             event.preventDefault();
54         });
55
56         onSelect(this.cancelSearch, () => {
57             this.resetListView();
58             this.resetSearchView();
59             this.loadGallery();
60         });
61
62         onChildEvent(this.listContainer, '.load-more button', 'click', async event => {
63             const wrapper = event.target.closest('.load-more');
64             showLoading(wrapper);
65             this.page += 1;
66             await this.loadGallery();
67             wrapper.remove();
68         });
69
70         this.listContainer.addEventListener('event-emit-select-image', this.onImageSelectEvent.bind(this));
71
72         this.listContainer.addEventListener('error', event => {
73             event.target.src = window.baseUrl('loading_error.png');
74         }, true);
75
76         onSelect(this.selectButton, () => {
77             if (this.callback) {
78                 this.callback(this.lastSelected);
79             }
80             this.hide();
81         });
82
83         onChildEvent(this.formContainer, '#image-manager-delete', 'click', () => {
84             if (this.lastSelected) {
85                 this.loadImageEditForm(this.lastSelected.id, true);
86             }
87         });
88
89         this.formContainer.addEventListener('ajax-form-success', () => {
90             this.refreshGallery();
91             this.resetEditForm();
92         });
93         this.container.addEventListener('dropzone-upload-success', this.refreshGallery.bind(this));
94     }
95
96     show(callback, type = 'gallery') {
97         this.resetAll();
98
99         this.callback = callback;
100         this.type = type;
101         this.getPopup().show();
102
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);
107
108         /** @var {Dropzone} * */
109         const dropzone = window.$components.firstOnElement(this.container, 'dropzone');
110         dropzone.toggleActive(!hideUploads);
111
112         if (!this.hasData) {
113             this.loadGallery();
114             this.hasData = true;
115         }
116     }
117
118     hide() {
119         this.getPopup().hide();
120     }
121
122     /**
123      * @returns {Popup}
124      */
125     getPopup() {
126         return window.$components.firstOnElement(this.popupEl, 'popup');
127     }
128
129     async loadGallery() {
130         const params = {
131             page: this.page,
132             search: this.searchInput.value || null,
133             uploaded_to: this.uploadedTo,
134             filter_type: this.filter === 'all' ? null : this.filter,
135         };
136
137         const {data: html} = await window.$http.get(`images/${this.type}`, params);
138         if (params.page === 1) {
139             this.listContainer.innerHTML = '';
140         }
141         this.addReturnedHtmlElementsToList(html);
142         removeLoading(this.listContainer);
143     }
144
145     addReturnedHtmlElementsToList(html) {
146         const el = document.createElement('div');
147         el.innerHTML = html;
148         window.$components.init(el);
149         for (const child of [...el.children]) {
150             this.listContainer.appendChild(child);
151         }
152     }
153
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');
158         }
159     }
160
161     resetAll() {
162         this.resetState();
163         this.resetListView();
164         this.resetSearchView();
165         this.resetEditForm();
166         this.setActiveFilterTab('all');
167         this.selectButton.classList.add('hidden');
168     }
169
170     resetSearchView() {
171         this.searchInput.value = '';
172     }
173
174     resetEditForm() {
175         this.formContainer.innerHTML = '';
176         this.formContainerPlaceholder.removeAttribute('hidden');
177     }
178
179     resetListView() {
180         showLoading(this.listContainer);
181         this.page = 1;
182     }
183
184     refreshGallery() {
185         this.resetListView();
186         this.loadGallery();
187     }
188
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');
196         });
197
198         if (!alreadySelected) {
199             event.target.classList.add('selected');
200             this.loadImageEditForm(image.id);
201         } else {
202             this.resetEditForm();
203         }
204         this.selectButton.classList.toggle('hidden', alreadySelected);
205
206         if (isDblClick && this.callback) {
207             this.callback(image);
208             this.hide();
209         }
210
211         this.lastSelected = image;
212         this.lastSelectedTime = Date.now();
213     }
214
215     async loadImageEditForm(imageId, requestDelete = false) {
216         if (!requestDelete) {
217             this.formContainer.innerHTML = '';
218         }
219
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);
225     }
226
227 }