<commit_before>// variant autocompletion

$(document).ready(function() {

  initAlert()
  initConfirm()
  initCancelOrder()

  if ($('#variant_autocomplete_template').length > 0) {
    window.variantTemplate = Handlebars.compile($('#variant_autocomplete_template').text());
    window.variantStockTemplate = Handlebars.compile($('#variant_autocomplete_stock_template').text());

    // handle variant selection, show stock level.
    $('#add_variant_id').change(function(){
      var variant_id = $(this).val();

      var variant = _.find(window.variants, function(variant){
        return variant.id == variant_id
      })
      $('#stock_details').html(variantStockTemplate({variant: variant}));
      $('#stock_details').show();

      $('button.add_variant').click(addVariantFromStockLocation);

      // Add some tips
      $('.with-tip').powerTip({
        smartPlacement: true,
        fadeInTime: 50,
        fadeOutTime: 50,
        intentPollInterval: 300
      });

    });

    //handle edit click
    $('a.edit-item').click(toggleItemEdit);

    //handle cancel click
    $('a.cancel-item').click(toggleItemEdit);

    handle_save_click = function(){
      var save = $(this);
      var shipment_number = save.data('shipment-number');
      var variant_id = save.data('variant-id');

      var quantity = parseInt(save.parents('tr').find('input.line_item_quantity').val());
      var maxQuantity = parseInt(save.parents('tr').find('input.line_item_quantity').attr("max"));

      if (quantity > maxQuantity) {
        quantity = maxQuantity;
        save.parents('tr').find('input.line_item_quantity').val(maxQuantity);
        ofnAlert(t("js.admin.orders.quantity_adjusted"));
      }
      toggleItemEdit();

      adjustItems(shipment_number, variant_id, quantity, true);
      return false;
    }
    $('a.save-item').click(handle_save_click);

    handle_delete_click = function(elementSelector, restock_item){
      var del = $(elementSelector);
      del.hide()
      var shipment_number = del.data('shipment-number');
      var variant_id = del.data('variant-id');

      toggleItemEdit();

      adjustItems(shipment_number, variant_id, 0, restock_item);
    }

    $('a.delete-item').click((event) => {
     try {
         var del = $('a.delete-item');
         var shipment_number = del.data('shipment-number');
         var variant_id = del.data('variant-id');
         var shipment = _.findWhere(shipments, {number: shipment_number + ''});
         var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});
         if (inventory_units.length !== shipment.inventory_units.length) {
             ofnConfirm((reStockItem) => {
                 handle_delete_click('#custom-confirm', reStockItem);
             });
         } else {
             adjustItems(shipment_number, variant_id, 0);
         }
     } catch (e) {
     }
    });
  }
});

adjustItems = function(shipment_number, variant_id, quantity, restock_item){
  var shipment = _.findWhere(shipments, {number: shipment_number + ''});
  var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});

  if (quantity === 0 && inventory_units.length === shipment.inventory_units.length) {
    ofnCancelOrderAlert((confirm, sendEmailCancellation, restock_item) => {
      if (confirm) {
        doAdjustItems(shipment_number, variant_id, quantity, inventory_units, restock_item, () => {
          var redirectTo = new URL(Spree.routes.cancel_order.toString());
          redirectTo.searchParams.append("send_cancellation_email", sendEmailCancellation);
          window.location.href = redirectTo.toString();
        });
      }
    });
    return;
  }
  doAdjustItems(shipment_number, variant_id, quantity, inventory_units, restock_item, () => {
    window.location.reload();
  });
}

doAdjustItems = function(shipment_number, variant_id, quantity, inventory_units, restock_item, callback) {
  var url = Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number;

  var new_quantity = 0;
  var data = { variant_id: variant_id };
  if (inventory_units.length < quantity) {
    url += "/add";
    new_quantity = (quantity - inventory_units.length);
  } else if (inventory_units.length > quantity) {
    url += "/remove"
    new_quantity = (inventory_units.length - quantity);
    data.restock_item = restock_item;
  }
  url += '.json';
  data.quantity = new_quantity;

  if (new_quantity == 0) {
    ofnAlert(t("js.admin.orders.quantity_unchanged"));
  } else {
    $.ajax({
      type: "PUT",
      url: Spree.url(url),
      data: data
    }).done(function( msg ) {
      callback();
    });
  }
}

toggleTrackingEdit = function(){
  var link = $(this);
  let input = link.parents('tbody').find('#tracking')[0]
  link.parents('tbody').find('tr.edit-tracking').toggle();
  // Set focus on input and
  // put cursor at it's end
  input.focus()
  input.setSelectionRange(-1, -1)
  link.parents('tbody').find('tr.show-tracking').toggle();
}

toggleNoteEdit = function(){
  var link = $(this);
  let input = link.parents('tbody').find('#note')[0]
  link.parents('tbody').find('tr.edit-note').toggle();
  // Set focus on input and
  // put cursor at it's end
  input.focus()
  input.setSelectionRange(-1, -1)
  link.parents('tbody').find('tr.show-note').toggle();
}<commit_msg>Refactor: create parent_node var to find child elements<commit_after>// variant autocompletion

