import { CocoComponent } from "@assets/js/base/coco"; import { getComponent } from "@helpers/alpine"; import { basename } from "@helpers/path"; export default CocoComponent("imagePicker", ({ src }) => { return { image: { name: basename(src), file: null, data: src, }, get name() { return this.image.name; }, get src() { return this.image.data; }, get hasImage() { return !!this.src; }, get uploader() { return getComponent(this.$refs.uploader.firstElementChild); }, setImage(file, silent = false) { this.image = { file: file.file, name: file.name, data: file.data, }; this.uploader.setFile(file); if (silent === false) { this.$dispatch("image-picker:select", { image: this.image }); } }, clearImage(silent = false) { this.uploader.clear(); this.image = { name: null, data: null, file: null }; if (silent === false) { this.$dispatch("image-picker:clear"); } }, browseFiles() { this.uploader.openPicker(); }, replaceImage() { this.uploader.openPicker(); }, }; });