]> BookStack Code Mirror - bookstack/blob - resources/assets/js/vues/image-manager.js
Update Dutch password_hint translation to correspond with validation rule
[bookstack] / resources / assets / js / vues / image-manager.js
1 import * as Dates from "../services/dates";
2 import dropzone from "./components/dropzone";
3
4 let page = 0;
5 let previousClickTime = 0;
6 let previousClickImage = 0;
7 let dataLoaded = false;
8 let callback = false;
9 let baseUrl = '';
10
11 let preSearchImages = [];
12 let preSearchHasMore = false;
13
14 const data = {
15     images: [],
16
17     imageType: false,
18     uploadedTo: false,
19
20     selectedImage: false,
21     dependantPages: false,
22     showing: false,
23     view: 'all',
24     hasMore: false,
25     searching: false,
26     searchTerm: '',
27
28     imageUpdateSuccess: false,
29     imageDeleteSuccess: false,
30     deleteConfirm: false,
31 };
32
33 const methods = {
34
35     show(providedCallback, imageType = null) {
36         callback = providedCallback;
37         this.showing = true;
38         this.$el.children[0].components.overlay.show();
39
40         // Get initial images if they have not yet been loaded in.
41         if (dataLoaded && imageType === this.imageType) return;
42         if (imageType) {
43             this.imageType = imageType;
44             this.resetState();
45         }
46         this.fetchData();
47         dataLoaded = true;
48     },
49
50     hide() {
51         if (this.$refs.dropzone) {
52             this.$refs.dropzone.onClose();
53         }
54         this.showing = false;
55         this.selectedImage = false;
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         if (!this.searching) return;
106         this.searching = false;
107         this.searchTerm = '';
108         this.images = preSearchImages;
109         this.hasMore = preSearchHasMore;
110     },
111
112     imageSelect(image) {
113         let dblClickTime = 300;
114         let currentTime = Date.now();
115         let timeDiff = currentTime - previousClickTime;
116         let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
117
118         if (isDblClick) {
119             this.callbackAndHide(image);
120         } else {
121             this.selectedImage = image;
122             this.deleteConfirm = false;
123             this.dependantPages = false;
124         }
125
126         previousClickTime = currentTime;
127         previousClickImage = image.id;
128     },
129
130     callbackAndHide(imageResult) {
131         if (callback) callback(imageResult);
132         this.hide();
133     },
134
135     saveImageDetails() {
136         let url = window.baseUrl(`/images/update/${this.selectedImage.id}`);
137         this.$http.put(url, this.selectedImage).then(response => {
138             this.$events.emit('success', trans('components.image_update_success'));
139         }).catch(error => {
140             if (error.response.status === 422) {
141                 let errors = error.response.data;
142                 let message = '';
143                 Object.keys(errors).forEach((key) => {
144                     message += errors[key].join('\n');
145                 });
146                 this.$events.emit('error', message);
147             }
148         });
149     },
150
151     deleteImage() {
152
153         if (!this.deleteConfirm) {
154             let url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
155             this.$http.get(url).then(resp => {
156                 this.dependantPages = resp.data;
157             }).catch(console.error).then(() => {
158                 this.deleteConfirm = true;
159             });
160             return;
161         }
162         let url = window.baseUrl(`/images/${this.selectedImage.id}`);
163         this.$http.delete(url).then(resp => {
164             this.images.splice(this.images.indexOf(this.selectedImage), 1);
165             this.selectedImage = false;
166             this.$events.emit('success', trans('components.image_delete_success'));
167             this.deleteConfirm = false;
168         });
169     },
170
171     getDate(stringDate) {
172         return Dates.formatDateTime(new Date(stringDate));
173     },
174
175     uploadSuccess(event) {
176         this.images.unshift(event.data);
177         this.$events.emit('success', trans('components.image_upload_success'));
178     },
179 };
180
181 const computed = {
182     uploadUrl() {
183         return window.baseUrl(`/images/${this.imageType}/upload`);
184     }
185 };
186
187 function mounted() {
188     window.ImageManager = this;
189     this.imageType = this.$el.getAttribute('image-type');
190     this.uploadedTo = this.$el.getAttribute('uploaded-to');
191     baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
192 }
193
194 export default {
195     mounted,
196     methods,
197     data,
198     computed,
199     components: {dropzone},
200 };