]> BookStack Code Mirror - bookstack/blob - resources/js/components/image-manager.js
Image manager: Redesigned header bar(s)
[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         this.loadMore = this.$refs.loadMore;
27
28         // Instance data
29         this.type = 'gallery';
30         this.lastSelected = {};
31         this.lastSelectedTime = 0;
32         this.callback = null;
33         this.resetState = () => {
34             this.hasData = false;
35             this.page = 1;
36             this.filter = 'all';
37         };
38         this.resetState();
39
40         this.setupListeners();
41     }
42
43     setupListeners() {
44         onSelect(this.filterTabs, e => {
45             this.resetAll();
46             this.filter = e.target.dataset.filter;
47             this.setActiveFilterTab(this.filter);
48             this.loadGallery();
49         });
50
51         this.searchForm.addEventListener('submit', event => {
52             this.resetListView();
53             this.loadGallery();
54             event.preventDefault();
55         });
56
57         onSelect(this.cancelSearch, () => {
58             this.resetListView();
59             this.resetSearchView();
60             this.loadGallery();
61         });
62
63         onChildEvent(this.container, '.load-more button', 'click', async event => {
64             const wrapper = event.target.closest('.load-more');
65             showLoading(wrapper);
66             this.page += 1;
67             await this.loadGallery();
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
149         const loadMore = el.querySelector('.load-more');
150         if (loadMore) {
151             loadMore.remove();
152             this.loadMore.innerHTML = loadMore.innerHTML;
153         }
154         this.loadMore.toggleAttribute('hidden', !loadMore);
155
156         window.$components.init(el);
157         for (const child of [...el.children]) {
158             this.listContainer.appendChild(child);
159         }
160     }
161
162     setActiveFilterTab(filterName) {
163         for (const tab of this.filterTabs) {
164             const selected = tab.dataset.filter === filterName;
165             tab.setAttribute('aria-selected', selected ? 'true' : 'false');
166         }
167     }
168
169     resetAll() {
170         this.resetState();
171         this.resetListView();
172         this.resetSearchView();
173         this.resetEditForm();
174         this.setActiveFilterTab('all');
175         this.selectButton.classList.add('hidden');
176     }
177
178     resetSearchView() {
179         this.searchInput.value = '';
180     }
181
182     resetEditForm() {
183         this.formContainer.innerHTML = '';
184         this.formContainerPlaceholder.removeAttribute('hidden');
185     }
186
187     resetListView() {
188         showLoading(this.listContainer);
189         this.page = 1;
190     }
191
192     refreshGallery() {
193         this.resetListView();
194         this.loadGallery();
195     }
196
197     onImageSelectEvent(event) {
198         const image = JSON.parse(event.detail.data);
199         const isDblClick = ((image && image.id === this.lastSelected.id)
200             && Date.now() - this.lastSelectedTime < 400);
201         const alreadySelected = event.target.classList.contains('selected');
202         [...this.listContainer.querySelectorAll('.selected')].forEach(el => {
203             el.classList.remove('selected');
204         });
205
206         if (!alreadySelected) {
207             event.target.classList.add('selected');
208             this.loadImageEditForm(image.id);
209         } else {
210             this.resetEditForm();
211         }
212         this.selectButton.classList.toggle('hidden', alreadySelected);
213
214         if (isDblClick && this.callback) {
215             this.callback(image);
216             this.hide();
217         }
218
219         this.lastSelected = image;
220         this.lastSelectedTime = Date.now();
221     }
222
223     async loadImageEditForm(imageId, requestDelete = false) {
224         if (!requestDelete) {
225             this.formContainer.innerHTML = '';
226         }
227
228         const params = requestDelete ? {delete: true} : {};
229         const {data: formHtml} = await window.$http.get(`/images/edit/${imageId}`, params);
230         this.formContainer.innerHTML = formHtml;
231         this.formContainerPlaceholder.setAttribute('hidden', '');
232         window.$components.init(this.formContainer);
233     }
234
235 }