|
| 1 | + <div class="form-group col-md-12 image" data-preview="#{{ $field['name'] }}" data-aspectRatio="{{ isset($field['aspect_ratio'])?$field['aspect_ratio']:0 }}" data-crop="{{ $field['crop'] }}"> |
| 2 | + <div> |
| 3 | + <label>{!! $field['label'] !!}</label> |
| 4 | + </div> |
| 5 | + <!-- Wrap the image or canvas element with a block element (container) --> |
| 6 | + <div class="row"> |
| 7 | + <div class="col-sm-6" style="margin-bottom: 20px;"> |
| 8 | + <img id="mainImage" src="{{ (isset($field['value']))?asset($field['value']):'' }}"> |
| 9 | + </div> |
| 10 | + @if($field['crop']) |
| 11 | + <div class="col-sm-3"> |
| 12 | + <div class="docs-preview clearfix"> |
| 13 | + <div id="{{ $field['name'] }}" class="img-preview preview-lg"> |
| 14 | + <img src="" style="display: block; min-width: 0px !important; min-height: 0px !important; max-width: none !important; max-height: none !important; margin-left: -32.875px; margin-top: -18.4922px; transform: none;"> |
| 15 | + </div> |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + @endif |
| 19 | + </div> |
| 20 | + <div class="btn-group"> |
| 21 | + <label class="btn btn-primary btn-file"> |
| 22 | + {{ trans('backpack::crud.choose_file') }} <input type="file" accept="image/*" id="uploadImage" class="hide"> |
| 23 | + <input type="hidden" id="hiddenImage" name="{{ $field['name'] }}"> |
| 24 | + </label> |
| 25 | + @if($field['crop']) |
| 26 | + <button class="btn btn-default" id="rotateLeft" type="button" style="display: none;"><i class="fa fa-rotate-left"></i></button> |
| 27 | + <button class="btn btn-default" id="rotateRight" type="button" style="display: none;"><i class="fa fa-rotate-right"></i></button> |
| 28 | + <button class="btn btn-default" id="zoomIn" type="button" style="display: none;"><i class="fa fa-search-plus"></i></button> |
| 29 | + <button class="btn btn-default" id="zoomOut" type="button" style="display: none;"><i class="fa fa-search-minus"></i></button> |
| 30 | + <button class="btn btn-warning" id="reset" type="button" style="display: none;"><i class="fa fa-times"></i></button> |
| 31 | + @endif |
| 32 | + <button class="btn btn-danger" id="remove" type="button"><i class="fa fa-trash"></i></button> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + |
| 36 | + |
| 37 | +{{-- ########################################## --}} |
| 38 | +{{-- Extra CSS and JS for this particular field --}} |
| 39 | +{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}} |
| 40 | +@if ($crud->checkIfFieldIsFirstOfItsType($field, $fields)) |
| 41 | + |
| 42 | + {{-- FIELD CSS - will be loaded in the after_styles section --}} |
| 43 | + @push('crud_fields_styles') |
| 44 | + {{-- YOUR CSS HERE --}} |
| 45 | + <link href="{{ asset('vendor/backpack/cropper/dist/cropper.min.css') }}" rel="stylesheet" type="text/css" /> |
| 46 | + <style> |
| 47 | + .hide { |
| 48 | + display: none; |
| 49 | + } |
| 50 | + .btn-group { |
| 51 | + margin-top: 10px; |
| 52 | + } |
| 53 | + img { |
| 54 | + max-width: 100%; /* This rule is very important, please do not ignore this! */ |
| 55 | + } |
| 56 | + .img-container, .img-preview { |
| 57 | + width: 100%; |
| 58 | + text-align: center; |
| 59 | + } |
| 60 | + .img-preview { |
| 61 | + float: left; |
| 62 | + margin-right: 10px; |
| 63 | + margin-bottom: 10px; |
| 64 | + overflow: hidden; |
| 65 | + } |
| 66 | + .preview-lg { |
| 67 | + width: 263px; |
| 68 | + height: 148px; |
| 69 | + } |
| 70 | +
|
| 71 | + .btn-file { |
| 72 | + position: relative; |
| 73 | + overflow: hidden; |
| 74 | + } |
| 75 | + .btn-file input[type=file] { |
| 76 | + position: absolute; |
| 77 | + top: 0; |
| 78 | + right: 0; |
| 79 | + min-width: 100%; |
| 80 | + min-height: 100%; |
| 81 | + font-size: 100px; |
| 82 | + text-align: right; |
| 83 | + filter: alpha(opacity=0); |
| 84 | + opacity: 0; |
| 85 | + outline: none; |
| 86 | + background: white; |
| 87 | + cursor: inherit; |
| 88 | + display: block; |
| 89 | + } |
| 90 | + </style> |
| 91 | + @endpush |
| 92 | + |
| 93 | + {{-- FIELD JS - will be loaded in the after_scripts section --}} |
| 94 | + @push('crud_fields_scripts') |
| 95 | + {{-- YOUR JS HERE --}} |
| 96 | + <script src="{{ asset('vendor/backpack/cropper/dist/cropper.min.js') }}"></script> |
| 97 | + <script> |
| 98 | + jQuery(document).ready(function($) { |
| 99 | + // Loop through all instances of the image field |
| 100 | + $('.form-group.image').each(function(index){ |
| 101 | + // Find DOM elements under this form-group element |
| 102 | + var $mainImage = $(this).find('#mainImage'); |
| 103 | + var $uploadImage = $(this).find("#uploadImage"); |
| 104 | + var $hiddenImage = $(this).find("#hiddenImage"); |
| 105 | + var $rotateLeft = $(this).find("#rotateLeft") |
| 106 | + var $rotateRight = $(this).find("#rotateRight") |
| 107 | + var $zoomIn = $(this).find("#zoomIn") |
| 108 | + var $zoomOut = $(this).find("#zoomOut") |
| 109 | + var $reset = $(this).find("#reset") |
| 110 | + var $remove = $(this).find("#remove") |
| 111 | + // Options either global for all image type fields, or use 'data-*' elements for options passed in via the CRUD controller |
| 112 | + var options = { |
| 113 | + viewMode: 2, |
| 114 | + checkOrientation: false, |
| 115 | + autoCropArea: 1, |
| 116 | + responsive: true, |
| 117 | + preview : $(this).attr('data-preview'), |
| 118 | + aspectRatio : $(this).attr('data-aspectRatio') |
| 119 | + }; |
| 120 | + var crop = $(this).attr('data-crop'); |
| 121 | +
|
| 122 | + // Hide 'Remove' button if there is no image saved |
| 123 | + if (!$mainImage.attr('src')){ |
| 124 | + $remove.hide(); |
| 125 | + } |
| 126 | + // Initialise hidden form input in case we submit with no change |
| 127 | + $hiddenImage.val($mainImage.attr('src')); |
| 128 | +
|
| 129 | +
|
| 130 | + // Only initialize cropper plugin if crop is set to true |
| 131 | + if(crop){ |
| 132 | +
|
| 133 | + $remove.click(function() { |
| 134 | + $mainImage.cropper("destroy"); |
| 135 | + $mainImage.attr('src',''); |
| 136 | + $hiddenImage.val(''); |
| 137 | + $rotateLeft.hide(); |
| 138 | + $rotateRight.hide(); |
| 139 | + $zoomIn.hide(); |
| 140 | + $zoomOut.hide(); |
| 141 | + $reset.hide(); |
| 142 | + $remove.hide(); |
| 143 | + }); |
| 144 | + } else { |
| 145 | +
|
| 146 | + $(this).find("#remove").click(function() { |
| 147 | + $mainImage.attr('src',''); |
| 148 | + $hiddenImage.val(''); |
| 149 | + $remove.hide(); |
| 150 | + }); |
| 151 | + } |
| 152 | +
|
| 153 | + $uploadImage.change(function() { |
| 154 | + var fileReader = new FileReader(), |
| 155 | + files = this.files, |
| 156 | + file; |
| 157 | +
|
| 158 | + if (!files.length) { |
| 159 | + return; |
| 160 | + } |
| 161 | + file = files[0]; |
| 162 | +
|
| 163 | + if (/^image\/\w+$/.test(file.type)) { |
| 164 | + fileReader.readAsDataURL(file); |
| 165 | + fileReader.onload = function () { |
| 166 | + $uploadImage.val(""); |
| 167 | + if(crop){ |
| 168 | + $mainImage.cropper(options).cropper("reset", true).cropper("replace", this.result); |
| 169 | + // Override form submit to copy canvas to hidden input before submitting |
| 170 | + $('form').submit(function() { |
| 171 | + var imageURL = $mainImage.cropper('getCroppedCanvas').toDataURL(); |
| 172 | + $hiddenImage.val(imageURL); |
| 173 | + return true; // return false to cancel form action |
| 174 | + }); |
| 175 | + $rotateLeft.click(function() { |
| 176 | + $mainImage.cropper("rotate", 90); |
| 177 | + }); |
| 178 | + $rotateRight.click(function() { |
| 179 | + $mainImage.cropper("rotate", -90); |
| 180 | + }); |
| 181 | + $zoomIn.click(function() { |
| 182 | + $mainImage.cropper("zoom", 0.1); |
| 183 | + }); |
| 184 | + $zoomOut.click(function() { |
| 185 | + $mainImage.cropper("zoom", -0.1); |
| 186 | + }); |
| 187 | + $reset.click(function() { |
| 188 | + $mainImage.cropper("reset"); |
| 189 | + }); |
| 190 | + $rotateLeft.show(); |
| 191 | + $rotateRight.show(); |
| 192 | + $zoomIn.show(); |
| 193 | + $zoomOut.show(); |
| 194 | + $reset.show(); |
| 195 | + $remove.show(); |
| 196 | +
|
| 197 | + } else { |
| 198 | + $mainImage.attr('src',this.result); |
| 199 | + $hiddenImage.val(this.result); |
| 200 | + $remove.show(); |
| 201 | + } |
| 202 | + }; |
| 203 | + } else { |
| 204 | + alert("Please choose an image file."); |
| 205 | + } |
| 206 | + }); |
| 207 | +
|
| 208 | + }); |
| 209 | + }); |
| 210 | + </script> |
| 211 | + |
| 212 | + |
| 213 | + @endpush |
| 214 | +@endif |
| 215 | +{{-- End of Extra CSS and JS --}} |
| 216 | +{{-- ########################################## --}} |
0 commit comments