<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 DataTables. */
horizon.datatables = {
  update: function () {
    var $rows_to_update = $('tr.warning.ajax-update');
    var $table = $rows_to_update.closest('table');
    var interval = $rows_to_update.attr('data-update-interval');
    var decay_constant = $table.attr('decay_constant');
    var requests = [];

    // do nothing if there are no rows to update.
    if($rows_to_update.length <= 0) { return; }

    // Do not update this row if the action column is expanded
    if ($rows_to_update.find('.actions_column .btn-group.open').length) {
      // Wait and try to update again in next interval instead
      setTimeout(horizon.datatables.update, interval);
      // Remove interval decay, since this will not hit server
      $table.removeAttr('decay_constant');
      return;
    }

    $rows_to_update.each(function() {
      var $row = $(this);
      var $table = $row.closest('table.datatable');

      requests.push(
        horizon.ajax.queue({
          url: $row.attr('data-update-url'),
          error: function (jqXHR) {
            switch (jqXHR.status) {
              // A 404 indicates the object is gone, and should be removed from the table
              case 404:
                // Update the footer count and reset to default empty row if needed
                var row_count, colspan, template, params;

                // existing count minus one for the row we're removing
                row_count = horizon.datatables.update_footer_count($table, -1);

                if(row_count === 0) {
                  colspan = $table.find('.table_column_header th').length;
                  template = horizon.templates.compiled_templates["#empty_row_template"];
                  params = {
                      "colspan": colspan,
                      no_items_label: gettext("No items to display.")
                  };
                  var empty_row = template.render(params);
                  $row.replaceWith(empty_row);
                } else {
                  $row.remove();
                }
                // Reset tablesorter's data cache.
                $table.trigger("update");
                // Enable launch action if quota is not exceeded
                horizon.datatables.update_actions();
                break;
              default:
                console.log(gettext("An error occurred while updating."));
                $row.removeClass("ajax-update");
                $row.find("i.ajax-updating").remove();
                break;
            }
          },
          success: function (data) {
            var $new_row = $(data);

            if ($new_row.hasClass('warning')) {
              var $container = $(document.createElement('div'))
                .addClass('progress-text horizon-loading-bar');

              var $progress = $(document.createElement('div'))
                .addClass('progress progress-striped active')
                .appendTo($container);

              $(document.createElement('div'))
                .addClass('progress-bar')
                .appendTo($progress);

              // if action/confirm is required, show progress-bar with "?"
              // icon to indicate user action is required
              if ($new_row.find('.btn-action-required').length > 0) {
                $(document.createElement('span'))
                  .addClass('fa fa-question-circle progress-bar-text')
                  .appendTo($container);
              }
              $new_row.find("td.warning:last").prepend($container);
            }

            // Only replace row if the html content has changed
            if($new_row.html() !== $row.html()) {

              // Directly accessing the checked property of the element
              // is MUCH faster than using jQuery's helper method
              var $checkbox = $row.find('.table-row-multi-select');
              if($checkbox.length && $checkbox[0].checked) {
                // Preserve the checkbox if it's already clicked
                $new_row.find('.table-row-multi-select').prop('checked', true);
              }
              $row.replaceWith($new_row);

              // TODO(matt-borland, tsufiev): ideally we should solve the
              // problem with not-working angular actions in a content added
              // by jQuery via replacing jQuery insert with Angular insert.
              // Should address this in Newton release
              recompileAngularContent($table);

              // Reset tablesorter's data cache.
              $table.trigger("update");
              // Reset decay constant.
              $table.removeAttr('decay_constant');
              // Check that quicksearch is enabled for this table
              // Reset quicksearch's data cache.
              if ($table.attr('id') in horizon.datatables.qs) {
                horizon.datatables.qs[$table.attr('id')].cache();
              }
            }
          },
          complete: function () {
            // Revalidate the button check for the updated table
            horizon.datatables.validate_button();
          }
        })
      );
    });

    $.when.apply($, requests).always(function() {
      decay_constant = decay_constant || 0;
      decay_constant++;
      $table.attr('decay_constant', decay_constant);
      var next_poll = interval * decay_constant;
      // Limit the interval to 30 secs
      if(next_poll > 30 * 1000) { next_poll = 30 * 1000; }
      setTimeout(horizon.datatables.update, next_poll);
    });
  },

  update_actions: function() {
    var $actions_to_update = $('.btn-launch.ajax-update, .btn-create.ajax-update');
    $actions_to_update.each(function() {
      var $action = $(this);
      horizon.ajax.queue({
        url: $action.attr('data-update-url'),
        error: function () {
          console.log(gettext("An error occurred while updating."));
        },
        success: function (data) {
          var $new_action = $(data);

          // Only replace row if the html content has changed
          if($new_action.html() != $action.html()) {
            $action.replaceWith($new_action);
          }
        }
      });
    });
  },

  validate_button: function ($form, disable_button) {
    // Enable or disable table batch action buttons based on row selection.
    $form = $form || $(".table_wrapper > form");
    $form.each(function () {
      var $this = $(this);
      var $action_buttons = $this.find('.table_actions button[data-batch-action="true"]');
      if (disable_button === undefined) {
        disable_button = $this.find(".table-row-multi-select").filter(":checked").length == 0;
      }
      $action_buttons.toggleClass("disabled", disable_button);
    });
  },

  initialize_checkboxes_behavior: function() {
    // Bind the "select all" checkbox action.
    $('.table_wrapper, #modal_wrapper')
      .on('change', '.table-row-multi-select', function() {
        var $this = $(this);
        var $table = $this.closest('table');
        var is_checked = $this.prop('checked');

        if ($this.hasClass('multi-select-header')) {

          // Only select / deselect the visible rows
          $table.find('tbody tr:visible .table-row-multi-select')
            .prop('checked', is_checked);

        } else {

          // Find the master checkbox
          var $multi_select_checkbox = $table.find('.multi-select-header');
<commit_msg>Change a behavior of more actions dropdown<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 DataTables. */
horizon.datatables = {
  update: function () {
    var $rows_to_update = $('tr.warning.ajax-update');
    var $table = $rows_to_update.closest('table');
    var interval = $rows_to_update.attr('data-update-interval');
    var decay_constant = $table.attr('decay_constant');
    var requests = [];

    // do nothing if there are no rows to update.
    if($rows_to_update.length <= 0) { return; }

    // Do not update this row if the action column is expanded
    if ($rows_to_update.find('.actions_column .btn-group.open').length) {
      // Wait and try to update again in next interval instead
      setTimeout(horizon.datatables.update, interval);
      // Remove interval decay, since this will not hit server
      $table.removeAttr('decay_constant');
      return;
    }

    $rows_to_update.each(function() {
      var $row = $(this);
      var $table = $row.closest('table.datatable');

      requests.push(
        horizon.ajax.queue({
          url: $row.attr('data-update-url'),
          error: function (jqXHR) {
            switch (jqXHR.status) {
              // A 404 indicates the object is gone, and should be removed from the table
              case 404:
                // Update the footer count and reset to default empty row if needed
                var row_count, colspan, template, params;

                // existing count minus one for the row we're removing
                row_count = horizon.datatables.update_footer_count($table, -1);

                if(row_count === 0) {
                  colspan = $table.find('.table_column_header th').length;
                  template = horizon.templates.compiled_templates["#empty_row_template"];
                  params = {
                      "colspan": colspan,
                      no_items_label: gettext("No items to display.")
                  };
                  var empty_row = template.render(params);
                  $row.replaceWith(empty_row);
                } else {
                  $row.remove();
                }
                // Reset tablesorter's data cache.
                $table.trigger("update");
                // Enable launch action if quota is not exceeded
                horizon.datatables.update_actions();
                break;
              default:
                console.log(gettext("An error occurred while updating."));
                $row.removeClass("ajax-update");
                $row.find("i.ajax-updating").remove();
                break;
            }
          },
          success: function (data) {
            var $new_row = $(data);

            if ($new_row.hasClass('warning')) {
              var $container = $(document.createElement('div'))
                .addClass('progress-text horizon-loading-bar');

              var $progress = $(document.createElement('div'))
                .addClass('progress progress-striped active')
                .appendTo($container);

              $(document.createElement('div'))
                .addClass('progress-bar')
                .appendTo($progress);

              // if action/confirm is required, show progress-bar with "?"
              // icon to indicate user action is required
              if ($new_row.find('.btn-action-required').length > 0) {
                $(document.createElement('span'))
                  .addClass('fa fa-question-circle progress-bar-text')
                  .appendTo($container);
              }
              $new_row.find("td.warning:last").prepend($container);
            }

            // Only replace row if the html content has changed
            if($new_row.html() !== $row.html()) {

              // Directly accessing the checked property of the element
              // is MUCH faster than using jQuery's helper method
              var $checkbox = $row.find('.table-row-multi-select');
              if($checkbox.length && $checkbox[0].checked) {
                // Preserve the checkbox if it's already clicked
                $new_row.find('.table-row-multi-select').prop('checked', true);
              }
              $row.replaceWith($new_row);

              // TODO(matt-borland, tsufiev): ideally we should solve the
              // problem with not-working angular actions in a content added
              // by jQuery via replacing jQuery insert with Angular insert.
              // Should address this in Newton release
              recompileAngularContent($table);

              // Reset tablesorter's data cache.
              $table.trigger("update");
              // Reset decay constant.
              $table.removeAttr('decay_constant');
              // Check that quicksearch is enabled for this table
              // Reset quicksearch's data cache.
              if ($table.attr('id') in horizon.datatables.qs) {
                horizon.datatables.qs[$table.attr('id')].cache();
              }
            }
          },
          complete: function () {
            // Revalidate the button check for the updated table
            horizon.datatables.validate_button();
          }
        })
      );
    });

    $.when.apply($, requests).always(function() {
      decay_constant = decay_constant || 0;
      decay_constant++;
      $table.attr('decay_constant', decay_constant);
      var next_poll = interval * decay_constant;
      // Limit the interval to 30 secs
      if(next_poll > 30 * 1000) { next_poll = 30 * 1000; }
      setTimeout(horizon.datatables.update, next_poll);
    });
  },

  update_actions: function() {
    var $actions_to_update = $('.btn-launch.ajax-update, .btn-create.ajax-update');
    $actions_to_update.each(function() {
      var $action = $(this);
      horizon.ajax.queue({
        url: $action.attr('data-update-url'),
        error: function () {
          console.log(gettext("An error occurred while updating."));
        },
        success: function (data) {
          var $new_action = $(data);

          // Only replace row if the html content has changed
          if($new_action.html() != $action.html()) {
            $action.replaceWith($new_action);
          }
        }
      });
    });
  },

  validate_button: function ($form, disable_button) {
    // Enable or disable table batch action buttons and more actions dropdown based on row selection.
    $form = $form || $(".table_wrapper > form");
    $form.each(function () {
      var $this = $(this);
      var $action_buttons = $this.find('.table_actions button[data-batch-action="true"]');
      var $more_dropdown = $this.find('.table_actions_menu > a.dropdown-toggle');
      if (disable_button === undefined) {
        disable_button = $this.find(".table-row-multi-select").filter(":checked").length == 0;
      }
      $action_buttons.toggleClass("disabled", disable_button);
      $more_dropdown.toggleClass("disabled", disable_button);
    });
  },

  initialize_checkboxes_behavior: function() {
    // Bind the "select all" checkbox action.
    $('.table_wrapper, #modal_wrapper')
      .on('change', '.table-row-multi-select', function() {
        var $this = $(this);
        var $table = $this.closest('table');
        var is_checked = $this.prop('checked');

        if ($this.hasClass('multi-select-header')) {

          // Only select / deselect the visible rows
          $table.find('tbody tr:visible .table-row-multi-select')
            .prop('checked', is_checked);

        } else {

          // Find the master checkbox
          var $multi_select_checkbox = $table.find('.multi-select-header');

{'fraction_non_alphanumeric': 0.1222710124311955, 'fraction_numerical': 0.003834498113674315, 'mean_word_length': 1.9724264705882353, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 10, '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': '4616001', 'n_tokens_mistral': 4306, 'n_tokens_neox': 4099, 'n_words': 1130}