/*
 * This file is part of the Sylius package.
 *
 * (c) Paweł Jędrzejewski
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

import $ from 'jquery';

const displayUploadedImage = function displayUploadedImage(input) {
  if (input.files && input.files[0]) {
    const reader = new FileReader();

    reader.onload = (event) => {
      const image = $(input).parent().siblings('.image');

      if (image.length > 0) {
        image.attr('src', event.target.result);
      } else {
        const img = $('<img class="ui small bordered image"/>');
        img.attr('src', event.target.result);
        $(input).parent().before(img);
      }
    };

    reader.readAsDataURL(input.files[0]);
  }
};

$.fn.extend({
  previewUploadedImage(root) {
    $(`${root} input[type="file"]`).each((idx, el) => {
      $(el).change((event) => {
        displayUploadedImage(event.currentTarget);
      });
    });

    $(`${root} [data-form-collection="add"]`).on('click', (evt) => {
      const self = $(evt.currentTarget);

      setTimeout(() => {
        self.parent().find('.column:last-child input[type="file"]').on('change', (event) => {
          displayUploadedImage(event.currentTarget);
        });
      }, 500);
    });
  },
});

@@ -830,400 +830,68 @@
   $(
-%60$%7B
 root
-%7D input%5Btype=%22file%22%5D%60).each((idx, el) =%3E %7B%0A      $(el).change((event) =%3E %7B%0A        displayUploadedImage(event.currentTarget);%0A      %7D);%0A    %7D);%0A%0A    $(%60$%7Broot%7D %5Bdata-form-collection=%22add%22%5D%60).on('click', (evt) =%3E %7B%0A      const self = $(evt.currentTarget);%0A%0A      setTimeout(() =%3E %7B%0A        self.parent().find('.column:last-child input%5Btype=%22file%22%5D').on('change', (event) =%3E %7B%0A  
+).on('change', 'input%5Btype=%22file%22%5D', function() %7B%0A
       
-  
 disp
@@ -911,54 +911,12 @@
 age(
-event.currentTarget);%0A        %7D);%0A      %7D, 500
+this
 );%0A 

Simplify code of `sylius-product-images-preview` module