<commit_before>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  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.
 */

require('jquery-ui/ui/widgets/selectable');

module.exports = function(TableScope) {

  TableScope.prototype.deselectCells = function(cells) {
    var $selected = $(cells.nodes()).filter('.ui-selected');

    cells.deselect();
    $selected.each(function () {
      var selectee = $.data(this, "selectable-item");

      $(this).removeClass('ui-selected');

      if (selectee) {
        selectee.selected = false;
        selectee.startselected = false;
      }
    });
  };

  TableScope.prototype.selectCellsRange = function(startCellIndex, endCell) {
    var cellIndex = endCell.index();
    var minColumnIndex = Math.min(startCellIndex.column, cellIndex.column);
    var maxColumnIndex = Math.max(startCellIndex.column, cellIndex.column);
    var minRowIndex = Math.min(startCellIndex.row, cellIndex.row);
    var maxRowIndex = Math.max(startCellIndex.row, cellIndex.row);
    var rowSelector = _.range(minRowIndex, maxRowIndex + 1);
    var columnSelector = _.range(minColumnIndex, maxColumnIndex + 1);

    this.table.cells(rowSelector, columnSelector).select();
  };

  TableScope.prototype.initTableSelect = function() {
    var self = this;

    $(this.table.table().container())
      .selectable({
        filter: 'tr[role="row"] td',
        delay: 150,
        cancel: 'thead',
        appendTo: self.element,
        start: function() {
          self.deselectCells(self.table.cells({ selected: true }));
        },
        stop: function() {
          var cells = self.table.cells('.ui-selected');

          cells.select();
          $(self.element).find('.ui-selected').removeClass('ui-selected');
        }
      })
      .on('click', 'td', function(e) {
        if (e.shiftKey) {
          return;
        }

        var cell = self.table.cell(e.target);
        var selected = cell.node().classList.contains('selected');

        self.table.cells({ selected: true }).deselect();
        !selected && cell.select();
      });

    self.table
      .on('key-blur', function () {
        self.focussedCell = null;
      })
      .on('key-focus', function (e, datatable, cell, originalEvent) {
        self.focussedCell = cell.index();

        if (!originalEvent.shiftKey) {
          return;
        }

        var selectedCells = self.table.cells({ selected: true });
        var selectedCellsIndexes = selectedCells.indexes();
        var minSelectedRowIndex = Math.min.apply(null, selectedCellsIndexes.pluck('row').sort().unique());
        var minSelectedColumnIndex = Math.min.apply(null, selectedCellsIndexes.pluck('column').sort().unique());

        selectedCells.deselect();
        self.selectCellsRange({ row: minSelectedRowIndex, column: minSelectedColumnIndex }, cell);
      });
  };
};<commit_msg>#5529 fix click selection in selected cells (#5532)<commit_after>/*
 *  Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
 *
 *  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.
 */

require('jquery-ui/ui/widgets/selectable');

module.exports = function(TableScope) {

  TableScope.prototype.deselectCells = function(cells) {
    var $selected = $(cells.nodes()).filter('.ui-selected');

    cells.deselect();
    $selected.each(function () {
      var selectee = $.data(this, "selectable-item");

      $(this).removeClass('ui-selected');

      if (selectee) {
        selectee.selected = false;
        selectee.startselected = false;
      }
    });
  };

  TableScope.prototype.selectCellsRange = function(startCellIndex, endCell) {
    var cellIndex = endCell.index();
    var minColumnIndex = Math.min(startCellIndex.column, cellIndex.column);
    var maxColumnIndex = Math.max(startCellIndex.column, cellIndex.column);
    var minRowIndex = Math.min(startCellIndex.row, cellIndex.row);
    var maxRowIndex = Math.max(startCellIndex.row, cellIndex.row);
    var rowSelector = _.range(minRowIndex, maxRowIndex + 1);
    var columnSelector = _.range(minColumnIndex, maxColumnIndex + 1);

    this.table.cells(rowSelector, columnSelector).select();
  };

  TableScope.prototype.initTableSelect = function() {
    var self = this;

    $(this.table.table().container())
      .selectable({
        filter: 'tr[role="row"] td',
        delay: 150,
        cancel: 'thead',
        appendTo: self.element,
        start: function() {
          self.deselectCells(self.table.cells({ selected: true }));
        },
        stop: function() {
          var cells = self.table.cells('.ui-selected');

          cells.select();
          $(self.element).find('.ui-selected').removeClass('ui-selected');
        }
      })
      .on('click', 'td', function(e) {
        if (e.shiftKey) {
          return;
        }

        var cell = self.table.cell(e.target);
        var selected = cell.node().classList.contains('selected');
        var selectedCells = self.table.cells({ selected: true });

        selectedCells.deselect();
        (!selected || self.focusChanged || selectedCells.indexes().length > 1) && cell.select();

        self.focusChanged = false;
      });

    self.table
      .on('key-blur', function () {
        self.prevFocussedCell = _.clone(self.focussedCell);
        self.focussedCell = null;
      })
      .on('key-focus', function (e, datatable, cell, originalEvent) {
        self.focussedCell = cell.index();
        self.focusChanged = true;

        if (!originalEvent.shiftKey) {
          return;
        }

        var selectedCells = self.table.cells({ selected: true });
        var selectedCellsIndexes = selectedCells.indexes();
        var minSelectedRowIndex = Math.min.apply(null, selectedCellsIndexes.pluck('row').sort().unique());
        var minSelectedColumnIndex = Math.min.apply(null, selectedCellsIndexes.pluck('column').sort().unique());

        selectedCells.deselect();
        self.selectCellsRange({ row: minSelectedRowIndex, column: minSelectedColumnIndex }, cell);
      });
  };
};
{'fraction_non_alphanumeric': 0.1347689674843126, 'fraction_numerical': 0.004991443240159726, 'mean_word_length': 2.8831672203765226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1072332', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1964, 'n_words': 470}