| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2019, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { DataModel } from './datamodel'; |
| 11 |
|
| 12 |
import { SelectionModel } from './selectionmodel'; |
| 13 |
|
| 14 |
/** |
| 15 |
* A basic selection model implementation. |
| 16 |
* |
| 17 |
* #### Notes |
| 18 |
* This selection model is sufficient for most use cases where |
| 19 |
* structural knowledge of the data source is *not* required. |
| 20 |
*/ |
| 21 |
export class BasicSelectionModel extends SelectionModel { |
| 22 |
/** |
| 23 |
* Whether the selection model is empty. |
| 24 |
*/ |
| 25 |
get isEmpty(): boolean { |
| 26 |
return this._selections.length === 0; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* The row index of the cursor. |
| 31 |
*/ |
| 32 |
get cursorRow(): number { |
| 33 |
return this._cursorRow; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* The column index of the cursor. |
| 38 |
*/ |
| 39 |
get cursorColumn(): number { |
| 40 |
return this._cursorColumn; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Move cursor down/up/left/right while making sure it remains |
| 45 |
* within the bounds of selected rectangles |
| 46 |
* |
| 47 |
* @param direction - The direction of the movement. |
| 48 |
*/ |
| 49 |
moveCursorWithinSelections( |
| 50 |
direction: SelectionModel.CursorMoveDirection |
| 51 |
): void { |
| 52 |
// Bail early if there are no selections or no existing cursor |
| 53 |
if (this.isEmpty || this.cursorRow === -1 || this._cursorColumn === -1) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
// Bail early if only single cell is selected |
| 58 |
const firstSelection = this._selections[0]; |
| 59 |
if ( |
| 60 |
this._selections.length === 1 && |
| 61 |
firstSelection.r1 === firstSelection.r2 && |
| 62 |
firstSelection.c1 === firstSelection.c2 |
| 63 |
) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// start from last selection rectangle |
| 68 |
if (this._cursorRectIndex === -1) { |
| 69 |
this._cursorRectIndex = this._selections.length - 1; |
| 70 |
} |
| 71 |
|
| 72 |
let cursorRect = this._selections[this._cursorRectIndex]; |
| 73 |
const dr = direction === 'down' ? 1 : direction === 'up' ? -1 : 0; |
| 74 |
const dc = direction === 'right' ? 1 : direction === 'left' ? -1 : 0; |
| 75 |
let newRow = this._cursorRow + dr; |
| 76 |
let newColumn = this._cursorColumn + dc; |
| 77 |
const r1 = Math.min(cursorRect.r1, cursorRect.r2); |
| 78 |
const r2 = Math.max(cursorRect.r1, cursorRect.r2); |
| 79 |
const c1 = Math.min(cursorRect.c1, cursorRect.c2); |
| 80 |
const c2 = Math.max(cursorRect.c1, cursorRect.c2); |
| 81 |
|
| 82 |
const moveToNextRect = () => { |
| 83 |
this._cursorRectIndex = |
| 84 |
(this._cursorRectIndex + 1) % this._selections.length; |
| 85 |
cursorRect = this._selections[this._cursorRectIndex]; |
| 86 |
newRow = Math.min(cursorRect.r1, cursorRect.r2); |
| 87 |
newColumn = Math.min(cursorRect.c1, cursorRect.c2); |
| 88 |
}; |
| 89 |
|
| 90 |
const moveToPreviousRect = () => { |
| 91 |
this._cursorRectIndex = |
| 92 |
this._cursorRectIndex === 0 |
| 93 |
? this._selections.length - 1 |
| 94 |
: this._cursorRectIndex - 1; |
| 95 |
cursorRect = this._selections[this._cursorRectIndex]; |
| 96 |
newRow = Math.max(cursorRect.r1, cursorRect.r2); |
| 97 |
newColumn = Math.max(cursorRect.c1, cursorRect.c2); |
| 98 |
}; |
| 99 |
|
| 100 |
if (newRow > r2) { |
| 101 |
newRow = r1; |
| 102 |
newColumn += 1; |
| 103 |
if (newColumn > c2) { |
| 104 |
moveToNextRect(); |
| 105 |
} |
| 106 |
} else if (newRow < r1) { |
| 107 |
newRow = r2; |
| 108 |
newColumn -= 1; |
| 109 |
if (newColumn < c1) { |
| 110 |
moveToPreviousRect(); |
| 111 |
} |
| 112 |
} else if (newColumn > c2) { |
| 113 |
newColumn = c1; |
| 114 |
newRow += 1; |
| 115 |
if (newRow > r2) { |
| 116 |
moveToNextRect(); |
| 117 |
} |
| 118 |
} else if (newColumn < c1) { |
| 119 |
newColumn = c2; |
| 120 |
newRow -= 1; |
| 121 |
if (newRow < r1) { |
| 122 |
moveToPreviousRect(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
this._cursorRow = newRow; |
| 127 |
this._cursorColumn = newColumn; |
| 128 |
|
| 129 |
// Emit the changed signal. |
| 130 |
this.emitChanged(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Get the current selection in the selection model. |
| 135 |
* |
| 136 |
* @returns The current selection or `null`. |
| 137 |
* |
| 138 |
* #### Notes |
| 139 |
* This is the selection which holds the cursor. |
| 140 |
*/ |
| 141 |
currentSelection(): SelectionModel.Selection | null { |
| 142 |
return this._selections[this._selections.length - 1] || null; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Get an iterator of the selections in the model. |
| 147 |
* |
| 148 |
* @returns A new iterator of the current selections. |
| 149 |
* |
| 150 |
* #### Notes |
| 151 |
* The data grid will render the selections in order. |
| 152 |
*/ |
| 153 |
*selections(): IterableIterator<SelectionModel.Selection> { |
| 154 |
yield* this._selections; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Select the specified cells. |
| 159 |
* |
| 160 |
* @param args - The arguments for the selection. |
| 161 |
*/ |
| 162 |
select(args: SelectionModel.SelectArgs): void { |
| 163 |
// Fetch the current row and column counts; |
| 164 |
let rowCount = this.dataModel.rowCount('body'); |
| 165 |
let columnCount = this.dataModel.columnCount('body'); |
| 166 |
|
| 167 |
// Bail early if there is no content. |
| 168 |
if (rowCount <= 0 || columnCount <= 0) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
// Unpack the arguments. |
| 173 |
let { r1, c1, r2, c2, cursorRow, cursorColumn, clear } = args; |
| 174 |
|
| 175 |
// Clear the necessary selections. |
| 176 |
if (clear === 'all') { |
| 177 |
this._selections.length = 0; |
| 178 |
} else if (clear === 'current') { |
| 179 |
this._selections.pop(); |
| 180 |
} |
| 181 |
|
| 182 |
// Clamp to the data model bounds. |
| 183 |
r1 = Math.max(0, Math.min(r1, rowCount - 1)); |
| 184 |
r2 = Math.max(0, Math.min(r2, rowCount - 1)); |
| 185 |
c1 = Math.max(0, Math.min(c1, columnCount - 1)); |
| 186 |
c2 = Math.max(0, Math.min(c2, columnCount - 1)); |
| 187 |
|
| 188 |
// Indicate if a row/column has already been selected. |
| 189 |
let alreadySelected = false; |
| 190 |
|
| 191 |
// Handle the selection mode. |
| 192 |
if (this.selectionMode === 'row') { |
| 193 |
c1 = 0; |
| 194 |
c2 = columnCount - 1; |
| 195 |
alreadySelected = |
| 196 |
this._selections.filter(selection => selection.r1 === r1).length !== 0; |
| 197 |
// Remove from selections if already selected. |
| 198 |
this._selections = alreadySelected |
| 199 |
? this._selections.filter(selection => selection.r1 !== r1) |
| 200 |
: this._selections; |
| 201 |
} else if (this.selectionMode === 'column') { |
| 202 |
r1 = 0; |
| 203 |
r2 = rowCount - 1; |
| 204 |
alreadySelected = |
| 205 |
this._selections.filter(selection => selection.c1 === c1).length !== 0; |
| 206 |
// Remove from selections if already selected. |
| 207 |
this._selections = alreadySelected |
| 208 |
? this._selections.filter(selection => selection.c1 !== c1) |
| 209 |
: this._selections; |
| 210 |
} |
| 211 |
|
| 212 |
// Alias the cursor row and column. |
| 213 |
let cr = cursorRow; |
| 214 |
let cc = cursorColumn; |
| 215 |
|
| 216 |
// Compute the new cursor location. |
| 217 |
if (cr < 0 || (cr < r1 && cr < r2) || (cr > r1 && cr > r2)) { |
| 218 |
cr = r1; |
| 219 |
} |
| 220 |
if (cc < 0 || (cc < c1 && cc < c2) || (cc > c1 && cc > c2)) { |
| 221 |
cc = c1; |
| 222 |
} |
| 223 |
|
| 224 |
// Update the cursor. |
| 225 |
this._cursorRow = cr; |
| 226 |
this._cursorColumn = cc; |
| 227 |
this._cursorRectIndex = this._selections.length; |
| 228 |
|
| 229 |
// Add the new selection if it wasn't already selected. |
| 230 |
if (!alreadySelected) { |
| 231 |
this._selections.push({ r1, c1, r2, c2 }); |
| 232 |
} |
| 233 |
|
| 234 |
// Emit the changed signal. |
| 235 |
this.emitChanged(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Clear all selections in the selection model. |
| 240 |
*/ |
| 241 |
clear(): void { |
| 242 |
// Bail early if there are no selections. |
| 243 |
if (this._selections.length === 0) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
// Reset the internal state. |
| 248 |
this._cursorRow = -1; |
| 249 |
this._cursorColumn = -1; |
| 250 |
this._cursorRectIndex = -1; |
| 251 |
this._selections.length = 0; |
| 252 |
|
| 253 |
// Emit the changed signal. |
| 254 |
this.emitChanged(); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* A signal handler for the data model `changed` signal. |
| 259 |
* |
| 260 |
* @param args - The arguments for the signal. |
| 261 |
*/ |
| 262 |
protected onDataModelChanged( |
| 263 |
sender: DataModel, |
| 264 |
args: DataModel.ChangedArgs |
| 265 |
): void { |
| 266 |
// Bail early if the model has no current selections. |
| 267 |
if (this._selections.length === 0) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
// Bail early if the cells have changed in place. |
| 272 |
if (args.type === 'cells-changed') { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
// Bail early if there is no change to the row or column count. |
| 277 |
if (args.type === 'rows-moved' || args.type === 'columns-moved') { |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
// Fetch the last row and column index. |
| 282 |
let lr = sender.rowCount('body') - 1; |
| 283 |
let lc = sender.columnCount('body') - 1; |
| 284 |
|
| 285 |
// Bail early if the data model is empty. |
| 286 |
if (lr < 0 || lc < 0) { |
| 287 |
this._selections.length = 0; |
| 288 |
this.emitChanged(); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// Fetch the selection mode. |
| 293 |
let mode = this.selectionMode; |
| 294 |
|
| 295 |
// Set up the assignment index variable. |
| 296 |
let j = 0; |
| 297 |
|
| 298 |
// Iterate over the current selections. |
| 299 |
for (let i = 0, n = this._selections.length; i < n; ++i) { |
| 300 |
// Unpack the selection. |
| 301 |
let { r1, c1, r2, c2 } = this._selections[i]; |
| 302 |
|
| 303 |
// Skip the selection if it will disappear. |
| 304 |
if ((lr < r1 && lr < r2) || (lc < c1 && lc < c2)) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
// Modify the bounds based on the selection mode. |
| 309 |
if (mode === 'row') { |
| 310 |
r1 = Math.max(0, Math.min(r1, lr)); |
| 311 |
r2 = Math.max(0, Math.min(r2, lr)); |
| 312 |
c1 = 0; |
| 313 |
c2 = lc; |
| 314 |
} else if (mode === 'column') { |
| 315 |
r1 = 0; |
| 316 |
r2 = lr; |
| 317 |
c1 = Math.max(0, Math.min(c1, lc)); |
| 318 |
c2 = Math.max(0, Math.min(c2, lc)); |
| 319 |
} else { |
| 320 |
r1 = Math.max(0, Math.min(r1, lr)); |
| 321 |
r2 = Math.max(0, Math.min(r2, lr)); |
| 322 |
c1 = Math.max(0, Math.min(c1, lc)); |
| 323 |
c2 = Math.max(0, Math.min(c2, lc)); |
| 324 |
} |
| 325 |
|
| 326 |
// Assign the modified selection to the array. |
| 327 |
this._selections[j++] = { r1, c1, r2, c2 }; |
| 328 |
} |
| 329 |
|
| 330 |
// Remove the stale selections. |
| 331 |
this._selections.length = j; |
| 332 |
|
| 333 |
// Emit the changed signal. |
| 334 |
this.emitChanged(); |
| 335 |
} |
| 336 |
|
| 337 |
private _cursorRow = -1; |
| 338 |
private _cursorColumn = -1; |
| 339 |
private _cursorRectIndex = -1; |
| 340 |
private _selections: SelectionModel.Selection[] = []; |
| 341 |
} |