<commit_before>function jQuerySuggest(timestamp){
  var cb_name = retrieveCbName(timestamp);
  populateVersionBoxContent(timestamp, cb_name);
  document.getElementById("cookbook_version_" + timestamp).value = "0.0.0";
}

function populateVersionBoxContent(timestamp, cb_name){
  $.getJSON('/cookbooks/'+cb_name, function(result){
                                     jQuery('#cookbook_version_'+timestamp).suggest(result[cb_name]);
                                   });
}

function clearVersionBox(box, timestamp){
  populateVersionBoxContent(timestamp, retrieveCbName(timestamp));
  //if (box.value == "0.0.0"){box.value = "";}
  error_message = document.getElementById('inline_error_message_' + timestamp);
  if (error_message != null)
    $(error_message).remove();
}

function validateVersionBoxValue(box, timestamp){
  if (box.value.match(/^\d+\.\d+\.\d+$/) == null){
    if (box.value.length != 0 && document.getElementById('inline_error_message_' + timestamp) == null)
      $(box).parent().append('<span class="inline_error_message" id="inline_error_message_' + timestamp + '" >Invalid version format. The version should be in the format of 0.0.0.</span>');
      if (box.value.length==0)
        box.value = "0.0.0";
  }
}

function buildCookbookList(cookbook_names, default_cookbook){
  if (default_cookbook != null && $.inArray(default_cookbook, cookbook_names) < 0){
    cookbook_names.push(default_cookbook);
  }
  var result = '<option value=""></option>';
  for(i=0; i<cookbook_names.length; i++){
    result += '<option value=' + '"' + cookbook_names[i] + '" ';
    if (cookbook_names[i] == default_cookbook)
      result += 'selected=true>' + cookbook_names[i] + '</option>';
    else
      result += '>' + cookbook_names[i] + '</option>';
  }
  return result;
}

function buildOperatorList(default_operator){
  return '<option value="&gt;="' +  (default_operator == ">=" ? "selected=true" : "") + '>&gt;=</option>'
       + '<option value="&gt;"' +  (default_operator == ">" ? "selected=true" : "" )+ '>&gt;</option>'
       + '<option value="="' + (default_operator == "=" ? "selected=true" : "") +'>=</option>'
       + '<option value="&lt;"' + (default_operator == "<" ? "selected=true" : "") + '>&lt;</option>'
       + '<option value="&lt;="' + (default_operator == "<=" ? "selected=true" : "") + '>&lt;=</option>'
       + '<option value="~&gt;"' + (default_operator == "~>" ? "selected=true" : "") + '>~&gt;</option>';
}

function retrieveCbName(timestamp){
  var select_box = document.getElementById("cookbook_name_" + timestamp);
  return select_box.options[select_box.selectedIndex].value;
}

function addTableRow(default_cookbook, default_operator, default_version){
    var cookbook_names_string = document.getElementById('cbVerPickerTable').getAttribute('data-cookbook_names');
    var cookbook_names = cookbook_names_string.substring(2, cookbook_names_string.length-2).split('","');
    var timestamp = new Date().getTime();
    var row = '<tr id=' + '"' + timestamp + '"><td>' + '<select size="1" name="cookbook_name_' + timestamp + '" ' + 'id="cookbook_name_' + timestamp + '" class="cookbook_version_constraints_cb_name" onchange="jQuerySuggest(' + timestamp + ')"'+'>'
            + buildCookbookList(cookbook_names, default_cookbook) + '</select>'
            + '</td>'
            + '<td><select name="operator_' + timestamp + '">' + buildOperatorList(default_operator) + '</select></td>'
            + '<td><input class="text" name="cookbook_version_' + timestamp +'" ' + 'id="cookbook_version_' + timestamp + '" ' + 'type="text" onfocus="clearVersionBox(this,' + timestamp + ')" onblur="validateVersionBoxValue(this,' + timestamp + ')" value="' + default_version + '"></td>'
            + '<td><a href="javascript::void(0)" onclick="removeTableRow($(this).parent().parent())">Remove</a></td>'
            + '</tr>';
    $("#cbVerPickerTable tbody").append(row);
    validateVersionBoxValue(document.getElementById("cookbook_version_" + timestamp));
}

function removeTableRow(row){
    row.remove();
}<commit_msg>CHEF-2039 Not displaying empty array as a cookbook name in the drop down menu on the environment create/edit form<commit_after>function jQuerySuggest(timestamp){
  var cb_name = retrieveCbName(timestamp);
  populateVersionBoxContent(timestamp, cb_name);
  document.getElementById("cookbook_version_" + timestamp).value = "0.0.0";
}

