Sha256: d48cbef2c6bdff58d6e1b181edc7b7f0844230cbe0583514d2b23677007d18d8

Contents?: true

Size: 1.35 KB

Versions: 22

Compression:

Stored size: 1.35 KB

Contents

import { CocoComponent } from "@assets/js/coco/component";
import { getData } from "@helpers/alpine";
import { basename } from "@helpers/path";

export default CocoComponent("imagePickerButton", ({ src }) => {
  return {
    selectedImage: {
      name: basename(src),
      file: null,
      data: src,
    },

    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", () => {
        const selectedImage = {
          name: file.name,
          file,
          data: reader.result,
        };

        this.$dispatch("image-picker:change", { image: selectedImage });

        this.selectedImage = selectedImage;

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

      reader.readAsDataURL(file);
    },

    clearSelectedImage() {
      this.dropdown.hide();
      this.selectedImage = { name: null, data: null, file: null };
    },

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

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.7 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.6 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.5 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.4 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.3 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.2 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.1 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.19.0 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.18.8 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.18.7 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.18.0 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.7 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.6 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.5 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.4 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.3 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.2 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.1 app/components/coco/buttons/image_picker_button/image_picker_button.js
coveragebook_components-0.17.0 app/components/coco/buttons/image_picker_button/image_picker_button.js