1 import * as Dates from "../services/dates";
2 import dropzone from "./components/dropzone";
5 let previousClickTime = 0;
6 let previousClickImage = 0;
7 let dataLoaded = false;
11 let preSearchImages = [];
12 let preSearchHasMore = false;
21 dependantPages: false,
28 imageUpdateSuccess: false,
29 imageDeleteSuccess: false,
35 show(providedCallback, imageType = null) {
36 callback = providedCallback;
38 this.$el.children[0].components.overlay.show();
40 // Get initial images if they have not yet been loaded in.
41 if (dataLoaded && imageType === this.imageType) return;
43 this.imageType = imageType;
51 if (this.$refs.dropzone) {
52 this.$refs.dropzone.onClose();
55 this.selectedImage = false;
56 this.$el.children[0].components.overlay.hide();
62 search: this.searching ? this.searchTerm : null,
63 uploaded_to: this.uploadedTo || null,
64 filter_type: this.filter,
67 const {data} = await this.$http.get(baseUrl, params);
68 this.images = this.images.concat(data.images);
69 this.hasMore = data.has_more;
73 setFilterType(filterType) {
74 this.filter = filterType;
82 this.deleteConfirm = false;
83 baseUrl = window.baseUrl(`/images/${this.imageType}`);
93 if (this.searchTerm === '') return this.cancelSearch();
95 // Cache current settings for later
96 if (!this.searching) {
97 preSearchImages = this.images;
98 preSearchHasMore = this.hasMore;
101 this.searching = true;
102 this.resetListView();
107 if (!this.searching) return;
108 this.searching = false;
109 this.searchTerm = '';
110 this.images = preSearchImages;
111 this.hasMore = preSearchHasMore;
115 const dblClickTime = 300;
116 const currentTime = Date.now();
117 const timeDiff = currentTime - previousClickTime;
118 const isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
121 this.callbackAndHide(image);
123 this.selectedImage = image;
124 this.deleteConfirm = false;
125 this.dependantPages = false;
128 previousClickTime = currentTime;
129 previousClickImage = image.id;
132 callbackAndHide(imageResult) {
133 if (callback) callback(imageResult);
137 async saveImageDetails() {
138 let url = window.baseUrl(`/images/${this.selectedImage.id}`);
140 await this.$http.put(url, this.selectedImage)
142 if (error.response.status === 422) {
143 let errors = error.response.data;
145 Object.keys(errors).forEach((key) => {
146 message += errors[key].join('\n');
148 this.$events.emit('error', message);
153 async deleteImage() {
155 if (!this.deleteConfirm) {
156 const url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
158 const {data} = await this.$http.get(url);
159 this.dependantPages = data;
161 console.error(error);
163 this.deleteConfirm = true;
167 const url = window.baseUrl(`/images/${this.selectedImage.id}`);
168 await this.$http.delete(url);
169 this.images.splice(this.images.indexOf(this.selectedImage), 1);
170 this.selectedImage = false;
171 this.$events.emit('success', trans('components.image_delete_success'));
172 this.deleteConfirm = false;
175 getDate(stringDate) {
176 return Dates.formatDateTime(new Date(stringDate));
179 uploadSuccess(event) {
180 this.images.unshift(event.data);
181 this.$events.emit('success', trans('components.image_upload_success'));
187 return window.baseUrl(`/images/${this.imageType}`);
192 window.ImageManager = this;
193 this.imageType = this.$el.getAttribute('image-type');
194 this.uploadedTo = this.$el.getAttribute('uploaded-to');
195 baseUrl = window.baseUrl('/images/' + this.imageType)
203 components: {dropzone},