range.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { CellRange } from './../3rdparty/walkontable/src';
  2. /**
  3. * The SelectionRange class is a simple CellRanges collection designed for easy manipulation of the multiple
  4. * consecutive and non-consecutive selections.
  5. *
  6. * @class SelectionRange
  7. * @util
  8. */
  9. class SelectionRange {
  10. constructor() {
  11. /**
  12. * List of all CellRanges added to the class instance.
  13. *
  14. * @type {CellRange[]}
  15. */
  16. this.ranges = [];
  17. }
  18. /**
  19. * Check if selected range is empty.
  20. *
  21. * @return {Boolean}
  22. */
  23. isEmpty() {
  24. return this.size() === 0;
  25. }
  26. /**
  27. * Set coordinates to the class instance. It clears all previously added coordinates and push `coords`
  28. * to the collection.
  29. *
  30. * @param {CellCoords} coords The CellCoords instance with defined visual coordinates.
  31. * @returns {SelectionRange}
  32. */
  33. set(coords) {
  34. this.clear();
  35. this.ranges.push(new CellRange(coords));
  36. return this;
  37. }
  38. /**
  39. * Add coordinates to the class instance. The new coordinates are added to the end of the range collection.
  40. *
  41. * @param {CellCoords} coords The CellCoords instance with defined visual coordinates.
  42. * @returns {SelectionRange}
  43. */
  44. add(coords) {
  45. this.ranges.push(new CellRange(coords));
  46. return this;
  47. }
  48. /**
  49. * Get last added coordinates from ranges, it returns a CellRange instance.
  50. *
  51. * @return {CellRange|undefined}
  52. */
  53. current() {
  54. return this.peekByIndex(0);
  55. }
  56. /**
  57. * Get previously added coordinates from ranges, it returns a CellRange instance.
  58. *
  59. * @return {CellRange|undefined}
  60. */
  61. previous() {
  62. return this.peekByIndex(-1);
  63. }
  64. /**
  65. * Returns `true` if coords is within selection coords. This method iterates through all selection layers to check if
  66. * the coords object is within selection range.
  67. *
  68. * @param {CellCoords} coords The CellCoords instance with defined visual coordinates.
  69. * @returns {Boolean}
  70. */
  71. includes(coords) {
  72. return this.ranges.some(cellRange => cellRange.includes(coords));
  73. }
  74. /**
  75. * Clear collection.
  76. *
  77. * @return {SelectionRange}
  78. */
  79. clear() {
  80. this.ranges.length = 0;
  81. return this;
  82. }
  83. /**
  84. * Get count of added all coordinates added to the selection.
  85. *
  86. * @return {Number}
  87. */
  88. size() {
  89. return this.ranges.length;
  90. }
  91. /**
  92. * Peek the coordinates based on the offset where that coordinate resides in the collection.
  93. *
  94. * @param {Number} [offset=0] An offset where the coordinate will be retrieved from.
  95. * @return {CellRange|undefined}
  96. */
  97. peekByIndex(offset = 0) {
  98. const rangeIndex = this.size() + offset - 1;
  99. let cellRange;
  100. if (rangeIndex >= 0) {
  101. cellRange = this.ranges[rangeIndex];
  102. }
  103. return cellRange;
  104. }
  105. [Symbol.iterator]() {
  106. return this.ranges[Symbol.iterator]();
  107. }
  108. }
  109. export default SelectionRange;