0

I need to clean my code because of code climate issues. This jquery is too similar. How can I fix this?

  var angle = 0;
  $('.rotate-receipt').on('click', function () {
      var index = $(this).data('button-index');
      angle = (angle + 90)%360;
      var className = 'rotate' + angle;
      $('#receipt-image-'+index).removeClass().addClass(className);
  });

  $('.zoom-in').on('click', function () {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      image.width(image.width() + 100);
  });

  $('.zoom-out').on('click', function () {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      image.width(image.width() - 100);
  });
});
J. Doe
  • 65
  • 1
  • 5

5 Answers5

1

For zoom in and zoom out you can try this:

$('.zoom-in,.zoom-out').on('click', function() {
      var index = $(this).data('button-index');
      var image = $('#receipt-image-'+index);
      if ($(this).hasClass('zoom-out')) {
         image.width(image.width() - 100);
      }
      else {
         image.width(image.width() + 100);
      }
});
Prem Raj
  • 849
  • 4
  • 15
0
var angle = 0;
$('.rotate-receipt,.zoom-in,.zoom-out').on('click', function () {
  var index = $(this).data('button-index');
  var image = $('#receipt-image-'+index);

  if($(this).hasClass("rotate-receipt")) {
    angle = (angle + 90)%360;
    var className = 'rotate' + angle;
    $('#receipt-image-'+index).removeClass().addClass(className);
  }
  else if($(this).hasClass("zoom-in")) {
    image.width(image.width() + 100);
  }
  else if($(this).hasClass("zoom-out")) {
    image.width(image.width() - 100);
  }

});

Hope it will be useful :)

priya_singh
  • 2,478
  • 1
  • 14
  • 32
0

Might as well throw in my 2 cents...

var angle = 0;
$('.rotate-receipt').on('click', function () {
  transform( this, 'rotate-receipt' );
});

$('.zoom-in').on('click', function () {
  transform( this, 'zoom-in' );
});

$('.zoom-out').on('click', function () {
  transform( this, 'zoom-out' );
});

function transform( el, type ) {

  var index = $(el).data('button-index');
  var image = $('#receipt-image-'+index);

  switch( type ) {

    case 'rotate-receipt':
      angle = (angle + 90)%360;
      var className = 'rotate' + angle;
      $('#receipt-image-'+index).removeClass().addClass(className);
      break;

    case 'zoom-in':
      image.width(image.width() + 100);
      break;

    case 'zoom-out':
      image.width(image.width() - 100);
      break;

  }

}
Rob Wood
  • 415
  • 5
  • 11
0
    $('.rotate-receipt, .zoom-in, .zoom-out').on('click' , function() {
        var index = $(this).data('button-index');
        if ($(this).hasClass('rotate-receipt')) {
            angle = (angle + 90)%360;
            var className = 'rotate' + angle;
            $('#receipt-image-'+index).removeClass().addClass(className);
        } else {
            var image = $('#receipt-image-'+index);
            var toAdd = $(this).hasClass('zoom-out') ? - 100 : 100;
            image.width(image.width() + toAdd);
        }
    })
Beginner
  • 4,118
  • 3
  • 17
  • 26
0

You can reduce the code of zoom in and out into one function. Here is the code:

$('.zoom-in').on('click', zoom_in_out(100));
$('.zoom-out').on('click', zoom_in_out(-100));

function zoom_in_out(zoom_value) {
  var index = $(this).data('button-index');
  var image = $('#receipt-image-'+index);
  image.width(image.width() + zoom_value);
});

You should try to reduce the code and not repeat yourself. This is a better approach not only beacuse it has less code but becasue the similar function is placed in one location. This way if you need to include some other common action, you just need to add in one place. It is more about better coding practice than just reducing the number of lines of code.

iiR
  • 707
  • 1
  • 6
  • 21