d56f5ffe364f726acaee9e5295838b95a84fe23e6320727b948f70e3182bbe582984cfeaaeeb4a8af26a4c1041b281476a1980edf45e7be354141509bb1085 15 KB

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