<commit_before>//= require protocols/import_export/import

// Global variables
var rowsSelected = [];
var protocolsTableEl = null;
var protocolsDatatable = null;
var repositoryType;

/**
 * Initializes page
 */
function init() {
  updateButtons();
  initProtocolsTable();
  initRowSelection();
  initKeywordFiltering();
  initProtocolPreviewModal();
  initLinkedChildrenModal();
  initCreateNewModal();
  initModals();
  initImport();
}

// Initialize protocols DataTable
function initProtocolsTable() {
  protocolsTableEl = $("#protocols-table");
  repositoryType = protocolsTableEl.data("type");

  protocolsDatatable = protocolsTableEl.DataTable({
    order: [[1, "asc"]],
    dom: "RBfl<'row'<'col-sm-12't>><'row'<'col-sm-7'i><'col-sm-5'p>>",
    stateSave: true,
    sScrollX: '100%',
    sScrollXInner: '100%',
    buttons: [],
    processing: true,
    serverSide: true,
    ajax: {
      url: protocolsTableEl.data("source"),
      type: "POST"
    },
    colReorder: {
      fixedColumnsLeft: 1000000 // Disable reordering
    },
    columnDefs: [{
      targets: 0,
      searchable: false,
      orderable: false,
      sWidth: "1%",
      render: function (data, type, full, meta) {
        return "<input type='checkbox'>";
      }
    }, {
      targets: [ 1, 2, 3, 4, 5 ],
      searchable: true,
      orderable: true
    }],
    columns: [
      { data: "0" },
      { data: "1" },
      { data: "2" },
      {
        data: "3",
        visible: repositoryType != "archive"
      },
      { data: "4" },
      { data: "5" },
      { data: "6" }
    ],
    oLanguage: {
      sSearch: I18n.t('general.filter')
    },
    rowCallback: function(row, data, dataIndex) {
      // Get row ID
      var rowId = data["DT_RowId"];

      $(row).attr("data-row-id", rowId);

      // Append URLs if they exist
      if (data["DT_CanEdit"]) {
        $(row).attr("data-can-edit", "true");
        $(row).attr("data-edit-url", data["DT_EditUrl"]);
      }
      if (data["DT_CanClone"]) {
        $(row).attr("data-can-clone", "true");
        $(row).attr("data-clone-url", data["DT_CloneUrl"]);
      }
      if (data["DT_CanMakePrivate"]) { $(row).attr("data-can-make-private", "true"); }
      if (data["DT_CanPublish"]) { $(row).attr("data-can-publish", "true"); }
      if (data["DT_CanArchive"]) { $(row).attr("data-can-archive", "true"); }
      if (data["DT_CanRestore"]) { $(row).attr("data-can-restore", "true"); }
      if (data["DT_CanExport"]) { $(row).attr("data-can-export", "true"); }

      // If row ID is in the list of selected row IDs
      if($.inArray(rowId, rowsSelected) !== -1){
        $(row).find("input[type='checkbox']").prop("checked", true);
        $(row).addClass("selected");
      }
    },
    fnDrawCallback: function(settings, json) {
      animateSpinner(this, false);
      $.initTooltips();
    },
    preDrawCallback: function(settings) {
      animateSpinner(this);
    },
    stateSaveCallback: function (settings, data) {
      // Set a cookie with the table state using the team id
      localStorage.setItem(
        "datatables_protocols_state/" +
        protocolsTableEl.data("team-id") +
        "/" + repositoryType,
        JSON.stringify(data)
      );
    },
    stateLoadCallback: function (settings) {
      // Load the table state for the current team
      var state = localStorage.getItem(
        "datatables_protocols_state/" +
        protocolsTableEl.data("team-id") +
        "/" + repositoryType
      );
      if (state !== null) {
        return JSON.parse(state);
      }
      return null;
    }
  });
}

function initRowSelection() {
  // Handle click on table cells with checkboxes
  protocolsTableEl.on("click", "tbody td, thead th:first-child", function(e) {
    $(this).parent().find("input[type='checkbox']").trigger("click");
  });

  // Handle clicks on checkbox
  protocolsTableEl.find("tbody").on("click", "input[type='checkbox']", function(e) {
    // Get row ID
    var row = $(this).closest("tr");
    var data = protocolsDatatable.row(row).data();
    var rowId = data["DT_RowId"];

    // Determine whether row ID is in the list of selected row IDs
    var index = $.inArray(rowId, rowsSelected);

    // If checkbox is checked and row ID is not in list of selected row IDs
    if (this.checked && index === -1) {
      rowsSelected.push(rowId);
      // Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
    } else if (!this.checked && index !== -1) {
      rowsSelected.splice(index, 1);
    }

    if (this.checked) {
      row.addClass("selected");
    } else {
      row.removeClass("selected");
    }

    updateDataTableSelectAllCheckbox();
    e.stopPropagation();
    updateButtons();
  });

  // Handle click on "Select all" control
  protocolsTableEl.find("thead input[name='select_all']").on("click", function(e) {
    if (this.checked) {
      protocolsTableEl.find("tbody input[type='checkbox']:not(:checked)").trigger("click");
    } else {
      protocolsTableEl.find("tbody input[type='checkbox']:checked").trigger("click");
    }

    // Prevent click event from propagating to parent
    e.stopPropagation();
  });
}

