<commit_before>/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/* Namespace for core functionality related to modal dialogs.
 *
 * Modals in Horizon are treated as a "stack", e.g new ones are added to the
 * top of the stack, and they are always removed in a last-in-first-out
 * order. This allows for things like swapping between modals as part of a
 * workflow, for confirmations, etc.
 *
 * When a new modal is loaded into the DOM, it fires a "new_modal" event which
 * event handlers can listen for. However, for consistency, it is better to
 * add methods which should be run on instantiation of any new modal to be
 * applied via the horizon.modals.addModalInitFunction method.
 */
horizon.modals = {
  // Storage for our current jqXHR object.
  _request: null,
  spinner: null,
  progress_bar: null,
  _init_functions: []
};

horizon.modals.addModalInitFunction = function (f) {
  horizon.modals._init_functions.push(f);
};

horizon.modals.initModal = function (modal) {
  $(horizon.modals._init_functions).each(function (index, f) {
    f(modal);
  });
};

/* Creates a modal dialog from the client-side template. */
horizon.modals.create = function (title, body, confirm, cancel) {
  if (!cancel) {
    cancel = gettext("Cancel");
  }
  var template = horizon.templates.compiled_templates["#modal_template"],
    params = {
      title: title, body: body, confirm: confirm, cancel: cancel,
      modal_backdrop: horizon.modals.MODAL_BACKDROP
    };
  return $(template.render(params)).appendTo("#modal_wrapper");
};

horizon.modals.success = function (data) {
  var modal;
  var modal_wrapper = $('#modal_wrapper');
  // Moves the modal_wrapper to the bottom. This shows it over other dialogs.
  modal_wrapper.parent().append(modal_wrapper);
  modal_wrapper.append(data);
  modal = $('#modal_wrapper > .modal:last');
  modal.modal();
  $(modal).trigger("new_modal", modal);
  return modal;
};

horizon.modals.modal_spinner = function (text) {
  // Adds a spinner with the desired text in a modal window.
  var template = horizon.templates.compiled_templates["#spinner-modal"];
  horizon.modals.spinner = $(template.render({text: text}));
  horizon.modals.spinner.appendTo("#modal_wrapper");
  horizon.modals.spinner.modal({backdrop: 'static'});
  horizon.modals.spinner.find(".modal-body").spin(horizon.conf.spinner_options.modal);
};

horizon.modals.progress_bar = function (text) {
  var template = horizon.templates.compiled_templates["#progress-modal"];
  horizon.modals.bar = $(template.render({text: text}))
    .appendTo("#modal_wrapper");
  horizon.modals.bar.modal({backdrop: 'static'});

  var $progress_bar = horizon.modals.bar.find('.progress-bar');
  horizon.modals.progress_bar.update = function(fraction) {
    var percent = Math.round(100 * fraction) + '%';
    $progress_bar
      .css('width', Math.round(100 * fraction) + '%')
      .parents('.progress-text').find('.progress-bar-text').text(percent);
  };
};

