Sha256: c538edbbf5638171a7927b132b25cc9758580c06b6ff3b7dd811c135abb0ee85

Contents?: true

Size: 1.18 KB

Versions: 33

Compression:

Stored size: 1.18 KB

Contents

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

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

        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

33 entries across 33 versions & 1 rubygems

Version Path
coveragebook_components-0.8.5 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.4 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.3 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.2 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.1 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.0 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.0.beta.3 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.0.beta.2 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.8.0.beta.1 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.10 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.9 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.8 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.7 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.6 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.5 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.4 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.3 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.2 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.1 app/components/coco/app/elements/image_picker_button/image_picker_button.js
coveragebook_components-0.7.0 app/components/coco/app/elements/image_picker_button/image_picker_button.js