VitalyD

Untitled

Sep 27th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <default-field :field="field">
  3.         <template slot="field">
  4.             <div class="mb-6">
  5.                 <template>
  6.                     <div class="uploader"
  7.                         @dragenter="OnDragEnter"
  8.                         @dragleave="OnDragLeave"
  9.                         @dragover.prevent
  10.                         @drop="onDrop"
  11.                         :class="{ dragging: isDragging }">
  12.                        
  13.                         <div class="upload-control" v-show="images.length">
  14.                             <label for="file">Добавить файл</label>
  15.                         </div>
  16.  
  17.                         <div v-show="!images.length">
  18.                             <i class="fa fa-cloud-upload"></i>
  19.                             <p>Перетащи</p>
  20.                             <div>или</div>
  21.                             <div class="file-input">
  22.                                 <label for="file">выбери файл</label>
  23.                                 <input type="file" id="file" @change="onInputChange" multiple>
  24.                             </div>
  25.                         </div>
  26.  
  27.                         <div class="images-preview" v-show="images.length">
  28.                             <draggable class="dragArea" :options="{group:'images'}">
  29.                             <vue-select-image :dataImages="images"
  30.                                                     @onselectimage="onSelectImage"
  31.                                                     :rootClass="'custom-style'">
  32.  
  33.                                 <div class="img-wrapper" v-for="(src, id) in images" :key="id">
  34.                                     <img :src="image" :alt="`Image Uplaoder ${index}`">
  35.                                     <div class="details">
  36.                                         <span class="name" v-text="files[id].name"></span>
  37.                                         <span class="size" v-text="getFileSize(files[id].size)"></span>
  38.                                         <button v-on:click="remove(id)"></button>
  39.                                     </div>
  40.                                 </div>
  41.  
  42.                             </vue-select-image>
  43.                             </draggable>
  44.                         </div>
  45.                     </div>
  46.                 </template>
  47.             </div>
  48.         </template>
  49.     </default-field>
  50. </template>
  51.  
  52. <script>
  53. import VueSelectImage from 'vue-select-image'
  54. import { FormField, HandlesValidationErrors } from 'laravel-nova';
  55. import draggable from 'vuedraggable'
  56.  
  57. export default {
  58.     mixins: [HandlesValidationErrors, FormField],
  59.            components: {
  60.                 draggable,
  61.                 VueSelectImage
  62.         },
  63. data: () => ({
  64.         isDragging: false,
  65.         dragCount: 0,
  66.         files: [],
  67.         images: [],
  68.         imageSelected: [],
  69.         initialSelected: [],
  70.         templateSingle: `
  71.         <vue-select-image :dataImages="dataImages"
  72.             @onselectimage="onSelectImage">
  73.         </vue-select-image>
  74.       `,
  75.       dataImages: [{
  76.         id: '1',
  77.         src: 'http://placekitten.com/200/200',
  78.         alt: 'jQuery'
  79.       }, {
  80.         id: '2',
  81.         src: 'http://placekitten.com/200/200',
  82.         alt: 'Angular'
  83.       }, {
  84.         id: '3',
  85.         src: 'http://placekitten.com/200/200',
  86.         alt: 'Vue.js'
  87.       }, {
  88.         id: '4',
  89.         src: 'http://placekitten.com/200/200',
  90.         alt: 'React'
  91.       }],
  92.     }),
  93.     methods: {
  94.         onSelectImage: function (data) {
  95.             console.log('fire event onSelectImage: ', data)
  96.          this.imageSelected = data
  97.         },
  98.         onSelectMultipleImage: function (data) {
  99.             console.log('fire event onSelectMultipleImage: ', data)
  100.             this.imageMultipleSelected = data
  101.         },
  102.         onUnselectSingleImage: function () {
  103.             this.$refs['single-select-image'].removeFromSingleSelected()
  104.         },
  105.         OnDragEnter(e) {
  106.             e.preventDefault();
  107.            
  108.             this.dragCount++;
  109.             this.isDragging = true;
  110.  
  111.             return false;
  112.         },
  113.         OnDragLeave(e) {
  114.             e.preventDefault();
  115.             this.dragCount--;
  116.  
  117.             if (this.dragCount <= 0)
  118.                 this.isDragging = false;
  119.         },
  120.         onInputChange(e) {
  121.             const files = e.target.files;
  122.  
  123.             Array.from(files).forEach(file => this.addImage(file));
  124.         },
  125.         onDrop(e) {
  126.             e.preventDefault();
  127.             e.stopPropagation();
  128.  
  129.             this.isDragging = false;
  130.  
  131.             const files = e.dataTransfer.files;
  132.  
  133.             Array.from(files).forEach(file => this.addImage(file));
  134.         },
  135.         addImage(file) {
  136.             if (!file.type.match('image.*')) {
  137.                 this.$toastr.e(`${file.name} is not an image`);
  138.                 return;
  139.             }
  140.  
  141.             this.files.push(file);
  142.  
  143.             const img = new Image(),
  144.                 reader = new FileReader();
  145.  
  146.             reader.onload = (e) => this.images.push(e.target.result);
  147.  
  148.             reader.readAsDataURL(file);
  149.         },
  150.         getFileSize(size) {
  151.             const fSExt = ['Bytes', 'KB', 'MB', 'GB'];
  152.             let i = 0;
  153.            
  154.             while(size > 900) {
  155.                 size /= 1024;
  156.                 i++;
  157.             }
  158.  
  159.             return `${(Math.round(size * 100) / 100)} ${fSExt[i]}`;
  160.         },
  161.         upload() {
  162.             const formData = new FormData();
  163.            
  164.             this.files.forEach(file => {
  165.                 formData.append('images[]', file, file.name);
  166.             });
  167.  
  168.             axios.post('/images-upload', formData)
  169.                 .then(response => {
  170.                     this.$toastr.s('Все изображения загружены');
  171.                     this.images = [];
  172.                     this.files = [];
  173.                 })
  174.         },
  175.         remove: function(index){
  176.             this.images.splice(index, 1);
  177.     },
  178.     },
  179.     computed: {
  180.         /**
  181.          * Get the input type.
  182.          */
  183.         inputType() {
  184.             return this.field.type || 'text'
  185.         },
  186.  
  187.         /**
  188.          * Get the input step amount.
  189.          */
  190.         inputStep() {
  191.             return this.field.step
  192.         },
  193.  
  194.         /**
  195.          * Get the input minimum amount.
  196.          */
  197.         inputMin() {
  198.             return this.field.min
  199.         },
  200.  
  201.         /**
  202.          * Get the input maximum amount.
  203.          */
  204.         inputMax() {
  205.             return this.field.max
  206.         },
  207.  
  208.         /**
  209.          * Get the pattern that should be used for the field
  210.          */
  211.         inputPattern() {
  212.             return this.field.pattern
  213.         },
  214.     },
  215. }
  216. </script>
Advertisement
Add Comment
Please, Sign In to add comment