<commit_before>import { Slick } from 'slickgrid-es6';

export default class {
  constructor(options) {
    let _grid;
    const _self = this;
    const _handler = new Slick.EventHandler();
    let _selectedRowsLookup = {};
    const _defaults = {
      columnId: '_checkbox_selector',
      cssClass: null,
      toolTip: I18n.t("gobierto_admin.custom_fields_plugins.data_grid.select_deselect_all"),
      width: 30,
      deleteRowsButtonClass: "js-delete-rows"
    };

    const _options = $.extend(true, {}, _defaults, options);

    function init(grid){
      _grid = grid;
      _handler
      .subscribe(_grid.onClick, handleClick)
      .subscribe(_grid.onHeaderClick, handleHeaderClick)
      .subscribe(_grid.onKeyDown, handleKeyDown);

      const deleteRowsButton = $(`<button class="small ${_options.deleteRowsButtonClass}">${I18n.t("gobierto_admin.custom_fields_plugins.data_grid.remove_rows")}</button>`);
      $(`#${_options.containerId}`).append(deleteRowsButton);

      $(`.${_options.deleteRowsButtonClass}`).hide();
      $(`.${_options.deleteRowsButtonClass}`).click(removeRow);
    }

    function destroy(){
      _handler.unsubscribeAll();
    }

    function handleKeyDown(e, args){
      if (e.which == 32){
        if (_grid.getColumns()[args.cell].id === _options.columnId){
          // if editing, try to commit
          if (!_grid.getEditorLock().isActive() || _grid.getEditorLock().commitCurrentEdit()){
            toggleRowSelection(args.row);
          }
          e.preventDefault();
          e.stopImmediatePropagation();
        }
      }
    }

    function handleClick(e, args){
      // clicking on a row select checkbox
      if (_grid.getColumns()[args.cell].id === _options.columnId && $(e.target).is(':checkbox')){
        // if editing, try to commit
        if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()){
          e.preventDefault();
          e.stopImmediatePropagation();
          return;
        }

        var currentSelectedRows = _grid.getSelectedRows();

        if ($(e.target).is(":checked")) {
          // Show delete button
          $(`.${_options.deleteRowsButtonClass}`).show();
          // ensure row is selected
          var newSelectedRows = currentSelectedRows.concat(args.row);
          _grid.setSelectedRows(newSelectedRows);
        } else {
          // Hide delete button
          $(`.${_options.deleteRowsButtonClass}`).hide();
          // ensure row is not selected
          var index = currentSelectedRows.indexOf(args.row);
          if (index != -1) {
            currentSelectedRows.splice(index, 1)
            _grid.setSelectedRows(currentSelectedRows);
          }
        }

        e.stopPropagation();
        e.stopImmediatePropagation();
      }
    }

    function removeRow(e) {
      e.preventDefault();
      let rowsToRemove = [];
      _grid.getSelectedRows().forEach(idx => {
        rowsToRemove.push(_grid.getData()[idx]);
      })

      rowsToRemove.forEach(row => {
        _grid.getData().forEach((d, idx) => {
          if(JSON.stringify(row) === JSON.stringify(d)){
            _grid.getData().splice(idx, 1);
          }
        })
      });
      _grid.setSelectedRows([]);
      _grid.invalidate()

      // Hide button
      $(`.${_options.deleteRowsButtonClass}`).hide();
    }

    function toggleRowSelection(row){
      if (_selectedRowsLookup[row]){
        _grid.setSelectedRows($.grep(_grid.getSelectedRows(), function(n){
          return n != row;
        }));
      } else {
        _grid.setSelectedRows(_grid.getSelectedRows().concat(row));
      }
    }

