8ac4077595608047cc02f42a32c01195c1206862165d640cdcb96552647806b7ea56593a67620372da132999a32d51cefbf4e0398679c152ec1cde239f645f 8.0 KB

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