]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/image-manager.js
Started work on revisions in image manager
[bookstack] / resources / assets / js / vues / image-manager.js
1 const dropzone = require('./components/dropzone');
2
3 let page = 0;
4 let previousClickTime = 0;
5 let previousClickImage = 0;
6 let dataLoaded = false;
7 let callback = false;
8 let baseUrl = '';
9
10 let preSearchImages = [];
11 let preSearchHasMore = false;
12
13 const data = {
14     images: [],
15
16     imageType: false,
17     uploadedTo: false,
18
19     selectedImage: false,
20     dependantPages: false,
21     showing: false,
22     view: 'all',
23     hasMore: false,
24     searching: false,
25     searchTerm: '',
26
27     revisions: [],
28     selectedRevision: null,
29
30     imageUpdateSuccess: false,
31     imageDeleteSuccess: false,
32     deleteConfirm: false,
33 };
34
35 const methods = {
36
37     show(providedCallback, imageType = null) {
38         callback = providedCallback;
39         this.showing = true;
40         this.$el.children[0].components.overlay.show();
41
42         // Get initial images if they have not yet been loaded in.
43         if (dataLoaded && imageType === this.imageType) return;
44         if (imageType) {
45             this.imageType = imageType;
46             this.resetState();
47         }
48         this.fetchData();
49         dataLoaded = true;
50     },
51
52     hide() {
53         this.showing = false;
54         this.selectedImage = false;
55         this.$refs.dropzone.onClose();
56         this.$el.children[0].components.overlay.hide();
57     },
58
59     fetchData() {
60         let url = baseUrl + page;
61         let query = {};
62         if (this.uploadedTo !== false) query.page_id = this.uploadedTo;
63         if (this.searching) query.term = this.searchTerm;
64
65         this.$http.get(url, {params: query}).then(response => {
66             this.images = this.images.concat(response.data.images);
67             this.hasMore = response.data.hasMore;
68             page++;
69         });
70     },
71
72     setView(viewName) {
73         this.view = viewName;
74         this.resetState();
75         this.fetchData();
76     },
77
78     resetState() {
79         this.cancelSearch();
80         this.images = [];
81         this.hasMore = false;
82         this.deleteConfirm = false;
83         page = 0;
84         baseUrl = window.baseUrl(`/images/${this.imageType}/${this.view}/`);
85     },
86
87     searchImages() {
88         if (this.searchTerm === '') return this.cancelSearch();
89
90         // Cache current settings for later
91         if (!this.searching) {
92             preSearchImages = this.images;
93             preSearchHasMore = this.hasMore;
94         }
95
96         this.searching = true;
97         this.images = [];
98         this.hasMore = false;
99         page = 0;
100         baseUrl = window.baseUrl(`/images/${this.imageType}/search/`);
101         this.fetchData();
102     },
103
104     cancelSearch() {
105         this.searching = false;
106         this.searchTerm = '';
107         this.images = preSearchImages;
108         this.hasMore = preSearchHasMore;
109     },
110
111     imageSelect(image) {
112         let dblClickTime = 300;
113         let currentTime = Date.now();
114         let timeDiff = currentTime - previousClickTime;
115         let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
116         this.revisions = [];
117         this.selectedRevision = null
118
119         if (isDblClick) {
120             this.callbackAndHide(image);
121         } else {
122             this.selectedImage = image;
123             this.deleteConfirm = false;
124             this.dependantPages = false;
125             if (this.imageType === 'drawio') {
126                 this.$http.get(window.baseUrl(`/images/revisions/${image.id}`)).then(resp => {
127                     this.revisions = resp.data;
128                 })
129             }
130         }
131
132         previousClickTime = currentTime;
133         previousClickImage = image.id;
134     },
135
136     callbackAndHide(imageResult) {
137         if (callback) callback(imageResult);
138         this.hide();
139     },
140
141     saveImageDetails() {
142         let url = window.baseUrl(`/images/update/${this.selectedImage.id}`);
143         this.$http.put(url, this.selectedImage).then(response => {
144             this.$events.emit('success', trans('components.image_update_success'));
145         }).catch(error => {
146             if (error.response.status === 422) {
147                 let errors = error.response.data;
148                 let message = '';
149                 Object.keys(errors).forEach((key) => {
150                     message += errors[key].join('\n');
151                 });
152                 this.$events.emit('error', message);
153             }
154         });
155     },
156
157     deleteImage() {
158
159         if (!this.deleteConfirm) {
160             let url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
161             this.$http.get(url).then(resp => {
162                 this.dependantPages = resp.data;
163             }).catch(console.error).then(() => {
164                 this.deleteConfirm = true;
165             });
166             return;
167         }
168
169         this.$http.delete(`/images/${this.selectedImage.id}`).then(resp => {
170             this.images.splice(this.images.indexOf(this.selectedImage), 1);
171             this.selectedImage = false;
172             this.$events.emit('success', trans('components.image_delete_success'));
173             this.deleteConfirm = false;
174         });
175     },
176
177     getDate(stringDate) {
178         return new Date(stringDate);
179     },
180
181     uploadSuccess(event) {
182         this.images.unshift(event.data);
183         this.$events.emit('success', trans('components.image_upload_success'));
184     },
185
186     selectRevision(revision) {
187         let rev = (this.selectedRevision === revision) ? null : revision;
188         this.selectedRevision = rev;
189     }
190 };
191
192 const computed = {
193     uploadUrl() {
194         return window.baseUrl(`/images/${this.imageType}/upload`);
195     }
196 };
197
198 function mounted() {
199     window.ImageManager = this;
200     this.imageType = this.$el.getAttribute('image-type');
201     this.uploadedTo = this.$el.getAttribute('uploaded-to');
202     baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
203 }
204
205 module.exports = {
206     mounted,
207     methods,
208     data,
209     computed,
210     components: {dropzone},
211 };