2d7e496bba52a0cf654260b72529bd2a00e4c8232235b00359e5e31159b6271019c047acbb08dfd09ef897447330eca327525cf23884e7bd034558c73c132b 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. 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; };
  2. 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; }; }();
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. import { addClass } from './../../../helpers/dom/element';
  5. import Border from './border';
  6. import CellCoords from './cell/coords';
  7. import CellRange from './cell/range';
  8. /**
  9. * @class Selection
  10. */
  11. var Selection = function () {
  12. /**
  13. * @param {Object} settings
  14. * @param {CellRange} cellRange
  15. */
  16. function Selection(settings, cellRange) {
  17. _classCallCheck(this, Selection);
  18. this.settings = settings;
  19. this.cellRange = cellRange || null;
  20. this.instanceBorders = {};
  21. }
  22. /**
  23. * Each Walkontable clone requires it's own border for every selection. This method creates and returns selection
  24. * borders per instance
  25. *
  26. * @param {Walkontable} wotInstance
  27. * @returns {Border}
  28. */
  29. _createClass(Selection, [{
  30. key: 'getBorder',
  31. value: function getBorder(wotInstance) {
  32. if (this.instanceBorders[wotInstance.guid]) {
  33. return this.instanceBorders[wotInstance.guid];
  34. }
  35. // where is this returned?
  36. this.instanceBorders[wotInstance.guid] = new Border(wotInstance, this.settings);
  37. }
  38. /**
  39. * Checks if selection is empty
  40. *
  41. * @returns {Boolean}
  42. */
  43. }, {
  44. key: 'isEmpty',
  45. value: function isEmpty() {
  46. return this.cellRange === null;
  47. }
  48. /**
  49. * Adds a cell coords to the selection
  50. *
  51. * @param {CellCoords} coords
  52. */
  53. }, {
  54. key: 'add',
  55. value: function add(coords) {
  56. if (this.isEmpty()) {
  57. this.cellRange = new CellRange(coords, coords, coords);
  58. } else {
  59. this.cellRange.expand(coords);
  60. }
  61. }
  62. /**
  63. * If selection range from or to property equals oldCoords, replace it with newCoords. Return boolean
  64. * information about success
  65. *
  66. * @param {CellCoords} oldCoords
  67. * @param {CellCoords} newCoords
  68. * @returns {Boolean}
  69. */
  70. }, {
  71. key: 'replace',
  72. value: function replace(oldCoords, newCoords) {
  73. if (!this.isEmpty()) {
  74. if (this.cellRange.from.isEqual(oldCoords)) {
  75. this.cellRange.from = newCoords;
  76. return true;
  77. }
  78. if (this.cellRange.to.isEqual(oldCoords)) {
  79. this.cellRange.to = newCoords;
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * Clears selection
  87. */
  88. }, {
  89. key: 'clear',
  90. value: function clear() {
  91. this.cellRange = null;
  92. }
  93. /**
  94. * Returns the top left (TL) and bottom right (BR) selection coordinates
  95. *
  96. * @returns {Array} Returns array of coordinates for example `[1, 1, 5, 5]`
  97. */
  98. }, {
  99. key: 'getCorners',
  100. value: function getCorners() {
  101. var topLeft = this.cellRange.getTopLeftCorner();
  102. var bottomRight = this.cellRange.getBottomRightCorner();
  103. return [topLeft.row, topLeft.col, bottomRight.row, bottomRight.col];
  104. }
  105. /**
  106. * Adds class name to cell element at given coords
  107. *
  108. * @param {Walkontable} wotInstance Walkontable instance
  109. * @param {Number} sourceRow Cell row coord
  110. * @param {Number} sourceColumn Cell column coord
  111. * @param {String} className Class name
  112. */
  113. }, {
  114. key: 'addClassAtCoords',
  115. value: function addClassAtCoords(wotInstance, sourceRow, sourceColumn, className) {
  116. var TD = wotInstance.wtTable.getCell(new CellCoords(sourceRow, sourceColumn));
  117. if ((typeof TD === 'undefined' ? 'undefined' : _typeof(TD)) === 'object') {
  118. addClass(TD, className);
  119. }
  120. }
  121. /**
  122. * @param wotInstance
  123. */
  124. }, {
  125. key: 'draw',
  126. value: function draw(wotInstance) {
  127. if (this.isEmpty()) {
  128. if (this.settings.border) {
  129. var border = this.getBorder(wotInstance);
  130. if (border) {
  131. border.disappear();
  132. }
  133. }
  134. return;
  135. }
  136. var renderedRows = wotInstance.wtTable.getRenderedRowsCount();
  137. var renderedColumns = wotInstance.wtTable.getRenderedColumnsCount();
  138. var corners = this.getCorners();
  139. var sourceRow = void 0,
  140. sourceCol = void 0,
  141. TH = void 0;
  142. for (var column = 0; column < renderedColumns; column++) {
  143. sourceCol = wotInstance.wtTable.columnFilter.renderedToSource(column);
  144. if (sourceCol >= corners[1] && sourceCol <= corners[3]) {
  145. TH = wotInstance.wtTable.getColumnHeader(sourceCol);
  146. if (TH) {
  147. var newClasses = [];
  148. if (this.settings.highlightHeaderClassName) {
  149. newClasses.push(this.settings.highlightHeaderClassName);
  150. }
  151. if (this.settings.highlightColumnClassName) {
  152. newClasses.push(this.settings.highlightColumnClassName);
  153. }
  154. addClass(TH, newClasses);
  155. }
  156. }
  157. }
  158. for (var row = 0; row < renderedRows; row++) {
  159. sourceRow = wotInstance.wtTable.rowFilter.renderedToSource(row);
  160. if (sourceRow >= corners[0] && sourceRow <= corners[2]) {
  161. TH = wotInstance.wtTable.getRowHeader(sourceRow);
  162. if (TH) {
  163. var _newClasses = [];
  164. if (this.settings.highlightHeaderClassName) {
  165. _newClasses.push(this.settings.highlightHeaderClassName);
  166. }
  167. if (this.settings.highlightRowClassName) {
  168. _newClasses.push(this.settings.highlightRowClassName);
  169. }
  170. addClass(TH, _newClasses);
  171. }
  172. }
  173. for (var _column = 0; _column < renderedColumns; _column++) {
  174. sourceCol = wotInstance.wtTable.columnFilter.renderedToSource(_column);
  175. if (sourceRow >= corners[0] && sourceRow <= corners[2] && sourceCol >= corners[1] && sourceCol <= corners[3]) {
  176. // selected cell
  177. if (this.settings.className) {
  178. this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.className);
  179. }
  180. } else if (sourceRow >= corners[0] && sourceRow <= corners[2]) {
  181. // selection is in this row
  182. if (this.settings.highlightRowClassName) {
  183. this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.highlightRowClassName);
  184. }
  185. } else if (sourceCol >= corners[1] && sourceCol <= corners[3]) {
  186. // selection is in this column
  187. if (this.settings.highlightColumnClassName) {
  188. this.addClassAtCoords(wotInstance, sourceRow, sourceCol, this.settings.highlightColumnClassName);
  189. }
  190. }
  191. }
  192. }
  193. wotInstance.getSetting('onBeforeDrawBorders', corners, this.settings.className);
  194. if (this.settings.border) {
  195. var _border = this.getBorder(wotInstance);
  196. if (_border) {
  197. // warning! border.appear modifies corners!
  198. _border.appear(corners);
  199. }
  200. }
  201. }
  202. }]);
  203. return Selection;
  204. }();
  205. export default Selection;