function populateVersionBoxContent(timestamp, cb_name){
  $.getJSON('/cookbooks/'+cb_name, function(result){
                                     jQuery('#cookbook_version_'+timestamp).suggest(result[cb_name]);
                                   });
}

function clearVersionBox(box, timestamp){
  populateVersionBoxContent(timestamp, retrieveCbName(timestamp));
  //if (box.value == "0.0.0"){box.value = "";}
  error_message = document.getElementById('inline_error_message_' + timestamp);
  if (error_message != null)
    $(error_message).remove();
}

function validateVersionBoxValue(box, timestamp){
  if (box.value.match(/^\d+\.\d+\.\d+$/) == null){
    if (box.value.length != 0 && document.getElementById('inline_error_message_' + timestamp) == null)
      $(box).parent().append('<span class="inline_error_message" id="inline_error_message_' + timestamp + '" >Invalid version format. The version should be in the format of 0.0.0.</span>');
      if (box.value.length==0)
        box.value = "0.0.0";
  }
}

function buildCookbookList(cookbook_names, default_cookbook){
  if (default_cookbook != null && $.inArray(default_cookbook, cookbook_names) < 0){
    cookbook_names.push(default_cookbook);
  }
  var result = '<option value=""></option>';
  for(i=0; i<cookbook_names.length; i++){
    result += '<option value=' + '"' + cookbook_names[i] + '" ';
    if (cookbook_names[i] == default_cookbook)
      result += 'selected=true>' + cookbook_names[i] + '</option>';
    else
      result += '>' + cookbook_names[i] + '</option>';
  }
  return result;
}

function buildOperatorList(default_operator){
  return '<option value="&gt;="' +  (default_operator == ">=" ? "selected=true" : "") + '>&gt;=</option>'
       + '<option value="&gt;"' +  (default_operator == ">" ? "selected=true" : "" )+ '>&gt;</option>'
       + '<option value="="' + (default_operator == "=" ? "selected=true" : "") +'>=</option>'
       + '<option value="&lt;"' + (default_operator == "<" ? "selected=true" : "") + '>&lt;</option>'
       + '<option value="&lt;="' + (default_operator == "<=" ? "selected=true" : "") + '>&lt;=</option>'
       + '<option value="~&gt;"' + (default_operator == "~>" ? "selected=true" : "") + '>~&gt;</option>';
}

function retrieveCbName(timestamp){
  var select_box = document.getElementById("cookbook_name_" + timestamp);
  return select_box.options[select_box.selectedIndex].value;
}

function addTableRow(default_cookbook, default_operator, default_version){
    var cookbook_names_string = document.getElementById('cbVerPickerTable').getAttribute('data-cookbook_names');
    var cookbook_names = cookbook_names_string.substring(2, cookbook_names_string.length-2).split('","');
    if (cookbook_names[0] == "[]")
      cookbook_names = [];
    var timestamp = new Date().getTime();
    var row = '<tr id=' + '"' + timestamp + '"><td>' + '<select size="1" name="cookbook_name_' + timestamp + '" ' + 'id="cookbook_name_' + timestamp + '" class="cookbook_version_constraints_cb_name" onchange="jQuerySuggest(' + timestamp + ')"'+'>'
            + buildCookbookList(cookbook_names, default_cookbook) + '</select>'
            + '</td>'
            + '<td><select name="operator_' + timestamp + '">' + buildOperatorList(default_operator) + '</select></td>'
            + '<td><input class="text" name="cookbook_version_' + timestamp +'" ' + 'id="cookbook_version_' + timestamp + '" ' + 'type="text" onfocus="clearVersionBox(this,' + timestamp + ')" onblur="validateVersionBoxValue(this,' + timestamp + ')" value="' + default_version + '"></td>'
            + '<td><a href="javascript::void(0)" onclick="removeTableRow($(this).parent().parent())">Remove</a></td>'
            + '</tr>';
    $("#cbVerPickerTable tbody").append(row);
    validateVersionBoxValue(document.getElementById("cookbook_version_" + timestamp));
}

function removeTableRow(row){
    row.remove();
}
{'fraction_non_alphanumeric': 0.19492140266021765, 'fraction_numerical': 0.005441354292623942, 'mean_word_length': 4.455804749340369, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2971422', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2521, 'n_words': 425}