| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- 'use strict';
- exports.__esModule = true;
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
- var _element = require('./../../../helpers/dom/element');
- var _border2 = require('./border');
- var _border3 = _interopRequireDefault(_border2);
- var _coords = require('./cell/coords');
- var _coords2 = _interopRequireDefault(_coords);
- var _range = require('./cell/range');
- var _range2 = _interopRequireDefault(_range);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- /**
- * @class Selection
- */
- var Selection = function () {
- /**
- * @param {Object} settings
- * @param {CellRange} cellRange
- */
- function Selection(settings, cellRange) {
- _classCallCheck(this, Selection);
- this.settings = settings;
- this.cellRange = cellRange || null;
- this.instanceBorders = {};
- }
- /**
- * Each Walkontable clone requires it's own border for every selection. This method creates and returns selection
- * borders per instance
- *
- * @param {Walkontable} wotInstance
- * @returns {Border}
- */
- _createClass(Selection, [{
- key: 'getBorder',
- value: function getBorder(wotInstance) {
- if (this.instanceBorders[wotInstance.guid]) {
- return this.instanceBorders[wotInstance.guid];
- }
- // where is this returned?
- this.instanceBorders[wotInstance.guid] = new _border3.default(wotInstance, this.settings);
- }
- /**
- * Checks if selection is empty
- *
- * @returns {Boolean}
- */
- }, {
- key: 'isEmpty',
- value: function isEmpty() {
- return this.cellRange === null;
- }
- /**
- * Adds a cell coords to the selection
- *
- * @param {CellCoords} coords
- */
- }, {
- key: 'add',
- value: function add(coords) {
- if (this.isEmpty()) {
- this.cellRange = new _range2.default(coords, coords, coords);
- } else {
- this.cellRange.expand(coords);
- }
- }
- /**
- * If selection range from or to property equals oldCoords, replace it with newCoords. Return boolean
- * information about success
- *
- * @param {CellCoords} oldCoords
- * @param {CellCoords} newCoords
- * @returns {Boolean}
- */
- }, {
- key: 'replace',
- value: function replace(oldCoords, newCoords) {
- if (!this.isEmpty()) {
- if (this.cellRange.from.isEqual(oldCoords)) {
- this.cellRange.from = newCoords;
- return true;
- }
- if (this.cellRange.to.isEqual(oldCoords)) {
- this.cellRange.to = newCoords;
- return true;
- }
- }
- return false;
- }
- /**
- * Clears selection
- */
- }, {
- key: 'clear',
- value: function clear() {
- this.cellRange = null;
- }
- /**
- * Returns the top left (TL) and bottom right (BR) selection coordinates
- *
- * @returns {Array} Returns array of coordinates for example `[1, 1, 5, 5]`
- */
- }, {
- key: 'getCorners',
- value: function getCorners() {
- var topLeft = this.cellRange.getTopLeftCorner();
- var bottomRight = this.cellRange.getBottomRightCorner();
- return [topLeft.row, topLeft.col, bottomRight.row, bottomRight.col];
- }
- /**
- * Adds class name to cell element at given coords
- *
- * @param {Walkontable} wotInstance Walkontable instance
- * @param {Number} sourceRow Cell row coord
- * @param {Number} sourceColumn Cell column coord
- * @param {String} className Class name
- */
- }, {
- key: 'addClassAtCoords',
- value: function addClassAtCoords(wotInstance, sourceRow, sourceColumn, className) {
- var TD = wotInstance.wtTable.getCell(new _coords2.default(sourceRow, sourceColumn));
- if ((typeof TD === 'undefined' ? 'undefined' : _typeof(TD)) === 'object') {
- (0, _element.addClass)(TD, className);
- }
- }
- /**
- * @param wotInstance
- */
- }, {
- key: 'draw',
- value: function draw(wotInstance) {
- if (this.isEmpty()) {
- if (this.settings.border) {
- var border = this.getBorder(wotInstance);
- if (border) {
- border.disappear();
- }
- }
- return;
- }
- var renderedRows = wotInstance.wtTable.getRenderedRowsCount();
- var renderedColumns = wotInstance.wtTable.getRenderedColumnsCount();
- var corners = this.getCorners();
- var sourceRow = void 0,
- sourceCol = void 0,
- TH = void 0;
- for (var column = 0; column < renderedColumns; column++) {
- sourceCol = wotInstance.wtTable.columnFilter.renderedToSource(column);
- if (sourceCol >= corners[1] && sourceCol <= corners[3]) {
- TH = wotInstance.wtTable.getColumnHeader(sourceCol);
- if (TH) {
- var newClasses = [];
- if (this.settings.highlightHeaderClassName) {
- newClasses.push(this.settings.highlightHeaderClassName);
- }
- if (this.settings.highlightColumnClassName) {
- newClasses.push(this.settings.highlightColumnClassName);
- }
- (0, _element.addClass)(TH, newClasses);
- }
- }
- }
- for (var row = 0; row < renderedRows; row++) {
- sourceRow = wotInstance.wtTable.rowFilter.renderedToSource(row);
- if (sourceRow >= corners[0] && sourceRow <= corners[2]) {
- TH = wotInstance.wtTable.getRowHeader(sourceRow);
- if (TH) {
- var _newClasses = [];
- if (this.settings.highlightHeaderClassName) {
- _newClasses.push(this.settings.highlightHeaderClassName);
- }
- if (this.settings.highlightRowClassName) {
- _newClasses.push(this.settings.highlightRowClassName);
- }
- (0, _element.addClass)(TH, _newClasses);
- }
- }
- for (var _column = 0; _column < renderedColumns; _column++) {
- sourceCol = wotInstance.wtTable.columnFilter.renderedToSource(_column);
- if (sourceRow >= corners[0] && sourceRow <= corners[2] && sourceCol >= corners[1] && sourceCol <= corners[3]) {
- // selected cell
- if (this.settings.className) {
- this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.className);
- }
- } else if (sourceRow >= corners[0] && sourceRow <= corners[2]) {
- // selection is in this row
- if (this.settings.highlightRowClassName) {
- this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.highlightRowClassName);
- }
- } else if (sourceCol >= corners[1] && sourceCol <= corners[3]) {
- // selection is in this column
- if (this.settings.highlightColumnClassName) {
- this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.highlightColumnClassName);
- }
- }
- }
- }
- wotInstance.getSetting('onBeforeDrawBorders', corners, this.settings.className);
- if (this.settings.border) {
- var _border = this.getBorder(wotInstance);
- if (_border) {
- // warning! border.appear modifies corners!
- _border.appear(corners);
- }
- }
- }
- }]);
- return Selection;
- }();
- exports.default = Selection;
|