|
1 | 1 | import { Controller } from '@hotwired/stimulus';
|
2 | 2 | import Cropper from 'cropperjs';
|
3 | 3 |
|
4 |
| -class controller extends Controller { |
| 4 | +class CropperController extends Controller { |
5 | 5 | connect() {
|
6 | 6 | const img = document.createElement('img');
|
7 | 7 | img.classList.add('cropperjs-image');
|
8 |
| - img.src = this.element.getAttribute('data-public-url'); |
| 8 | + img.src = this.publicUrlValue; |
9 | 9 | const parent = this.element.parentNode;
|
10 |
| - parent.appendChild(img); |
11 |
| - const options = { |
12 |
| - viewMode: parseInt(this.element.getAttribute('data-view-mode')), |
13 |
| - dragMode: this.element.getAttribute('data-drag-mode'), |
14 |
| - responsive: this.element.hasAttribute('data-responsive'), |
15 |
| - restore: this.element.hasAttribute('data-restore'), |
16 |
| - checkCrossOrigin: this.element.hasAttribute('data-check-cross-origin'), |
17 |
| - checkOrientation: this.element.hasAttribute('data-check-orientation'), |
18 |
| - modal: this.element.hasAttribute('data-modal'), |
19 |
| - guides: this.element.hasAttribute('data-guides'), |
20 |
| - center: this.element.hasAttribute('data-center'), |
21 |
| - highlight: this.element.hasAttribute('data-highlight'), |
22 |
| - background: this.element.hasAttribute('data-background'), |
23 |
| - autoCrop: this.element.hasAttribute('data-auto-crop'), |
24 |
| - autoCropArea: parseFloat(this.element.getAttribute('data-auto-crop-area')), |
25 |
| - movable: this.element.hasAttribute('data-movable'), |
26 |
| - rotatable: this.element.hasAttribute('data-rotatable'), |
27 |
| - scalable: this.element.hasAttribute('data-scalable'), |
28 |
| - zoomable: this.element.hasAttribute('data-zoomable'), |
29 |
| - zoomOnTouch: this.element.hasAttribute('data-zoom-on-touch'), |
30 |
| - zoomOnWheel: this.element.hasAttribute('data-zoom-on-wheel'), |
31 |
| - wheelZoomRatio: parseFloat(this.element.getAttribute('data-wheel-zoom-ratio')), |
32 |
| - cropBoxMovable: this.element.hasAttribute('data-crop-box-movable'), |
33 |
| - cropBoxResizable: this.element.hasAttribute('data-crop-box-resizable'), |
34 |
| - toggleDragModeOnDblclick: this.element.hasAttribute('data-toggle-drag-mode-on-dblclick'), |
35 |
| - minContainerWidth: parseInt(this.element.getAttribute('data-min-container-width')), |
36 |
| - minContainerHeight: parseInt(this.element.getAttribute('data-min-container-height')), |
37 |
| - minCanvasWidth: parseInt(this.element.getAttribute('data-min-canvas-width')), |
38 |
| - minCanvasHeight: parseInt(this.element.getAttribute('data-min-canvas-height')), |
39 |
| - minCropBoxWidth: parseInt(this.element.getAttribute('data-min-crop-box-width')), |
40 |
| - minCropBoxHeight: parseInt(this.element.getAttribute('data-min-crop-box-height')), |
41 |
| - }; |
42 |
| - if (this.element.getAttribute('data-aspect-ratio')) { |
43 |
| - options.aspectRatio = parseFloat(this.element.getAttribute('data-aspect-ratio')); |
44 |
| - } |
45 |
| - if (this.element.getAttribute('data-initial-aspect-ratio')) { |
46 |
| - options.initialAspectRatio = parseFloat(this.element.getAttribute('data-initial-aspect-ratio')); |
| 10 | + if (!parent) { |
| 11 | + throw new Error('Missing parent node for Cropperjs'); |
47 | 12 | }
|
| 13 | + parent.appendChild(img); |
| 14 | + const options = this.optionsValue; |
| 15 | + this._dispatchEvent('cropperjs:pre-connect', { options, img }); |
48 | 16 | const cropper = new Cropper(img, options);
|
49 | 17 | img.addEventListener('crop', (event) => {
|
50 | 18 | this.element.value = JSON.stringify(event.detail);
|
51 | 19 | });
|
52 | 20 | this._dispatchEvent('cropperjs:connect', { cropper, options, img });
|
53 | 21 | }
|
54 |
| - _dispatchEvent(name, payload = null, canBubble = false, cancelable = false) { |
55 |
| - const userEvent = document.createEvent('CustomEvent'); |
56 |
| - userEvent.initCustomEvent(name, canBubble, cancelable, payload); |
57 |
| - this.element.dispatchEvent(userEvent); |
| 22 | + _dispatchEvent(name, payload) { |
| 23 | + this.element.dispatchEvent(new CustomEvent(name, { detail: payload })); |
58 | 24 | }
|
59 | 25 | }
|
| 26 | +CropperController.values = { |
| 27 | + publicUrl: String, |
| 28 | + options: Object, |
| 29 | +}; |
60 | 30 |
|
61 |
| -export { controller as default }; |
| 31 | +export { CropperController as default }; |
0 commit comments