    function handleHeaderClick(e, args){
      if (args.column.id == _options.columnId && $(e.target).is(':checkbox')){
        // if editing, try to commit
        if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()){
          e.preventDefault();
          e.stopImmediatePropagation();
          return;
        }

        if ($(e.target).is(':checked')){
          const rows = [];
          for (let i = 0; i < _grid.getDataLength(); i++){
            rows.push(i);
          }
          _grid.setSelectedRows(rows);
        } else {
          _grid.setSelectedRows([]);
        }<commit_msg>Check all rows with the header option<commit_after>import { Slick } from 'slickgrid-es6';

export default class {
  constructor(options) {
    let _grid;
    const _self = this;
    const _handler = new Slick.EventHandler();
    let _selectedRowsLookup = {};
    const _defaults = {
      columnId: '_checkbox_selector',
      cssClass: null,
      toolTip: I18n.t("gobierto_admin.custom_fields_plugins.data_grid.select_deselect_all"),
      width: 30,
      deleteRowsButtonClass: "js-delete-rows"
    };

    const _options = $.extend(true, {}, _defaults, options);

    function init(grid){
      _grid = grid;
      _handler
      .subscribe(_grid.onClick, handleClick)
      .subscribe(_grid.onHeaderClick, handleHeaderClick)
      .subscribe(_grid.onKeyDown, handleKeyDown);

      const deleteRowsButton = $(`<button class="small ${_options.deleteRowsButtonClass}">${I18n.t("gobierto_admin.custom_fields_plugins.data_grid.remove_rows")}</button>`);
      $(`#${_options.containerId}`).append(deleteRowsButton);

      $(`.${_options.deleteRowsButtonClass}`).hide();
      $(`.${_options.deleteRowsButtonClass}`).click(removeRow);
    }

    function destroy(){
      _handler.unsubscribeAll();
    }

    function handleKeyDown(e, args){
      if (e.which == 32){
        if (_grid.getColumns()[args.cell].id === _options.columnId){
          // if editing, try to commit
          if (!_grid.getEditorLock().isActive() || _grid.getEditorLock().commitCurrentEdit()){
            toggleRowSelection(args.row);
          }
          e.preventDefault();
          e.stopImmediatePropagation();
        }
      }
    }

    function handleClick(e, args){
      // clicking on a row select checkbox
      if (_grid.getColumns()[args.cell].id === _options.columnId && $(e.target).is(':checkbox')){
        // if editing, try to commit
        if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()){
          e.preventDefault();
          e.stopImmediatePropagation();
          return;
        }

        var currentSelectedRows = _grid.getSelectedRows();

        if ($(e.target).is(":checked")) {
          // Show delete button
          $(`.${_options.deleteRowsButtonClass}`).show();
          // ensure row is selected
          var newSelectedRows = currentSelectedRows.concat(args.row);
          _grid.setSelectedRows(newSelectedRows);
        } else {
          // Hide delete button
          $(`.${_options.deleteRowsButtonClass}`).hide();
          // ensure row is not selected
          var index = currentSelectedRows.indexOf(args.row);
          if (index != -1) {
            currentSelectedRows.splice(index, 1)
            _grid.setSelectedRows(currentSelectedRows);
          }
        }

        e.stopPropagation();
        e.stopImmediatePropagation();
      }
    }

    function removeRow(e) {
      e.preventDefault();
      let rowsToRemove = [];
      _grid.getSelectedRows().forEach(idx => {
        rowsToRemove.push(_grid.getData()[idx]);
      })

      rowsToRemove.forEach(row => {
        _grid.getData().forEach((d, idx) => {
          if(JSON.stringify(row) === JSON.stringify(d)){
            _grid.getData().splice(idx, 1);
          }
        })
      });
      _grid.setSelectedRows([]);
      _grid.invalidate()

      // Hide button
      $(`.${_options.deleteRowsButtonClass}`).hide();
    }

    function toggleRowSelection(row){
      if (_selectedRowsLookup[row]){
        _grid.setSelectedRows($.grep(_grid.getSelectedRows(), function(n){
          return n != row;
        }));
      } else {
        _grid.setSelectedRows(_grid.getSelectedRows().concat(row));
      }
    }

    function handleHeaderClick(e, args){
      if (args.column.id == _options.columnId && $(e.target).is(':checkbox')){
        // if editing, try to commit
        if (_grid.getEditorLock().isActive() && !_grid.getEditorLock().commitCurrentEdit()){
          e.preventDefault();
          e.stopImmediatePropagation();
          return;
        }

        // Check / uncheck all checkboxes
        if ($(e.target).is(':checked')){
          $(`#${_options.containerId} input:checkbox`).attr("checked", true);
          $(`.${_options.deleteRowsButtonClass}`).show();
        } else {
          $(`#${_options.containerId} input:checkbox`).attr("checked", false);
          $(`.${_options.deleteRowsButtonClass}`).hide();
        }

        if ($(e.target).is(':checked')){
          const rows = [];
          for (let i = 0; i < _grid.getDataLength(); i++){
            rows.push(i);
          }
          _grid.setSelectedRows(rows);
        } else {
          _grid.setSelectedRows([]);
        }
{'fraction_non_alphanumeric': 0.15034807994610375, 'fraction_numerical': 0.0029193801931282283, 'mean_word_length': 2.3751420992800303, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 13, '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': '2283752', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2575, 'n_words': 427}