7a11253ce3adee322781b2ccc2408291cb55d4f0fe6782af6dce98e7bda50449f6184c20ab490faddca8c13bd9d2ae35d5f0a7253a34d7b69febfe6a9c25c2 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import CellCoords from './../cell/coords';
  2. /**
  3. * A cell range is a set of exactly two CellCoords (that can be the same or different)
  4. *
  5. * @class CellRange
  6. */
  7. class CellRange {
  8. /**
  9. * @param {CellCoords} highlight Used to draw bold border around a cell where selection was
  10. * started and to edit the cell when you press Enter
  11. * @param {CellCoords} from Usually the same as highlight, but in Excel there is distinction - one can change
  12. * highlight within a selection
  13. * @param {CellCoords} to End selection
  14. */
  15. constructor(highlight, from, to) {
  16. this.highlight = highlight;
  17. this.from = from;
  18. this.to = to;
  19. }
  20. /**
  21. * Checks if given coords are valid in context of a given Walkontable instance
  22. *
  23. * @param {Walkontable} wotInstance
  24. * @returns {Boolean}
  25. */
  26. isValid(wotInstance) {
  27. return this.from.isValid(wotInstance) && this.to.isValid(wotInstance);
  28. }
  29. /**
  30. * Checks if this cell range is restricted to one cell
  31. *
  32. * @returns {Boolean}
  33. */
  34. isSingle() {
  35. return this.from.row === this.to.row && this.from.col === this.to.col;
  36. }
  37. /**
  38. * Returns selected range height (in number of rows)
  39. *
  40. * @returns {Number}
  41. */
  42. getHeight() {
  43. return Math.max(this.from.row, this.to.row) - Math.min(this.from.row, this.to.row) + 1;
  44. }
  45. /**
  46. * Returns selected range width (in number of columns)
  47. *
  48. * @returns {Number}
  49. */
  50. getWidth() {
  51. return Math.max(this.from.col, this.to.col) - Math.min(this.from.col, this.to.col) + 1;
  52. }
  53. /**
  54. * Checks if given cell coords is within `from` and `to` cell coords of this range
  55. *
  56. * @param {CellCoords} cellCoords
  57. * @returns {Boolean}
  58. */
  59. includes(cellCoords) {
  60. let {row, col} = cellCoords;
  61. let topLeft = this.getTopLeftCorner();
  62. let bottomRight = this.getBottomRightCorner();
  63. return topLeft.row <= row && bottomRight.row >= row &&
  64. topLeft.col <= col && bottomRight.col >= col;
  65. }
  66. /**
  67. * Checks if given range is within of this range
  68. *
  69. * @param {CellRange} testedRange
  70. * @returns {Boolean}
  71. */
  72. includesRange(testedRange) {
  73. return this.includes(testedRange.getTopLeftCorner()) && this.includes(testedRange.getBottomRightCorner());
  74. }
  75. /**
  76. * Checks if given range is equal to this range
  77. *
  78. * @param {CellRange} testedRange
  79. * @returns {Boolean}
  80. */
  81. isEqual(testedRange) {
  82. return (Math.min(this.from.row, this.to.row) == Math.min(testedRange.from.row, testedRange.to.row)) &&
  83. (Math.max(this.from.row, this.to.row) == Math.max(testedRange.from.row, testedRange.to.row)) &&
  84. (Math.min(this.from.col, this.to.col) == Math.min(testedRange.from.col, testedRange.to.col)) &&
  85. (Math.max(this.from.col, this.to.col) == Math.max(testedRange.from.col, testedRange.to.col));
  86. }
  87. /**
  88. * Checks if tested range overlaps with the range.
  89. * Range A is considered to to be overlapping with range B if intersection of A and B or B and A is not empty.
  90. *
  91. * @param {CellRange} testedRange
  92. * @returns {Boolean}
  93. */
  94. overlaps(testedRange) {
  95. return testedRange.isSouthEastOf(this.getTopLeftCorner()) && testedRange.isNorthWestOf(this.getBottomRightCorner());
  96. }
  97. /**
  98. * @param {CellRange} testedCoords
  99. * @returns {Boolean}
  100. */
  101. isSouthEastOf(testedCoords) {
  102. return this.getTopLeftCorner().isSouthEastOf(testedCoords) || this.getBottomRightCorner().isSouthEastOf(testedCoords);
  103. }
  104. /**
  105. * @param {CellRange} testedCoords
  106. * @returns {Boolean}
  107. */
  108. isNorthWestOf(testedCoords) {
  109. return this.getTopLeftCorner().isNorthWestOf(testedCoords) || this.getBottomRightCorner().isNorthWestOf(testedCoords);
  110. }
  111. /**
  112. * Adds a cell to a range (only if exceeds corners of the range). Returns information if range was expanded
  113. *
  114. * @param {CellCoords} cellCoords
  115. * @returns {Boolean}
  116. */
  117. expand(cellCoords) {
  118. let topLeft = this.getTopLeftCorner();
  119. let bottomRight = this.getBottomRightCorner();
  120. if (cellCoords.row < topLeft.row || cellCoords.col < topLeft.col ||
  121. cellCoords.row > bottomRight.row || cellCoords.col > bottomRight.col) {
  122. this.from = new CellCoords(Math.min(topLeft.row, cellCoords.row), Math.min(topLeft.col, cellCoords.col));
  123. this.to = new CellCoords(Math.max(bottomRight.row, cellCoords.row), Math.max(bottomRight.col, cellCoords.col));
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * @param {CellRange} expandingRange
  130. * @returns {Boolean}
  131. */
  132. expandByRange(expandingRange) {
  133. if (this.includesRange(expandingRange) || !this.overlaps(expandingRange)) {
  134. return false;
  135. }
  136. let topLeft = this.getTopLeftCorner();
  137. let bottomRight = this.getBottomRightCorner();
  138. let topRight = this.getTopRightCorner();
  139. let bottomLeft = this.getBottomLeftCorner();
  140. let expandingTopLeft = expandingRange.getTopLeftCorner();
  141. let expandingBottomRight = expandingRange.getBottomRightCorner();
  142. let resultTopRow = Math.min(topLeft.row, expandingTopLeft.row);
  143. let resultTopCol = Math.min(topLeft.col, expandingTopLeft.col);
  144. let resultBottomRow = Math.max(bottomRight.row, expandingBottomRight.row);
  145. let resultBottomCol = Math.max(bottomRight.col, expandingBottomRight.col);
  146. let finalFrom = new CellCoords(resultTopRow, resultTopCol),
  147. finalTo = new CellCoords(resultBottomRow, resultBottomCol);
  148. let isCorner = new CellRange(finalFrom, finalFrom, finalTo).isCorner(this.from, expandingRange),
  149. onlyMerge = expandingRange.isEqual(new CellRange(finalFrom, finalFrom, finalTo));
  150. if (isCorner && !onlyMerge) {
  151. if (this.from.col > finalFrom.col) {
  152. finalFrom.col = resultBottomCol;
  153. finalTo.col = resultTopCol;
  154. }
  155. if (this.from.row > finalFrom.row) {
  156. finalFrom.row = resultBottomRow;
  157. finalTo.row = resultTopRow;
  158. }
  159. }
  160. this.from = finalFrom;
  161. this.to = finalTo;
  162. return true;
  163. }
  164. /**
  165. * @returns {String}
  166. */
  167. getDirection() {
  168. if (this.from.isNorthWestOf(this.to)) { // NorthWest - SouthEast
  169. return 'NW-SE';
  170. } else if (this.from.isNorthEastOf(this.to)) { // NorthEast - SouthWest
  171. return 'NE-SW';
  172. } else if (this.from.isSouthEastOf(this.to)) { // SouthEast - NorthWest
  173. return 'SE-NW';
  174. } else if (this.from.isSouthWestOf(this.to)) { // SouthWest - NorthEast
  175. return 'SW-NE';
  176. }
  177. }
  178. /**
  179. * @param {String} direction
  180. */
  181. setDirection(direction) {
  182. switch (direction) {
  183. case 'NW-SE':
  184. [this.from, this.to] = [this.getTopLeftCorner(), this.getBottomRightCorner()];
  185. break;
  186. case 'NE-SW':
  187. [this.from, this.to] = [this.getTopRightCorner(), this.getBottomLeftCorner()];
  188. break;
  189. case 'SE-NW':
  190. [this.from, this.to] = [this.getBottomRightCorner(), this.getTopLeftCorner()];
  191. break;
  192. case 'SW-NE':
  193. [this.from, this.to] = [this.getBottomLeftCorner(), this.getTopRightCorner()];
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. /**
  200. * Get top left corner of this range
  201. *
  202. * @returns {CellCoords}
  203. */
  204. getTopLeftCorner() {
  205. return new CellCoords(Math.min(this.from.row, this.to.row), Math.min(this.from.col, this.to.col));
  206. }
  207. /**
  208. * Get bottom right corner of this range
  209. *
  210. * @returns {CellCoords}
  211. */
  212. getBottomRightCorner() {
  213. return new CellCoords(Math.max(this.from.row, this.to.row), Math.max(this.from.col, this.to.col));
  214. }
  215. /**
  216. * Get top right corner of this range
  217. *
  218. * @returns {CellCoords}
  219. */
  220. getTopRightCorner() {
  221. return new CellCoords(Math.min(this.from.row, this.to.row), Math.max(this.from.col, this.to.col));
  222. }
  223. /**
  224. * Get bottom left corner of this range
  225. *
  226. * @returns {CellCoords}
  227. */
  228. getBottomLeftCorner() {
  229. return new CellCoords(Math.max(this.from.row, this.to.row), Math.min(this.from.col, this.to.col));
  230. }
  231. /**
  232. * @param {CellCoords} coords
  233. * @param {CellRange} expandedRange
  234. * @returns {*}
  235. */
  236. isCorner(coords, expandedRange) {
  237. if (expandedRange) {
  238. if (expandedRange.includes(coords)) {
  239. if (this.getTopLeftCorner().isEqual(new CellCoords(expandedRange.from.row, expandedRange.from.col)) ||
  240. this.getTopRightCorner().isEqual(new CellCoords(expandedRange.from.row, expandedRange.to.col)) ||
  241. this.getBottomLeftCorner().isEqual(new CellCoords(expandedRange.to.row, expandedRange.from.col)) ||
  242. this.getBottomRightCorner().isEqual(new CellCoords(expandedRange.to.row, expandedRange.to.col))) {
  243. return true;
  244. }
  245. }
  246. }
  247. return coords.isEqual(this.getTopLeftCorner()) || coords.isEqual(this.getTopRightCorner()) ||
  248. coords.isEqual(this.getBottomLeftCorner()) || coords.isEqual(this.getBottomRightCorner());
  249. }
  250. /**
  251. * @param {CellCoords} coords
  252. * @param {CellRange} expandedRange
  253. * @returns {CellCoords}
  254. */
  255. getOppositeCorner(coords, expandedRange) {
  256. if (!(coords instanceof CellCoords)) {
  257. return false;
  258. }
  259. if (expandedRange) {
  260. if (expandedRange.includes(coords)) {
  261. if (this.getTopLeftCorner().isEqual(new CellCoords(expandedRange.from.row, expandedRange.from.col))) {
  262. return this.getBottomRightCorner();
  263. }
  264. if (this.getTopRightCorner().isEqual(new CellCoords(expandedRange.from.row, expandedRange.to.col))) {
  265. return this.getBottomLeftCorner();
  266. }
  267. if (this.getBottomLeftCorner().isEqual(new CellCoords(expandedRange.to.row, expandedRange.from.col))) {
  268. return this.getTopRightCorner();
  269. }
  270. if (this.getBottomRightCorner().isEqual(new CellCoords(expandedRange.to.row, expandedRange.to.col))) {
  271. return this.getTopLeftCorner();
  272. }
  273. }
  274. }
  275. if (coords.isEqual(this.getBottomRightCorner())) {
  276. return this.getTopLeftCorner();
  277. } else if (coords.isEqual(this.getTopLeftCorner())) {
  278. return this.getBottomRightCorner();
  279. } else if (coords.isEqual(this.getTopRightCorner())) {
  280. return this.getBottomLeftCorner();
  281. } else if (coords.isEqual(this.getBottomLeftCorner())) {
  282. return this.getTopRightCorner();
  283. }
  284. }
  285. /**
  286. * @param {CellRange} range
  287. * @returns {Array}
  288. */
  289. getBordersSharedWith(range) {
  290. if (!this.includesRange(range)) {
  291. return [];
  292. }
  293. const thisBorders = {
  294. top: Math.min(this.from.row, this.to.row),
  295. bottom: Math.max(this.from.row, this.to.row),
  296. left: Math.min(this.from.col, this.to.col),
  297. right: Math.max(this.from.col, this.to.col)
  298. };
  299. const rangeBorders = {
  300. top: Math.min(range.from.row, range.to.row),
  301. bottom: Math.max(range.from.row, range.to.row),
  302. left: Math.min(range.from.col, range.to.col),
  303. right: Math.max(range.from.col, range.to.col)
  304. };
  305. const result = [];
  306. if (thisBorders.top == rangeBorders.top) {
  307. result.push('top');
  308. }
  309. if (thisBorders.right == rangeBorders.right) {
  310. result.push('right');
  311. }
  312. if (thisBorders.bottom == rangeBorders.bottom) {
  313. result.push('bottom');
  314. }
  315. if (thisBorders.left == rangeBorders.left) {
  316. result.push('left');
  317. }
  318. return result;
  319. }
  320. /**
  321. * Get inner selected cell coords defined by this range
  322. *
  323. * @returns {Array}
  324. */
  325. getInner() {
  326. let topLeft = this.getTopLeftCorner();
  327. let bottomRight = this.getBottomRightCorner();
  328. let out = [];
  329. for (let r = topLeft.row; r <= bottomRight.row; r++) {
  330. for (let c = topLeft.col; c <= bottomRight.col; c++) {
  331. if (!(this.from.row === r && this.from.col === c) && !(this.to.row === r && this.to.col === c)) {
  332. out.push(new CellCoords(r, c));
  333. }
  334. }
  335. }
  336. return out;
  337. }
  338. /**
  339. * Get all selected cell coords defined by this range
  340. *
  341. * @returns {Array}
  342. */
  343. getAll() {
  344. let topLeft = this.getTopLeftCorner();
  345. let bottomRight = this.getBottomRightCorner();
  346. let out = [];
  347. for (let r = topLeft.row; r <= bottomRight.row; r++) {
  348. for (let c = topLeft.col; c <= bottomRight.col; c++) {
  349. if (topLeft.row === r && topLeft.col === c) {
  350. out.push(topLeft);
  351. } else if (bottomRight.row === r && bottomRight.col === c) {
  352. out.push(bottomRight);
  353. } else {
  354. out.push(new CellCoords(r, c));
  355. }
  356. }
  357. }
  358. return out;
  359. }
  360. /**
  361. * Runs a callback function against all cells in the range. You can break the iteration by returning
  362. * `false` in the callback function
  363. *
  364. * @param callback {Function}
  365. */
  366. forAll(callback) {
  367. let topLeft = this.getTopLeftCorner();
  368. let bottomRight = this.getBottomRightCorner();
  369. for (let r = topLeft.row; r <= bottomRight.row; r++) {
  370. for (let c = topLeft.col; c <= bottomRight.col; c++) {
  371. let breakIteration = callback(r, c);
  372. if (breakIteration === false) {
  373. return;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. export default CellRange;