<commit_before>{% extends 'os2webscanner/base.html' %}
{% block topnav %}{% endblock %}
{% block rendertype %}iframe-modal{% endblock %}

{% block jquery_script %}
<script type="text/javascript">
  (function($) {
    // adding a new expression
    $("#button-add-expression").click(function() {
      var inputContainer = $("#{{ form.pattern_0.auto_id }}_container").find(".form-group").last(); // Take the last item instead of just number _1, as we may have added more items
      var inputNameParts = inputContainer.find("input[type=\"text\"]").first().attr("name").split(/(.+_)/); // define .+_ as capturing group so we can get at the delimiter inside nameParts[1]
      var baseName = inputNameParts[1];
      var numInput = parseInt(inputNameParts[2]) + 1;
      var newInputName = baseName + (numInput);
      var containerClone = inputContainer.clone();
      var inputClone = containerClone.find("input[type=\"text\"]").first().prop("required", false);
      var labelClone = containerClone.find("label").first();

      labelClone.attr("for", "id_" + newInputName);
      labelClone.text("{{ form.pattern_0.label }}" + " " + (numInput + 1)); // plus one because the numInput is 0-indexed, and label should be 1-indexed
      inputClone.attr("name", newInputName).attr("id", "id_" + newInputName).val("").attr("value", "");
      if(!containerClone.find(".button-remove-expression").length) { // if we haven't already cloned a button for removal...
        inputClone.after($("<span/>", { // ... add the button to remove expression
          class: "input-group-btn",
          html: $("<button/>", {
            class: "btn btn-default button-remove-expression",
            type: "button",
            text: "Fjern udtryk"
          })
        }));
        inputClone.next(".input-group-btn").addBack().wrapAll("<div class=\"input-group\"></div>"); // wrap it in the input-group wrapper for proper Bootstrap styling
      }
      inputContainer.after(containerClone);
      recalcIframeHeight();
    });

    // removing an expression. We need to bind dynamically as fields do not necessarily exist on page load
    $("body").on("click", ".button-remove-expression", function() {
      var btn = $(this);
      var inputValue = btn.closest(".input-group-btn").prev("input").val();
      if(inputValue) {
        var confirmation = confirm("Feltet indeholder en værdi. Tryk OK hvis du stadig vil slette feltet.");
        if(confirmation) {
          btn.closest(".form-group").remove();
        }
      } else {
        btn.closest(".form-group").remove();
      }
      cleanupPatternNumbers();
    });

    $("form").submit(function() {
      // delete any empty expression fields, BUT not the first one
      $("#{{ form.pattern_0.auto_id }}_container").find("input[type=\"text\"]").filter(function(idx) {
        return !this.value && idx > 0;
      }).closest(".pattern-container").remove();

      // clean up the pattern numbers on form submit, just to be safe (even though we already did it when adding a new expression field).
      cleanupPatternNumbers();
    });

    // function to clean up pattern numbers, as we need to have them numbered sequentially without gaps
    function cleanupPatternNumbers() {
      $("#{{ form.pattern_0.auto_id }}_container").find(".form-group").each(function(idx) {
        var $this = $(this);
        var input = $this.find("input[type=\"text\"]");
        var label = $this.find("label");
        var oldInputName = input.attr("name").split(/(.+_)/);
        input.attr("name", oldInputName[1] + idx).attr("id", "id_" + oldInputName[1] + idx);
        label.attr("for", "id_" + oldInputName[1] + idx).text("{{ form.pattern_0.label }} " + (idx + 1)); // we want 1-indexed label texts
      });
      recalcIframeHeight();
    }
  })(jQuery);<commit_msg>Fixed JavaScript so that correct wrapper element containing empty input field is deleted<commit_after>{% extends 'os2webscanner/base.html' %}
{% block topnav %}{% endblock %}
{% block rendertype %}iframe-modal{% endblock %}

{% block jquery_script %}
<script type="text/javascript">
  (function($) {
    // adding a new expression
    $("#button-add-expression").click(function() {
      var inputContainer = $("#{{ form.pattern_0.auto_id }}_container").find(".form-group").last(); // Take the last item instead of just number _1, as we may have added more items
      var inputNameParts = inputContainer.find("input[type=\"text\"]").first().attr("name").split(/(.+_)/); // define .+_ as capturing group so we can get at the delimiter inside nameParts[1]
      var baseName = inputNameParts[1];
      var numInput = parseInt(inputNameParts[2]) + 1;
      var newInputName = baseName + (numInput);
      var containerClone = inputContainer.clone();
      var inputClone = containerClone.find("input[type=\"text\"]").first().prop("required", false);
      var labelClone = containerClone.find("label").first();

      labelClone.attr("for", "id_" + newInputName);
      labelClone.text("{{ form.pattern_0.label }}" + " " + (numInput + 1)); // plus one because the numInput is 0-indexed, and label should be 1-indexed
      inputClone.attr("name", newInputName).attr("id", "id_" + newInputName).val("").attr("value", "");
      if(!containerClone.find(".button-remove-expression").length) { // if we haven't already cloned a button for removal...
        inputClone.after($("<span/>", { // ... add the button to remove expression
          class: "input-group-btn",
          html: $("<button/>", {
            class: "btn btn-default button-remove-expression",
            type: "button",
            text: "<NAME>"
          })
        }));
        inputClone.next(".input-group-btn").addBack().wrapAll("<div class=\"input-group\"></div>"); // wrap it in the input-group wrapper for proper Bootstrap styling
      }
      inputContainer.after(containerClone);
      recalcIframeHeight();
    });

    // removing an expression. We need to bind dynamically as fields do not necessarily exist on page load
    $("body").on("click", ".button-remove-expression", function() {
      var btn = $(this);
      var inputValue = btn.closest(".input-group-btn").prev("input").val();
      if(inputValue) {
        var confirmation = confirm("Feltet indeholder en værdi. Tryk OK hvis du stadig vil slette feltet.");
        if(confirmation) {
          btn.closest(".form-group").remove();
        }
      } else {
        btn.closest(".form-group").remove();
      }
      cleanupPatternNumbers();
    });

    $("form").submit(function() {
      // delete any empty expression fields, BUT not the first one
      $("#{{ form.pattern_0.auto_id }}_container").find("input[type=\"text\"]").filter(function(idx) {
        return !this.value && idx > 0;
      }).closest(".form-group").remove();

      // clean up the pattern numbers on form submit, just to be safe (even though we already did it when adding a new expression field).
      cleanupPatternNumbers();
    });

    // function to clean up pattern numbers, as we need to have them numbered sequentially without gaps
    function cleanupPatternNumbers() {
      $("#{{ form.pattern_0.auto_id }}_container").find(".form-group").each(function(idx) {
        var $this = $(this);
        var input = $this.find("input[type=\"text\"]");
        var label = $this.find("label");
        var oldInputName = input.attr("name").split(/(.+_)/);
        input.attr("name", oldInputName[1] + idx).attr("id", "id_" + oldInputName[1] + idx);
        label.attr("for", "id_" + oldInputName[1] + idx).text("{{ form.pattern_0.label }} " + (idx + 1)); // we want 1-indexed label texts
      });
      recalcIframeHeight();
    }
  })(jQuery);
{'fraction_non_alphanumeric': 0.17104920593595418, 'fraction_numerical': 0.005206977349648529, 'mean_word_length': 3.069385593220339, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, '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': '6101508', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2187, 'n_words': 580}