| 1 |
/* |
| 2 |
* Copyright (c) Jupyter Development Team. |
| 3 |
* Distributed under the terms of the Modified BSD License. |
| 4 |
*/ |
| 5 |
|
| 6 |
import { DataModel } from './datamodel'; |
| 7 |
|
| 8 |
/** |
| 9 |
* An interface describing a merged cell group. |
| 10 |
* r1: start row |
| 11 |
* r2: end row |
| 12 |
* c1: start column |
| 13 |
* c2: end column |
| 14 |
*/ |
| 15 |
export interface CellGroup { |
| 16 |
r1: number; |
| 17 |
r2: number; |
| 18 |
c1: number; |
| 19 |
c2: number; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* A collection of helper functions relating to merged cell groups |
| 24 |
*/ |
| 25 |
export namespace CellGroup { |
| 26 |
/** |
| 27 |
* Checks if two cell-groups are intersecting |
| 28 |
* in the given axis. |
| 29 |
* @param group1 |
| 30 |
* @param group2 |
| 31 |
* @param axis |
| 32 |
*/ |
| 33 |
export function areCellGroupsIntersectingAtAxis( |
| 34 |
group1: CellGroup, |
| 35 |
group2: CellGroup, |
| 36 |
axis: 'row' | 'column' |
| 37 |
): boolean { |
| 38 |
if (axis === 'row') { |
| 39 |
return ( |
| 40 |
(group1.r1 >= group2.r1 && group1.r1 <= group2.r2) || |
| 41 |
(group1.r2 >= group2.r1 && group1.r2 <= group2.r2) || |
| 42 |
(group2.r1 >= group1.r1 && group2.r1 <= group1.r2) || |
| 43 |
(group2.r2 >= group1.r1 && group2.r2 <= group1.r2) |
| 44 |
); |
| 45 |
} |
| 46 |
return ( |
| 47 |
(group1.c1 >= group2.c1 && group1.c1 <= group2.c2) || |
| 48 |
(group1.c2 >= group2.c1 && group1.c2 <= group2.c2) || |
| 49 |
(group2.c1 >= group1.c1 && group2.c1 <= group1.c2) || |
| 50 |
(group2.c2 >= group1.c1 && group2.c2 <= group1.c2) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Checks if cell-groups are intersecting. |
| 56 |
* @param group1 |
| 57 |
* @param group2 |
| 58 |
*/ |
| 59 |
export function areCellGroupsIntersecting( |
| 60 |
group1: CellGroup, |
| 61 |
group2: CellGroup |
| 62 |
): boolean { |
| 63 |
return ( |
| 64 |
((group1.r1 >= group2.r1 && group1.r1 <= group2.r2) || |
| 65 |
(group1.r2 >= group2.r1 && group1.r2 <= group2.r2) || |
| 66 |
(group2.r1 >= group1.r1 && group2.r1 <= group1.r2) || |
| 67 |
(group2.r2 >= group1.r1 && group2.r2 <= group1.r2)) && |
| 68 |
((group1.c1 >= group2.c1 && group1.c1 <= group2.c2) || |
| 69 |
(group1.c2 >= group2.c1 && group1.c2 <= group2.c2) || |
| 70 |
(group2.c1 >= group1.c1 && group2.c1 <= group1.c2) || |
| 71 |
(group2.c2 >= group1.c1 && group2.c2 <= group1.c2)) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Retrieves the index of the cell-group to which |
| 77 |
* the cell at the given row, column belongs. |
| 78 |
* @param dataModel |
| 79 |
* @param rgn |
| 80 |
* @param row |
| 81 |
* @param column |
| 82 |
*/ |
| 83 |
export function getGroupIndex( |
| 84 |
dataModel: DataModel, |
| 85 |
rgn: DataModel.CellRegion, |
| 86 |
row: number, |
| 87 |
column: number |
| 88 |
): number { |
| 89 |
const numGroups = dataModel.groupCount(rgn); |
| 90 |
for (let i = 0; i < numGroups; i++) { |
| 91 |
const group = dataModel.group(rgn, i)!; |
| 92 |
if ( |
| 93 |
row >= group.r1 && |
| 94 |
row <= group.r2 && |
| 95 |
column >= group.c1 && |
| 96 |
column <= group.c2 |
| 97 |
) { |
| 98 |
return i; |
| 99 |
} |
| 100 |
} |
| 101 |
return -1; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns a cell-group for the given row/index coordinates. |
| 106 |
* @param dataModel |
| 107 |
* @param rgn |
| 108 |
* @param row |
| 109 |
* @param column |
| 110 |
*/ |
| 111 |
export function getGroup( |
| 112 |
dataModel: DataModel, |
| 113 |
rgn: DataModel.CellRegion, |
| 114 |
row: number, |
| 115 |
column: number |
| 116 |
): CellGroup | null { |
| 117 |
const groupIndex = getGroupIndex(dataModel, rgn, row, column); |
| 118 |
if (groupIndex === -1) { |
| 119 |
return null; |
| 120 |
} |
| 121 |
|
| 122 |
return dataModel.group(rgn, groupIndex); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns all cell groups which belong to |
| 127 |
* a given cell cell region. |
| 128 |
* @param dataModel |
| 129 |
* @param rgn |
| 130 |
*/ |
| 131 |
export function getCellGroupsAtRegion( |
| 132 |
dataModel: DataModel, |
| 133 |
rgn: DataModel.CellRegion |
| 134 |
): CellGroup[] { |
| 135 |
let groupsAtRegion: CellGroup[] = []; |
| 136 |
const numGroups = dataModel.groupCount(rgn); |
| 137 |
|
| 138 |
for (let i = 0; i < numGroups; i++) { |
| 139 |
const group = dataModel.group(rgn, i)!; |
| 140 |
groupsAtRegion.push(group); |
| 141 |
} |
| 142 |
return groupsAtRegion; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Calculates and returns a merged cell-group from |
| 147 |
* two cell-group objects. |
| 148 |
* @param groups |
| 149 |
*/ |
| 150 |
export function joinCellGroups(groups: CellGroup[]): CellGroup { |
| 151 |
let startRow = Number.MAX_VALUE; |
| 152 |
let endRow = Number.MIN_VALUE; |
| 153 |
let startColumn = Number.MAX_VALUE; |
| 154 |
let endColumn = Number.MIN_VALUE; |
| 155 |
|
| 156 |
for (const group of groups) { |
| 157 |
startRow = Math.min(startRow, group.r1); |
| 158 |
endRow = Math.max(endRow, group.r2); |
| 159 |
startColumn = Math.min(startColumn, group.c1); |
| 160 |
endColumn = Math.max(endColumn, group.c2); |
| 161 |
} |
| 162 |
|
| 163 |
return { r1: startRow, r2: endRow, c1: startColumn, c2: endColumn }; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Merges a cell group with other cells groups in the |
| 168 |
* same region if they intersect. |
| 169 |
* @param dataModel the data model of the grid. |
| 170 |
* @param group the target cell group. |
| 171 |
* @param region the region of the cell group. |
| 172 |
* @returns a new cell group after merging has happened. |
| 173 |
*/ |
| 174 |
export function joinCellGroupWithMergedCellGroups( |
| 175 |
dataModel: DataModel, |
| 176 |
group: CellGroup, |
| 177 |
region: DataModel.CellRegion |
| 178 |
): CellGroup { |
| 179 |
let joinedGroup: CellGroup = { ...group }; |
| 180 |
|
| 181 |
const mergedCellGroups: CellGroup[] = getCellGroupsAtRegion( |
| 182 |
dataModel, |
| 183 |
region |
| 184 |
); |
| 185 |
|
| 186 |
for (let g = 0; g < mergedCellGroups.length; g++) { |
| 187 |
const mergedGroup = mergedCellGroups[g]; |
| 188 |
if (areCellGroupsIntersecting(joinedGroup, mergedGroup)) { |
| 189 |
joinedGroup = joinCellGroups([joinedGroup, mergedGroup]); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return joinedGroup; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Retrieves a list of cell groups intersecting at |
| 198 |
* a given row. |
| 199 |
* @param dataModel data model of the grid. |
| 200 |
* @param rgn the cell region. |
| 201 |
* @param row the target row to look for intersections at. |
| 202 |
* @returns all cell groups intersecting with the row. |
| 203 |
*/ |
| 204 |
export function getCellGroupsAtRow( |
| 205 |
dataModel: DataModel, |
| 206 |
rgn: DataModel.CellRegion, |
| 207 |
row: number |
| 208 |
): CellGroup[] { |
| 209 |
let groupsAtRow = []; |
| 210 |
const numGroups = dataModel.groupCount(rgn); |
| 211 |
|
| 212 |
for (let i = 0; i < numGroups; i++) { |
| 213 |
const group = dataModel.group(rgn, i)!; |
| 214 |
if (row >= group.r1 && row <= group.r2) { |
| 215 |
groupsAtRow.push(group); |
| 216 |
} |
| 217 |
} |
| 218 |
return groupsAtRow; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Retrieves a list of cell groups intersecting at |
| 223 |
* a given column. |
| 224 |
* @param dataModel data model of the grid. |
| 225 |
* @param rgn the cell region. |
| 226 |
* @param column the target column to look for intersections at. |
| 227 |
* @returns all cell groups intersecting with the column. |
| 228 |
*/ |
| 229 |
export function getCellGroupsAtColumn( |
| 230 |
dataModel: DataModel, |
| 231 |
rgn: DataModel.CellRegion, |
| 232 |
column: number |
| 233 |
): CellGroup[] { |
| 234 |
let groupsAtColumn = []; |
| 235 |
const numGroups = dataModel.groupCount(rgn); |
| 236 |
|
| 237 |
for (let i = 0; i < numGroups; i++) { |
| 238 |
const group = dataModel.group(rgn, i)!; |
| 239 |
if (column >= group.c1 && column <= group.c2) { |
| 240 |
groupsAtColumn.push(group); |
| 241 |
} |
| 242 |
} |
| 243 |
return groupsAtColumn; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Merges a target cell group with any cell groups |
| 248 |
* it intersects with at a given row or column. |
| 249 |
* @param dataModel data model of the grid. |
| 250 |
* @param regions list of cell regions. |
| 251 |
* @param axis row or column. |
| 252 |
* @param group the target cell group. |
| 253 |
* @returns a new merged cell group. |
| 254 |
*/ |
| 255 |
export function joinCellGroupsIntersectingAtAxis( |
| 256 |
dataModel: DataModel, |
| 257 |
regions: DataModel.CellRegion[], |
| 258 |
axis: 'row' | 'column', |
| 259 |
group: CellGroup |
| 260 |
): CellGroup { |
| 261 |
let groupsAtAxis: CellGroup[] = []; |
| 262 |
if (axis === 'row') { |
| 263 |
for (const region of regions) { |
| 264 |
for (let r = group.r1; r <= group.r2; r++) { |
| 265 |
groupsAtAxis = groupsAtAxis.concat( |
| 266 |
CellGroup.getCellGroupsAtRow(dataModel, region, r) |
| 267 |
); |
| 268 |
} |
| 269 |
} |
| 270 |
} else { |
| 271 |
for (const region of regions) { |
| 272 |
for (let c = group.c1; c <= group.c2; c++) { |
| 273 |
groupsAtAxis = groupsAtAxis.concat( |
| 274 |
CellGroup.getCellGroupsAtColumn(dataModel, region, c) |
| 275 |
); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
let mergedGroupAtAxis: CellGroup = CellGroup.joinCellGroups(groupsAtAxis); |
| 281 |
|
| 282 |
if (groupsAtAxis.length > 0) { |
| 283 |
let mergedCellGroups: CellGroup[] = []; |
| 284 |
for (const region of regions) { |
| 285 |
mergedCellGroups = mergedCellGroups.concat( |
| 286 |
CellGroup.getCellGroupsAtRegion(dataModel, region) |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
for (let g = 0; g < mergedCellGroups.length; g++) { |
| 291 |
const group = mergedCellGroups[g]; |
| 292 |
if ( |
| 293 |
CellGroup.areCellGroupsIntersectingAtAxis( |
| 294 |
mergedGroupAtAxis, |
| 295 |
group, |
| 296 |
axis |
| 297 |
) |
| 298 |
) { |
| 299 |
mergedGroupAtAxis = CellGroup.joinCellGroups([ |
| 300 |
group, |
| 301 |
mergedGroupAtAxis |
| 302 |
]); |
| 303 |
mergedCellGroups.splice(g, 1); |
| 304 |
g = 0; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
return mergedGroupAtAxis; |
| 309 |
} |
| 310 |
} |