Passed
Push — master ( b31e19...f7c593 )
by Odiseo
08:36
created

  C

Complexity

Conditions 11

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 12
c 0
b 0
f 0
dl 0
loc 19
rs 5.4

How to fix   Complexity   

Complexity

Complex classes like odiseo-vendor-images-preview.js ➔ displayUploadedImage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
(function ( $ ) {
2
  'use strict';
3
4
  $.fn.extend({
5
    previewUploadedImage: function (root) {
6
      $(root + ' input[type="file"]').each(function() {
7
        $(this).change(function() {
8
          displayUploadedImage(this);
9
        });
10
      });
11
12
      $(root + ' [data-form-collection="add"]').on('click', function() {
13
        var self = $(this);
14
15
        setTimeout(function() {
16
          self.parent().find('.column:last-child input[type="file"]').on('change', function() {
17
            displayUploadedImage(this);
18
          });
19
        }, 500);
20
      });
21
    }
22
  });
23
24
  function displayUploadedImage(input) {
25
    if (input.files && input.files[0]) {
26
      var reader = new FileReader();
0 ignored issues
show
Bug introduced by
The variable FileReader seems to be never declared. If this is a global, consider adding a /** global: FileReader */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
27
28
      reader.onload = function (e) {
29
        var image = $(input).siblings('.image');
30
31
        if (image.length > 0) {
32
          image.attr('src', e.target.result);
33
        } else {
34
          var img = $('<img class="ui small bordered image"/>');
35
          img.attr('src', e.target.result);
36
          $(input).parent().before(img);
37
        }
38
      };
39
40
      reader.readAsDataURL(input.files[0]);
41
    }
42
  }
43
})( jQuery );
44
45
(function($) {
46
  $(document).ready(function () {
47
    $(document).previewUploadedImage('.odiseo-image')
48
  });
49
})(jQuery);
50