horizon.modals.init_wizard = function () {
  // If workflow is in wizard mode, initialize wizard.
  var _max_visited_step = 0;
  var _validate_steps = function (start, end) {
    var $form = $('.workflow > form'),
      response = {};

    if (typeof end === 'undefined') {
      end = start;
    }

    // Clear old errors.
    $form.find('div.row div.alert-danger').remove();
    $form.find('.form-group.has-error').each(function () {
      var $group = $(this);
      $group.removeClass('has-error');
      $group.find('span.help-block.alert').remove();
    });

    // Temporarilly remove "disabled" attribute to get the values serialized
    var disabled = $form.find('select[disabled="disabled"]').removeAttr("disabled");

    // Send the data for validation.
    $.ajax({
      type: 'POST',
      url: $form.attr('action'),
      headers: {
        'X-Horizon-Validate-Step-Start': start,
        'X-Horizon-Validate-Step-End': end
      },
      data: $form.serialize(),
      dataType: 'json',
      async: false,
      success: function (data) { response = data; }
    });

    // Re-enable the "disabled" attribute on disabled inputs.
    disabled.attr("disabled", "disabled");

    // Handle errors.
    if (response.has_errors) {
      var first_field = true;

      $.each(response.errors, function (step_slug, step_errors) {
        var step_id = response.workflow_slug + '__' + step_slug,
          $fieldset = $form.find('#' + step_id);
        $.each(step_errors, function (field, errors) {
          var $field;
          if (field === '__all__') {
            // Add global errors.
            $.each(errors, function (index, error) {
              $fieldset.find('div.row').prepend(
                '<div class="alert alert-danger">' +
                error + '</div>');
            });
            $fieldset.find('input,  select, textarea').first().focus();
            return;
          }
          // Add field errors.
          $field = $fieldset.find('[name="' + field + '"]');
          $field.closest('.form-group').addClass('has-error');
          $.each(errors, function (index, error) {
            $field.after(<commit_msg>Add col-sm-12 to network modal error<commit_after>/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

/* Namespace for core functionality related to modal dialogs.
 *
 * Modals in Horizon are treated as a "stack", e.g new ones are added to the
 * top of the stack, and they are always removed in a last-in-first-out
 * order. This allows for things like swapping between modals as part of a
 * workflow, for confirmations, etc.
 *
 * When a new modal is loaded into the DOM, it fires a "new_modal" event which
 * event handlers can listen for. However, for consistency, it is better to
 * add methods which should be run on instantiation of any new modal to be
 * applied via the horizon.modals.addModalInitFunction method.
 */
horizon.modals = {
  // Storage for our current jqXHR object.
  _request: null,
  spinner: null,
  progress_bar: null,
  _init_functions: []
};

horizon.modals.addModalInitFunction = function (f) {
  horizon.modals._init_functions.push(f);
};

horizon.modals.initModal = function (modal) {
  $(horizon.modals._init_functions).each(function (index, f) {
    f(modal);
  });
};

/* Creates a modal dialog from the client-side template. */
horizon.modals.create = function (title, body, confirm, cancel) {
  if (!cancel) {
    cancel = gettext("Cancel");
  }
  var template = horizon.templates.compiled_templates["#modal_template"],
    params = {
      title: title, body: body, confirm: confirm, cancel: cancel,
      modal_backdrop: horizon.modals.MODAL_BACKDROP
    };
  return $(template.render(params)).appendTo("#modal_wrapper");
};

horizon.modals.success = function (data) {
  var modal;
  var modal_wrapper = $('#modal_wrapper');
  // Moves the modal_wrapper to the bottom. This shows it over other dialogs.
  modal_wrapper.parent().append(modal_wrapper);
  modal_wrapper.append(data);
  modal = $('#modal_wrapper > .modal:last');
  modal.modal();
  $(modal).trigger("new_modal", modal);
  return modal;
};

horizon.modals.modal_spinner = function (text) {
  // Adds a spinner with the desired text in a modal window.
  var template = horizon.templates.compiled_templates["#spinner-modal"];
  horizon.modals.spinner = $(template.render({text: text}));
  horizon.modals.spinner.appendTo("#modal_wrapper");
  horizon.modals.spinner.modal({backdrop: 'static'});
  horizon.modals.spinner.find(".modal-body").spin(horizon.conf.spinner_options.modal);
};

horizon.modals.progress_bar = function (text) {
  var template = horizon.templates.compiled_templates["#progress-modal"];
  horizon.modals.bar = $(template.render({text: text}))
    .appendTo("#modal_wrapper");
  horizon.modals.bar.modal({backdrop: 'static'});

  var $progress_bar = horizon.modals.bar.find('.progress-bar');
  horizon.modals.progress_bar.update = function(fraction) {
    var percent = Math.round(100 * fraction) + '%';
    $progress_bar
      .css('width', Math.round(100 * fraction) + '%')
      .parents('.progress-text').find('.progress-bar-text').text(percent);
  };
};

horizon.modals.init_wizard = function () {
  // If workflow is in wizard mode, initialize wizard.
  var _max_visited_step = 0;
  var _validate_steps = function (start, end) {
    var $form = $('.workflow > form'),
      response = {};

    if (typeof end === 'undefined') {
      end = start;
    }

    // Clear old errors.
    $form.find('div.row div.alert-danger').remove();
    $form.find('.form-group.has-error').each(function () {
      var $group = $(this);
      $group.removeClass('has-error');
      $group.find('span.help-block.alert').remove();
    });

    // Temporarilly remove "disabled" attribute to get the values serialized
    var disabled = $form.find('select[disabled="disabled"]').removeAttr("disabled");

    // Send the data for validation.
    $.ajax({
      type: 'POST',
      url: $form.attr('action'),
      headers: {
        'X-Horizon-Validate-Step-Start': start,
        'X-Horizon-Validate-Step-End': end
      },
      data: $form.serialize(),
      dataType: 'json',
      async: false,
      success: function (data) { response = data; }
    });

    // Re-enable the "disabled" attribute on disabled inputs.
    disabled.attr("disabled", "disabled");

    // Handle errors.
    if (response.has_errors) {
      var first_field = true;

      $.each(response.errors, function (step_slug, step_errors) {
        var step_id = response.workflow_slug + '__' + step_slug,
          $fieldset = $form.find('#' + step_id);
        $.each(step_errors, function (field, errors) {
          var $field;
          if (field === '__all__') {
            // Add global errors.
            $.each(errors, function (index, error) {
              $fieldset.find('div.row').prepend(
                '<div class="col-sm-12"><div class="alert alert-danger">' +
                error + '</div></div>');
            });
            $fieldset.find('input,  select, textarea').first().focus();
            return;
          }
          // Add field errors.
          $field = $fieldset.find('[name="' + field + '"]');
          $field.closest('.form-group').addClass('has-error');
          $.each(errors, function (index, error) {
            $field.after(
{'fraction_non_alphanumeric': 0.14280645733546213, 'fraction_numerical': 0.0023061912364733014, 'mean_word_length': 3.1697485207100593, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1073663', 'n_tokens_mistral': 3377, 'n_tokens_neox': 3343, 'n_words': 923}