2018-06-30 12:58:11 +08:00
|
|
|
import * as Dates from "../services/dates";
|
2018-11-10 05:17:35 +08:00
|
|
|
import dropzone from "./components/dropzone";
|
2017-08-10 04:33:00 +08:00
|
|
|
|
2019-04-27 21:18:00 +08:00
|
|
|
let page = 1;
|
2017-08-10 04:33:00 +08: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 21:18:00 +08:00
|
|
|
filter: null,
|
2017-08-10 04:33:00 +08:00
|
|
|
hasMore: false,
|
|
|
|
searching: false,
|
|
|
|
searchTerm: '',
|
|
|
|
|
|
|
|
imageUpdateSuccess: false,
|
|
|
|
imageDeleteSuccess: false,
|
2018-05-13 19:07:38 +08:00
|
|
|
deleteConfirm: false,
|
2017-08-10 04:33:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const methods = {
|
|
|
|
|
2018-05-13 19:07:38 +08:00
|
|
|
show(providedCallback, imageType = null) {
|
2017-08-10 04:33:00 +08: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 19:07:38 +08:00
|
|
|
if (dataLoaded && imageType === this.imageType) return;
|
|
|
|
if (imageType) {
|
|
|
|
this.imageType = imageType;
|
|
|
|
this.resetState();
|
|
|
|
}
|
2017-08-10 04:33:00 +08:00
|
|
|
this.fetchData();
|
|
|
|
dataLoaded = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
hide() {
|
2018-05-20 23:40:30 +08:00
|
|
|
if (this.$refs.dropzone) {
|
|
|
|
this.$refs.dropzone.onClose();
|
|
|
|
}
|
2017-08-10 04:33:00 +08:00
|
|
|
this.showing = false;
|
2018-03-19 02:08:37 +08:00
|
|
|
this.selectedImage = false;
|
2017-08-10 04:33:00 +08:00
|
|
|
this.$el.children[0].components.overlay.hide();
|
|
|
|
},
|
|
|
|
|
2019-04-27 21:18:00 +08:00
|
|
|
async fetchData() {
|
2019-06-08 07:02:51 +08:00
|
|
|
const params = {
|
2019-04-27 21:18:00 +08:00
|
|
|
page,
|
|
|
|
search: this.searching ? this.searchTerm : null,
|
|
|
|
uploaded_to: this.uploadedTo || null,
|
|
|
|
filter_type: this.filter,
|
|
|
|
};
|
|
|
|
|
2019-06-08 07:02:51 +08:00
|
|
|
const {data} = await this.$http.get(baseUrl, params);
|
2019-04-27 21:18:00 +08:00
|
|
|
this.images = this.images.concat(data.images);
|
|
|
|
this.hasMore = data.has_more;
|
|
|
|
page++;
|
2017-08-10 04:33:00 +08:00
|
|
|
},
|
|
|
|
|
2019-04-27 21:18:00 +08:00
|
|
|
setFilterType(filterType) {
|
|
|
|
this.filter = filterType;
|
2018-05-13 19:07:38 +08:00
|
|
|
this.resetState();
|
|
|
|
this.fetchData();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetState() {
|
2017-08-10 04:33:00 +08:00
|
|
|
this.cancelSearch();
|
2019-04-27 21:18:00 +08:00
|
|
|
this.resetListView();
|
|
|
|
this.deleteConfirm = false;
|
|
|
|
baseUrl = window.baseUrl(`/images/${this.imageType}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetListView() {
|
2017-08-10 04:33:00 +08:00
|
|
|
this.images = [];
|
|
|
|
this.hasMore = false;
|
2019-04-27 21:18:00 +08:00
|
|
|
page = 1;
|
2017-08-10 04:33:00 +08: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 21:18:00 +08:00
|
|
|
this.resetListView();
|
2017-08-10 04:33:00 +08:00
|
|
|
this.fetchData();
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelSearch() {
|
2018-05-27 21:33:50 +08:00
|
|
|
if (!this.searching) return;
|
2017-08-10 04:33:00 +08:00
|
|
|
this.searching = false;
|
|
|
|
this.searchTerm = '';
|
|
|
|
this.images = preSearchImages;
|
|
|
|
this.hasMore = preSearchHasMore;
|
|
|
|
},
|
|
|
|
|
|
|
|
imageSelect(image) {
|
2019-04-27 21:18:00 +08:00
|
|
|
const dblClickTime = 300;
|
|
|
|
const currentTime = Date.now();
|
|
|
|
const timeDiff = currentTime - previousClickTime;
|
|
|
|
const isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
|
2017-08-10 04:33:00 +08:00
|
|
|
|
|
|
|
if (isDblClick) {
|
|
|
|
this.callbackAndHide(image);
|
|
|
|
} else {
|
|
|
|
this.selectedImage = image;
|
2018-05-13 19:07:38 +08:00
|
|
|
this.deleteConfirm = false;
|
2017-08-10 04:33:00 +08:00
|
|
|
this.dependantPages = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
previousClickTime = currentTime;
|
|
|
|
previousClickImage = image.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
callbackAndHide(imageResult) {
|
|
|
|
if (callback) callback(imageResult);
|
|
|
|
this.hide();
|
|
|
|
},
|
|
|
|
|
2019-04-27 21:18:00 +08:00
|
|
|
async saveImageDetails() {
|
2019-04-21 22:52:29 +08:00
|
|
|
let url = window.baseUrl(`/images/${this.selectedImage.id}`);
|
2019-04-27 21:18:00 +08:00
|
|
|
try {
|
|
|
|
await this.$http.put(url, this.selectedImage)
|
|
|
|
} catch (error) {
|
2017-08-10 04:33:00 +08: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 21:18:00 +08:00
|
|
|
}
|
2017-08-10 04:33:00 +08:00
|
|
|
},
|
|
|
|
|
2019-04-27 21:18:00 +08:00
|
|
|
async deleteImage() {
|
2018-05-13 19:07:38 +08:00
|
|
|
|
|
|
|
if (!this.deleteConfirm) {
|
2019-04-27 21:18:00 +08: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 19:07:38 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-04-27 21:18:00 +08: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-10 04:33:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getDate(stringDate) {
|
2018-06-30 12:58:11 +08:00
|
|
|
return Dates.formatDateTime(new Date(stringDate));
|
2017-08-10 04:33:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
uploadSuccess(event) {
|
|
|
|
this.images.unshift(event.data);
|
|
|
|
this.$events.emit('success', trans('components.image_upload_success'));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const computed = {
|
|
|
|
uploadUrl() {
|
2019-04-27 21:18:00 +08:00
|
|
|
return window.baseUrl(`/images/${this.imageType}`);
|
2017-08-10 04:33:00 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function mounted() {
|
|
|
|
window.ImageManager = this;
|
|
|
|
this.imageType = this.$el.getAttribute('image-type');
|
|
|
|
this.uploadedTo = this.$el.getAttribute('uploaded-to');
|
2019-04-27 21:18:00 +08:00
|
|
|
baseUrl = window.baseUrl('/images/' + this.imageType)
|
2017-08-10 04:33:00 +08:00
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default {
|
2017-08-10 04:33:00 +08:00
|
|
|
mounted,
|
|
|
|
methods,
|
|
|
|
data,
|
|
|
|
computed,
|
|
|
|
components: {dropzone},
|
2018-03-19 02:08:37 +08:00
|
|
|
};
|