Sha256: 3eb2aae42d2fb61330f0b48fbb74161bc254c9e2c3c296f24aa99e6e0c35c1e3

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

import { CocoComponent } from "@js/coco";
import { getData } from "@helpers/alpine";

export default CocoComponent("appImagePickerButton", ({ image }) => {
  return {
    selectedImage: image,

    dropdown: null,

    init() {
      this.$watch("selectedImage", (file) => {
        const dataTransfer = new DataTransfer();
        if (file && file.file instanceof File) {
          dataTransfer.items.add(file.file);
        }
        this.$refs.fileInput.files = dataTransfer.files;

        if (this.dropdown) {
          this.dropdown.hide();
        }
      });
    },

    getPicker() {
      return getData(
        this.$root.querySelector("[data-role='image-picker'").firstElementChild
      );
    },

    handleExternalDrop(event) {
      this.readFile(event.dataTransfer.files[0]);
    },

    readFile(file) {
      const reader = new FileReader();

      reader.addEventListener("load", () => {
        this.selectedImage = {
          name: file.name,
          file: file,
          data: reader.result,
        };
      });

      // reader.addEventListener("error", () => {
      //   this.errors.push(`Error reading file: ${file.name}`);
      // });

      reader.readAsDataURL(file);
    },

    clearSelectedImage() {
      this.dropdown.hide();
      this.selectedImage = null;
    },

    setSelectedImage(image) {
      this.dropdown.hide();
      this.selectedImage = image;
    },

    onDropdownMount({ detail }) {
      this.dropdown = detail.dropdown;
      this.getPicker().setImage(this.selectedImage, true);
    },
  };
});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coveragebook_components-0.5.0 app/components/coco/app/elements/image_picker_button/image_picker_button.js