1 const dropzone = require('./components/dropzone');
4 let previousClickTime = 0;
5 let previousClickImage = 0;
6 let dataLoaded = false;
10 let preSearchImages = [];
11 let preSearchHasMore = false;
20 dependantPages: false,
27 imageUpdateSuccess: false,
28 imageDeleteSuccess: false,
33 show(providedCallback) {
34 callback = providedCallback;
36 this.$el.children[0].components.overlay.show();
38 // Get initial images if they have not yet been loaded in.
39 if (dataLoaded) return;
46 this.selectedImage = false;
47 this.$refs.dropzone.onClose();
48 this.$el.children[0].components.overlay.hide();
52 let url = baseUrl + page;
54 if (this.uploadedTo !== false) query.page_id = this.uploadedTo;
55 if (this.searching) query.term = this.searchTerm;
57 this.$http.get(url, {params: query}).then(response => {
58 this.images = this.images.concat(response.data.images);
59 this.hasMore = response.data.hasMore;
70 baseUrl = window.baseUrl(`/images/${this.imageType}/${viewName}/`);
75 if (this.searchTerm === '') return this.cancelSearch();
77 // Cache current settings for later
78 if (!this.searching) {
79 preSearchImages = this.images;
80 preSearchHasMore = this.hasMore;
83 this.searching = true;
87 baseUrl = window.baseUrl(`/images/${this.imageType}/search/`);
92 this.searching = false;
94 this.images = preSearchImages;
95 this.hasMore = preSearchHasMore;
99 let dblClickTime = 300;
100 let currentTime = Date.now();
101 let timeDiff = currentTime - previousClickTime;
102 let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
105 this.callbackAndHide(image);
107 this.selectedImage = image;
108 this.dependantPages = false;
111 previousClickTime = currentTime;
112 previousClickImage = image.id;
115 callbackAndHide(imageResult) {
116 if (callback) callback(imageResult);
121 let url = window.baseUrl(`/images/update/${this.selectedImage.id}`);
122 this.$http.put(url, this.selectedImage).then(response => {
123 this.$events.emit('success', trans('components.image_update_success'));
125 if (error.response.status === 422) {
126 let errors = error.response.data;
128 Object.keys(errors).forEach((key) => {
129 message += errors[key].join('\n');
131 this.$events.emit('error', message);
137 let force = this.dependantPages !== false;
138 let url = window.baseUrl('/images/' + this.selectedImage.id);
139 if (force) url += '?force=true';
140 this.$http.delete(url).then(response => {
141 this.images.splice(this.images.indexOf(this.selectedImage), 1);
142 this.selectedImage = false;
143 this.$events.emit('success', trans('components.image_delete_success'));
145 if (error.response.status === 400) {
146 this.dependantPages = error.response.data;
151 getDate(stringDate) {
152 return new Date(stringDate);
155 uploadSuccess(event) {
156 this.images.unshift(event.data);
157 this.$events.emit('success', trans('components.image_upload_success'));
163 return window.baseUrl(`/images/${this.imageType}/upload`);
168 window.ImageManager = this;
169 this.imageType = this.$el.getAttribute('image-type');
170 this.uploadedTo = this.$el.getAttribute('uploaded-to');
171 baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
179 components: {dropzone},