123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import Cropper from 'cropperjs'
- import 'cropperjs/dist/cropper.css'
- import { library } from '@fortawesome/fontawesome-svg-core'
- import {
- faCircleNotch
- } from '@fortawesome/free-solid-svg-icons'
- library.add(
- faCircleNotch
- )
- const ImageCropper = {
- props: {
- trigger: {
- type: [String, window.Element],
- required: true
- },
- submitHandler: {
- type: Function,
- required: true
- },
- cropperOptions: {
- type: Object,
- default () {
- return {
- aspectRatio: 1,
- autoCropArea: 1,
- viewMode: 1,
- movable: false,
- zoomable: false,
- guides: false
- }
- }
- },
- mimes: {
- type: String,
- default: 'image/png, image/gif, image/jpeg, image/bmp, image/x-icon'
- },
- saveButtonLabel: {
- type: String
- },
- saveWithoutCroppingButtonlabel: {
- type: String
- },
- cancelButtonLabel: {
- type: String
- }
- },
- data () {
- return {
- cropper: undefined,
- dataUrl: undefined,
- filename: undefined,
- submitting: false
- }
- },
- computed: {
- saveText () {
- return this.saveButtonLabel || this.$t('image_cropper.save')
- },
- saveWithoutCroppingText () {
- return this.saveWithoutCroppingButtonlabel || this.$t('image_cropper.save_without_cropping')
- },
- cancelText () {
- return this.cancelButtonLabel || this.$t('image_cropper.cancel')
- }
- },
- methods: {
- destroy () {
- if (this.cropper) {
- this.cropper.destroy()
- }
- this.$refs.input.value = ''
- this.dataUrl = undefined
- this.$emit('close')
- },
- submit (cropping = true) {
- this.submitting = true
- this.submitHandler(cropping && this.cropper, this.file)
- .then(() => this.destroy())
- .finally(() => {
- this.submitting = false
- })
- },
- pickImage () {
- this.$refs.input.click()
- },
- createCropper () {
- this.cropper = new Cropper(this.$refs.img, this.cropperOptions)
- },
- getTriggerDOM () {
- return typeof this.trigger === 'object' ? this.trigger : document.querySelector(this.trigger)
- },
- readFile () {
- const fileInput = this.$refs.input
- if (fileInput.files != null && fileInput.files[0] != null) {
- this.file = fileInput.files[0]
- let reader = new window.FileReader()
- reader.onload = (e) => {
- this.dataUrl = e.target.result
- this.$emit('open')
- }
- reader.readAsDataURL(this.file)
- this.$emit('changed', this.file, reader)
- }
- }
- },
- mounted () {
- // listen for click event on trigger
- const trigger = this.getTriggerDOM()
- if (!trigger) {
- this.$emit('error', 'No image make trigger found.', 'user')
- } else {
- trigger.addEventListener('click', this.pickImage)
- }
- // listen for input file changes
- const fileInput = this.$refs.input
- fileInput.addEventListener('change', this.readFile)
- },
- beforeUnmount: function () {
- // remove the event listeners
- const trigger = this.getTriggerDOM()
- if (trigger) {
- trigger.removeEventListener('click', this.pickImage)
- }
- const fileInput = this.$refs.input
- fileInput.removeEventListener('change', this.readFile)
- }
- }
- export default ImageCropper
|