2018-06-30 06:58:11 +02:00
|
|
|
import * as Dates from "../services/dates";
|
2018-11-09 22:17:35 +01:00
|
|
|
import dropzone from "./components/dropzone";
|
2017-08-09 22:33:00 +02:00
|
|
|
|
2019-04-27 15:18:00 +02:00
|
|
|
let page = 1;
|
2017-08-09 22:33:00 +02:00
|
|
|
let previousClickTime = 0;
|
|
|
|
let previousClickImage = 0;
|
|
|
|
let dataLoaded = false;
|
|
|
|
let callback = false;
|
|
|
|
let baseUrl = '';
|
|
|
|
|
|
|
|
let preSearchImages = [];
|
|
|
|
let preSearchHasMore = false;
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
images: [],
|
|
|
|
|
|
|
|
imageType: false,
|
|
|
|
uploadedTo: false,
|
|
|
|
|
|
|
|
selectedImage: false,
|
|
|
|
dependantPages: false,
|
|
|
|
showing: false,
|
2019-04-27 15:18:00 +02:00
|
|
|
filter: null,
|
2017-08-09 22:33:00 +02:00
|
|
|
hasMore: false,
|
|
|
|
searching: false,
|
|
|
|
searchTerm: '',
|
|
|
|
|
|
|
|
imageUpdateSuccess: false,
|
|
|
|
imageDeleteSuccess: false,
|
2018-05-13 13:07:38 +02:00
|
|
|
deleteConfirm: false,
|
2017-08-09 22:33:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const methods = {
|
|
|
|
|
2018-05-13 13:07:38 +02:00
|
|
|
show(providedCallback, imageType = null) {
|
2017-08-09 22:33:00 +02:00
|
|
|
callback = providedCallback;
|
|
|
|
this.showing = true;
|
|
|
|
this.$el.children[0].components.overlay.show();
|
|
|
|
|
|
|
|
// Get initial images if they have not yet been loaded in.
|
2018-05-13 13:07:38 +02:00
|
|
|
if (dataLoaded && imageType === this.imageType) return;
|
|
|
|
if (imageType) {
|
|
|
|
this.imageType = imageType;
|
|
|
|
this.resetState();
|
|
|
|
}
|
2017-08-09 22:33:00 +02:00
|
|
|
this.fetchData();
|
|
|
|
dataLoaded = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
hide() {
|
2018-05-20 17:40:30 +02:00
|
|
|
if (this.$refs.dropzone) {
|
|
|
|
this.$refs.dropzone.onClose();
|
|
|
|
}
|
2017-08-09 22:33:00 +02:00
|
|
|
this.showing = false;
|
2018-03-18 19:08:37 +01:00
|
|
|
this.selectedImage = false;
|
2017-08-09 22:33:00 +02:00
|
|
|
this.$el.children[0].components.overlay.hide();
|
|
|
|
},
|
|
|
|
|
2019-04-27 15:18:00 +02:00
|
|
|
async fetchData() {
|
2019-06-08 01:02:51 +02:00
|
|
|
const params = {
|
2019-04-27 15:18:00 +02:00
|
|
|
page,
|
|
|
|
search: this.searching ? this.searchTerm : null,
|
|
|
|
uploaded_to: this.uploadedTo || null,
|
|
|
|
filter_type: this.filter,
|
|
|
|
};
|
|
|
|
|
2019-06-08 01:02:51 +02:00
|
|
|
const {data} = await this.$http.get(baseUrl, params);
|
2019-04-27 15:18:00 +02:00
|
|
|
this.images = this.images.concat(data.images);
|
|
|
|
this.hasMore = data.has_more;
|
|
|
|
page++;
|
2017-08-09 22:33:00 +02:00
|
|
|
},
|
|
|
|
|
2019-04-27 15:18:00 +02:00
|
|
|
setFilterType(filterType) {
|
|
|
|
this.filter = filterType;
|
2018-05-13 13:07:38 +02:00
|
|
|
this.resetState();
|
|
|
|
this.fetchData();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetState() {
|
2017-08-09 22:33:00 +02:00
|
|
|
this.cancelSearch();
|
2019-04-27 15:18:00 +02:00
|
|
|
this.resetListView();
|
|
|
|
this.deleteConfirm = false;
|
|
|
|
baseUrl = window.baseUrl(`/images/${this.imageType}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetListView() {
|
2017-08-09 22:33:00 +02:00
|
|
|
this.images = [];
|
|
|
|
this.hasMore = false;
|
2019-04-27 15:18:00 +02:00
|
|
|
page = 1;
|
2017-08-09 22:33:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
searchImages() {
|
|
|
|
if (this.searchTerm === '') return this.cancelSearch();
|
|
|
|
|
|
|
|
// Cache current settings for later
|
|
|
|
if (!this.searching) {
|
|
|
|
preSearchImages = this.images;
|
|
|
|
preSearchHasMore = this.hasMore;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.searching = true;
|
2019-04-27 15:18:00 +02:00
|
|
|
this.resetListView();
|
2017-08-09 22:33:00 +02:00
|
|
|
this.fetchData();
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelSearch() {
|
2018-05-27 15:33:50 +02:00
|
|
|
if (!this.searching) return;
|
2017-08-09 22:33:00 +02:00
|
|
|
this.searching = false;
|
|
|
|
this.searchTerm = '';
|
|
|
|
this.images = preSearchImages;
|
|
|
|
this.hasMore = preSearchHasMore;
|
|
|
|
},
|
|
|
|
|
|
|
|
imageSelect(image) {
|
2019-04-27 15:18:00 +02:00
|
|
|
const dblClickTime = 300;
|
|
|
|
const currentTime = Date.now();
|
|
|
|
const timeDiff = currentTime - previousClickTime;
|
|
|
|
const isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
|
2017-08-09 22:33:00 +02:00
|
|
|
|
|
|
|
if (isDblClick) {
|
|
|
|
this.callbackAndHide(image);
|
|
|
|
} else {
|
|
|
|
this.selectedImage = image;
|
2018-05-13 13:07:38 +02:00
|
|
|
this.deleteConfirm = false;
|
2017-08-09 22:33:00 +02:00
|
|
|
this.dependantPages = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousClickTime = currentTime;
|
|
|
|
previousClickImage = image.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
callbackAndHide(imageResult) {
|
|
|
|
if (callback) callback(imageResult);
|
|
|
|
this.hide();
|
|
|
|
},
|
|
|
|
|
2019-04-27 15:18:00 +02:00
|
|
|
async saveImageDetails() {
|
2019-04-21 16:52:29 +02:00
|
|
|
let url = window.baseUrl(`/images/${this.selectedImage.id}`);
|
2019-04-27 15:18:00 +02:00
|
|
|
try {
|
|
|
|
await this.$http.put(url, this.selectedImage)
|
|
|
|
} catch (error) {
|
2017-08-09 22:33:00 +02:00
|
|
|
if (error.response.status === 422) {
|
|
|
|
let errors = error.response.data;
|
|
|
|
let message = '';
|
|
|
|
Object.keys(errors).forEach((key) => {
|
|
|
|
message += errors[key].join('\n');
|
|
|
|
});
|
|
|
|
this.$events.emit('error', message);
|
|
|
|
}
|
2019-04-27 15:18:00 +02:00
|
|
|
}
|
2017-08-09 22:33:00 +02:00
|
|
|
},
|
|
|
|
|
2019-04-27 15:18:00 +02:00
|
|
|
async deleteImage() {
|
2018-05-13 13:07:38 +02:00
|
|
|
|
|
|
|
if (!this.deleteConfirm) {
|
2019-04-27 15:18:00 +02:00
|
|
|
const url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
|
|
|
|
try {
|
|
|
|
const {data} = await this.$http.get(url);
|
|
|
|
this.dependantPages = data;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
this.deleteConfirm = true;
|
2018-05-13 13:07:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-04-27 15:18:00 +02:00
|
|
|
|
|
|
|
const url = window.baseUrl(`/images/${this.selectedImage.id}`);
|
|
|
|
await this.$http.delete(url);
|
|
|
|
this.images.splice(this.images.indexOf(this.selectedImage), 1);
|
|
|
|
this.selectedImage = false;
|
|
|
|
this.$events.emit('success', trans('components.image_delete_success'));
|
|
|
|
this.deleteConfirm = false;
|
2017-08-09 22:33:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getDate(stringDate) {
|
2018-06-30 06:58:11 +02:00
|
|
|
return Dates.formatDateTime(new Date(stringDate));
|
2017-08-09 22:33:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
uploadSuccess(event) {
|
|
|
|
this.images.unshift(event.data);
|
|
|
|
this.$events.emit('success', trans('components.image_upload_success'));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const computed = {
|
|
|
|
uploadUrl() {
|
2019-04-27 15:18:00 +02:00
|
|
|
return window.baseUrl(`/images/${this.imageType}`);
|
2017-08-09 22:33:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function mounted() {
|
|
|
|
window.ImageManager = this;
|
|
|
|
this.imageType = this.$el.getAttribute('image-type');
|
|
|
|
this.uploadedTo = this.$el.getAttribute('uploaded-to');
|
2019-04-27 15:18:00 +02:00
|
|
|
baseUrl = window.baseUrl('/images/' + this.imageType)
|
2017-08-09 22:33:00 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 22:17:35 +01:00
|
|
|
export default {
|
2017-08-09 22:33:00 +02:00
|
|
|
mounted,
|
|
|
|
methods,
|
|
|
|
data,
|
|
|
|
computed,
|
|
|
|
components: {dropzone},
|
2018-03-18 19:08:37 +01:00
|
|
|
};
|