$(document).ready(function() {

  initAlert()
  initConfirm()
  initCancelOrder()

  if ($('#variant_autocomplete_template').length > 0) {
    window.variantTemplate = Handlebars.compile($('#variant_autocomplete_template').text());
    window.variantStockTemplate = Handlebars.compile($('#variant_autocomplete_stock_template').text());

    // handle variant selection, show stock level.
    $('#add_variant_id').change(function(){
      var variant_id = $(this).val();

      var variant = _.find(window.variants, function(variant){
        return variant.id == variant_id
      })
      $('#stock_details').html(variantStockTemplate({variant: variant}));
      $('#stock_details').show();

      $('button.add_variant').click(addVariantFromStockLocation);

      // Add some tips
      $('.with-tip').powerTip({
        smartPlacement: true,
        fadeInTime: 50,
        fadeOutTime: 50,
        intentPollInterval: 300
      });

    });

    //handle edit click
    $('a.edit-item').click(toggleItemEdit);

    //handle cancel click
    $('a.cancel-item').click(toggleItemEdit);

    handle_save_click = function(){
      var save = $(this);
      var shipment_number = save.data('shipment-number');
      var variant_id = save.data('variant-id');

      var quantity = parseInt(save.parents('tr').find('input.line_item_quantity').val());
      var maxQuantity = parseInt(save.parents('tr').find('input.line_item_quantity').attr("max"));

      if (quantity > maxQuantity) {
        quantity = maxQuantity;
        save.parents('tr').find('input.line_item_quantity').val(maxQuantity);
        ofnAlert(t("js.admin.orders.quantity_adjusted"));
      }
      toggleItemEdit();

      adjustItems(shipment_number, variant_id, quantity, true);
      return false;
    }
    $('a.save-item').click(handle_save_click);

    handle_delete_click = function(elementSelector, restock_item){
      var del = $(elementSelector);
      del.hide()
      var shipment_number = del.data('shipment-number');
      var variant_id = del.data('variant-id');

      toggleItemEdit();

      adjustItems(shipment_number, variant_id, 0, restock_item);
    }

    $('a.delete-item').click((event) => {
     try {
         var del = $('a.delete-item');
         var shipment_number = del.data('shipment-number');
         var variant_id = del.data('variant-id');
         var shipment = _.findWhere(shipments, {number: shipment_number + ''});
         var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});
         if (inventory_units.length !== shipment.inventory_units.length) {
             ofnConfirm((reStockItem) => {
                 handle_delete_click('#custom-confirm', reStockItem);
             });
         } else {
             adjustItems(shipment_number, variant_id, 0);
         }
     } catch (e) {
     }
    });
  }
});

adjustItems = function(shipment_number, variant_id, quantity, restock_item){
  var shipment = _.findWhere(shipments, {number: shipment_number + ''});
  var inventory_units = _.where(shipment.inventory_units, {variant_id: variant_id});

  if (quantity === 0 && inventory_units.length === shipment.inventory_units.length) {
    ofnCancelOrderAlert((confirm, sendEmailCancellation, restock_item) => {
      if (confirm) {
        doAdjustItems(shipment_number, variant_id, quantity, inventory_units, restock_item, () => {
          var redirectTo = new URL(Spree.routes.cancel_order.toString());
          redirectTo.searchParams.append("send_cancellation_email", sendEmailCancellation);
          window.location.href = redirectTo.toString();
        });
      }
    });
    return;
  }
  doAdjustItems(shipment_number, variant_id, quantity, inventory_units, restock_item, () => {
    window.location.reload();
  });
}

doAdjustItems = function(shipment_number, variant_id, quantity, inventory_units, restock_item, callback) {
  var url = Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number;

  var new_quantity = 0;
  var data = { variant_id: variant_id };
  if (inventory_units.length < quantity) {
    url += "/add";
    new_quantity = (quantity - inventory_units.length);
  } else if (inventory_units.length > quantity) {
    url += "/remove"
    new_quantity = (inventory_units.length - quantity);
    data.restock_item = restock_item;
  }
  url += '.json';
  data.quantity = new_quantity;

  if (new_quantity == 0) {
    ofnAlert(t("js.admin.orders.quantity_unchanged"));
  } else {
    $.ajax({
      type: "PUT",
      url: Spree.url(url),
      data: data
    }).done(function( msg ) {
      callback();
    });
  }
}

toggleTrackingEdit = function(){
  var link = $(this);
  var parent_node = link.parents('tbody')
  let input = parent_node.find('#tracking')[0]
  parent_node.find('tr.edit-tracking').toggle();
  // Set focus on input and
  // put cursor at it's end
  input.focus()
  input.setSelectionRange(-1, -1)
  parent_node.find('tr.show-tracking').toggle();
}

toggleNoteEdit = function(){
  var link = $(this);
  var parent_node = link.parents('tbody')
  let input = parent_node.find('#note')[0]
  parent_node.find('tr.edit-note').toggle();
  // Set focus on input and
  // put cursor at it's end
  input.focus()
  input.setSelectionRange(-1, -1)
  parent_node.find('tr.show-note').toggle();
}
{'fraction_non_alphanumeric': 0.1531203288490284, 'fraction_numerical': 0.0035500747384155456, 'mean_word_length': 3.334008097165992, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2770297', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3218, 'n_words': 587}