function initKeywordFiltering() {
  protocolsTableEl.find("tbody").on("click", "a[data-action='filter']", function(e) {
    var link = $(this);
    var searchInput = $("#protocols-table_wrapper input[type='search']");<commit_msg>Fix protocols table select all checkbox [SCI-3999]<commit_after>//= require protocols/import_export/import

// Global variables
var rowsSelected = [];
var protocolsTableEl = null;
var protocolsDatatable = null;
var repositoryType;

/**
 * Initializes page
 */
function init() {
  updateButtons();
  initProtocolsTable();
  initKeywordFiltering();
  initProtocolPreviewModal();
  initLinkedChildrenModal();
  initCreateNewModal();
  initModals();
  initImport();
}

// Initialize protocols DataTable
function initProtocolsTable() {
  protocolsTableEl = $("#protocols-table");
  repositoryType = protocolsTableEl.data("type");

  protocolsDatatable = protocolsTableEl.DataTable({
    order: [[1, "asc"]],
    dom: "RBfl<'row'<'col-sm-12't>><'row'<'col-sm-7'i><'col-sm-5'p>>",
    stateSave: true,
    sScrollX: '100%',
    sScrollXInner: '100%',
    buttons: [],
    processing: true,
    serverSide: true,
    ajax: {
      url: protocolsTableEl.data("source"),
      type: "POST"
    },
    colReorder: {
      fixedColumnsLeft: 1000000 // Disable reordering
    },
    columnDefs: [{
      targets: 0,
      searchable: false,
      orderable: false,
      sWidth: "1%",
      render: function (data, type, full, meta) {
        return "<input type='checkbox'>";
      }
    }, {
      targets: [ 1, 2, 3, 4, 5 ],
      searchable: true,
      orderable: true
    }],
    columns: [
      { data: "0" },
      { data: "1" },
      { data: "2" },
      {
        data: "3",
        visible: repositoryType != "archive"
      },
      { data: "4" },
      { data: "5" },
      { data: "6" }
    ],
    oLanguage: {
      sSearch: I18n.t('general.filter')
    },
    rowCallback: function(row, data, dataIndex) {
      // Get row ID
      var rowId = data["DT_RowId"];

      $(row).attr("data-row-id", rowId);

      // Append URLs if they exist
      if (data["DT_CanEdit"]) {
        $(row).attr("data-can-edit", "true");
        $(row).attr("data-edit-url", data["DT_EditUrl"]);
      }
      if (data["DT_CanClone"]) {
        $(row).attr("data-can-clone", "true");
        $(row).attr("data-clone-url", data["DT_CloneUrl"]);
      }
      if (data["DT_CanMakePrivate"]) { $(row).attr("data-can-make-private", "true"); }
      if (data["DT_CanPublish"]) { $(row).attr("data-can-publish", "true"); }
      if (data["DT_CanArchive"]) { $(row).attr("data-can-archive", "true"); }
      if (data["DT_CanRestore"]) { $(row).attr("data-can-restore", "true"); }
      if (data["DT_CanExport"]) { $(row).attr("data-can-export", "true"); }

      // If row ID is in the list of selected row IDs
      if($.inArray(rowId, rowsSelected) !== -1){
        $(row).find("input[type='checkbox']").prop("checked", true);
        $(row).addClass("selected");
      }
    },
    fnDrawCallback: function(settings, json) {
      animateSpinner(this, false);
      initRowSelection();
      $.initTooltips();
    },
    preDrawCallback: function(settings) {
      animateSpinner(this);
    },
    stateSaveCallback: function (settings, data) {
      // Set a cookie with the table state using the team id
      localStorage.setItem(
        "datatables_protocols_state/" +
        protocolsTableEl.data("team-id") +
        "/" + repositoryType,
        JSON.stringify(data)
      );
    },
    stateLoadCallback: function (settings) {
      // Load the table state for the current team
      var state = localStorage.getItem(
        "datatables_protocols_state/" +
        protocolsTableEl.data("team-id") +
        "/" + repositoryType
      );
      if (state !== null) {
        return JSON.parse(state);
      }
      return null;
    }
  });
}

function initRowSelection() {
  let protocolsTableScrollHead = protocolsTableEl.closest('.dataTables_scroll').find('.dataTables_scrollHead');

  // Handle click on table cells with checkboxes
  protocolsTableEl.on("click", "tbody td, thead th:first-child", function(e) {
    $(this).parent().find("input[type='checkbox']").trigger("click");
  });

  // Handle clicks on checkbox
  protocolsTableEl.find("tbody").on("click", "input[type='checkbox']", function(e) {
    // Get row ID
    var row = $(this).closest("tr");
    var data = protocolsDatatable.row(row).data();
    var rowId = data["DT_RowId"];

    // Determine whether row ID is in the list of selected row IDs
    var index = $.inArray(rowId, rowsSelected);

    // If checkbox is checked and row ID is not in list of selected row IDs
    if (this.checked && index === -1) {
      rowsSelected.push(rowId);
      // Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
    } else if (!this.checked && index !== -1) {
      rowsSelected.splice(index, 1);
    }

    if (this.checked) {
      row.addClass("selected");
    } else {
      row.removeClass("selected");
    }

    updateDataTableSelectAllCheckbox();
    e.stopPropagation();
    updateButtons();
  });

  // Handle click on "Select all" control
  protocolsTableScrollHead.find("thead input[name='select_all']").on('click', function(e) {
    if (this.checked) {
      protocolsTableEl.find("tbody input[type='checkbox']:not(:checked)").trigger('click');
    } else {
      protocolsTableEl.find("tbody input[type='checkbox']:checked").trigger('click');
    }

    // Prevent click event from propagating to parent
    e.stopPropagation();
  });
}

function initKeywordFiltering() {
  protocolsTableEl.find("tbody").on("click", "a[data-action='filter']", function(e) {
    var link = $(this);
    var searchInput = $("#protocols-table_wrapper input[type='search']");
{'fraction_non_alphanumeric': 0.17017190929041698, 'fraction_numerical': 0.0073152889539136795, 'mean_word_length': 2.9130590339892666, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '2876802', 'n_tokens_mistral': 3516, 'n_tokens_neox': 3420, 'n_words': 750}