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
|
|
|
|
|
|
|
let page = 0;
|
|
|
|
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,
|
|
|
|
view: 'all',
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
|
|
|
|
fetchData() {
|
|
|
|
let url = baseUrl + page;
|
|
|
|
let query = {};
|
|
|
|
if (this.uploadedTo !== false) query.page_id = this.uploadedTo;
|
|
|
|
if (this.searching) query.term = this.searchTerm;
|
|
|
|
|
|
|
|
this.$http.get(url, {params: query}).then(response => {
|
|
|
|
this.images = this.images.concat(response.data.images);
|
|
|
|
this.hasMore = response.data.hasMore;
|
|
|
|
page++;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setView(viewName) {
|
2018-05-13 19:07:38 +08:00
|
|
|
this.view = viewName;
|
|
|
|
this.resetState();
|
|
|
|
this.fetchData();
|
|
|
|
},
|
|
|
|
|
|
|
|
resetState() {
|
2017-08-10 04:33:00 +08:00
|
|
|
this.cancelSearch();
|
|
|
|
this.images = [];
|
|
|
|
this.hasMore = false;
|
2018-05-13 19:07:38 +08:00
|
|
|
this.deleteConfirm = false;
|
2017-08-10 04:33:00 +08:00
|
|
|
page = 0;
|
2018-05-13 19:07:38 +08:00
|
|
|
baseUrl = window.baseUrl(`/images/${this.imageType}/${this.view}/`);
|
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;
|
|
|
|
this.images = [];
|
|
|
|
this.hasMore = false;
|
|
|
|
page = 0;
|
|
|
|
baseUrl = window.baseUrl(`/images/${this.imageType}/search/`);
|
|
|
|
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) {
|
|
|
|
let dblClickTime = 300;
|
|
|
|
let currentTime = Date.now();
|
|
|
|
let timeDiff = currentTime - previousClickTime;
|
|
|
|
let isDblClick = timeDiff < dblClickTime && image.id === previousClickImage;
|
|
|
|
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
|
|
|
|
saveImageDetails() {
|
|
|
|
let url = window.baseUrl(`/images/update/${this.selectedImage.id}`);
|
|
|
|
this.$http.put(url, this.selectedImage).then(response => {
|
|
|
|
this.$events.emit('success', trans('components.image_update_success'));
|
|
|
|
}).catch(error => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteImage() {
|
2018-05-13 19:07:38 +08:00
|
|
|
|
|
|
|
if (!this.deleteConfirm) {
|
|
|
|
let url = window.baseUrl(`/images/usage/${this.selectedImage.id}`);
|
|
|
|
this.$http.get(url).then(resp => {
|
|
|
|
this.dependantPages = resp.data;
|
|
|
|
}).catch(console.error).then(() => {
|
|
|
|
this.deleteConfirm = true;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2018-11-25 03:16:04 +08:00
|
|
|
let url = window.baseUrl(`/images/${this.selectedImage.id}`);
|
|
|
|
this.$http.delete(url).then(resp => {
|
2017-08-10 04:33:00 +08:00
|
|
|
this.images.splice(this.images.indexOf(this.selectedImage), 1);
|
|
|
|
this.selectedImage = false;
|
|
|
|
this.$events.emit('success', trans('components.image_delete_success'));
|
2018-05-13 19:07:38 +08:00
|
|
|
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() {
|
|
|
|
return window.baseUrl(`/images/${this.imageType}/upload`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function mounted() {
|
|
|
|
window.ImageManager = this;
|
|
|
|
this.imageType = this.$el.getAttribute('image-type');
|
|
|
|
this.uploadedTo = this.$el.getAttribute('uploaded-to');
|
|
|
|
baseUrl = window.baseUrl('/images/' + this.imageType + '/all/')
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|