| 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 { IDisposable } from '@lumino/disposable'; |
| 11 |
|
| 12 |
import { ClipboardExt, ElementExt, Platform } from '@lumino/domutils'; |
| 13 |
|
| 14 |
import { |
| 15 |
ConflatableMessage, |
| 16 |
IMessageHandler, |
| 17 |
Message, |
| 18 |
MessageLoop |
| 19 |
} from '@lumino/messaging'; |
| 20 |
|
| 21 |
import { GridLayout, ScrollBar, Widget } from '@lumino/widgets'; |
| 22 |
|
| 23 |
import { AsyncCellRenderer } from './asynccellrenderer'; |
| 24 |
|
| 25 |
import { CellRenderer } from './cellrenderer'; |
| 26 |
|
| 27 |
import { DataModel, MutableDataModel } from './datamodel'; |
| 28 |
|
| 29 |
import { CellGroup } from './cellgroup'; |
| 30 |
|
| 31 |
import { GraphicsContext } from './graphicscontext'; |
| 32 |
|
| 33 |
import { RendererMap } from './renderermap'; |
| 34 |
|
| 35 |
import { SectionList } from './sectionlist'; |
| 36 |
|
| 37 |
import { SelectionModel } from './selectionmodel'; |
| 38 |
|
| 39 |
import { |
| 40 |
CellEditorController, |
| 41 |
ICellEditorController |
| 42 |
} from './celleditorcontroller'; |
| 43 |
|
| 44 |
import { TextRenderer } from './textrenderer'; |
| 45 |
|
| 46 |
/** |
| 47 |
* A widget which implements a high-performance tabular data grid. |
| 48 |
* |
| 49 |
* #### Notes |
| 50 |
* A data grid is implemented as a composition of child widgets. These |
| 51 |
* child widgets are considered an implementation detail. Manipulating |
| 52 |
* the child widgets of a data grid directly is undefined behavior. |
| 53 |
* |
| 54 |
* This class is not designed to be subclassed. |
| 55 |
* |
| 56 |
* See also the related [example](../../examples/datagrid/index.html) and |
| 57 |
* its [source](https://github.com/jupyterlab/lumino/tree/main/examples/example-datagrid). |
| 58 |
*/ |
| 59 |
export class DataGrid extends Widget { |
| 60 |
/** |
| 61 |
* Construct a new data grid. |
| 62 |
* |
| 63 |
* @param options - The options for initializing the data grid. |
| 64 |
*/ |
| 65 |
constructor(options: DataGrid.IOptions = {}) { |
| 66 |
super(); |
| 67 |
this.addClass('lm-DataGrid'); |
| 68 |
|
| 69 |
// Parse the simple options. |
| 70 |
this._style = options.style || DataGrid.defaultStyle; |
| 71 |
this._stretchLastRow = options.stretchLastRow || false; |
| 72 |
this._stretchLastColumn = options.stretchLastColumn || false; |
| 73 |
this._headerVisibility = options.headerVisibility || 'all'; |
| 74 |
this._cellRenderers = options.cellRenderers || new RendererMap(); |
| 75 |
this._copyConfig = options.copyConfig || DataGrid.defaultCopyConfig; |
| 76 |
|
| 77 |
// Connect to the renderer map changed signal. |
| 78 |
this._cellRenderers.changed.connect(this._onRenderersChanged, this); |
| 79 |
|
| 80 |
// Parse the default sizes. |
| 81 |
let defaultSizes = options.defaultSizes || DataGrid.defaultSizes; |
| 82 |
let minimumSizes = options.minimumSizes || DataGrid.minimumSizes; |
| 83 |
|
| 84 |
// Set up the sections lists. |
| 85 |
this._rowSections = new SectionList({ |
| 86 |
defaultSize: defaultSizes.rowHeight, |
| 87 |
minimumSize: minimumSizes.rowHeight |
| 88 |
}); |
| 89 |
this._columnSections = new SectionList({ |
| 90 |
defaultSize: defaultSizes.columnWidth, |
| 91 |
minimumSize: minimumSizes.columnWidth |
| 92 |
}); |
| 93 |
this._rowHeaderSections = new SectionList({ |
| 94 |
defaultSize: defaultSizes.rowHeaderWidth, |
| 95 |
minimumSize: minimumSizes.rowHeaderWidth |
| 96 |
}); |
| 97 |
this._columnHeaderSections = new SectionList({ |
| 98 |
defaultSize: defaultSizes.columnHeaderHeight, |
| 99 |
minimumSize: minimumSizes.columnHeaderHeight |
| 100 |
}); |
| 101 |
|
| 102 |
// Create the canvas, buffer, and overlay objects. |
| 103 |
this._canvas = Private.createCanvas(); |
| 104 |
this._buffer = Private.createCanvas(); |
| 105 |
this._overlay = Private.createCanvas(); |
| 106 |
|
| 107 |
// Get the graphics contexts for the canvases. |
| 108 |
this._canvasGC = this._canvas.getContext('2d')!; |
| 109 |
this._bufferGC = this._buffer.getContext('2d')!; |
| 110 |
this._overlayGC = this._overlay.getContext('2d')!; |
| 111 |
|
| 112 |
// Set up the on-screen canvas. |
| 113 |
this._canvas.style.position = 'absolute'; |
| 114 |
this._canvas.style.top = '0px'; |
| 115 |
this._canvas.style.left = '0px'; |
| 116 |
this._canvas.style.width = '0px'; |
| 117 |
this._canvas.style.height = '0px'; |
| 118 |
|
| 119 |
// Set up the on-screen overlay. |
| 120 |
this._overlay.style.position = 'absolute'; |
| 121 |
this._overlay.style.top = '0px'; |
| 122 |
this._overlay.style.left = '0px'; |
| 123 |
this._overlay.style.width = '0px'; |
| 124 |
this._overlay.style.height = '0px'; |
| 125 |
|
| 126 |
// Create the internal widgets for the data grid. |
| 127 |
this._viewport = new Widget(); |
| 128 |
this._viewport.node.tabIndex = -1; |
| 129 |
this._viewport.node.style.outline = 'none'; |
| 130 |
this._vScrollBar = new ScrollBar({ orientation: 'vertical' }); |
| 131 |
this._hScrollBar = new ScrollBar({ orientation: 'horizontal' }); |
| 132 |
this._scrollCorner = new Widget(); |
| 133 |
|
| 134 |
this._editorController = new CellEditorController(); |
| 135 |
|
| 136 |
// Add the extra class names to the child widgets. |
| 137 |
this._viewport.addClass('lm-DataGrid-viewport'); |
| 138 |
this._vScrollBar.addClass('lm-DataGrid-scrollBar'); |
| 139 |
this._hScrollBar.addClass('lm-DataGrid-scrollBar'); |
| 140 |
this._scrollCorner.addClass('lm-DataGrid-scrollCorner'); |
| 141 |
|
| 142 |
// Add the on-screen canvas to the viewport node. |
| 143 |
this._viewport.node.appendChild(this._canvas); |
| 144 |
|
| 145 |
// Add the on-screen overlay to the viewport node. |
| 146 |
this._viewport.node.appendChild(this._overlay); |
| 147 |
|
| 148 |
// Install the message hooks. |
| 149 |
MessageLoop.installMessageHook(this._viewport, this); |
| 150 |
MessageLoop.installMessageHook(this._hScrollBar, this); |
| 151 |
MessageLoop.installMessageHook(this._vScrollBar, this); |
| 152 |
|
| 153 |
// Hide the scroll bars and corner from the outset. |
| 154 |
this._vScrollBar.hide(); |
| 155 |
this._hScrollBar.hide(); |
| 156 |
this._scrollCorner.hide(); |
| 157 |
|
| 158 |
// Connect to the scroll bar signals. |
| 159 |
this._vScrollBar.thumbMoved.connect(this._onThumbMoved, this); |
| 160 |
this._hScrollBar.thumbMoved.connect(this._onThumbMoved, this); |
| 161 |
this._vScrollBar.pageRequested.connect(this._onPageRequested, this); |
| 162 |
this._hScrollBar.pageRequested.connect(this._onPageRequested, this); |
| 163 |
this._vScrollBar.stepRequested.connect(this._onStepRequested, this); |
| 164 |
this._hScrollBar.stepRequested.connect(this._onStepRequested, this); |
| 165 |
|
| 166 |
// Set the layout cell config for the child widgets. |
| 167 |
GridLayout.setCellConfig(this._viewport, { row: 0, column: 0 }); |
| 168 |
GridLayout.setCellConfig(this._vScrollBar, { row: 0, column: 1 }); |
| 169 |
GridLayout.setCellConfig(this._hScrollBar, { row: 1, column: 0 }); |
| 170 |
GridLayout.setCellConfig(this._scrollCorner, { row: 1, column: 1 }); |
| 171 |
|
| 172 |
// Create the layout for the data grid. |
| 173 |
let layout = new GridLayout({ |
| 174 |
rowCount: 2, |
| 175 |
columnCount: 2, |
| 176 |
rowSpacing: 0, |
| 177 |
columnSpacing: 0, |
| 178 |
fitPolicy: 'set-no-constraint' |
| 179 |
}); |
| 180 |
|
| 181 |
// Set the stretch factors for the grid. |
| 182 |
layout.setRowStretch(0, 1); |
| 183 |
layout.setRowStretch(1, 0); |
| 184 |
layout.setColumnStretch(0, 1); |
| 185 |
layout.setColumnStretch(1, 0); |
| 186 |
|
| 187 |
// Add the child widgets to the layout. |
| 188 |
layout.addWidget(this._viewport); |
| 189 |
layout.addWidget(this._vScrollBar); |
| 190 |
layout.addWidget(this._hScrollBar); |
| 191 |
layout.addWidget(this._scrollCorner); |
| 192 |
|
| 193 |
// Install the layout on the data grid. |
| 194 |
this.layout = layout; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Dispose of the resources held by the widgets. |
| 199 |
*/ |
| 200 |
dispose(): void { |
| 201 |
// Release the mouse. |
| 202 |
this._releaseMouse(); |
| 203 |
|
| 204 |
// Dispose of the handlers. |
| 205 |
if (this._keyHandler) { |
| 206 |
this._keyHandler.dispose(); |
| 207 |
} |
| 208 |
if (this._mouseHandler) { |
| 209 |
this._mouseHandler.dispose(); |
| 210 |
} |
| 211 |
this._keyHandler = null; |
| 212 |
this._mouseHandler = null; |
| 213 |
|
| 214 |
// Clear the models. |
| 215 |
this._dataModel = null; |
| 216 |
this._selectionModel = null; |
| 217 |
|
| 218 |
// Clear the section lists. |
| 219 |
this._rowSections.clear(); |
| 220 |
this._columnSections.clear(); |
| 221 |
this._rowHeaderSections.clear(); |
| 222 |
this._columnHeaderSections.clear(); |
| 223 |
|
| 224 |
// Dispose of the base class. |
| 225 |
super.dispose(); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get the data model for the data grid. |
| 230 |
*/ |
| 231 |
get dataModel(): DataModel | null { |
| 232 |
return this._dataModel; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Set the data model for the data grid. |
| 237 |
* |
| 238 |
* #### Notes |
| 239 |
* This will automatically remove the current selection model. |
| 240 |
*/ |
| 241 |
set dataModel(value: DataModel | null) { |
| 242 |
// Do nothing if the model does not change. |
| 243 |
if (this._dataModel === value) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
// Release the mouse. |
| 248 |
this._releaseMouse(); |
| 249 |
|
| 250 |
// Clear the selection model. |
| 251 |
this.selectionModel = null; |
| 252 |
|
| 253 |
// Disconnect the change handler from the old model. |
| 254 |
if (this._dataModel) { |
| 255 |
this._dataModel.changed.disconnect(this._onDataModelChanged, this); |
| 256 |
} |
| 257 |
|
| 258 |
// Connect the change handler for the new model. |
| 259 |
if (value) { |
| 260 |
value.changed.connect(this._onDataModelChanged, this); |
| 261 |
} |
| 262 |
|
| 263 |
// Update the internal model reference. |
| 264 |
this._dataModel = value; |
| 265 |
|
| 266 |
// Clear the section lists. |
| 267 |
this._rowSections.clear(); |
| 268 |
this._columnSections.clear(); |
| 269 |
this._rowHeaderSections.clear(); |
| 270 |
this._columnHeaderSections.clear(); |
| 271 |
|
| 272 |
// Populate the section lists. |
| 273 |
if (value) { |
| 274 |
this._rowSections.insert(0, value.rowCount('body')); |
| 275 |
this._columnSections.insert(0, value.columnCount('body')); |
| 276 |
this._rowHeaderSections.insert(0, value.columnCount('row-header')); |
| 277 |
this._columnHeaderSections.insert(0, value.rowCount('column-header')); |
| 278 |
} |
| 279 |
|
| 280 |
// Reset the scroll position. |
| 281 |
this._scrollX = 0; |
| 282 |
this._scrollY = 0; |
| 283 |
|
| 284 |
// Sync the viewport. |
| 285 |
this._syncViewport(); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Get the selection model for the data grid. |
| 290 |
*/ |
| 291 |
get selectionModel(): SelectionModel | null { |
| 292 |
return this._selectionModel; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Set the selection model for the data grid. |
| 297 |
*/ |
| 298 |
set selectionModel(value: SelectionModel | null) { |
| 299 |
// Do nothing if the selection model does not change. |
| 300 |
if (this._selectionModel === value) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
// Release the mouse. |
| 305 |
this._releaseMouse(); |
| 306 |
|
| 307 |
// Ensure the data models are a match. |
| 308 |
if (value && value.dataModel !== this._dataModel) { |
| 309 |
throw new Error('SelectionModel.dataModel !== DataGrid.dataModel'); |
| 310 |
} |
| 311 |
|
| 312 |
// Disconnect the change handler from the old model. |
| 313 |
if (this._selectionModel) { |
| 314 |
this._selectionModel.changed.disconnect(this._onSelectionsChanged, this); |
| 315 |
} |
| 316 |
|
| 317 |
// Connect the change handler for the new model. |
| 318 |
if (value) { |
| 319 |
value.changed.connect(this._onSelectionsChanged, this); |
| 320 |
} |
| 321 |
|
| 322 |
// Update the internal selection model reference. |
| 323 |
this._selectionModel = value; |
| 324 |
|
| 325 |
// Schedule a repaint of the overlay. |
| 326 |
this.repaintOverlay(); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get the key handler for the data grid. |
| 331 |
*/ |
| 332 |
get keyHandler(): DataGrid.IKeyHandler | null { |
| 333 |
return this._keyHandler; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Set the key handler for the data grid. |
| 338 |
*/ |
| 339 |
set keyHandler(value: DataGrid.IKeyHandler | null) { |
| 340 |
this._keyHandler = value; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Get the mouse handler for the data grid. |
| 345 |
*/ |
| 346 |
get mouseHandler(): DataGrid.IMouseHandler | null { |
| 347 |
return this._mouseHandler; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Set the mouse handler for the data grid. |
| 352 |
*/ |
| 353 |
set mouseHandler(value: DataGrid.IMouseHandler | null) { |
| 354 |
// Bail early if the mouse handler does not change. |
| 355 |
if (this._mouseHandler === value) { |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
// Release the mouse. |
| 360 |
this._releaseMouse(); |
| 361 |
|
| 362 |
// Update the internal mouse handler. |
| 363 |
this._mouseHandler = value; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get the style for the data grid. |
| 368 |
*/ |
| 369 |
get style(): DataGrid.Style { |
| 370 |
return this._style; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Set the style for the data grid. |
| 375 |
*/ |
| 376 |
set style(value: DataGrid.Style) { |
| 377 |
// Bail if the style does not change. |
| 378 |
if (this._style === value) { |
| 379 |
return; |
| 380 |
} |
| 381 |
|
| 382 |
// Update the internal style. |
| 383 |
this._style = { ...value }; |
| 384 |
|
| 385 |
// Schedule a repaint of the content. |
| 386 |
this.repaintContent(); |
| 387 |
|
| 388 |
// Schedule a repaint of the overlay. |
| 389 |
this.repaintOverlay(); |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Get the cell renderer map for the data grid. |
| 394 |
*/ |
| 395 |
get cellRenderers(): RendererMap { |
| 396 |
return this._cellRenderers; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Set the cell renderer map for the data grid. |
| 401 |
*/ |
| 402 |
set cellRenderers(value: RendererMap) { |
| 403 |
// Bail if the renderer map does not change. |
| 404 |
if (this._cellRenderers === value) { |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
// Disconnect the old map. |
| 409 |
this._cellRenderers.changed.disconnect(this._onRenderersChanged, this); |
| 410 |
|
| 411 |
// Connect the new map. |
| 412 |
value.changed.connect(this._onRenderersChanged, this); |
| 413 |
|
| 414 |
// Update the internal renderer map. |
| 415 |
this._cellRenderers = value; |
| 416 |
|
| 417 |
// Schedule a repaint of the grid content. |
| 418 |
this.repaintContent(); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* Get the header visibility for the data grid. |
| 423 |
*/ |
| 424 |
get headerVisibility(): DataGrid.HeaderVisibility { |
| 425 |
return this._headerVisibility; |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Set the header visibility for the data grid. |
| 430 |
*/ |
| 431 |
set headerVisibility(value: DataGrid.HeaderVisibility) { |
| 432 |
// Bail if the visibility does not change. |
| 433 |
if (this._headerVisibility === value) { |
| 434 |
return; |
| 435 |
} |
| 436 |
|
| 437 |
// Update the internal visibility. |
| 438 |
this._headerVisibility = value; |
| 439 |
|
| 440 |
// Sync the viewport. |
| 441 |
this._syncViewport(); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Get the default sizes for the various sections of the data grid. |
| 446 |
*/ |
| 447 |
get defaultSizes(): DataGrid.DefaultSizes { |
| 448 |
let rowHeight = this._rowSections.defaultSize; |
| 449 |
let columnWidth = this._columnSections.defaultSize; |
| 450 |
let rowHeaderWidth = this._rowHeaderSections.defaultSize; |
| 451 |
let columnHeaderHeight = this._columnHeaderSections.defaultSize; |
| 452 |
return { rowHeight, columnWidth, rowHeaderWidth, columnHeaderHeight }; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Set the default sizes for the various sections of the data grid. |
| 457 |
*/ |
| 458 |
set defaultSizes(value: DataGrid.DefaultSizes) { |
| 459 |
// Update the section default sizes. |
| 460 |
this._rowSections.defaultSize = value.rowHeight; |
| 461 |
this._columnSections.defaultSize = value.columnWidth; |
| 462 |
this._rowHeaderSections.defaultSize = value.rowHeaderWidth; |
| 463 |
this._columnHeaderSections.defaultSize = value.columnHeaderHeight; |
| 464 |
|
| 465 |
// Sync the viewport. |
| 466 |
this._syncViewport(); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Get the minimum sizes for the various sections of the data grid. |
| 471 |
*/ |
| 472 |
get minimumSizes(): DataGrid.DefaultSizes { |
| 473 |
let rowHeight = this._rowSections.minimumSize; |
| 474 |
let columnWidth = this._columnSections.minimumSize; |
| 475 |
let rowHeaderWidth = this._rowHeaderSections.minimumSize; |
| 476 |
let columnHeaderHeight = this._columnHeaderSections.minimumSize; |
| 477 |
return { rowHeight, columnWidth, rowHeaderWidth, columnHeaderHeight }; |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Set the minimum sizes for the various sections of the data grid. |
| 482 |
*/ |
| 483 |
set minimumSizes(value: DataGrid.DefaultSizes) { |
| 484 |
// Update the section default sizes. |
| 485 |
this._rowSections.minimumSize = value.rowHeight; |
| 486 |
this._columnSections.minimumSize = value.columnWidth; |
| 487 |
this._rowHeaderSections.minimumSize = value.rowHeaderWidth; |
| 488 |
this._columnHeaderSections.minimumSize = value.columnHeaderHeight; |
| 489 |
|
| 490 |
// Sync the viewport. |
| 491 |
this._syncViewport(); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Get the copy configuration for the data grid. |
| 496 |
*/ |
| 497 |
get copyConfig(): DataGrid.CopyConfig { |
| 498 |
return this._copyConfig; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Set the copy configuration for the data grid. |
| 503 |
*/ |
| 504 |
set copyConfig(value: DataGrid.CopyConfig) { |
| 505 |
this._copyConfig = value; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Get whether the last row is stretched. |
| 510 |
*/ |
| 511 |
get stretchLastRow(): boolean { |
| 512 |
return this._stretchLastRow; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Set whether the last row is stretched. |
| 517 |
*/ |
| 518 |
set stretchLastRow(value: boolean) { |
| 519 |
// Bail early if the value does not change. |
| 520 |
if (value === this._stretchLastRow) { |
| 521 |
return; |
| 522 |
} |
| 523 |
|
| 524 |
// Update the internal value. |
| 525 |
this._stretchLastRow = value; |
| 526 |
|
| 527 |
// Sync the viewport |
| 528 |
this._syncViewport(); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* Get whether the last column is stretched. |
| 533 |
*/ |
| 534 |
get stretchLastColumn(): boolean { |
| 535 |
return this._stretchLastColumn; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Set whether the last column is stretched. |
| 540 |
*/ |
| 541 |
set stretchLastColumn(value: boolean) { |
| 542 |
// Bail early if the value does not change. |
| 543 |
if (value === this._stretchLastColumn) { |
| 544 |
return; |
| 545 |
} |
| 546 |
|
| 547 |
// Update the internal value. |
| 548 |
this._stretchLastColumn = value; |
| 549 |
|
| 550 |
// Sync the viewport |
| 551 |
this._syncViewport(); |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* The virtual width of the row headers. |
| 556 |
*/ |
| 557 |
get headerWidth(): number { |
| 558 |
if (this._headerVisibility === 'none') { |
| 559 |
return 0; |
| 560 |
} |
| 561 |
if (this._headerVisibility === 'column') { |
| 562 |
return 0; |
| 563 |
} |
| 564 |
return this._rowHeaderSections.length; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* The virtual height of the column headers. |
| 569 |
*/ |
| 570 |
get headerHeight(): number { |
| 571 |
if (this._headerVisibility === 'none') { |
| 572 |
return 0; |
| 573 |
} |
| 574 |
if (this._headerVisibility === 'row') { |
| 575 |
return 0; |
| 576 |
} |
| 577 |
return this._columnHeaderSections.length; |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* The virtual width of the grid body. |
| 582 |
* |
| 583 |
* #### Notes |
| 584 |
* This does *not* account for a stretched last column. |
| 585 |
*/ |
| 586 |
get bodyWidth(): number { |
| 587 |
return this._columnSections.length; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* The virtual height of the grid body. |
| 592 |
* |
| 593 |
* #### Notes |
| 594 |
* This does *not* account for a stretched last row. |
| 595 |
*/ |
| 596 |
get bodyHeight(): number { |
| 597 |
return this._rowSections.length; |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* The virtual width of the entire grid. |
| 602 |
* |
| 603 |
* #### Notes |
| 604 |
* This does *not* account for a stretched last column. |
| 605 |
*/ |
| 606 |
get totalWidth(): number { |
| 607 |
return this.headerWidth + this.bodyWidth; |
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* The virtual height of the entire grid. |
| 612 |
* |
| 613 |
* #### Notes |
| 614 |
* This does *not* account for a stretched last row. |
| 615 |
*/ |
| 616 |
get totalHeight(): number { |
| 617 |
return this.headerHeight + this.bodyHeight; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* The actual width of the viewport. |
| 622 |
*/ |
| 623 |
get viewportWidth(): number { |
| 624 |
return this._viewportWidth; |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* The actual height of the viewport. |
| 629 |
*/ |
| 630 |
get viewportHeight(): number { |
| 631 |
return this._viewportHeight; |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* The width of the visible portion of the grid body. |
| 636 |
*/ |
| 637 |
get pageWidth(): number { |
| 638 |
return Math.max(0, this.viewportWidth - this.headerWidth); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* The height of the visible portion of the grid body. |
| 643 |
*/ |
| 644 |
get pageHeight(): number { |
| 645 |
return Math.max(0, this.viewportHeight - this.headerHeight); |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* The current scroll X position of the viewport. |
| 650 |
*/ |
| 651 |
get scrollX(): number { |
| 652 |
return this._hScrollBar.value; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* The current scroll Y position of the viewport. |
| 657 |
*/ |
| 658 |
get scrollY(): number { |
| 659 |
return this._vScrollBar.value; |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* The maximum scroll X position for the grid. |
| 664 |
*/ |
| 665 |
get maxScrollX(): number { |
| 666 |
return Math.max(0, this.bodyWidth - this.pageWidth - 1); |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* The maximum scroll Y position for the grid. |
| 671 |
*/ |
| 672 |
get maxScrollY(): number { |
| 673 |
return Math.max(0, this.bodyHeight - this.pageHeight - 1); |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* The viewport widget for the data grid. |
| 678 |
*/ |
| 679 |
get viewport(): Widget { |
| 680 |
return this._viewport; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* The cell editor controller object for the data grid. |
| 685 |
*/ |
| 686 |
get editorController(): ICellEditorController | null { |
| 687 |
return this._editorController; |
| 688 |
} |
| 689 |
|
| 690 |
set editorController(controller: ICellEditorController | null) { |
| 691 |
this._editorController = controller; |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* Whether the cell editing is enabled for the data grid. |
| 696 |
*/ |
| 697 |
get editingEnabled(): boolean { |
| 698 |
return this._editingEnabled; |
| 699 |
} |
| 700 |
|
| 701 |
set editingEnabled(enabled: boolean) { |
| 702 |
this._editingEnabled = enabled; |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Whether the grid cells are editable. |
| 707 |
* |
| 708 |
* `editingEnabled` flag must be on and grid must have required |
| 709 |
* selection model, editor controller and data model properties. |
| 710 |
*/ |
| 711 |
get editable(): boolean { |
| 712 |
return ( |
| 713 |
this._editingEnabled && |
| 714 |
this._selectionModel !== null && |
| 715 |
this._editorController !== null && |
| 716 |
this.dataModel instanceof MutableDataModel |
| 717 |
); |
| 718 |
} |
| 719 |
|
| 720 |
/** |
| 721 |
* The rendering context for painting the data grid. |
| 722 |
*/ |
| 723 |
protected get canvasGC(): CanvasRenderingContext2D { |
| 724 |
return this._canvasGC; |
| 725 |
} |
| 726 |
|
| 727 |
/** |
| 728 |
* The row sections of the data grid. |
| 729 |
*/ |
| 730 |
protected get rowSections(): SectionList { |
| 731 |
return this._rowSections; |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* The column sections of the data grid. |
| 736 |
*/ |
| 737 |
protected get columnSections(): SectionList { |
| 738 |
return this._columnSections; |
| 739 |
} |
| 740 |
|
| 741 |
/** |
| 742 |
* The row header sections of the data grid. |
| 743 |
*/ |
| 744 |
protected get rowHeaderSections(): SectionList { |
| 745 |
return this._rowHeaderSections; |
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
* The column header sections of the data grid. |
| 750 |
*/ |
| 751 |
protected get columnHeaderSections(): SectionList { |
| 752 |
return this._columnHeaderSections; |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Scroll the grid to the specified row. |
| 757 |
* |
| 758 |
* @param row - The row index of the cell. |
| 759 |
* |
| 760 |
* #### Notes |
| 761 |
* This is a no-op if the row is already visible. |
| 762 |
*/ |
| 763 |
scrollToRow(row: number): void { |
| 764 |
// Fetch the row count. |
| 765 |
let nr = this._rowSections.count; |
| 766 |
|
| 767 |
// Bail early if there is no content. |
| 768 |
if (nr === 0) { |
| 769 |
return; |
| 770 |
} |
| 771 |
|
| 772 |
// Floor the row index. |
| 773 |
row = Math.floor(row); |
| 774 |
|
| 775 |
// Clamp the row index. |
| 776 |
row = Math.max(0, Math.min(row, nr - 1)); |
| 777 |
|
| 778 |
// Get the virtual bounds of the row. |
| 779 |
let y1 = this._rowSections.offsetOf(row); |
| 780 |
let y2 = this._rowSections.extentOf(row); |
| 781 |
|
| 782 |
// Get the virtual bounds of the viewport. |
| 783 |
let vy1 = this._scrollY; |
| 784 |
let vy2 = this._scrollY + this.pageHeight - 1; |
| 785 |
|
| 786 |
// Set up the delta variables. |
| 787 |
let dy = 0; |
| 788 |
|
| 789 |
// Compute the delta Y scroll. |
| 790 |
if (y1 < vy1) { |
| 791 |
dy = y1 - vy1 - 10; |
| 792 |
} else if (y2 > vy2) { |
| 793 |
dy = y2 - vy2 + 10; |
| 794 |
} |
| 795 |
|
| 796 |
// Bail early if no scroll is needed. |
| 797 |
if (dy === 0) { |
| 798 |
return; |
| 799 |
} |
| 800 |
|
| 801 |
// Scroll by the computed delta. |
| 802 |
this.scrollBy(0, dy); |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Scroll the grid to the specified column. |
| 807 |
* |
| 808 |
* @param column - The column index of the cell. |
| 809 |
* |
| 810 |
* #### Notes |
| 811 |
* This is a no-op if the column is already visible. |
| 812 |
*/ |
| 813 |
scrollToColumn(column: number): void { |
| 814 |
// Fetch the column count. |
| 815 |
let nc = this._columnSections.count; |
| 816 |
|
| 817 |
// Bail early if there is no content. |
| 818 |
if (nc === 0) { |
| 819 |
return; |
| 820 |
} |
| 821 |
|
| 822 |
// Floor the column index. |
| 823 |
column = Math.floor(column); |
| 824 |
|
| 825 |
// Clamp the column index. |
| 826 |
column = Math.max(0, Math.min(column, nc - 1)); |
| 827 |
|
| 828 |
// Get the virtual bounds of the column. |
| 829 |
let x1 = this._columnSections.offsetOf(column); |
| 830 |
let x2 = this._columnSections.extentOf(column); |
| 831 |
|
| 832 |
// Get the virtual bounds of the viewport. |
| 833 |
let vx1 = this._scrollX; |
| 834 |
let vx2 = this._scrollX + this.pageWidth - 1; |
| 835 |
|
| 836 |
// Set up the delta variables. |
| 837 |
let dx = 0; |
| 838 |
|
| 839 |
// Compute the delta X scroll. |
| 840 |
if (x1 < vx1) { |
| 841 |
dx = x1 - vx1 - 10; |
| 842 |
} else if (x2 > vx2) { |
| 843 |
dx = x2 - vx2 + 10; |
| 844 |
} |
| 845 |
|
| 846 |
// Bail early if no scroll is needed. |
| 847 |
if (dx === 0) { |
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
// Scroll by the computed delta. |
| 852 |
this.scrollBy(dx, 0); |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* Scroll the grid to the specified cell. |
| 857 |
* |
| 858 |
* @param row - The row index of the cell. |
| 859 |
* |
| 860 |
* @param column - The column index of the cell. |
| 861 |
* |
| 862 |
* #### Notes |
| 863 |
* This is a no-op if the cell is already visible. |
| 864 |
*/ |
| 865 |
scrollToCell(row: number, column: number): void { |
| 866 |
// Fetch the row and column count. |
| 867 |
let nr = this._rowSections.count; |
| 868 |
let nc = this._columnSections.count; |
| 869 |
|
| 870 |
// Bail early if there is no content. |
| 871 |
if (nr === 0 || nc === 0) { |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
// Floor the cell index. |
| 876 |
row = Math.floor(row); |
| 877 |
column = Math.floor(column); |
| 878 |
|
| 879 |
// Clamp the cell index. |
| 880 |
row = Math.max(0, Math.min(row, nr - 1)); |
| 881 |
column = Math.max(0, Math.min(column, nc - 1)); |
| 882 |
|
| 883 |
// Get the virtual bounds of the cell. |
| 884 |
let x1 = this._columnSections.offsetOf(column); |
| 885 |
let x2 = this._columnSections.extentOf(column); |
| 886 |
let y1 = this._rowSections.offsetOf(row); |
| 887 |
let y2 = this._rowSections.extentOf(row); |
| 888 |
|
| 889 |
// Get the virtual bounds of the viewport. |
| 890 |
let vx1 = this._scrollX; |
| 891 |
let vx2 = this._scrollX + this.pageWidth - 1; |
| 892 |
let vy1 = this._scrollY; |
| 893 |
let vy2 = this._scrollY + this.pageHeight - 1; |
| 894 |
|
| 895 |
// Set up the delta variables. |
| 896 |
let dx = 0; |
| 897 |
let dy = 0; |
| 898 |
|
| 899 |
// Compute the delta X scroll. |
| 900 |
if (x1 < vx1) { |
| 901 |
dx = x1 - vx1 - 10; |
| 902 |
} else if (x2 > vx2) { |
| 903 |
dx = x2 - vx2 + 10; |
| 904 |
} |
| 905 |
|
| 906 |
// Compute the delta Y scroll. |
| 907 |
if (y1 < vy1) { |
| 908 |
dy = y1 - vy1 - 10; |
| 909 |
} else if (y2 > vy2) { |
| 910 |
dy = y2 - vy2 + 10; |
| 911 |
} |
| 912 |
|
| 913 |
// Bail early if no scroll is needed. |
| 914 |
if (dx === 0 && dy === 0) { |
| 915 |
return; |
| 916 |
} |
| 917 |
|
| 918 |
// Scroll by the computed delta. |
| 919 |
this.scrollBy(dx, dy); |
| 920 |
} |
| 921 |
|
| 922 |
/** |
| 923 |
* Move cursor down/up/left/right while making sure it remains |
| 924 |
* within the bounds of selected rectangles |
| 925 |
* |
| 926 |
* @param direction - The direction of the movement. |
| 927 |
*/ |
| 928 |
moveCursor(direction: SelectionModel.CursorMoveDirection): void { |
| 929 |
// Bail early if there is no selection |
| 930 |
if ( |
| 931 |
!this.dataModel || |
| 932 |
!this._selectionModel || |
| 933 |
this._selectionModel.isEmpty |
| 934 |
) { |
| 935 |
return; |
| 936 |
} |
| 937 |
|
| 938 |
const iter = this._selectionModel.selections(); |
| 939 |
const onlyOne = iter.next() && !iter.next(); |
| 940 |
|
| 941 |
// if there is a single selection that is a single cell selection |
| 942 |
// then move the selection and cursor within grid bounds |
| 943 |
if (onlyOne) { |
| 944 |
const currentSel = this._selectionModel.currentSelection()!; |
| 945 |
if (currentSel.r1 === currentSel.r2 && currentSel.c1 === currentSel.c2) { |
| 946 |
const dr = direction === 'down' ? 1 : direction === 'up' ? -1 : 0; |
| 947 |
const dc = direction === 'right' ? 1 : direction === 'left' ? -1 : 0; |
| 948 |
let newRow = currentSel.r1 + dr; |
| 949 |
let newColumn = currentSel.c1 + dc; |
| 950 |
const rowCount = this.dataModel.rowCount('body'); |
| 951 |
const columnCount = this.dataModel.columnCount('body'); |
| 952 |
if (newRow >= rowCount) { |
| 953 |
newRow = 0; |
| 954 |
newColumn += 1; |
| 955 |
} else if (newRow === -1) { |
| 956 |
newRow = rowCount - 1; |
| 957 |
newColumn -= 1; |
| 958 |
} |
| 959 |
if (newColumn >= columnCount) { |
| 960 |
newColumn = 0; |
| 961 |
newRow += 1; |
| 962 |
if (newRow >= rowCount) { |
| 963 |
newRow = 0; |
| 964 |
} |
| 965 |
} else if (newColumn === -1) { |
| 966 |
newColumn = columnCount - 1; |
| 967 |
newRow -= 1; |
| 968 |
if (newRow === -1) { |
| 969 |
newRow = rowCount - 1; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
this._selectionModel.select({ |
| 974 |
r1: newRow, |
| 975 |
c1: newColumn, |
| 976 |
r2: newRow, |
| 977 |
c2: newColumn, |
| 978 |
cursorRow: newRow, |
| 979 |
cursorColumn: newColumn, |
| 980 |
clear: 'all' |
| 981 |
}); |
| 982 |
|
| 983 |
return; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
// if there are multiple selections, move cursor |
| 988 |
// within selection rectangles |
| 989 |
this._selectionModel.moveCursorWithinSelections(direction); |
| 990 |
} |
| 991 |
|
| 992 |
/** |
| 993 |
* Scroll the grid to the current cursor position. |
| 994 |
* |
| 995 |
* #### Notes |
| 996 |
* This is a no-op if the cursor is already visible or |
| 997 |
* if there is no selection model installed on the grid. |
| 998 |
*/ |
| 999 |
scrollToCursor(): void { |
| 1000 |
// Bail early if there is no selection model. |
| 1001 |
if (!this._selectionModel) { |
| 1002 |
return; |
| 1003 |
} |
| 1004 |
|
| 1005 |
// Fetch the cursor row and column. |
| 1006 |
let row = this._selectionModel.cursorRow; |
| 1007 |
let column = this._selectionModel.cursorColumn; |
| 1008 |
|
| 1009 |
// Scroll to the cursor cell. |
| 1010 |
this.scrollToCell(row, column); |
| 1011 |
} |
| 1012 |
|
| 1013 |
/** |
| 1014 |
* Scroll the viewport by the specified amount. |
| 1015 |
* |
| 1016 |
* @param dx - The X scroll amount. |
| 1017 |
* |
| 1018 |
* @param dy - The Y scroll amount. |
| 1019 |
*/ |
| 1020 |
scrollBy(dx: number, dy: number): void { |
| 1021 |
this.scrollTo(this.scrollX + dx, this.scrollY + dy); |
| 1022 |
} |
| 1023 |
|
| 1024 |
/** |
| 1025 |
* Scroll the viewport by one page. |
| 1026 |
* |
| 1027 |
* @param dir - The desired direction of the scroll. |
| 1028 |
*/ |
| 1029 |
scrollByPage(dir: 'up' | 'down' | 'left' | 'right'): void { |
| 1030 |
let dx = 0; |
| 1031 |
let dy = 0; |
| 1032 |
switch (dir) { |
| 1033 |
case 'up': |
| 1034 |
dy = -this.pageHeight; |
| 1035 |
break; |
| 1036 |
case 'down': |
| 1037 |
dy = this.pageHeight; |
| 1038 |
break; |
| 1039 |
case 'left': |
| 1040 |
dx = -this.pageWidth; |
| 1041 |
break; |
| 1042 |
case 'right': |
| 1043 |
dx = this.pageWidth; |
| 1044 |
break; |
| 1045 |
default: |
| 1046 |
throw 'unreachable'; |
| 1047 |
} |
| 1048 |
this.scrollTo(this.scrollX + dx, this.scrollY + dy); |
| 1049 |
} |
| 1050 |
|
| 1051 |
/** |
| 1052 |
* Scroll the viewport by one cell-aligned step. |
| 1053 |
* |
| 1054 |
* @param dir - The desired direction of the scroll. |
| 1055 |
*/ |
| 1056 |
scrollByStep(dir: 'up' | 'down' | 'left' | 'right'): void { |
| 1057 |
let r: number; |
| 1058 |
let c: number; |
| 1059 |
let x = this.scrollX; |
| 1060 |
let y = this.scrollY; |
| 1061 |
let rows = this._rowSections; |
| 1062 |
let columns = this._columnSections; |
| 1063 |
switch (dir) { |
| 1064 |
case 'up': |
| 1065 |
r = rows.indexOf(y - 1); |
| 1066 |
y = r < 0 ? y : rows.offsetOf(r); |
| 1067 |
break; |
| 1068 |
case 'down': |
| 1069 |
r = rows.indexOf(y); |
| 1070 |
y = r < 0 ? y : rows.offsetOf(r) + rows.sizeOf(r); |
| 1071 |
break; |
| 1072 |
case 'left': |
| 1073 |
c = columns.indexOf(x - 1); |
| 1074 |
x = c < 0 ? x : columns.offsetOf(c); |
| 1075 |
break; |
| 1076 |
case 'right': |
| 1077 |
c = columns.indexOf(x); |
| 1078 |
x = c < 0 ? x : columns.offsetOf(c) + columns.sizeOf(c); |
| 1079 |
break; |
| 1080 |
default: |
| 1081 |
throw 'unreachable'; |
| 1082 |
} |
| 1083 |
this.scrollTo(x, y); |
| 1084 |
} |
| 1085 |
|
| 1086 |
/** |
| 1087 |
* Scroll to the specified offset position. |
| 1088 |
* |
| 1089 |
* @param x - The desired X position. |
| 1090 |
* |
| 1091 |
* @param y - The desired Y position. |
| 1092 |
*/ |
| 1093 |
scrollTo(x: number, y: number): void { |
| 1094 |
// Floor and clamp the position to the allowable range. |
| 1095 |
x = Math.max(0, Math.min(Math.floor(x), this.maxScrollX)); |
| 1096 |
y = Math.max(0, Math.min(Math.floor(y), this.maxScrollY)); |
| 1097 |
|
| 1098 |
// Update the scroll bar values with the desired position. |
| 1099 |
this._hScrollBar.value = x; |
| 1100 |
this._vScrollBar.value = y; |
| 1101 |
|
| 1102 |
// Post a scroll request message to the viewport. |
| 1103 |
MessageLoop.postMessage(this._viewport, Private.ScrollRequest); |
| 1104 |
} |
| 1105 |
|
| 1106 |
/** |
| 1107 |
* Get the row count for a particular region in the data grid. |
| 1108 |
* |
| 1109 |
* @param region - The row region of interest. |
| 1110 |
* |
| 1111 |
* @returns The row count for the specified region. |
| 1112 |
*/ |
| 1113 |
rowCount(region: DataModel.RowRegion): number { |
| 1114 |
let count: number; |
| 1115 |
if (region === 'body') { |
| 1116 |
count = this._rowSections.count; |
| 1117 |
} else { |
| 1118 |
count = this._columnHeaderSections.count; |
| 1119 |
} |
| 1120 |
return count; |
| 1121 |
} |
| 1122 |
|
| 1123 |
/** |
| 1124 |
* Get the column count for a particular region in the data grid. |
| 1125 |
* |
| 1126 |
* @param region - The column region of interest. |
| 1127 |
* |
| 1128 |
* @returns The column count for the specified region. |
| 1129 |
*/ |
| 1130 |
columnCount(region: DataModel.ColumnRegion): number { |
| 1131 |
let count: number; |
| 1132 |
if (region === 'body') { |
| 1133 |
count = this._columnSections.count; |
| 1134 |
} else { |
| 1135 |
count = this._rowHeaderSections.count; |
| 1136 |
} |
| 1137 |
return count; |
| 1138 |
} |
| 1139 |
|
| 1140 |
/** |
| 1141 |
* Get the row at a virtual offset in the data grid. |
| 1142 |
* |
| 1143 |
* @param region - The region which holds the row of interest. |
| 1144 |
* |
| 1145 |
* @param offset - The virtual offset of the row of interest. |
| 1146 |
* |
| 1147 |
* @returns The index of the row, or `-1` if the offset is out of range. |
| 1148 |
* |
| 1149 |
* #### Notes |
| 1150 |
* This method accounts for a stretched last row. |
| 1151 |
*/ |
| 1152 |
rowAt(region: DataModel.RowRegion, offset: number): number { |
| 1153 |
// Bail early if the offset is negative. |
| 1154 |
if (offset < 0) { |
| 1155 |
return -1; |
| 1156 |
} |
| 1157 |
|
| 1158 |
// Return early for the column header region. |
| 1159 |
if (region === 'column-header') { |
| 1160 |
return this._columnHeaderSections.indexOf(offset); |
| 1161 |
} |
| 1162 |
|
| 1163 |
// Fetch the index. |
| 1164 |
let index = this._rowSections.indexOf(offset); |
| 1165 |
|
| 1166 |
// Return early if the section is found. |
| 1167 |
if (index >= 0) { |
| 1168 |
return index; |
| 1169 |
} |
| 1170 |
|
| 1171 |
// Bail early if the last row is not stretched. |
| 1172 |
if (!this._stretchLastRow) { |
| 1173 |
return -1; |
| 1174 |
} |
| 1175 |
|
| 1176 |
// Fetch the geometry. |
| 1177 |
let bh = this.bodyHeight; |
| 1178 |
let ph = this.pageHeight; |
| 1179 |
|
| 1180 |
// Bail early if no row stretching is required. |
| 1181 |
if (ph <= bh) { |
| 1182 |
return -1; |
| 1183 |
} |
| 1184 |
|
| 1185 |
// Bail early if the offset is out of bounds. |
| 1186 |
if (offset >= ph) { |
| 1187 |
return -1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
// Otherwise, return the last row. |
| 1191 |
return this._rowSections.count - 1; |
| 1192 |
} |
| 1193 |
|
| 1194 |
/** |
| 1195 |
* Get the column at a virtual offset in the data grid. |
| 1196 |
* |
| 1197 |
* @param region - The region which holds the column of interest. |
| 1198 |
* |
| 1199 |
* @param offset - The virtual offset of the column of interest. |
| 1200 |
* |
| 1201 |
* @returns The index of the column, or `-1` if the offset is out of range. |
| 1202 |
* |
| 1203 |
* #### Notes |
| 1204 |
* This method accounts for a stretched last column. |
| 1205 |
*/ |
| 1206 |
columnAt(region: DataModel.ColumnRegion, offset: number): number { |
| 1207 |
if (offset < 0) { |
| 1208 |
return -1; |
| 1209 |
} |
| 1210 |
|
| 1211 |
// Return early for the row header region. |
| 1212 |
if (region === 'row-header') { |
| 1213 |
return this._rowHeaderSections.indexOf(offset); |
| 1214 |
} |
| 1215 |
|
| 1216 |
// Fetch the index. |
| 1217 |
let index = this._columnSections.indexOf(offset); |
| 1218 |
|
| 1219 |
// Return early if the section is found. |
| 1220 |
if (index >= 0) { |
| 1221 |
return index; |
| 1222 |
} |
| 1223 |
|
| 1224 |
// Bail early if the last column is not stretched. |
| 1225 |
if (!this._stretchLastColumn) { |
| 1226 |
return -1; |
| 1227 |
} |
| 1228 |
|
| 1229 |
// Fetch the geometry. |
| 1230 |
let bw = this.bodyWidth; |
| 1231 |
let pw = this.pageWidth; |
| 1232 |
|
| 1233 |
// Bail early if no column stretching is required. |
| 1234 |
if (pw <= bw) { |
| 1235 |
return -1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
// Bail early if the offset is out of bounds. |
| 1239 |
if (offset >= pw) { |
| 1240 |
return -1; |
| 1241 |
} |
| 1242 |
|
| 1243 |
// Otherwise, return the last column. |
| 1244 |
return this._columnSections.count - 1; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/** |
| 1248 |
* Get the offset of a row in the data grid. |
| 1249 |
* |
| 1250 |
* @param region - The region which holds the row of interest. |
| 1251 |
* |
| 1252 |
* @param index - The index of the row of interest. |
| 1253 |
* |
| 1254 |
* @returns The offset of the row, or `-1` if the index is out of range. |
| 1255 |
* |
| 1256 |
* #### Notes |
| 1257 |
* A stretched last row has no effect on the return value. |
| 1258 |
*/ |
| 1259 |
rowOffset(region: DataModel.RowRegion, index: number): number { |
| 1260 |
let offset: number; |
| 1261 |
if (region === 'body') { |
| 1262 |
offset = this._rowSections.offsetOf(index); |
| 1263 |
} else { |
| 1264 |
offset = this._columnHeaderSections.offsetOf(index); |
| 1265 |
} |
| 1266 |
return offset; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/** |
| 1270 |
* Get the offset of a column in the data grid. |
| 1271 |
* |
| 1272 |
* @param region - The region which holds the column of interest. |
| 1273 |
* |
| 1274 |
* @param index - The index of the column of interest. |
| 1275 |
* |
| 1276 |
* @returns The offset of the column, or `-1` if the index is out of range. |
| 1277 |
* |
| 1278 |
* #### Notes |
| 1279 |
* A stretched last column has no effect on the return value. |
| 1280 |
*/ |
| 1281 |
columnOffset(region: DataModel.ColumnRegion, index: number): number { |
| 1282 |
let offset: number; |
| 1283 |
if (region === 'body') { |
| 1284 |
offset = this._columnSections.offsetOf(index); |
| 1285 |
} else { |
| 1286 |
offset = this._rowHeaderSections.offsetOf(index); |
| 1287 |
} |
| 1288 |
return offset; |
| 1289 |
} |
| 1290 |
|
| 1291 |
/** |
| 1292 |
* Get the size of a row in the data grid. |
| 1293 |
* |
| 1294 |
* @param region - The region which holds the row of interest. |
| 1295 |
* |
| 1296 |
* @param index - The index of the row of interest. |
| 1297 |
* |
| 1298 |
* @returns The size of the row, or `-1` if the index is out of range. |
| 1299 |
* |
| 1300 |
* #### Notes |
| 1301 |
* This method accounts for a stretched last row. |
| 1302 |
*/ |
| 1303 |
rowSize(region: DataModel.RowRegion, index: number): number { |
| 1304 |
// Return early for the column header region. |
| 1305 |
if (region === 'column-header') { |
| 1306 |
return this._columnHeaderSections.sizeOf(index); |
| 1307 |
} |
| 1308 |
|
| 1309 |
// Fetch the row size. |
| 1310 |
let size = this._rowSections.sizeOf(index); |
| 1311 |
|
| 1312 |
// Bail early if the index is out of bounds. |
| 1313 |
if (size < 0) { |
| 1314 |
return size; |
| 1315 |
} |
| 1316 |
|
| 1317 |
// Return early if the last row is not stretched. |
| 1318 |
if (!this._stretchLastRow) { |
| 1319 |
return size; |
| 1320 |
} |
| 1321 |
|
| 1322 |
// Return early if its not the last row. |
| 1323 |
if (index < this._rowSections.count - 1) { |
| 1324 |
return size; |
| 1325 |
} |
| 1326 |
|
| 1327 |
// Fetch the geometry. |
| 1328 |
let bh = this.bodyHeight; |
| 1329 |
let ph = this.pageHeight; |
| 1330 |
|
| 1331 |
// Return early if no stretching is needed. |
| 1332 |
if (ph <= bh) { |
| 1333 |
return size; |
| 1334 |
} |
| 1335 |
|
| 1336 |
// Return the adjusted size. |
| 1337 |
return size + (ph - bh); |
| 1338 |
} |
| 1339 |
|
| 1340 |
/** |
| 1341 |
* Get the size of a column in the data grid. |
| 1342 |
* |
| 1343 |
* @param region - The region which holds the column of interest. |
| 1344 |
* |
| 1345 |
* @param index - The index of the column of interest. |
| 1346 |
* |
| 1347 |
* @returns The size of the column, or `-1` if the index is out of range. |
| 1348 |
* |
| 1349 |
* #### Notes |
| 1350 |
* This method accounts for a stretched last column. |
| 1351 |
*/ |
| 1352 |
columnSize(region: DataModel.ColumnRegion, index: number): number { |
| 1353 |
// Return early for the row header region. |
| 1354 |
if (region === 'row-header') { |
| 1355 |
return this._rowHeaderSections.sizeOf(index); |
| 1356 |
} |
| 1357 |
|
| 1358 |
// Fetch the column size. |
| 1359 |
let size = this._columnSections.sizeOf(index); |
| 1360 |
|
| 1361 |
// Bail early if the index is out of bounds. |
| 1362 |
if (size < 0) { |
| 1363 |
return size; |
| 1364 |
} |
| 1365 |
|
| 1366 |
// Return early if the last column is not stretched. |
| 1367 |
if (!this._stretchLastColumn) { |
| 1368 |
return size; |
| 1369 |
} |
| 1370 |
|
| 1371 |
// Return early if its not the last column. |
| 1372 |
if (index < this._columnSections.count - 1) { |
| 1373 |
return size; |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Fetch the geometry. |
| 1377 |
let bw = this.bodyWidth; |
| 1378 |
let pw = this.pageWidth; |
| 1379 |
|
| 1380 |
// Return early if no stretching is needed. |
| 1381 |
if (pw <= bw) { |
| 1382 |
return size; |
| 1383 |
} |
| 1384 |
|
| 1385 |
// Return the adjusted size. |
| 1386 |
return size + (pw - bw); |
| 1387 |
} |
| 1388 |
|
| 1389 |
/** |
| 1390 |
* Resize a row in the data grid. |
| 1391 |
* |
| 1392 |
* @param region - The region which holds the row of interest. |
| 1393 |
* |
| 1394 |
* @param index - The index of the row of interest. |
| 1395 |
* |
| 1396 |
* @param size - The desired size of the row. |
| 1397 |
*/ |
| 1398 |
resizeRow(region: DataModel.RowRegion, index: number, size: number): void { |
| 1399 |
let msg = new Private.RowResizeRequest(region, index, size); |
| 1400 |
MessageLoop.postMessage(this._viewport, msg); |
| 1401 |
} |
| 1402 |
|
| 1403 |
/** |
| 1404 |
* Resize a column in the data grid. |
| 1405 |
* |
| 1406 |
* @param region - The region which holds the column of interest. |
| 1407 |
* |
| 1408 |
* @param index - The index of the column of interest. |
| 1409 |
* |
| 1410 |
* @param size - The desired size of the column. |
| 1411 |
*/ |
| 1412 |
resizeColumn( |
| 1413 |
region: DataModel.ColumnRegion, |
| 1414 |
index: number, |
| 1415 |
size: number | null |
| 1416 |
): void { |
| 1417 |
let msg = new Private.ColumnResizeRequest(region, index, size); |
| 1418 |
MessageLoop.postMessage(this._viewport, msg); |
| 1419 |
} |
| 1420 |
|
| 1421 |
/** |
| 1422 |
* Reset modified rows to their default size. |
| 1423 |
* |
| 1424 |
* @param region - The row region of interest. |
| 1425 |
*/ |
| 1426 |
resetRows(region: DataModel.RowRegion | 'all'): void { |
| 1427 |
switch (region) { |
| 1428 |
case 'all': |
| 1429 |
this._rowSections.reset(); |
| 1430 |
this._columnHeaderSections.reset(); |
| 1431 |
break; |
| 1432 |
case 'body': |
| 1433 |
this._rowSections.reset(); |
| 1434 |
break; |
| 1435 |
case 'column-header': |
| 1436 |
this._columnHeaderSections.reset(); |
| 1437 |
break; |
| 1438 |
default: |
| 1439 |
throw 'unreachable'; |
| 1440 |
} |
| 1441 |
this.repaintContent(); |
| 1442 |
this.repaintOverlay(); |
| 1443 |
} |
| 1444 |
|
| 1445 |
/** |
| 1446 |
* Reset modified columns to their default size. |
| 1447 |
* |
| 1448 |
* @param region - The column region of interest. |
| 1449 |
*/ |
| 1450 |
resetColumns(region: DataModel.ColumnRegion | 'all'): void { |
| 1451 |
switch (region) { |
| 1452 |
case 'all': |
| 1453 |
this._columnSections.reset(); |
| 1454 |
this._rowHeaderSections.reset(); |
| 1455 |
break; |
| 1456 |
case 'body': |
| 1457 |
this._columnSections.reset(); |
| 1458 |
break; |
| 1459 |
case 'row-header': |
| 1460 |
this._rowHeaderSections.reset(); |
| 1461 |
break; |
| 1462 |
default: |
| 1463 |
throw 'unreachable'; |
| 1464 |
} |
| 1465 |
this.repaintContent(); |
| 1466 |
this.repaintOverlay(); |
| 1467 |
} |
| 1468 |
|
| 1469 |
/** |
| 1470 |
* Auto sizes column-header widths based on their text content. |
| 1471 |
* @param area which area to resize: 'body', 'row-header' or 'all'. |
| 1472 |
* @param padding padding added to resized columns (pixels). |
| 1473 |
* @param numCols specify cap on the number of column resizes (optional). |
| 1474 |
*/ |
| 1475 |
fitColumnNames( |
| 1476 |
area: DataGrid.ColumnFitType = 'all', |
| 1477 |
padding: number = 15, |
| 1478 |
numCols?: number |
| 1479 |
): void { |
| 1480 |
// Attempt resizing only if a data model is present. |
| 1481 |
if (this.dataModel) { |
| 1482 |
// Tracking remaining columns to be resized if numCols arg passed. |
| 1483 |
let colsRemaining = |
| 1484 |
numCols === undefined || numCols < 0 ? undefined : numCols; |
| 1485 |
|
| 1486 |
if (area === 'row-header' || area === 'all') { |
| 1487 |
// Respecting any column resize cap, if one has been passed. |
| 1488 |
if (colsRemaining !== undefined) { |
| 1489 |
const rowColumnCount = this.dataModel.columnCount('row-header'); |
| 1490 |
/* |
| 1491 |
If we have more row-header columns than columns available |
| 1492 |
for resize, resize only remaining columns as per allowance |
| 1493 |
and set remaining resize allowance number to 0. |
| 1494 |
*/ |
| 1495 |
if (colsRemaining - rowColumnCount < 0) { |
| 1496 |
this._fitRowColumnHeaders(this.dataModel, padding, colsRemaining); |
| 1497 |
colsRemaining = 0; |
| 1498 |
} else { |
| 1499 |
/* |
| 1500 |
Otherwise the entire row-header column count can be resized. |
| 1501 |
Resize all row-header columns and subtract from remaining |
| 1502 |
column resize allowance. |
| 1503 |
*/ |
| 1504 |
this._fitRowColumnHeaders(this.dataModel, padding, rowColumnCount); |
| 1505 |
colsRemaining = colsRemaining - rowColumnCount; |
| 1506 |
} |
| 1507 |
} else { |
| 1508 |
// No column resize cap passed - resizing all columns. |
| 1509 |
this._fitRowColumnHeaders(this.dataModel, padding); |
| 1510 |
} |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (area === 'body' || area === 'all') { |
| 1514 |
// Respecting any column resize cap, if one has been passed. |
| 1515 |
if (colsRemaining !== undefined) { |
| 1516 |
const bodyColumnCount = this.dataModel.columnCount('body'); |
| 1517 |
/* |
| 1518 |
If we have more body columns than columns available |
| 1519 |
for resize, resize only remaining columns as per allowance |
| 1520 |
and set remaining resize allowance number to 0. |
| 1521 |
*/ |
| 1522 |
if (colsRemaining - bodyColumnCount < 0) { |
| 1523 |
this._fitBodyColumnHeaders(this.dataModel, padding, colsRemaining); |
| 1524 |
} else { |
| 1525 |
/* |
| 1526 |
Otherwise the entire body column count can be resized. |
| 1527 |
Resize based on the smallest number between remaining |
| 1528 |
resize allowance and body column count. |
| 1529 |
*/ |
| 1530 |
this._fitBodyColumnHeaders( |
| 1531 |
this.dataModel, |
| 1532 |
padding, |
| 1533 |
Math.min(colsRemaining, bodyColumnCount) |
| 1534 |
); |
| 1535 |
} |
| 1536 |
} else { |
| 1537 |
// No column resize cap passed - resizing all columns. |
| 1538 |
this._fitBodyColumnHeaders(this.dataModel, padding); |
| 1539 |
} |
| 1540 |
} |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
/** |
| 1545 |
* Map a client position to local viewport coordinates. |
| 1546 |
* |
| 1547 |
* @param clientX - The client X position of the mouse. |
| 1548 |
* |
| 1549 |
* @param clientY - The client Y position of the mouse. |
| 1550 |
* |
| 1551 |
* @returns The local viewport coordinates for the position. |
| 1552 |
*/ |
| 1553 |
mapToLocal(clientX: number, clientY: number): { lx: number; ly: number } { |
| 1554 |
// Fetch the viewport rect. |
| 1555 |
let rect = this._viewport.node.getBoundingClientRect(); |
| 1556 |
|
| 1557 |
// Extract the rect coordinates. |
| 1558 |
let { left, top } = rect; |
| 1559 |
|
| 1560 |
// Round the rect coordinates for sub-pixel positioning. |
| 1561 |
left = Math.floor(left); |
| 1562 |
top = Math.floor(top); |
| 1563 |
|
| 1564 |
// Convert to local coordinates. |
| 1565 |
let lx = clientX - left; |
| 1566 |
let ly = clientY - top; |
| 1567 |
|
| 1568 |
// Return the local coordinates. |
| 1569 |
return { lx, ly }; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/** |
| 1573 |
* Map a client position to virtual grid coordinates. |
| 1574 |
* |
| 1575 |
* @param clientX - The client X position of the mouse. |
| 1576 |
* |
| 1577 |
* @param clientY - The client Y position of the mouse. |
| 1578 |
* |
| 1579 |
* @returns The virtual grid coordinates for the position. |
| 1580 |
*/ |
| 1581 |
mapToVirtual(clientX: number, clientY: number): { vx: number; vy: number } { |
| 1582 |
// Convert to local coordiates. |
| 1583 |
let { lx, ly } = this.mapToLocal(clientX, clientY); |
| 1584 |
|
| 1585 |
// Convert to virtual coordinates. |
| 1586 |
let vx = lx + this.scrollX - this.headerWidth; |
| 1587 |
let vy = ly + this.scrollY - this.headerHeight; |
| 1588 |
|
| 1589 |
// Return the local coordinates. |
| 1590 |
return { vx, vy }; |
| 1591 |
} |
| 1592 |
|
| 1593 |
/** |
| 1594 |
* Hit test the viewport for the given client position. |
| 1595 |
* |
| 1596 |
* @param clientX - The client X position of the mouse. |
| 1597 |
* |
| 1598 |
* @param clientY - The client Y position of the mouse. |
| 1599 |
* |
| 1600 |
* @returns The hit test result, or `null` if the client |
| 1601 |
* position is out of bounds. |
| 1602 |
* |
| 1603 |
* #### Notes |
| 1604 |
* This method accounts for a stretched last row and/or column. |
| 1605 |
*/ |
| 1606 |
hitTest(clientX: number, clientY: number): DataGrid.HitTestResult { |
| 1607 |
// Convert the mouse position into local coordinates. |
| 1608 |
let { lx, ly } = this.mapToLocal(clientX, clientY); |
| 1609 |
|
| 1610 |
// Fetch the header and body dimensions. |
| 1611 |
let hw = this.headerWidth; |
| 1612 |
let hh = this.headerHeight; |
| 1613 |
let bw = this.bodyWidth; |
| 1614 |
let bh = this.bodyHeight; |
| 1615 |
let ph = this.pageHeight; |
| 1616 |
let pw = this.pageWidth; |
| 1617 |
|
| 1618 |
// Adjust the body width for a stretched last column. |
| 1619 |
if (this._stretchLastColumn && pw > bw) { |
| 1620 |
bw = pw; |
| 1621 |
} |
| 1622 |
|
| 1623 |
// Adjust the body height for a stretched last row. |
| 1624 |
if (this._stretchLastRow && ph > bh) { |
| 1625 |
bh = ph; |
| 1626 |
} |
| 1627 |
|
| 1628 |
// Check for a corner header hit. |
| 1629 |
if (lx >= 0 && lx < hw && ly >= 0 && ly < hh) { |
| 1630 |
// Convert to unscrolled virtual coordinates. |
| 1631 |
let vx = lx; |
| 1632 |
let vy = ly; |
| 1633 |
|
| 1634 |
// Fetch the row and column index. |
| 1635 |
let row = this.rowAt('column-header', vy); |
| 1636 |
let column = this.columnAt('row-header', vx); |
| 1637 |
|
| 1638 |
// Fetch the cell offset position. |
| 1639 |
let ox = this.columnOffset('row-header', column); |
| 1640 |
let oy = this.rowOffset('column-header', row); |
| 1641 |
|
| 1642 |
// Fetch cell width and height. |
| 1643 |
let width = this.columnSize('row-header', column); |
| 1644 |
let height = this.rowSize('column-header', row); |
| 1645 |
|
| 1646 |
// Compute the leading and trailing positions. |
| 1647 |
let x = vx - ox; |
| 1648 |
let y = vy - oy; |
| 1649 |
|
| 1650 |
// Return the hit test result. |
| 1651 |
return { region: 'corner-header', row, column, x, y, width, height }; |
| 1652 |
} |
| 1653 |
|
| 1654 |
// Check for a column header hit. |
| 1655 |
if (ly >= 0 && ly < hh && lx >= 0 && lx < hw + bw) { |
| 1656 |
// Convert to unscrolled virtual coordinates. |
| 1657 |
let vx = lx + this._scrollX - hw; |
| 1658 |
let vy = ly; |
| 1659 |
|
| 1660 |
// Fetch the row and column index. |
| 1661 |
let row = this.rowAt('column-header', vy); |
| 1662 |
let column = this.columnAt('body', vx); |
| 1663 |
|
| 1664 |
// Fetch the cell offset position. |
| 1665 |
let ox = this.columnOffset('body', column); |
| 1666 |
let oy = this.rowOffset('column-header', row); |
| 1667 |
|
| 1668 |
// Fetch the cell width and height. |
| 1669 |
let width = this.columnSize('body', column); |
| 1670 |
let height = this.rowSize('column-header', row); |
| 1671 |
|
| 1672 |
// Compute the leading and trailing positions. |
| 1673 |
let x = vx - ox; |
| 1674 |
let y = vy - oy; |
| 1675 |
|
| 1676 |
// Return the hit test result. |
| 1677 |
return { region: 'column-header', row, column, x, y, width, height }; |
| 1678 |
} |
| 1679 |
|
| 1680 |
// Check for a row header hit. |
| 1681 |
if (lx >= 0 && lx < hw && ly >= 0 && ly < hh + bh) { |
| 1682 |
// Convert to unscrolled virtual coordinates. |
| 1683 |
let vx = lx; |
| 1684 |
let vy = ly + this._scrollY - hh; |
| 1685 |
|
| 1686 |
// Fetch the row and column index. |
| 1687 |
let row = this.rowAt('body', vy); |
| 1688 |
let column = this.columnAt('row-header', vx); |
| 1689 |
|
| 1690 |
// Fetch the cell offset position. |
| 1691 |
let ox = this.columnOffset('row-header', column); |
| 1692 |
let oy = this.rowOffset('body', row); |
| 1693 |
|
| 1694 |
// Fetch the cell width and height. |
| 1695 |
let width = this.columnSize('row-header', column); |
| 1696 |
let height = this.rowSize('body', row); |
| 1697 |
|
| 1698 |
// Compute the leading and trailing positions. |
| 1699 |
let x = vx - ox; |
| 1700 |
let y = vy - oy; |
| 1701 |
|
| 1702 |
// Return the hit test result. |
| 1703 |
return { region: 'row-header', row, column, x, y, width, height }; |
| 1704 |
} |
| 1705 |
|
| 1706 |
// Check for a body hit. |
| 1707 |
if (lx >= hw && lx < hw + bw && ly >= hh && ly < hh + bh) { |
| 1708 |
// Convert to unscrolled virtual coordinates. |
| 1709 |
let vx = lx + this._scrollX - hw; |
| 1710 |
let vy = ly + this._scrollY - hh; |
| 1711 |
|
| 1712 |
// Fetch the row and column index. |
| 1713 |
let row = this.rowAt('body', vy); |
| 1714 |
let column = this.columnAt('body', vx); |
| 1715 |
|
| 1716 |
// Fetch the cell offset position. |
| 1717 |
let ox = this.columnOffset('body', column); |
| 1718 |
let oy = this.rowOffset('body', row); |
| 1719 |
|
| 1720 |
// Fetch the cell width and height. |
| 1721 |
let width = this.columnSize('body', column); |
| 1722 |
let height = this.rowSize('body', row); |
| 1723 |
|
| 1724 |
// Compute the part coordinates. |
| 1725 |
let x = vx - ox; |
| 1726 |
let y = vy - oy; |
| 1727 |
|
| 1728 |
// Return the result. |
| 1729 |
return { region: 'body', row, column, x, y, width, height }; |
| 1730 |
} |
| 1731 |
|
| 1732 |
// Otherwise, it's a void space hit. |
| 1733 |
let row = -1; |
| 1734 |
let column = -1; |
| 1735 |
let x = -1; |
| 1736 |
let y = -1; |
| 1737 |
let width = -1; |
| 1738 |
let height = -1; |
| 1739 |
|
| 1740 |
// Return the hit test result. |
| 1741 |
return { region: 'void', row, column, x, y, width, height }; |
| 1742 |
} |
| 1743 |
|
| 1744 |
/** |
| 1745 |
* Copy the current selection to the system clipboard. |
| 1746 |
* |
| 1747 |
* #### Notes |
| 1748 |
* The grid must have a data model and a selection model. |
| 1749 |
* |
| 1750 |
* The behavior can be configured via `DataGrid.copyConfig`. |
| 1751 |
*/ |
| 1752 |
copyToClipboard(): void { |
| 1753 |
// Fetch the data model. |
| 1754 |
let dataModel = this._dataModel; |
| 1755 |
|
| 1756 |
// Bail early if there is no data model. |
| 1757 |
if (!dataModel) { |
| 1758 |
return; |
| 1759 |
} |
| 1760 |
|
| 1761 |
// Fetch the selection model. |
| 1762 |
let selectionModel = this._selectionModel; |
| 1763 |
|
| 1764 |
// Bail early if there is no selection model. |
| 1765 |
if (!selectionModel) { |
| 1766 |
return; |
| 1767 |
} |
| 1768 |
|
| 1769 |
// Coerce the selections to an array. |
| 1770 |
let selections = Array.from(selectionModel.selections()); |
| 1771 |
|
| 1772 |
// Bail early if there are no selections. |
| 1773 |
if (selections.length === 0) { |
| 1774 |
return; |
| 1775 |
} |
| 1776 |
|
| 1777 |
// Alert that multiple selections cannot be copied. |
| 1778 |
if (selections.length > 1) { |
| 1779 |
alert('Cannot copy multiple grid selections.'); |
| 1780 |
return; |
| 1781 |
} |
| 1782 |
|
| 1783 |
// Fetch the model counts. |
| 1784 |
let br = dataModel.rowCount('body'); |
| 1785 |
let bc = dataModel.columnCount('body'); |
| 1786 |
|
| 1787 |
// Bail early if there is nothing to copy. |
| 1788 |
if (br === 0 || bc === 0) { |
| 1789 |
return; |
| 1790 |
} |
| 1791 |
|
| 1792 |
// Unpack the selection. |
| 1793 |
let { r1, c1, r2, c2 } = selections[0]; |
| 1794 |
|
| 1795 |
// Clamp the selection to the model bounds. |
| 1796 |
r1 = Math.max(0, Math.min(r1, br - 1)); |
| 1797 |
c1 = Math.max(0, Math.min(c1, bc - 1)); |
| 1798 |
r2 = Math.max(0, Math.min(r2, br - 1)); |
| 1799 |
c2 = Math.max(0, Math.min(c2, bc - 1)); |
| 1800 |
|
| 1801 |
// Ensure the limits are well-orderd. |
| 1802 |
if (r2 < r1) [r1, r2] = [r2, r1]; |
| 1803 |
if (c2 < c1) [c1, c2] = [c2, c1]; |
| 1804 |
|
| 1805 |
// Fetch the header counts. |
| 1806 |
let rhc = dataModel.columnCount('row-header'); |
| 1807 |
let chr = dataModel.rowCount('column-header'); |
| 1808 |
|
| 1809 |
// Unpack the copy config. |
| 1810 |
let separator = this._copyConfig.separator; |
| 1811 |
let format = this._copyConfig.format; |
| 1812 |
let headers = this._copyConfig.headers; |
| 1813 |
let warningThreshold = this._copyConfig.warningThreshold; |
| 1814 |
|
| 1815 |
// Compute the number of cells to be copied. |
| 1816 |
let rowCount = r2 - r1 + 1; |
| 1817 |
let colCount = c2 - c1 + 1; |
| 1818 |
switch (headers) { |
| 1819 |
case 'none': |
| 1820 |
rhc = 0; |
| 1821 |
chr = 0; |
| 1822 |
break; |
| 1823 |
case 'row': |
| 1824 |
chr = 0; |
| 1825 |
colCount += rhc; |
| 1826 |
break; |
| 1827 |
case 'column': |
| 1828 |
rhc = 0; |
| 1829 |
rowCount += chr; |
| 1830 |
break; |
| 1831 |
case 'all': |
| 1832 |
rowCount += chr; |
| 1833 |
colCount += rhc; |
| 1834 |
break; |
| 1835 |
default: |
| 1836 |
throw 'unreachable'; |
| 1837 |
} |
| 1838 |
|
| 1839 |
// Compute the total cell count. |
| 1840 |
let cellCount = rowCount * colCount; |
| 1841 |
|
| 1842 |
// Allow the user to cancel a large copy request. |
| 1843 |
if (cellCount > warningThreshold) { |
| 1844 |
let msg = `Copying ${cellCount} cells may take a while. Continue?`; |
| 1845 |
if (!window.confirm(msg)) { |
| 1846 |
return; |
| 1847 |
} |
| 1848 |
} |
| 1849 |
|
| 1850 |
// Set up the format args. |
| 1851 |
let args = { |
| 1852 |
region: 'body' as DataModel.CellRegion, |
| 1853 |
row: 0, |
| 1854 |
column: 0, |
| 1855 |
value: null as any, |
| 1856 |
metadata: {} as DataModel.Metadata |
| 1857 |
}; |
| 1858 |
|
| 1859 |
// Allocate the array of rows. |
| 1860 |
let rows = new Array<string[]>(rowCount); |
| 1861 |
|
| 1862 |
// Iterate over the rows. |
| 1863 |
for (let j = 0; j < rowCount; ++j) { |
| 1864 |
// Allocate the array of cells. |
| 1865 |
let cells = new Array<string>(colCount); |
| 1866 |
|
| 1867 |
// Iterate over the columns. |
| 1868 |
for (let i = 0; i < colCount; ++i) { |
| 1869 |
// Set up the format variables. |
| 1870 |
let region: DataModel.CellRegion; |
| 1871 |
let row: number; |
| 1872 |
let column: number; |
| 1873 |
|
| 1874 |
// Populate the format variables. |
| 1875 |
if (j < chr && i < rhc) { |
| 1876 |
region = 'corner-header'; |
| 1877 |
row = j; |
| 1878 |
column = i; |
| 1879 |
} else if (j < chr) { |
| 1880 |
region = 'column-header'; |
| 1881 |
row = j; |
| 1882 |
column = i - rhc + c1; |
| 1883 |
} else if (i < rhc) { |
| 1884 |
region = 'row-header'; |
| 1885 |
row = j - chr + r1; |
| 1886 |
column = i; |
| 1887 |
} else { |
| 1888 |
region = 'body'; |
| 1889 |
row = j - chr + r1; |
| 1890 |
column = i - rhc + c1; |
| 1891 |
} |
| 1892 |
|
| 1893 |
// Populate the format args. |
| 1894 |
args.region = region; |
| 1895 |
args.row = row; |
| 1896 |
args.column = column; |
| 1897 |
args.value = dataModel.data(region, row, column); |
| 1898 |
args.metadata = dataModel.metadata(region, row, column); |
| 1899 |
|
| 1900 |
// Format the cell. |
| 1901 |
cells[i] = format(args); |
| 1902 |
} |
| 1903 |
|
| 1904 |
// Save the row of cells. |
| 1905 |
rows[j] = cells; |
| 1906 |
} |
| 1907 |
|
| 1908 |
// Convert the cells into lines. |
| 1909 |
let lines = rows.map(cells => cells.join(separator)); |
| 1910 |
|
| 1911 |
// Convert the lines into text. |
| 1912 |
let text = lines.join('\n'); |
| 1913 |
|
| 1914 |
// Copy the text to the clipboard. |
| 1915 |
ClipboardExt.copyText(text); |
| 1916 |
} |
| 1917 |
|
| 1918 |
/** |
| 1919 |
* Process a message sent to the widget. |
| 1920 |
* |
| 1921 |
* @param msg - The message sent to the widget. |
| 1922 |
*/ |
| 1923 |
processMessage(msg: Message): void { |
| 1924 |
// Ignore child show/hide messages. The data grid controls the |
| 1925 |
// visibility of its children, and will manually dispatch the |
| 1926 |
// fit-request messages as a result of visibility change. |
| 1927 |
if (msg.type === 'child-shown' || msg.type === 'child-hidden') { |
| 1928 |
return; |
| 1929 |
} |
| 1930 |
|
| 1931 |
// Recompute the scroll bar minimums before the layout refits. |
| 1932 |
if (msg.type === 'fit-request') { |
| 1933 |
let vsbLimits = ElementExt.sizeLimits(this._vScrollBar.node); |
| 1934 |
let hsbLimits = ElementExt.sizeLimits(this._hScrollBar.node); |
| 1935 |
this._vScrollBarMinWidth = vsbLimits.minWidth; |
| 1936 |
this._hScrollBarMinHeight = hsbLimits.minHeight; |
| 1937 |
} |
| 1938 |
|
| 1939 |
// Process all other messages as normal. |
| 1940 |
super.processMessage(msg); |
| 1941 |
} |
| 1942 |
|
| 1943 |
/** |
| 1944 |
* Intercept a message sent to a message handler. |
| 1945 |
* |
| 1946 |
* @param handler - The target handler of the message. |
| 1947 |
* |
| 1948 |
* @param msg - The message to be sent to the handler. |
| 1949 |
* |
| 1950 |
* @returns `true` if the message should continue to be processed |
| 1951 |
* as normal, or `false` if processing should cease immediately. |
| 1952 |
*/ |
| 1953 |
messageHook(handler: IMessageHandler, msg: Message): boolean { |
| 1954 |
// Process viewport messages. |
| 1955 |
if (handler === this._viewport) { |
| 1956 |
this._processViewportMessage(msg); |
| 1957 |
return true; |
| 1958 |
} |
| 1959 |
|
| 1960 |
// Process horizontal scroll bar messages. |
| 1961 |
if (handler === this._hScrollBar && msg.type === 'activate-request') { |
| 1962 |
this.activate(); |
| 1963 |
return false; |
| 1964 |
} |
| 1965 |
|
| 1966 |
// Process vertical scroll bar messages. |
| 1967 |
if (handler === this._vScrollBar && msg.type === 'activate-request') { |
| 1968 |
this.activate(); |
| 1969 |
return false; |
| 1970 |
} |
| 1971 |
|
| 1972 |
// Ignore all other messages. |
| 1973 |
return true; |
| 1974 |
} |
| 1975 |
|
| 1976 |
/** |
| 1977 |
* Handle the DOM events for the data grid. |
| 1978 |
* |
| 1979 |
* @param event - The DOM event sent to the data grid. |
| 1980 |
* |
| 1981 |
* #### Notes |
| 1982 |
* This method implements the DOM `EventListener` interface and is |
| 1983 |
* called in response to events on the data grid's DOM node. It |
| 1984 |
* should not be called directly by user code. |
| 1985 |
*/ |
| 1986 |
handleEvent(event: Event): void { |
| 1987 |
switch (event.type) { |
| 1988 |
case 'keydown': |
| 1989 |
this._evtKeyDown(event as KeyboardEvent); |
| 1990 |
break; |
| 1991 |
case 'mousedown': |
| 1992 |
this._evtMouseDown(event as MouseEvent); |
| 1993 |
break; |
| 1994 |
case 'mousemove': |
| 1995 |
this._evtMouseMove(event as MouseEvent); |
| 1996 |
break; |
| 1997 |
case 'mouseup': |
| 1998 |
this._evtMouseUp(event as MouseEvent); |
| 1999 |
break; |
| 2000 |
case 'dblclick': |
| 2001 |
this._evtMouseDoubleClick(event as MouseEvent); |
| 2002 |
break; |
| 2003 |
case 'mouseleave': |
| 2004 |
this._evtMouseLeave(event as MouseEvent); |
| 2005 |
break; |
| 2006 |
case 'contextmenu': |
| 2007 |
this._evtContextMenu(event as MouseEvent); |
| 2008 |
break; |
| 2009 |
case 'wheel': |
| 2010 |
this._evtWheel(event as WheelEvent); |
| 2011 |
break; |
| 2012 |
case 'resize': |
| 2013 |
this._refreshDPI(); |
| 2014 |
break; |
| 2015 |
} |
| 2016 |
} |
| 2017 |
|
| 2018 |
/** |
| 2019 |
* Get the current viewport. |
| 2020 |
* |
| 2021 |
* @returns The current viewport as row/column coordinates. |
| 2022 |
* Returns undefined if the grid is not visible. |
| 2023 |
*/ |
| 2024 |
get currentViewport(): DataGrid.IBodyRegion | undefined { |
| 2025 |
let width = this.viewport.node.offsetWidth; |
| 2026 |
let height = this.viewport.node.offsetHeight; |
| 2027 |
|
| 2028 |
width = Math.round(width); |
| 2029 |
height = Math.round(height); |
| 2030 |
|
| 2031 |
if (width <= 0 || height <= 0) { |
| 2032 |
return; |
| 2033 |
} |
| 2034 |
|
| 2035 |
const contentW = this._columnSections.length - this.scrollX; |
| 2036 |
const contentH = this._rowSections.length - this.scrollY; |
| 2037 |
|
| 2038 |
const contentX = this.headerWidth; |
| 2039 |
const contentY = this.headerHeight; |
| 2040 |
|
| 2041 |
const x1 = contentX; |
| 2042 |
const y1 = contentY; |
| 2043 |
const x2 = Math.min(width - 1, contentX + contentW - 1); |
| 2044 |
const y2 = Math.min(height - 1, contentY + contentH - 1); |
| 2045 |
|
| 2046 |
const firstRow = this._rowSections.indexOf(y1 - contentY + this.scrollY); |
| 2047 |
const firstColumn = this._columnSections.indexOf( |
| 2048 |
x1 - contentX + this.scrollX |
| 2049 |
); |
| 2050 |
const lastRow = this._rowSections.indexOf(y2 - contentY + this.scrollY); |
| 2051 |
const lastColumn = this._columnSections.indexOf( |
| 2052 |
x2 - contentX + this.scrollX |
| 2053 |
); |
| 2054 |
|
| 2055 |
return { |
| 2056 |
firstRow, |
| 2057 |
firstColumn, |
| 2058 |
lastRow, |
| 2059 |
lastColumn |
| 2060 |
}; |
| 2061 |
} |
| 2062 |
|
| 2063 |
/** |
| 2064 |
* A message handler invoked on an `'activate-request'` message. |
| 2065 |
*/ |
| 2066 |
protected onActivateRequest(msg: Message): void { |
| 2067 |
this.viewport.node.focus({ preventScroll: true }); |
| 2068 |
} |
| 2069 |
|
| 2070 |
/** |
| 2071 |
* A message handler invoked on a `'before-attach'` message. |
| 2072 |
*/ |
| 2073 |
protected onBeforeAttach(msg: Message): void { |
| 2074 |
window.addEventListener('resize', this); |
| 2075 |
this.node.addEventListener('wheel', this); |
| 2076 |
this._viewport.node.addEventListener('keydown', this); |
| 2077 |
this._viewport.node.addEventListener('mousedown', this); |
| 2078 |
this._viewport.node.addEventListener('mousemove', this); |
| 2079 |
this._viewport.node.addEventListener('dblclick', this); |
| 2080 |
this._viewport.node.addEventListener('mouseleave', this); |
| 2081 |
this._viewport.node.addEventListener('contextmenu', this); |
| 2082 |
this.repaintContent(); |
| 2083 |
this.repaintOverlay(); |
| 2084 |
} |
| 2085 |
|
| 2086 |
/** |
| 2087 |
* A message handler invoked on an `'after-detach'` message. |
| 2088 |
*/ |
| 2089 |
protected onAfterDetach(msg: Message): void { |
| 2090 |
window.removeEventListener('resize', this); |
| 2091 |
this.node.removeEventListener('wheel', this); |
| 2092 |
this._viewport.node.removeEventListener('keydown', this); |
| 2093 |
this._viewport.node.removeEventListener('mousedown', this); |
| 2094 |
this._viewport.node.removeEventListener('mousemove', this); |
| 2095 |
this._viewport.node.removeEventListener('mouseleave', this); |
| 2096 |
this._viewport.node.removeEventListener('dblclick', this); |
| 2097 |
this._viewport.node.removeEventListener('contextmenu', this); |
| 2098 |
this._releaseMouse(); |
| 2099 |
} |
| 2100 |
|
| 2101 |
/** |
| 2102 |
* A message handler invoked on a `'before-show'` message. |
| 2103 |
*/ |
| 2104 |
protected onBeforeShow(msg: Message): void { |
| 2105 |
this.repaintContent(); |
| 2106 |
this.repaintOverlay(); |
| 2107 |
} |
| 2108 |
|
| 2109 |
/** |
| 2110 |
* A message handler invoked on a `'resize'` message. |
| 2111 |
*/ |
| 2112 |
protected onResize(msg: Widget.ResizeMessage): void { |
| 2113 |
if (this._editorController) { |
| 2114 |
this._editorController.cancel(); |
| 2115 |
} |
| 2116 |
|
| 2117 |
this._syncScrollState(); |
| 2118 |
} |
| 2119 |
|
| 2120 |
/** |
| 2121 |
* Schedule a repaint of all of the grid content. |
| 2122 |
*/ |
| 2123 |
protected repaintContent(): void { |
| 2124 |
let msg = new Private.PaintRequest('all', 0, 0, 0, 0); |
| 2125 |
MessageLoop.postMessage(this._viewport, msg); |
| 2126 |
} |
| 2127 |
|
| 2128 |
/** |
| 2129 |
* Schedule a repaint of specific grid content. |
| 2130 |
*/ |
| 2131 |
protected repaintRegion( |
| 2132 |
region: DataModel.CellRegion, |
| 2133 |
r1: number, |
| 2134 |
c1: number, |
| 2135 |
r2: number, |
| 2136 |
c2: number |
| 2137 |
): void { |
| 2138 |
let msg = new Private.PaintRequest(region, r1, c1, r2, c2); |
| 2139 |
MessageLoop.postMessage(this._viewport, msg); |
| 2140 |
} |
| 2141 |
|
| 2142 |
/** |
| 2143 |
* Schedule a repaint of the overlay. |
| 2144 |
*/ |
| 2145 |
protected repaintOverlay(): void { |
| 2146 |
MessageLoop.postMessage(this._viewport, Private.OverlayPaintRequest); |
| 2147 |
} |
| 2148 |
|
| 2149 |
private _getMaxWidthInColumn( |
| 2150 |
index: number, |
| 2151 |
columnRegion: 'row-header' | 'body' |
| 2152 |
): number | null { |
| 2153 |
const dataModel = this.dataModel; |
| 2154 |
|
| 2155 |
if (!dataModel) { |
| 2156 |
return null; |
| 2157 |
} |
| 2158 |
|
| 2159 |
const columnHeaderRegion = |
| 2160 |
columnRegion == 'row-header' ? 'corner-header' : 'column-header'; |
| 2161 |
|
| 2162 |
return Math.max( |
| 2163 |
this._getMaxWidthInArea( |
| 2164 |
dataModel, |
| 2165 |
index, |
| 2166 |
columnHeaderRegion, |
| 2167 |
'column-header' |
| 2168 |
), |
| 2169 |
this._getMaxWidthInArea(dataModel, index, columnRegion, 'body') |
| 2170 |
); |
| 2171 |
} |
| 2172 |
|
| 2173 |
private _getMaxWidthInArea( |
| 2174 |
dataModel: DataModel, |
| 2175 |
index: number, |
| 2176 |
region: DataModel.CellRegion, |
| 2177 |
rowRegion: DataModel.RowRegion |
| 2178 |
): number { |
| 2179 |
const numRows = dataModel.rowCount(rowRegion); |
| 2180 |
// Will only allocate up to 1_000_000 elements otherwise performance can tank. |
| 2181 |
const configs = Array.from( |
| 2182 |
{ length: Math.min(numRows, 1_000_000) }, |
| 2183 |
(_val, idx) => DataGrid._getConfig(dataModel, idx, index, region) |
| 2184 |
); |
| 2185 |
|
| 2186 |
// Heuristic: Sort by the length of the text to render and only fully calculate the text width |
| 2187 |
// for the top 100_000 rows by text length |
| 2188 |
if (numRows > 100_000) { |
| 2189 |
// Sort by descending length |
| 2190 |
configs.sort(x => -this._getTextToRender(x).length); |
| 2191 |
} |
| 2192 |
|
| 2193 |
let maxWidth = 0; |
| 2194 |
for (let i = 0; i < numRows && i < 100_000; ++i) { |
| 2195 |
const textWidth = this._getCellTextWidth(configs[i]); |
| 2196 |
maxWidth = Math.max(maxWidth, textWidth); |
| 2197 |
} |
| 2198 |
|
| 2199 |
return maxWidth; |
| 2200 |
} |
| 2201 |
|
| 2202 |
private static _getConfig( |
| 2203 |
dataModel: DataModel, |
| 2204 |
row: number, |
| 2205 |
col: number, |
| 2206 |
location: DataModel.CellRegion |
| 2207 |
): CellRenderer.CellConfig { |
| 2208 |
return { |
| 2209 |
x: 0, |
| 2210 |
y: 0, |
| 2211 |
width: 0, |
| 2212 |
height: 0, |
| 2213 |
region: location, |
| 2214 |
row: row, |
| 2215 |
column: col, |
| 2216 |
value: DataGrid._getCellValue(dataModel, location, row, col), |
| 2217 |
metadata: DataGrid._getCellMetadata(dataModel, location, row, col) |
| 2218 |
}; |
| 2219 |
} |
| 2220 |
|
| 2221 |
private _getTextToRender(config: CellRenderer.CellConfig): string { |
| 2222 |
const renderer = this.cellRenderers.get(config) as TextRenderer; |
| 2223 |
return renderer.getText(config); |
| 2224 |
} |
| 2225 |
|
| 2226 |
private _getCellTextWidth(config: CellRenderer.CellConfig): number { |
| 2227 |
// Get the renderer for the given cell. |
| 2228 |
const renderer = this.cellRenderers.get(config) as TextRenderer; |
| 2229 |
|
| 2230 |
// Use the canvas context to measure the cell's text width |
| 2231 |
const gc = this.canvasGC; |
| 2232 |
gc.font = CellRenderer.resolveOption(renderer.font, config); |
| 2233 |
gc.fillStyle = CellRenderer.resolveOption(renderer.textColor, config); |
| 2234 |
gc.textAlign = CellRenderer.resolveOption( |
| 2235 |
renderer.horizontalAlignment, |
| 2236 |
config |
| 2237 |
); |
| 2238 |
gc.textBaseline = 'bottom'; |
| 2239 |
|
| 2240 |
const text = this._getTextToRender(config); |
| 2241 |
|
| 2242 |
return gc.measureText(text).width + 2 * renderer.horizontalPadding; |
| 2243 |
} |
| 2244 |
|
| 2245 |
/** |
| 2246 |
* Ensure the canvas is at least the specified size. |
| 2247 |
* |
| 2248 |
* This method will retain the valid canvas content. |
| 2249 |
*/ |
| 2250 |
private _resizeCanvasIfNeeded(width: number, height: number): void { |
| 2251 |
// Scale the size by the dpi ratio. |
| 2252 |
width = width * this._dpiRatio; |
| 2253 |
height = height * this._dpiRatio; |
| 2254 |
|
| 2255 |
// Compute the maximum canvas size for the given width and height. |
| 2256 |
let maxW = (Math.ceil((width + 1) / 512) + 1) * 512; |
| 2257 |
let maxH = (Math.ceil((height + 1) / 512) + 1) * 512; |
| 2258 |
|
| 2259 |
// Get the current size of the canvas. |
| 2260 |
let curW = this._canvas.width; |
| 2261 |
let curH = this._canvas.height; |
| 2262 |
|
| 2263 |
// Bail early if the canvas size is within bounds. |
| 2264 |
if (curW >= width && curH >= height && curW <= maxW && curH <= maxH) { |
| 2265 |
return; |
| 2266 |
} |
| 2267 |
|
| 2268 |
// Compute the expanded canvas size. |
| 2269 |
let expW = maxW - 512; |
| 2270 |
let expH = maxH - 512; |
| 2271 |
|
| 2272 |
// Set the transforms to the identity matrix. |
| 2273 |
this._canvasGC.setTransform(1, 0, 0, 1, 0, 0); |
| 2274 |
this._bufferGC.setTransform(1, 0, 0, 1, 0, 0); |
| 2275 |
this._overlayGC.setTransform(1, 0, 0, 1, 0, 0); |
| 2276 |
|
| 2277 |
// Resize the buffer if needed. |
| 2278 |
if (curW < width) { |
| 2279 |
this._buffer.width = expW; |
| 2280 |
} else if (curW > maxW) { |
| 2281 |
this._buffer.width = maxW; |
| 2282 |
} |
| 2283 |
|
| 2284 |
// Resize the buffer height if needed. |
| 2285 |
if (curH < height) { |
| 2286 |
this._buffer.height = expH; |
| 2287 |
} else if (curH > maxH) { |
| 2288 |
this._buffer.height = maxH; |
| 2289 |
} |
| 2290 |
|
| 2291 |
// Test whether there is content to blit. |
| 2292 |
let needBlit = curW > 0 && curH > 0 && width > 0 && height > 0; |
| 2293 |
|
| 2294 |
// Copy the valid canvas content into the buffer if needed. |
| 2295 |
if (needBlit) { |
| 2296 |
this._bufferGC.drawImage(this._canvas, 0, 0); |
| 2297 |
} |
| 2298 |
|
| 2299 |
// Resize the canvas width if needed. |
| 2300 |
if (curW < width) { |
| 2301 |
this._canvas.width = expW; |
| 2302 |
this._canvas.style.width = `${expW / this._dpiRatio}px`; |
| 2303 |
} else if (curW > maxW) { |
| 2304 |
this._canvas.width = maxW; |
| 2305 |
this._canvas.style.width = `${maxW / this._dpiRatio}px`; |
| 2306 |
} |
| 2307 |
|
| 2308 |
// Resize the canvas height if needed. |
| 2309 |
if (curH < height) { |
| 2310 |
this._canvas.height = expH; |
| 2311 |
this._canvas.style.height = `${expH / this._dpiRatio}px`; |
| 2312 |
} else if (curH > maxH) { |
| 2313 |
this._canvas.height = maxH; |
| 2314 |
this._canvas.style.height = `${maxH / this._dpiRatio}px`; |
| 2315 |
} |
| 2316 |
|
| 2317 |
// Copy the valid canvas content from the buffer if needed. |
| 2318 |
if (needBlit) { |
| 2319 |
this._canvasGC.drawImage(this._buffer, 0, 0); |
| 2320 |
} |
| 2321 |
|
| 2322 |
// Copy the valid overlay content into the buffer if needed. |
| 2323 |
if (needBlit) { |
| 2324 |
this._bufferGC.drawImage(this._overlay, 0, 0); |
| 2325 |
} |
| 2326 |
|
| 2327 |
// Resize the overlay width if needed. |
| 2328 |
if (curW < width) { |
| 2329 |
this._overlay.width = expW; |
| 2330 |
this._overlay.style.width = `${expW / this._dpiRatio}px`; |
| 2331 |
} else if (curW > maxW) { |
| 2332 |
this._overlay.width = maxW; |
| 2333 |
this._overlay.style.width = `${maxW / this._dpiRatio}px`; |
| 2334 |
} |
| 2335 |
|
| 2336 |
// Resize the overlay height if needed. |
| 2337 |
if (curH < height) { |
| 2338 |
this._overlay.height = expH; |
| 2339 |
this._overlay.style.height = `${expH / this._dpiRatio}px`; |
| 2340 |
} else if (curH > maxH) { |
| 2341 |
this._overlay.height = maxH; |
| 2342 |
this._overlay.style.height = `${maxH / this._dpiRatio}px`; |
| 2343 |
} |
| 2344 |
|
| 2345 |
// Copy the valid overlay content from the buffer if needed. |
| 2346 |
if (needBlit) { |
| 2347 |
this._overlayGC.drawImage(this._buffer, 0, 0); |
| 2348 |
} |
| 2349 |
} |
| 2350 |
|
| 2351 |
/** |
| 2352 |
* Sync the scroll bars and scroll state with the viewport. |
| 2353 |
* |
| 2354 |
* #### Notes |
| 2355 |
* If the visibility of either scroll bar changes, a synchronous |
| 2356 |
* fit-request will be dispatched to the data grid to immediately |
| 2357 |
* resize the viewport. |
| 2358 |
*/ |
| 2359 |
private _syncScrollState(): void { |
| 2360 |
// Fetch the viewport dimensions. |
| 2361 |
let bw = this.bodyWidth; |
| 2362 |
let bh = this.bodyHeight; |
| 2363 |
let pw = this.pageWidth; |
| 2364 |
let ph = this.pageHeight; |
| 2365 |
|
| 2366 |
// Get the current scroll bar visibility. |
| 2367 |
let hasVScroll = !this._vScrollBar.isHidden; |
| 2368 |
let hasHScroll = !this._hScrollBar.isHidden; |
| 2369 |
|
| 2370 |
// Get the minimum sizes of the scroll bars. |
| 2371 |
let vsw = this._vScrollBarMinWidth; |
| 2372 |
let hsh = this._hScrollBarMinHeight; |
| 2373 |
|
| 2374 |
// Get the page size as if no scroll bars are visible. |
| 2375 |
let apw = pw + (hasVScroll ? vsw : 0); |
| 2376 |
let aph = ph + (hasHScroll ? hsh : 0); |
| 2377 |
|
| 2378 |
// Test whether scroll bars are needed for the adjusted size. |
| 2379 |
let needVScroll = aph < bh - 1; |
| 2380 |
let needHScroll = apw < bw - 1; |
| 2381 |
|
| 2382 |
// Re-test the horizontal scroll if a vertical scroll is needed. |
| 2383 |
if (needVScroll && !needHScroll) { |
| 2384 |
needHScroll = apw - vsw < bw - 1; |
| 2385 |
} |
| 2386 |
|
| 2387 |
// Re-test the vertical scroll if a horizontal scroll is needed. |
| 2388 |
if (needHScroll && !needVScroll) { |
| 2389 |
needVScroll = aph - hsh < bh - 1; |
| 2390 |
} |
| 2391 |
|
| 2392 |
// If the visibility changes, immediately refit the grid. |
| 2393 |
if (needVScroll !== hasVScroll || needHScroll !== hasHScroll) { |
| 2394 |
this._vScrollBar.setHidden(!needVScroll); |
| 2395 |
this._hScrollBar.setHidden(!needHScroll); |
| 2396 |
this._scrollCorner.setHidden(!needVScroll || !needHScroll); |
| 2397 |
MessageLoop.sendMessage(this, Widget.Msg.FitRequest); |
| 2398 |
} |
| 2399 |
|
| 2400 |
// Update the scroll bar limits. |
| 2401 |
this._vScrollBar.maximum = this.maxScrollY; |
| 2402 |
this._vScrollBar.page = this.pageHeight; |
| 2403 |
this._hScrollBar.maximum = this.maxScrollX; |
| 2404 |
this._hScrollBar.page = this.pageWidth; |
| 2405 |
|
| 2406 |
// Re-clamp the scroll position. |
| 2407 |
this._scrollTo(this._scrollX, this._scrollY); |
| 2408 |
} |
| 2409 |
|
| 2410 |
/** |
| 2411 |
* Sync the viewport to the given scroll position. |
| 2412 |
* |
| 2413 |
* #### Notes |
| 2414 |
* This schedules a full repaint and syncs the scroll state. |
| 2415 |
*/ |
| 2416 |
private _syncViewport(): void { |
| 2417 |
this.repaintContent(); |
| 2418 |
this.repaintOverlay(); |
| 2419 |
this._syncScrollState(); |
| 2420 |
} |
| 2421 |
|
| 2422 |
/** |
| 2423 |
* Process a message sent to the viewport |
| 2424 |
*/ |
| 2425 |
private _processViewportMessage(msg: Message): void { |
| 2426 |
switch (msg.type) { |
| 2427 |
case 'resize': |
| 2428 |
this._onViewportResize(msg as Widget.ResizeMessage); |
| 2429 |
break; |
| 2430 |
case 'scroll-request': |
| 2431 |
this._onViewportScrollRequest(msg); |
| 2432 |
break; |
| 2433 |
case 'paint-request': |
| 2434 |
this._onViewportPaintRequest(msg as Private.PaintRequest); |
| 2435 |
break; |
| 2436 |
case 'overlay-paint-request': |
| 2437 |
this._onViewportOverlayPaintRequest(msg); |
| 2438 |
break; |
| 2439 |
case 'row-resize-request': |
| 2440 |
this._onViewportRowResizeRequest(msg as Private.RowResizeRequest); |
| 2441 |
break; |
| 2442 |
case 'column-resize-request': |
| 2443 |
this._onViewportColumnResizeRequest(msg as Private.ColumnResizeRequest); |
| 2444 |
break; |
| 2445 |
default: |
| 2446 |
break; |
| 2447 |
} |
| 2448 |
} |
| 2449 |
|
| 2450 |
/** |
| 2451 |
* A message hook invoked on a viewport `'resize'` message. |
| 2452 |
*/ |
| 2453 |
private _onViewportResize(msg: Widget.ResizeMessage): void { |
| 2454 |
// Bail early if the viewport is not visible. |
| 2455 |
if (!this._viewport.isVisible) { |
| 2456 |
return; |
| 2457 |
} |
| 2458 |
|
| 2459 |
// Unpack the message data. |
| 2460 |
let { width, height } = msg; |
| 2461 |
|
| 2462 |
// Measure the viewport node if the dimensions are unknown. |
| 2463 |
if (width === -1) { |
| 2464 |
width = this._viewport.node.offsetWidth; |
| 2465 |
} |
| 2466 |
if (height === -1) { |
| 2467 |
height = this._viewport.node.offsetHeight; |
| 2468 |
} |
| 2469 |
|
| 2470 |
// Round the dimensions to the nearest pixel. |
| 2471 |
width = Math.round(width); |
| 2472 |
height = Math.round(height); |
| 2473 |
|
| 2474 |
// Get the current size of the viewport. |
| 2475 |
let oldWidth = this._viewportWidth; |
| 2476 |
let oldHeight = this._viewportHeight; |
| 2477 |
|
| 2478 |
// Updated internal viewport size. |
| 2479 |
this._viewportWidth = width; |
| 2480 |
this._viewportHeight = height; |
| 2481 |
|
| 2482 |
// Resize the canvas if needed. |
| 2483 |
this._resizeCanvasIfNeeded(width, height); |
| 2484 |
|
| 2485 |
// Bail early if there is nothing to paint. |
| 2486 |
if (width === 0 || height === 0) { |
| 2487 |
return; |
| 2488 |
} |
| 2489 |
|
| 2490 |
// Paint the whole grid if the old size was zero. |
| 2491 |
if (oldWidth === 0 || oldHeight === 0) { |
| 2492 |
this.paintContent(0, 0, width, height); |
| 2493 |
this._paintOverlay(); |
| 2494 |
return; |
| 2495 |
} |
| 2496 |
|
| 2497 |
// Paint the right edge as needed. |
| 2498 |
if (this._stretchLastColumn && this.pageWidth > this.bodyWidth) { |
| 2499 |
let bx = this._columnSections.offsetOf(this._columnSections.count - 1); |
| 2500 |
let x = Math.min(this.headerWidth + bx, oldWidth); |
| 2501 |
this.paintContent(x, 0, width - x, height); |
| 2502 |
} else if (width > oldWidth) { |
| 2503 |
this.paintContent(oldWidth, 0, width - oldWidth + 1, height); |
| 2504 |
} |
| 2505 |
|
| 2506 |
// Paint the bottom edge as needed. |
| 2507 |
if (this._stretchLastRow && this.pageHeight > this.bodyHeight) { |
| 2508 |
let by = this._rowSections.offsetOf(this._rowSections.count - 1); |
| 2509 |
let y = Math.min(this.headerHeight + by, oldHeight); |
| 2510 |
this.paintContent(0, y, width, height - y); |
| 2511 |
} else if (height > oldHeight) { |
| 2512 |
this.paintContent(0, oldHeight, width, height - oldHeight + 1); |
| 2513 |
} |
| 2514 |
|
| 2515 |
// Paint the overlay. |
| 2516 |
this._paintOverlay(); |
| 2517 |
} |
| 2518 |
|
| 2519 |
/** |
| 2520 |
* A message hook invoked on a viewport `'scroll-request'` message. |
| 2521 |
*/ |
| 2522 |
private _onViewportScrollRequest(msg: Message): void { |
| 2523 |
this._scrollTo(this._hScrollBar.value, this._vScrollBar.value); |
| 2524 |
} |
| 2525 |
|
| 2526 |
/** |
| 2527 |
* A message hook invoked on a viewport `'paint-request'` message. |
| 2528 |
*/ |
| 2529 |
private _onViewportPaintRequest(msg: Private.PaintRequest): void { |
| 2530 |
// Bail early if the viewport is not visible. |
| 2531 |
if (!this._viewport.isVisible) { |
| 2532 |
return; |
| 2533 |
} |
| 2534 |
|
| 2535 |
// Bail early if the viewport has zero area. |
| 2536 |
if (this._viewportWidth === 0 || this._viewportHeight === 0) { |
| 2537 |
return; |
| 2538 |
} |
| 2539 |
|
| 2540 |
// Set up the paint limits. |
| 2541 |
let xMin = 0; |
| 2542 |
let yMin = 0; |
| 2543 |
let xMax = this._viewportWidth - 1; |
| 2544 |
let yMax = this._viewportHeight - 1; |
| 2545 |
|
| 2546 |
// Fetch the scroll position. |
| 2547 |
let sx = this._scrollX; |
| 2548 |
let sy = this._scrollY; |
| 2549 |
|
| 2550 |
// Fetch the header dimensions. |
| 2551 |
let hw = this.headerWidth; |
| 2552 |
let hh = this.headerHeight; |
| 2553 |
|
| 2554 |
// Fetch the section lists. |
| 2555 |
let rs = this._rowSections; |
| 2556 |
let cs = this._columnSections; |
| 2557 |
let rhs = this._rowHeaderSections; |
| 2558 |
let chs = this._columnHeaderSections; |
| 2559 |
|
| 2560 |
// Unpack the message data. |
| 2561 |
let { region, r1, c1, r2, c2 } = msg; |
| 2562 |
|
| 2563 |
// Set up the paint variables. |
| 2564 |
let x1: number; |
| 2565 |
let y1: number; |
| 2566 |
let x2: number; |
| 2567 |
let y2: number; |
| 2568 |
|
| 2569 |
// Fill the paint variables based on the paint region. |
| 2570 |
switch (region) { |
| 2571 |
case 'all': |
| 2572 |
x1 = xMin; |
| 2573 |
y1 = yMin; |
| 2574 |
x2 = xMax; |
| 2575 |
y2 = yMax; |
| 2576 |
break; |
| 2577 |
case 'body': |
| 2578 |
r1 = Math.max(0, Math.min(r1, rs.count)); |
| 2579 |
c1 = Math.max(0, Math.min(c1, cs.count)); |
| 2580 |
r2 = Math.max(0, Math.min(r2, rs.count)); |
| 2581 |
c2 = Math.max(0, Math.min(c2, cs.count)); |
| 2582 |
x1 = cs.offsetOf(c1) - sx + hw; |
| 2583 |
y1 = rs.offsetOf(r1) - sy + hh; |
| 2584 |
x2 = cs.extentOf(c2) - sx + hw; |
| 2585 |
y2 = rs.extentOf(r2) - sy + hh; |
| 2586 |
break; |
| 2587 |
case 'row-header': |
| 2588 |
r1 = Math.max(0, Math.min(r1, rs.count)); |
| 2589 |
c1 = Math.max(0, Math.min(c1, rhs.count)); |
| 2590 |
r2 = Math.max(0, Math.min(r2, rs.count)); |
| 2591 |
c2 = Math.max(0, Math.min(c2, rhs.count)); |
| 2592 |
x1 = rhs.offsetOf(c1); |
| 2593 |
y1 = rs.offsetOf(r1) - sy + hh; |
| 2594 |
x2 = rhs.extentOf(c2); |
| 2595 |
y2 = rs.extentOf(r2) - sy + hh; |
| 2596 |
break; |
| 2597 |
case 'column-header': |
| 2598 |
r1 = Math.max(0, Math.min(r1, chs.count)); |
| 2599 |
c1 = Math.max(0, Math.min(c1, cs.count)); |
| 2600 |
r2 = Math.max(0, Math.min(r2, chs.count)); |
| 2601 |
c2 = Math.max(0, Math.min(c2, cs.count)); |
| 2602 |
x1 = cs.offsetOf(c1) - sx + hw; |
| 2603 |
y1 = chs.offsetOf(r1); |
| 2604 |
x2 = cs.extentOf(c2) - sx + hw; |
| 2605 |
y2 = chs.extentOf(r2); |
| 2606 |
break; |
| 2607 |
case 'corner-header': |
| 2608 |
r1 = Math.max(0, Math.min(r1, chs.count)); |
| 2609 |
c1 = Math.max(0, Math.min(c1, rhs.count)); |
| 2610 |
r2 = Math.max(0, Math.min(r2, chs.count)); |
| 2611 |
c2 = Math.max(0, Math.min(c2, rhs.count)); |
| 2612 |
x1 = rhs.offsetOf(c1); |
| 2613 |
y1 = chs.offsetOf(r1); |
| 2614 |
x2 = rhs.extentOf(c2); |
| 2615 |
y2 = chs.extentOf(r2); |
| 2616 |
break; |
| 2617 |
default: |
| 2618 |
throw 'unreachable'; |
| 2619 |
} |
| 2620 |
|
| 2621 |
// Bail early if the dirty rect is outside the bounds. |
| 2622 |
if (x2 < xMin || y2 < yMin || x1 > xMax || y1 > yMax) { |
| 2623 |
return; |
| 2624 |
} |
| 2625 |
|
| 2626 |
// Clamp the dirty rect to the paint bounds. |
| 2627 |
x1 = Math.max(xMin, Math.min(x1, xMax)); |
| 2628 |
y1 = Math.max(yMin, Math.min(y1, yMax)); |
| 2629 |
x2 = Math.max(xMin, Math.min(x2, xMax)); |
| 2630 |
y2 = Math.max(yMin, Math.min(y2, yMax)); |
| 2631 |
|
| 2632 |
// Paint the content of the dirty rect. |
| 2633 |
this.paintContent(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 2634 |
} |
| 2635 |
|
| 2636 |
/** |
| 2637 |
* A message hook invoked on a viewport `'overlay-paint-request'` message. |
| 2638 |
*/ |
| 2639 |
private _onViewportOverlayPaintRequest(msg: Message): void { |
| 2640 |
// Bail early if the viewport is not visible. |
| 2641 |
if (!this._viewport.isVisible) { |
| 2642 |
return; |
| 2643 |
} |
| 2644 |
|
| 2645 |
// Bail early if the viewport has zero area. |
| 2646 |
if (this._viewportWidth === 0 || this._viewportHeight === 0) { |
| 2647 |
return; |
| 2648 |
} |
| 2649 |
|
| 2650 |
// Paint the content of the overlay. |
| 2651 |
this._paintOverlay(); |
| 2652 |
} |
| 2653 |
|
| 2654 |
/** |
| 2655 |
* A message hook invoked on a viewport `'row-resize-request'` message. |
| 2656 |
*/ |
| 2657 |
private _onViewportRowResizeRequest(msg: Private.RowResizeRequest): void { |
| 2658 |
if (msg.region === 'body') { |
| 2659 |
this._resizeRow(msg.index, msg.size); |
| 2660 |
} else { |
| 2661 |
this._resizeColumnHeader(msg.index, msg.size); |
| 2662 |
} |
| 2663 |
} |
| 2664 |
|
| 2665 |
/** |
| 2666 |
* A message hook invoked on a viewport `'column-resize-request'` message. |
| 2667 |
*/ |
| 2668 |
private _onViewportColumnResizeRequest( |
| 2669 |
msg: Private.ColumnResizeRequest |
| 2670 |
): void { |
| 2671 |
if (msg.region === 'body') { |
| 2672 |
this._resizeColumn(msg.index, msg.size); |
| 2673 |
} else { |
| 2674 |
this._resizeRowHeader(msg.index, msg.size); |
| 2675 |
} |
| 2676 |
} |
| 2677 |
|
| 2678 |
/** |
| 2679 |
* Handle the `thumbMoved` signal from a scroll bar. |
| 2680 |
*/ |
| 2681 |
private _onThumbMoved(sender: ScrollBar): void { |
| 2682 |
MessageLoop.postMessage(this._viewport, Private.ScrollRequest); |
| 2683 |
} |
| 2684 |
|
| 2685 |
/** |
| 2686 |
* Handle the `pageRequested` signal from a scroll bar. |
| 2687 |
*/ |
| 2688 |
private _onPageRequested( |
| 2689 |
sender: ScrollBar, |
| 2690 |
dir: 'decrement' | 'increment' |
| 2691 |
): void { |
| 2692 |
if (sender === this._vScrollBar) { |
| 2693 |
this.scrollByPage(dir === 'decrement' ? 'up' : 'down'); |
| 2694 |
} else { |
| 2695 |
this.scrollByPage(dir === 'decrement' ? 'left' : 'right'); |
| 2696 |
} |
| 2697 |
} |
| 2698 |
|
| 2699 |
/** |
| 2700 |
* Handle the `stepRequested` signal from a scroll bar. |
| 2701 |
*/ |
| 2702 |
private _onStepRequested( |
| 2703 |
sender: ScrollBar, |
| 2704 |
dir: 'decrement' | 'increment' |
| 2705 |
): void { |
| 2706 |
if (sender === this._vScrollBar) { |
| 2707 |
this.scrollByStep(dir === 'decrement' ? 'up' : 'down'); |
| 2708 |
} else { |
| 2709 |
this.scrollByStep(dir === 'decrement' ? 'left' : 'right'); |
| 2710 |
} |
| 2711 |
} |
| 2712 |
|
| 2713 |
/** |
| 2714 |
* A signal handler for the data model `changed` signal. |
| 2715 |
*/ |
| 2716 |
private _onDataModelChanged( |
| 2717 |
sender: DataModel, |
| 2718 |
args: DataModel.ChangedArgs |
| 2719 |
): void { |
| 2720 |
switch (args.type) { |
| 2721 |
case 'rows-inserted': |
| 2722 |
this._onRowsInserted(args); |
| 2723 |
break; |
| 2724 |
case 'columns-inserted': |
| 2725 |
this._onColumnsInserted(args); |
| 2726 |
break; |
| 2727 |
case 'rows-removed': |
| 2728 |
this._onRowsRemoved(args); |
| 2729 |
break; |
| 2730 |
case 'columns-removed': |
| 2731 |
this._onColumnsRemoved(args); |
| 2732 |
break; |
| 2733 |
case 'rows-moved': |
| 2734 |
this._onRowsMoved(args); |
| 2735 |
break; |
| 2736 |
case 'columns-moved': |
| 2737 |
this._onColumnsMoved(args); |
| 2738 |
break; |
| 2739 |
case 'cells-changed': |
| 2740 |
this._onCellsChanged(args); |
| 2741 |
break; |
| 2742 |
case 'model-reset': |
| 2743 |
this._onModelReset(args); |
| 2744 |
break; |
| 2745 |
default: |
| 2746 |
throw 'unreachable'; |
| 2747 |
} |
| 2748 |
} |
| 2749 |
|
| 2750 |
/** |
| 2751 |
* A signal handler for the selection model `changed` signal. |
| 2752 |
*/ |
| 2753 |
private _onSelectionsChanged(sender: SelectionModel): void { |
| 2754 |
this.repaintOverlay(); |
| 2755 |
} |
| 2756 |
|
| 2757 |
/** |
| 2758 |
* Handle rows being inserted in the data model. |
| 2759 |
*/ |
| 2760 |
private _onRowsInserted(args: DataModel.RowsChangedArgs): void { |
| 2761 |
// Unpack the arg data. |
| 2762 |
let { region, index, span } = args; |
| 2763 |
|
| 2764 |
// Bail early if there are no sections to insert. |
| 2765 |
if (span <= 0) { |
| 2766 |
return; |
| 2767 |
} |
| 2768 |
|
| 2769 |
// Look up the relevant section list. |
| 2770 |
let list: SectionList; |
| 2771 |
if (region === 'body') { |
| 2772 |
list = this._rowSections; |
| 2773 |
} else { |
| 2774 |
list = this._columnHeaderSections; |
| 2775 |
} |
| 2776 |
|
| 2777 |
// Insert the span, maintaining the scroll position as needed. |
| 2778 |
if (this._scrollY === this.maxScrollY && this.maxScrollY > 0) { |
| 2779 |
list.insert(index, span); |
| 2780 |
this._scrollY = this.maxScrollY; |
| 2781 |
} else { |
| 2782 |
list.insert(index, span); |
| 2783 |
} |
| 2784 |
|
| 2785 |
// Sync the viewport. |
| 2786 |
this._syncViewport(); |
| 2787 |
} |
| 2788 |
|
| 2789 |
/** |
| 2790 |
* Handle columns being inserted into the data model. |
| 2791 |
*/ |
| 2792 |
private _onColumnsInserted(args: DataModel.ColumnsChangedArgs): void { |
| 2793 |
// Unpack the arg data. |
| 2794 |
let { region, index, span } = args; |
| 2795 |
|
| 2796 |
// Bail early if there are no sections to insert. |
| 2797 |
if (span <= 0) { |
| 2798 |
return; |
| 2799 |
} |
| 2800 |
|
| 2801 |
// Look up the relevant section list. |
| 2802 |
let list: SectionList; |
| 2803 |
if (region === 'body') { |
| 2804 |
list = this._columnSections; |
| 2805 |
} else { |
| 2806 |
list = this._rowHeaderSections; |
| 2807 |
} |
| 2808 |
|
| 2809 |
// Insert the span, maintaining the scroll position as needed. |
| 2810 |
if (this._scrollX === this.maxScrollX && this.maxScrollX > 0) { |
| 2811 |
list.insert(index, span); |
| 2812 |
this._scrollX = this.maxScrollX; |
| 2813 |
} else { |
| 2814 |
list.insert(index, span); |
| 2815 |
} |
| 2816 |
|
| 2817 |
// Sync the viewport. |
| 2818 |
this._syncViewport(); |
| 2819 |
} |
| 2820 |
|
| 2821 |
/** |
| 2822 |
* Handle rows being removed from the data model. |
| 2823 |
*/ |
| 2824 |
private _onRowsRemoved(args: DataModel.RowsChangedArgs): void { |
| 2825 |
// Unpack the arg data. |
| 2826 |
let { region, index, span } = args; |
| 2827 |
|
| 2828 |
// Bail early if there are no sections to remove. |
| 2829 |
if (span <= 0) { |
| 2830 |
return; |
| 2831 |
} |
| 2832 |
|
| 2833 |
// Look up the relevant section list. |
| 2834 |
let list: SectionList; |
| 2835 |
if (region === 'body') { |
| 2836 |
list = this._rowSections; |
| 2837 |
} else { |
| 2838 |
list = this._columnHeaderSections; |
| 2839 |
} |
| 2840 |
|
| 2841 |
// Bail if the index or is invalid |
| 2842 |
if (index < 0 || index >= list.count) { |
| 2843 |
return; |
| 2844 |
} |
| 2845 |
|
| 2846 |
// Remove the span, maintaining the scroll position as needed. |
| 2847 |
if (this._scrollY === this.maxScrollY && this.maxScrollY > 0) { |
| 2848 |
list.remove(index, span); |
| 2849 |
this._scrollY = this.maxScrollY; |
| 2850 |
} else { |
| 2851 |
list.remove(index, span); |
| 2852 |
} |
| 2853 |
|
| 2854 |
// Sync the viewport. |
| 2855 |
this._syncViewport(); |
| 2856 |
} |
| 2857 |
|
| 2858 |
/** |
| 2859 |
* Handle columns being removed from the data model. |
| 2860 |
*/ |
| 2861 |
private _onColumnsRemoved(args: DataModel.ColumnsChangedArgs): void { |
| 2862 |
// Unpack the arg data. |
| 2863 |
let { region, index, span } = args; |
| 2864 |
|
| 2865 |
// Bail early if there are no sections to remove. |
| 2866 |
if (span <= 0) { |
| 2867 |
return; |
| 2868 |
} |
| 2869 |
|
| 2870 |
// Look up the relevant section list. |
| 2871 |
let list: SectionList; |
| 2872 |
if (region === 'body') { |
| 2873 |
list = this._columnSections; |
| 2874 |
} else { |
| 2875 |
list = this._rowHeaderSections; |
| 2876 |
} |
| 2877 |
|
| 2878 |
// Bail if the index or is invalid |
| 2879 |
if (index < 0 || index >= list.count) { |
| 2880 |
return; |
| 2881 |
} |
| 2882 |
|
| 2883 |
// Remove the span, maintaining the scroll position as needed. |
| 2884 |
if (this._scrollX === this.maxScrollX && this.maxScrollX > 0) { |
| 2885 |
list.remove(index, span); |
| 2886 |
this._scrollX = this.maxScrollX; |
| 2887 |
} else { |
| 2888 |
list.remove(index, span); |
| 2889 |
} |
| 2890 |
|
| 2891 |
// Sync the viewport. |
| 2892 |
this._syncViewport(); |
| 2893 |
} |
| 2894 |
|
| 2895 |
/** |
| 2896 |
* Handle rows moving in the data model. |
| 2897 |
*/ |
| 2898 |
private _onRowsMoved(args: DataModel.RowsMovedArgs): void { |
| 2899 |
// Unpack the arg data. |
| 2900 |
let { region, index, span, destination } = args; |
| 2901 |
|
| 2902 |
// Bail early if there are no sections to move. |
| 2903 |
if (span <= 0) { |
| 2904 |
return; |
| 2905 |
} |
| 2906 |
|
| 2907 |
// Look up the relevant section list. |
| 2908 |
let list: SectionList; |
| 2909 |
if (region === 'body') { |
| 2910 |
list = this._rowSections; |
| 2911 |
} else { |
| 2912 |
list = this._columnHeaderSections; |
| 2913 |
} |
| 2914 |
|
| 2915 |
// Bail early if the index is out of range. |
| 2916 |
if (index < 0 || index >= list.count) { |
| 2917 |
return; |
| 2918 |
} |
| 2919 |
|
| 2920 |
// Clamp the move span to the limit. |
| 2921 |
span = Math.min(span, list.count - index); |
| 2922 |
|
| 2923 |
// Clamp the destination index to the limit. |
| 2924 |
destination = Math.min(Math.max(0, destination), list.count - span); |
| 2925 |
|
| 2926 |
// Bail early if there is no effective move. |
| 2927 |
if (index === destination) { |
| 2928 |
return; |
| 2929 |
} |
| 2930 |
|
| 2931 |
// Compute the first affected index. |
| 2932 |
let r1 = Math.min(index, destination); |
| 2933 |
|
| 2934 |
// Compute the last affected index. |
| 2935 |
let r2 = Math.max(index + span - 1, destination + span - 1); |
| 2936 |
|
| 2937 |
// Move the sections in the list. |
| 2938 |
list.move(index, span, destination); |
| 2939 |
|
| 2940 |
// Schedule a repaint of the dirty cells. |
| 2941 |
if (region === 'body') { |
| 2942 |
this.repaintRegion('body', r1, 0, r2, Infinity); |
| 2943 |
this.repaintRegion('row-header', r1, 0, r2, Infinity); |
| 2944 |
} else { |
| 2945 |
this.repaintRegion('column-header', r1, 0, r2, Infinity); |
| 2946 |
this.repaintRegion('corner-header', r1, 0, r2, Infinity); |
| 2947 |
} |
| 2948 |
|
| 2949 |
// Sync the viewport. |
| 2950 |
this._syncViewport(); |
| 2951 |
} |
| 2952 |
|
| 2953 |
/** |
| 2954 |
* Handle columns moving in the data model. |
| 2955 |
*/ |
| 2956 |
private _onColumnsMoved(args: DataModel.ColumnsMovedArgs): void { |
| 2957 |
// Unpack the arg data. |
| 2958 |
let { region, index, span, destination } = args; |
| 2959 |
|
| 2960 |
// Bail early if there are no sections to move. |
| 2961 |
if (span <= 0) { |
| 2962 |
return; |
| 2963 |
} |
| 2964 |
|
| 2965 |
// Look up the relevant section list. |
| 2966 |
let list: SectionList; |
| 2967 |
if (region === 'body') { |
| 2968 |
list = this._columnSections; |
| 2969 |
} else { |
| 2970 |
list = this._rowHeaderSections; |
| 2971 |
} |
| 2972 |
|
| 2973 |
// Bail early if the index is out of range. |
| 2974 |
if (index < 0 || index >= list.count) { |
| 2975 |
return; |
| 2976 |
} |
| 2977 |
|
| 2978 |
// Clamp the move span to the limit. |
| 2979 |
span = Math.min(span, list.count - index); |
| 2980 |
|
| 2981 |
// Clamp the destination index to the limit. |
| 2982 |
destination = Math.min(Math.max(0, destination), list.count - span); |
| 2983 |
|
| 2984 |
// Bail early if there is no effective move. |
| 2985 |
if (index === destination) { |
| 2986 |
return; |
| 2987 |
} |
| 2988 |
|
| 2989 |
// Move the sections in the list. |
| 2990 |
list.move(index, span, destination); |
| 2991 |
|
| 2992 |
// Compute the first affected index. |
| 2993 |
let c1 = Math.min(index, destination); |
| 2994 |
|
| 2995 |
// Compute the last affected index. |
| 2996 |
let c2 = Math.max(index + span - 1, destination + span - 1); |
| 2997 |
|
| 2998 |
// Schedule a repaint of the dirty cells. |
| 2999 |
if (region === 'body') { |
| 3000 |
this.repaintRegion('body', 0, c1, Infinity, c2); |
| 3001 |
this.repaintRegion('column-header', 0, c1, Infinity, c2); |
| 3002 |
} else { |
| 3003 |
this.repaintRegion('row-header', 0, c1, Infinity, c2); |
| 3004 |
this.repaintRegion('corner-header', 0, c1, Infinity, c2); |
| 3005 |
} |
| 3006 |
|
| 3007 |
// Sync the viewport. |
| 3008 |
this._syncViewport(); |
| 3009 |
} |
| 3010 |
|
| 3011 |
/** |
| 3012 |
* Handle cells changing in the data model. |
| 3013 |
*/ |
| 3014 |
private _onCellsChanged(args: DataModel.CellsChangedArgs): void { |
| 3015 |
// Unpack the arg data. |
| 3016 |
let { region, row, column, rowSpan, columnSpan } = args; |
| 3017 |
|
| 3018 |
// Bail early if there are no cells to modify. |
| 3019 |
if (rowSpan <= 0 && columnSpan <= 0) { |
| 3020 |
return; |
| 3021 |
} |
| 3022 |
|
| 3023 |
// Compute the changed cell bounds. |
| 3024 |
let r1 = row; |
| 3025 |
let c1 = column; |
| 3026 |
let r2 = r1 + rowSpan - 1; |
| 3027 |
let c2 = c1 + columnSpan - 1; |
| 3028 |
|
| 3029 |
// Schedule a repaint of the cell content. |
| 3030 |
this.repaintRegion(region, r1, c1, r2, c2); |
| 3031 |
} |
| 3032 |
|
| 3033 |
/** |
| 3034 |
* Handle a full data model reset. |
| 3035 |
*/ |
| 3036 |
private _onModelReset(args: DataModel.ModelResetArgs): void { |
| 3037 |
// Look up the various current section counts. |
| 3038 |
let nr = this._rowSections.count; |
| 3039 |
let nc = this._columnSections.count; |
| 3040 |
let nrh = this._rowHeaderSections.count; |
| 3041 |
let nch = this._columnHeaderSections.count; |
| 3042 |
|
| 3043 |
// Compute the delta count for each region. |
| 3044 |
let dr = this._dataModel!.rowCount('body') - nr; |
| 3045 |
let dc = this._dataModel!.columnCount('body') - nc; |
| 3046 |
let drh = this._dataModel!.columnCount('row-header') - nrh; |
| 3047 |
let dch = this._dataModel!.rowCount('column-header') - nch; |
| 3048 |
|
| 3049 |
// Update the row sections, if needed. |
| 3050 |
if (dr > 0) { |
| 3051 |
this._rowSections.insert(nr, dr); |
| 3052 |
} else if (dr < 0) { |
| 3053 |
this._rowSections.remove(nr + dr, -dr); |
| 3054 |
} |
| 3055 |
|
| 3056 |
// Update the column sections, if needed. |
| 3057 |
if (dc > 0) { |
| 3058 |
this._columnSections.insert(nc, dc); |
| 3059 |
} else if (dc < 0) { |
| 3060 |
this._columnSections.remove(nc + dc, -dc); |
| 3061 |
} |
| 3062 |
|
| 3063 |
// Update the row header sections, if needed. |
| 3064 |
if (drh > 0) { |
| 3065 |
this._rowHeaderSections.insert(nrh, drh); |
| 3066 |
} else if (drh < 0) { |
| 3067 |
this._rowHeaderSections.remove(nrh + drh, -drh); |
| 3068 |
} |
| 3069 |
|
| 3070 |
// Update the column header sections, if needed. |
| 3071 |
if (dch > 0) { |
| 3072 |
this._columnHeaderSections.insert(nch, dch); |
| 3073 |
} else if (dch < 0) { |
| 3074 |
this._columnHeaderSections.remove(nch + dch, -dch); |
| 3075 |
} |
| 3076 |
|
| 3077 |
// Sync the viewport. |
| 3078 |
this._syncViewport(); |
| 3079 |
} |
| 3080 |
|
| 3081 |
/** |
| 3082 |
* A signal handler for the renderer map `changed` signal. |
| 3083 |
*/ |
| 3084 |
private _onRenderersChanged(): void { |
| 3085 |
this.repaintContent(); |
| 3086 |
} |
| 3087 |
|
| 3088 |
/** |
| 3089 |
* Handle the `'keydown'` event for the data grid. |
| 3090 |
*/ |
| 3091 |
private _evtKeyDown(event: KeyboardEvent): void { |
| 3092 |
if (this._mousedown) { |
| 3093 |
event.preventDefault(); |
| 3094 |
event.stopPropagation(); |
| 3095 |
} else if (this._keyHandler) { |
| 3096 |
this._keyHandler.onKeyDown(this, event); |
| 3097 |
} |
| 3098 |
} |
| 3099 |
|
| 3100 |
/** |
| 3101 |
* Handle the `'mousedown'` event for the data grid. |
| 3102 |
*/ |
| 3103 |
private _evtMouseDown(event: MouseEvent): void { |
| 3104 |
// Ignore everything except the left mouse button. |
| 3105 |
if (event.button !== 0) { |
| 3106 |
return; |
| 3107 |
} |
| 3108 |
|
| 3109 |
// Activate the grid. |
| 3110 |
this.activate(); |
| 3111 |
|
| 3112 |
// Stop the event propagation. |
| 3113 |
event.preventDefault(); |
| 3114 |
event.stopPropagation(); |
| 3115 |
|
| 3116 |
// Add the extra document listeners. |
| 3117 |
document.addEventListener('keydown', this, true); |
| 3118 |
document.addEventListener('mouseup', this, true); |
| 3119 |
document.addEventListener('mousedown', this, true); |
| 3120 |
document.addEventListener('mousemove', this, true); |
| 3121 |
document.addEventListener('contextmenu', this, true); |
| 3122 |
|
| 3123 |
// Flip the mousedown flag. |
| 3124 |
this._mousedown = true; |
| 3125 |
|
| 3126 |
// Dispatch to the mouse handler. |
| 3127 |
if (this._mouseHandler) { |
| 3128 |
this._mouseHandler.onMouseDown(this, event); |
| 3129 |
} |
| 3130 |
} |
| 3131 |
|
| 3132 |
/** |
| 3133 |
* Handle the `'mousemove'` event for the data grid. |
| 3134 |
*/ |
| 3135 |
private _evtMouseMove(event: MouseEvent): void { |
| 3136 |
// Stop the event propagation if the mouse is down. |
| 3137 |
if (this._mousedown) { |
| 3138 |
event.preventDefault(); |
| 3139 |
event.stopPropagation(); |
| 3140 |
} |
| 3141 |
|
| 3142 |
// Bail if there is no mouse handler. |
| 3143 |
if (!this._mouseHandler) { |
| 3144 |
return; |
| 3145 |
} |
| 3146 |
|
| 3147 |
// Dispatch to the mouse handler. |
| 3148 |
if (this._mousedown) { |
| 3149 |
this._mouseHandler.onMouseMove(this, event); |
| 3150 |
} else { |
| 3151 |
this._mouseHandler.onMouseHover(this, event); |
| 3152 |
} |
| 3153 |
} |
| 3154 |
|
| 3155 |
/** |
| 3156 |
* Handle the `'mouseup'` event for the data grid. |
| 3157 |
*/ |
| 3158 |
private _evtMouseUp(event: MouseEvent): void { |
| 3159 |
// Ignore everything except the left mouse button. |
| 3160 |
if (event.button !== 0) { |
| 3161 |
return; |
| 3162 |
} |
| 3163 |
|
| 3164 |
// Stop the event propagation. |
| 3165 |
event.preventDefault(); |
| 3166 |
event.stopPropagation(); |
| 3167 |
|
| 3168 |
// Dispatch to the mouse handler. |
| 3169 |
if (this._mouseHandler) { |
| 3170 |
this._mouseHandler.onMouseUp(this, event); |
| 3171 |
} |
| 3172 |
|
| 3173 |
// Release the mouse. |
| 3174 |
this._releaseMouse(); |
| 3175 |
} |
| 3176 |
|
| 3177 |
/** |
| 3178 |
* Handle the `'dblclick'` event for the data grid. |
| 3179 |
*/ |
| 3180 |
private _evtMouseDoubleClick(event: MouseEvent): void { |
| 3181 |
// Ignore everything except the left mouse button. |
| 3182 |
if (event.button !== 0) { |
| 3183 |
return; |
| 3184 |
} |
| 3185 |
|
| 3186 |
// Stop the event propagation. |
| 3187 |
event.preventDefault(); |
| 3188 |
event.stopPropagation(); |
| 3189 |
|
| 3190 |
// Dispatch to the mouse handler. |
| 3191 |
if (this._mouseHandler) { |
| 3192 |
this._mouseHandler.onMouseDoubleClick(this, event); |
| 3193 |
} |
| 3194 |
|
| 3195 |
// Release the mouse. |
| 3196 |
this._releaseMouse(); |
| 3197 |
} |
| 3198 |
|
| 3199 |
/** |
| 3200 |
* Handle the `'mouseleave'` event for the data grid. |
| 3201 |
*/ |
| 3202 |
private _evtMouseLeave(event: MouseEvent): void { |
| 3203 |
if (this._mousedown) { |
| 3204 |
event.preventDefault(); |
| 3205 |
event.stopPropagation(); |
| 3206 |
} else if (this._mouseHandler) { |
| 3207 |
this._mouseHandler.onMouseLeave(this, event); |
| 3208 |
} |
| 3209 |
} |
| 3210 |
|
| 3211 |
/** |
| 3212 |
* Handle the `'contextmenu'` event for the data grid. |
| 3213 |
*/ |
| 3214 |
private _evtContextMenu(event: MouseEvent): void { |
| 3215 |
if (this._mousedown) { |
| 3216 |
event.preventDefault(); |
| 3217 |
event.stopPropagation(); |
| 3218 |
} else if (this._mouseHandler) { |
| 3219 |
this._mouseHandler.onContextMenu(this, event); |
| 3220 |
} |
| 3221 |
} |
| 3222 |
|
| 3223 |
/** |
| 3224 |
* Handle the `'wheel'` event for the data grid. |
| 3225 |
*/ |
| 3226 |
private _evtWheel(event: WheelEvent): void { |
| 3227 |
// Ignore the event if `accel` is held. |
| 3228 |
if (Platform.accelKey(event)) { |
| 3229 |
return; |
| 3230 |
} |
| 3231 |
|
| 3232 |
// Bail early if there is no mouse handler. |
| 3233 |
if (!this._mouseHandler) { |
| 3234 |
return; |
| 3235 |
} |
| 3236 |
|
| 3237 |
// Dispatch to the mouse handler. |
| 3238 |
this._mouseHandler.onWheel(this, event); |
| 3239 |
} |
| 3240 |
|
| 3241 |
/** |
| 3242 |
* Release the mouse grab. |
| 3243 |
*/ |
| 3244 |
private _releaseMouse(): void { |
| 3245 |
// Clear the mousedown flag. |
| 3246 |
this._mousedown = false; |
| 3247 |
|
| 3248 |
// Relase the mouse handler. |
| 3249 |
if (this._mouseHandler) { |
| 3250 |
this._mouseHandler.release(); |
| 3251 |
} |
| 3252 |
|
| 3253 |
// Remove the document listeners. |
| 3254 |
document.removeEventListener('keydown', this, true); |
| 3255 |
document.removeEventListener('mouseup', this, true); |
| 3256 |
document.removeEventListener('mousedown', this, true); |
| 3257 |
document.removeEventListener('mousemove', this, true); |
| 3258 |
document.removeEventListener('contextmenu', this, true); |
| 3259 |
} |
| 3260 |
|
| 3261 |
/** |
| 3262 |
* Refresh the dpi ratio. |
| 3263 |
*/ |
| 3264 |
private _refreshDPI(): void { |
| 3265 |
// Get the best integral value for the dpi ratio. |
| 3266 |
let dpiRatio = Math.ceil(window.devicePixelRatio); |
| 3267 |
|
| 3268 |
// Bail early if the computed dpi ratio has not changed. |
| 3269 |
if (this._dpiRatio === dpiRatio) { |
| 3270 |
return; |
| 3271 |
} |
| 3272 |
|
| 3273 |
// Update the internal dpi ratio. |
| 3274 |
this._dpiRatio = dpiRatio; |
| 3275 |
|
| 3276 |
// Schedule a repaint of the content. |
| 3277 |
this.repaintContent(); |
| 3278 |
|
| 3279 |
// Schedule a repaint of the overlay. |
| 3280 |
this.repaintOverlay(); |
| 3281 |
|
| 3282 |
// Update the canvas size for the new dpi ratio. |
| 3283 |
this._resizeCanvasIfNeeded(this._viewportWidth, this._viewportHeight); |
| 3284 |
|
| 3285 |
// Ensure the canvas style is scaled for the new ratio. |
| 3286 |
this._canvas.style.width = `${this._canvas.width / this._dpiRatio}px`; |
| 3287 |
this._canvas.style.height = `${this._canvas.height / this._dpiRatio}px`; |
| 3288 |
|
| 3289 |
// Ensure the overlay style is scaled for the new ratio. |
| 3290 |
this._overlay.style.width = `${this._overlay.width / this._dpiRatio}px`; |
| 3291 |
this._overlay.style.height = `${this._overlay.height / this._dpiRatio}px`; |
| 3292 |
} |
| 3293 |
|
| 3294 |
/** |
| 3295 |
* Resize a row section immediately. |
| 3296 |
*/ |
| 3297 |
private _resizeRow(index: number, size: number): void { |
| 3298 |
// Look up the target section list. |
| 3299 |
let list = this._rowSections; |
| 3300 |
|
| 3301 |
// Bail early if the index is out of range. |
| 3302 |
if (index < 0 || index >= list.count) { |
| 3303 |
return; |
| 3304 |
} |
| 3305 |
|
| 3306 |
// Look up the old size of the section. |
| 3307 |
let oldSize = list.sizeOf(index); |
| 3308 |
|
| 3309 |
// Normalize the new size of the section. |
| 3310 |
let newSize = list.clampSize(size); |
| 3311 |
|
| 3312 |
// Bail early if the size does not change. |
| 3313 |
if (oldSize === newSize) { |
| 3314 |
return; |
| 3315 |
} |
| 3316 |
|
| 3317 |
// Resize the section in the list. |
| 3318 |
list.resize(index, newSize); |
| 3319 |
|
| 3320 |
// Get the current size of the viewport. |
| 3321 |
let vw = this._viewportWidth; |
| 3322 |
let vh = this._viewportHeight; |
| 3323 |
|
| 3324 |
// If there is nothing to paint, sync the scroll state. |
| 3325 |
if (!this._viewport.isVisible || vw === 0 || vh === 0) { |
| 3326 |
this._syncScrollState(); |
| 3327 |
return; |
| 3328 |
} |
| 3329 |
|
| 3330 |
// Compute the size delta. |
| 3331 |
let delta = newSize - oldSize; |
| 3332 |
|
| 3333 |
// Look up the column header height. |
| 3334 |
let hh = this.headerHeight; |
| 3335 |
|
| 3336 |
// Compute the viewport offset of the section. |
| 3337 |
let offset = list.offsetOf(index) + hh - this._scrollY; |
| 3338 |
|
| 3339 |
// Bail early if there is nothing to paint. |
| 3340 |
if (hh >= vh || offset >= vh) { |
| 3341 |
this._syncScrollState(); |
| 3342 |
return; |
| 3343 |
} |
| 3344 |
|
| 3345 |
// Update the scroll position if the section is not visible. |
| 3346 |
if (offset + oldSize <= hh) { |
| 3347 |
this._scrollY += delta; |
| 3348 |
this._syncScrollState(); |
| 3349 |
return; |
| 3350 |
} |
| 3351 |
|
| 3352 |
// Compute the paint origin of the section. |
| 3353 |
let pos = Math.max(hh, offset); |
| 3354 |
|
| 3355 |
// Paint from the section onward if it spans the viewport. |
| 3356 |
if (offset + oldSize >= vh || offset + newSize >= vh) { |
| 3357 |
this.paintContent(0, pos, vw, vh - pos); |
| 3358 |
this._paintOverlay(); |
| 3359 |
this._syncScrollState(); |
| 3360 |
return; |
| 3361 |
} |
| 3362 |
|
| 3363 |
// Compute the X blit dimensions. |
| 3364 |
let sx = 0; |
| 3365 |
let sw = vw; |
| 3366 |
let dx = 0; |
| 3367 |
|
| 3368 |
// Compute the Y blit dimensions. |
| 3369 |
let sy: number; |
| 3370 |
let sh: number; |
| 3371 |
let dy: number; |
| 3372 |
if (offset + newSize <= hh) { |
| 3373 |
sy = hh - delta; |
| 3374 |
sh = vh - sy; |
| 3375 |
dy = hh; |
| 3376 |
} else { |
| 3377 |
sy = offset + oldSize; |
| 3378 |
sh = vh - sy; |
| 3379 |
dy = sy + delta; |
| 3380 |
} |
| 3381 |
|
| 3382 |
// Blit the valid content to the destination. |
| 3383 |
this._blitContent(this._canvas, sx, sy, sw, sh, dx, dy); |
| 3384 |
|
| 3385 |
// Repaint the section if needed. |
| 3386 |
if (newSize > 0 && offset + newSize > hh) { |
| 3387 |
this.paintContent(0, pos, vw, offset + newSize - pos); |
| 3388 |
} |
| 3389 |
|
| 3390 |
// Paint the trailing space as needed. |
| 3391 |
if (this._stretchLastRow && this.pageHeight > this.bodyHeight) { |
| 3392 |
let r = this._rowSections.count - 1; |
| 3393 |
let y = hh + this._rowSections.offsetOf(r); |
| 3394 |
this.paintContent(0, y, vw, vh - y); |
| 3395 |
} else if (delta < 0) { |
| 3396 |
this.paintContent(0, vh + delta, vw, -delta); |
| 3397 |
} |
| 3398 |
|
| 3399 |
// Repaint merged cells that are intersected by the resized row |
| 3400 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 3401 |
for (const rgn of ['body', 'row-header'] as DataModel.CellRegion[]) { |
| 3402 |
const cellGroups = CellGroup.getCellGroupsAtRow( |
| 3403 |
this.dataModel!, |
| 3404 |
rgn, |
| 3405 |
index |
| 3406 |
); |
| 3407 |
|
| 3408 |
let paintRgn = { |
| 3409 |
region: rgn, |
| 3410 |
xMin: 0, |
| 3411 |
xMax: 0, |
| 3412 |
yMin: 0, |
| 3413 |
yMax: 0 |
| 3414 |
}; |
| 3415 |
|
| 3416 |
let backgroundColor = undefined; |
| 3417 |
|
| 3418 |
switch (rgn) { |
| 3419 |
case 'body': |
| 3420 |
paintRgn.xMin = this.headerWidth; |
| 3421 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 3422 |
paintRgn.yMin = this.headerHeight; |
| 3423 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 3424 |
|
| 3425 |
backgroundColor = this._style.backgroundColor; |
| 3426 |
break; |
| 3427 |
case 'row-header': |
| 3428 |
paintRgn.xMin = 0; |
| 3429 |
paintRgn.xMax = this.headerWidth; |
| 3430 |
paintRgn.yMin = this.headerHeight; |
| 3431 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 3432 |
|
| 3433 |
backgroundColor = this._style.headerBackgroundColor; |
| 3434 |
break; |
| 3435 |
} |
| 3436 |
|
| 3437 |
this._paintMergedCells(cellGroups, paintRgn, backgroundColor); |
| 3438 |
} |
| 3439 |
|
| 3440 |
// Paint the overlay. |
| 3441 |
this._paintOverlay(); |
| 3442 |
|
| 3443 |
// Sync the scroll state. |
| 3444 |
this._syncScrollState(); |
| 3445 |
} |
| 3446 |
|
| 3447 |
/** |
| 3448 |
* Resize a column section immediately. |
| 3449 |
*/ |
| 3450 |
private _resizeColumn(index: number, size: number | null): void { |
| 3451 |
// Look up the target section list. |
| 3452 |
let list = this._columnSections; |
| 3453 |
|
| 3454 |
// Bail early if the index is out of range. |
| 3455 |
if (index < 0 || index >= list.count) { |
| 3456 |
return; |
| 3457 |
} |
| 3458 |
|
| 3459 |
const adjustedSize = size ?? this._getMaxWidthInColumn(index, 'body'); |
| 3460 |
|
| 3461 |
if (!adjustedSize || adjustedSize == 0) { |
| 3462 |
return; |
| 3463 |
} |
| 3464 |
|
| 3465 |
// Look up the old size of the section. |
| 3466 |
let oldSize = list.sizeOf(index); |
| 3467 |
|
| 3468 |
// Normalize the new size of the section. |
| 3469 |
let newSize = list.clampSize(adjustedSize); |
| 3470 |
|
| 3471 |
// Bail early if the size does not change. |
| 3472 |
if (oldSize === newSize) { |
| 3473 |
return; |
| 3474 |
} |
| 3475 |
|
| 3476 |
// Resize the section in the list. |
| 3477 |
list.resize(index, newSize); |
| 3478 |
|
| 3479 |
// Get the current size of the viewport. |
| 3480 |
let vw = this._viewportWidth; |
| 3481 |
let vh = this._viewportHeight; |
| 3482 |
|
| 3483 |
// If there is nothing to paint, sync the scroll state. |
| 3484 |
if (!this._viewport.isVisible || vw === 0 || vh === 0) { |
| 3485 |
this._syncScrollState(); |
| 3486 |
return; |
| 3487 |
} |
| 3488 |
|
| 3489 |
// Compute the size delta. |
| 3490 |
let delta = newSize - oldSize; |
| 3491 |
|
| 3492 |
// Look up the row header width. |
| 3493 |
let hw = this.headerWidth; |
| 3494 |
|
| 3495 |
// Compute the viewport offset of the section. |
| 3496 |
let offset = list.offsetOf(index) + hw - this._scrollX; |
| 3497 |
|
| 3498 |
// Bail early if there is nothing to paint. |
| 3499 |
if (hw >= vw || offset >= vw) { |
| 3500 |
this._syncScrollState(); |
| 3501 |
return; |
| 3502 |
} |
| 3503 |
|
| 3504 |
// Update the scroll position if the section is not visible. |
| 3505 |
if (offset + oldSize <= hw) { |
| 3506 |
this._scrollX += delta; |
| 3507 |
this._syncScrollState(); |
| 3508 |
return; |
| 3509 |
} |
| 3510 |
|
| 3511 |
// Compute the paint origin of the section. |
| 3512 |
let pos = Math.max(hw, offset); |
| 3513 |
|
| 3514 |
// Paint from the section onward if it spans the viewport. |
| 3515 |
if (offset + oldSize >= vw || offset + newSize >= vw) { |
| 3516 |
this.paintContent(pos, 0, vw - pos, vh); |
| 3517 |
this._paintOverlay(); |
| 3518 |
this._syncScrollState(); |
| 3519 |
return; |
| 3520 |
} |
| 3521 |
|
| 3522 |
// Compute the Y blit dimensions. |
| 3523 |
let sy = 0; |
| 3524 |
let sh = vh; |
| 3525 |
let dy = 0; |
| 3526 |
|
| 3527 |
// Compute the X blit dimensions. |
| 3528 |
let sx: number; |
| 3529 |
let sw: number; |
| 3530 |
let dx: number; |
| 3531 |
if (offset + newSize <= hw) { |
| 3532 |
sx = hw - delta; |
| 3533 |
sw = vw - sx; |
| 3534 |
dx = hw; |
| 3535 |
} else { |
| 3536 |
sx = offset + oldSize; |
| 3537 |
sw = vw - sx; |
| 3538 |
dx = sx + delta; |
| 3539 |
} |
| 3540 |
|
| 3541 |
// Blit the valid content to the destination. |
| 3542 |
this._blitContent(this._canvas, sx, sy, sw, sh, dx, dy); |
| 3543 |
|
| 3544 |
// Repaint the section if needed. |
| 3545 |
if (newSize > 0 && offset + newSize > hw) { |
| 3546 |
this.paintContent(pos, 0, offset + newSize - pos, vh); |
| 3547 |
} |
| 3548 |
|
| 3549 |
// Paint the trailing space as needed. |
| 3550 |
if (this._stretchLastColumn && this.pageWidth > this.bodyWidth) { |
| 3551 |
let c = this._columnSections.count - 1; |
| 3552 |
let x = hw + this._columnSections.offsetOf(c); |
| 3553 |
this.paintContent(x, 0, vw - x, vh); |
| 3554 |
} else if (delta < 0) { |
| 3555 |
this.paintContent(vw + delta, 0, -delta, vh); |
| 3556 |
} |
| 3557 |
|
| 3558 |
// Repaint merged cells that are intersected by the resized column |
| 3559 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 3560 |
for (const rgn of ['body', 'column-header'] as DataModel.CellRegion[]) { |
| 3561 |
const cellGroups = CellGroup.getCellGroupsAtColumn( |
| 3562 |
this.dataModel!, |
| 3563 |
rgn, |
| 3564 |
index |
| 3565 |
); |
| 3566 |
|
| 3567 |
let paintRgn = { |
| 3568 |
region: rgn, |
| 3569 |
xMin: 0, |
| 3570 |
xMax: 0, |
| 3571 |
yMin: 0, |
| 3572 |
yMax: 0 |
| 3573 |
}; |
| 3574 |
|
| 3575 |
let backgroundColor = undefined; |
| 3576 |
|
| 3577 |
switch (rgn) { |
| 3578 |
case 'body': |
| 3579 |
paintRgn.xMin = this.headerWidth; |
| 3580 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 3581 |
paintRgn.yMin = this.headerHeight; |
| 3582 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 3583 |
|
| 3584 |
backgroundColor = this._style.backgroundColor; |
| 3585 |
break; |
| 3586 |
case 'column-header': |
| 3587 |
paintRgn.xMin = this.headerWidth; |
| 3588 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 3589 |
paintRgn.yMin = 0; |
| 3590 |
paintRgn.yMax = this.headerHeight; |
| 3591 |
|
| 3592 |
backgroundColor = this._style.headerBackgroundColor; |
| 3593 |
break; |
| 3594 |
} |
| 3595 |
|
| 3596 |
this._paintMergedCells(cellGroups, paintRgn, backgroundColor); |
| 3597 |
} |
| 3598 |
|
| 3599 |
// Paint the overlay. |
| 3600 |
this._paintOverlay(); |
| 3601 |
|
| 3602 |
// Sync the scroll state after painting. |
| 3603 |
this._syncScrollState(); |
| 3604 |
} |
| 3605 |
|
| 3606 |
/** |
| 3607 |
* Resize a row header section immediately. |
| 3608 |
*/ |
| 3609 |
private _resizeRowHeader(index: number, size: number | null): void { |
| 3610 |
// Look up the target section list. |
| 3611 |
let list = this._rowHeaderSections; |
| 3612 |
|
| 3613 |
// Bail early if the index is out of range. |
| 3614 |
if (index < 0 || index >= list.count) { |
| 3615 |
return; |
| 3616 |
} |
| 3617 |
|
| 3618 |
const adjustedSize = size ?? this._getMaxWidthInColumn(index, 'row-header'); |
| 3619 |
|
| 3620 |
if (!adjustedSize || adjustedSize == 0) { |
| 3621 |
return; |
| 3622 |
} |
| 3623 |
|
| 3624 |
// Look up the old size of the section. |
| 3625 |
let oldSize = list.sizeOf(index); |
| 3626 |
|
| 3627 |
// Normalize the new size of the section. |
| 3628 |
let newSize = list.clampSize(adjustedSize); |
| 3629 |
|
| 3630 |
// Bail early if the size does not change. |
| 3631 |
if (oldSize === newSize) { |
| 3632 |
return; |
| 3633 |
} |
| 3634 |
|
| 3635 |
// Resize the section in the list. |
| 3636 |
list.resize(index, newSize); |
| 3637 |
|
| 3638 |
// Get the current size of the viewport. |
| 3639 |
let vw = this._viewportWidth; |
| 3640 |
let vh = this._viewportHeight; |
| 3641 |
|
| 3642 |
// If there is nothing to paint, sync the scroll state. |
| 3643 |
if (!this._viewport.isVisible || vw === 0 || vh === 0) { |
| 3644 |
this._syncScrollState(); |
| 3645 |
return; |
| 3646 |
} |
| 3647 |
|
| 3648 |
// Compute the size delta. |
| 3649 |
let delta = newSize - oldSize; |
| 3650 |
|
| 3651 |
// Look up the offset of the section. |
| 3652 |
let offset = list.offsetOf(index); |
| 3653 |
|
| 3654 |
// Bail early if the section is fully outside the viewport. |
| 3655 |
if (offset >= vw) { |
| 3656 |
this._syncScrollState(); |
| 3657 |
return; |
| 3658 |
} |
| 3659 |
|
| 3660 |
// Paint the entire tail if the section spans the viewport. |
| 3661 |
if (offset + oldSize >= vw || offset + newSize >= vw) { |
| 3662 |
this.paintContent(offset, 0, vw - offset, vh); |
| 3663 |
this._paintOverlay(); |
| 3664 |
this._syncScrollState(); |
| 3665 |
return; |
| 3666 |
} |
| 3667 |
|
| 3668 |
// Compute the blit content dimensions. |
| 3669 |
let sx = offset + oldSize; |
| 3670 |
let sy = 0; |
| 3671 |
let sw = vw - sx; |
| 3672 |
let sh = vh; |
| 3673 |
let dx = sx + delta; |
| 3674 |
let dy = 0; |
| 3675 |
|
| 3676 |
// Blit the valid content to the destination. |
| 3677 |
this._blitContent(this._canvas, sx, sy, sw, sh, dx, dy); |
| 3678 |
|
| 3679 |
// Repaint the header section if needed. |
| 3680 |
if (newSize > 0) { |
| 3681 |
this.paintContent(offset, 0, newSize, vh); |
| 3682 |
} |
| 3683 |
|
| 3684 |
// Paint the trailing space as needed. |
| 3685 |
if (this._stretchLastColumn && this.pageWidth > this.bodyWidth) { |
| 3686 |
let c = this._columnSections.count - 1; |
| 3687 |
let x = this.headerWidth + this._columnSections.offsetOf(c); |
| 3688 |
this.paintContent(x, 0, vw - x, vh); |
| 3689 |
} else if (delta < 0) { |
| 3690 |
this.paintContent(vw + delta, 0, -delta, vh); |
| 3691 |
} |
| 3692 |
|
| 3693 |
// Repaint merged cells that are intersected by the resized row |
| 3694 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 3695 |
for (const rgn of [ |
| 3696 |
'corner-header', |
| 3697 |
'row-header' |
| 3698 |
] as DataModel.CellRegion[]) { |
| 3699 |
const cellGroups = CellGroup.getCellGroupsAtColumn( |
| 3700 |
this.dataModel!, |
| 3701 |
rgn, |
| 3702 |
index |
| 3703 |
); |
| 3704 |
|
| 3705 |
let paintRgn = { |
| 3706 |
region: rgn, |
| 3707 |
xMin: 0, |
| 3708 |
xMax: 0, |
| 3709 |
yMin: 0, |
| 3710 |
yMax: 0 |
| 3711 |
}; |
| 3712 |
|
| 3713 |
switch (rgn) { |
| 3714 |
case 'corner-header': |
| 3715 |
paintRgn.xMin = 0; |
| 3716 |
paintRgn.xMax = this.headerWidth; |
| 3717 |
paintRgn.yMin = 0; |
| 3718 |
paintRgn.yMax = this.headerHeight; |
| 3719 |
break; |
| 3720 |
case 'row-header': |
| 3721 |
paintRgn.xMin = 0; |
| 3722 |
paintRgn.xMax = this.headerWidth; |
| 3723 |
paintRgn.yMin = this.headerHeight; |
| 3724 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 3725 |
break; |
| 3726 |
} |
| 3727 |
|
| 3728 |
this._paintMergedCells( |
| 3729 |
cellGroups, |
| 3730 |
paintRgn, |
| 3731 |
this._style.headerBackgroundColor |
| 3732 |
); |
| 3733 |
} |
| 3734 |
|
| 3735 |
// Paint the overlay. |
| 3736 |
this._paintOverlay(); |
| 3737 |
|
| 3738 |
// Sync the scroll state after painting. |
| 3739 |
this._syncScrollState(); |
| 3740 |
} |
| 3741 |
|
| 3742 |
/** |
| 3743 |
* Resize a column header section immediately. |
| 3744 |
*/ |
| 3745 |
private _resizeColumnHeader(index: number, size: number): void { |
| 3746 |
// Look up the target section list. |
| 3747 |
let list = this._columnHeaderSections; |
| 3748 |
|
| 3749 |
// Bail early if the index is out of range. |
| 3750 |
if (index < 0 || index >= list.count) { |
| 3751 |
return; |
| 3752 |
} |
| 3753 |
|
| 3754 |
// Look up the old size of the section. |
| 3755 |
let oldSize = list.sizeOf(index); |
| 3756 |
|
| 3757 |
// Normalize the new size of the section. |
| 3758 |
let newSize = list.clampSize(size); |
| 3759 |
|
| 3760 |
// Bail early if the size does not change. |
| 3761 |
if (oldSize === newSize) { |
| 3762 |
return; |
| 3763 |
} |
| 3764 |
|
| 3765 |
// Resize the section in the list. |
| 3766 |
list.resize(index, newSize); |
| 3767 |
|
| 3768 |
// Get the current size of the viewport. |
| 3769 |
let vw = this._viewportWidth; |
| 3770 |
let vh = this._viewportHeight; |
| 3771 |
|
| 3772 |
// If there is nothing to paint, sync the scroll state. |
| 3773 |
if (!this._viewport.isVisible || vw === 0 || vh === 0) { |
| 3774 |
this._syncScrollState(); |
| 3775 |
return; |
| 3776 |
} |
| 3777 |
|
| 3778 |
// Paint the overlay. |
| 3779 |
this._paintOverlay(); |
| 3780 |
|
| 3781 |
// Compute the size delta. |
| 3782 |
let delta = newSize - oldSize; |
| 3783 |
|
| 3784 |
// Look up the offset of the section. |
| 3785 |
let offset = list.offsetOf(index); |
| 3786 |
|
| 3787 |
// Bail early if the section is fully outside the viewport. |
| 3788 |
if (offset >= vh) { |
| 3789 |
this._syncScrollState(); |
| 3790 |
return; |
| 3791 |
} |
| 3792 |
|
| 3793 |
// Paint the entire tail if the section spans the viewport. |
| 3794 |
if (offset + oldSize >= vh || offset + newSize >= vh) { |
| 3795 |
this.paintContent(0, offset, vw, vh - offset); |
| 3796 |
this._paintOverlay(); |
| 3797 |
this._syncScrollState(); |
| 3798 |
return; |
| 3799 |
} |
| 3800 |
|
| 3801 |
// Compute the blit content dimensions. |
| 3802 |
let sx = 0; |
| 3803 |
let sy = offset + oldSize; |
| 3804 |
let sw = vw; |
| 3805 |
let sh = vh - sy; |
| 3806 |
let dx = 0; |
| 3807 |
let dy = sy + delta; |
| 3808 |
|
| 3809 |
// Blit the valid contents to the destination. |
| 3810 |
this._blitContent(this._canvas, sx, sy, sw, sh, dx, dy); |
| 3811 |
|
| 3812 |
// Repaint the header section if needed. |
| 3813 |
if (newSize > 0) { |
| 3814 |
this.paintContent(0, offset, vw, newSize); |
| 3815 |
} |
| 3816 |
|
| 3817 |
// Paint the trailing space as needed. |
| 3818 |
if (this._stretchLastRow && this.pageHeight > this.bodyHeight) { |
| 3819 |
let r = this._rowSections.count - 1; |
| 3820 |
let y = this.headerHeight + this._rowSections.offsetOf(r); |
| 3821 |
this.paintContent(0, y, vw, vh - y); |
| 3822 |
} else if (delta < 0) { |
| 3823 |
this.paintContent(0, vh + delta, vw, -delta); |
| 3824 |
} |
| 3825 |
|
| 3826 |
// Repaint merged cells that are intersected by the resized row |
| 3827 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 3828 |
for (const rgn of [ |
| 3829 |
'corner-header', |
| 3830 |
'column-header' |
| 3831 |
] as DataModel.CellRegion[]) { |
| 3832 |
const cellGroups = CellGroup.getCellGroupsAtRow( |
| 3833 |
this.dataModel!, |
| 3834 |
rgn, |
| 3835 |
index |
| 3836 |
); |
| 3837 |
|
| 3838 |
let paintRgn = { |
| 3839 |
region: rgn, |
| 3840 |
xMin: 0, |
| 3841 |
xMax: 0, |
| 3842 |
yMin: 0, |
| 3843 |
yMax: 0 |
| 3844 |
}; |
| 3845 |
|
| 3846 |
switch (rgn) { |
| 3847 |
case 'corner-header': |
| 3848 |
paintRgn.xMin = 0; |
| 3849 |
paintRgn.xMax = this.headerWidth; |
| 3850 |
paintRgn.yMin = 0; |
| 3851 |
paintRgn.yMax = this.headerHeight; |
| 3852 |
break; |
| 3853 |
case 'column-header': |
| 3854 |
paintRgn.xMin = this.headerWidth; |
| 3855 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 3856 |
paintRgn.yMin = 0; |
| 3857 |
paintRgn.yMax = this.headerHeight; |
| 3858 |
break; |
| 3859 |
} |
| 3860 |
|
| 3861 |
this._paintMergedCells( |
| 3862 |
cellGroups, |
| 3863 |
paintRgn, |
| 3864 |
this._style.headerBackgroundColor |
| 3865 |
); |
| 3866 |
} |
| 3867 |
|
| 3868 |
// Paint the overlay. |
| 3869 |
this._paintOverlay(); |
| 3870 |
|
| 3871 |
// Sync the scroll state after painting. |
| 3872 |
this._syncScrollState(); |
| 3873 |
} |
| 3874 |
|
| 3875 |
/** |
| 3876 |
* Scroll immediately to the specified offset position. |
| 3877 |
*/ |
| 3878 |
private _scrollTo(x: number, y: number): void { |
| 3879 |
// Bail if no data model found. |
| 3880 |
if (!this.dataModel) { |
| 3881 |
return; |
| 3882 |
} |
| 3883 |
|
| 3884 |
// Floor and clamp the position to the allowable range. |
| 3885 |
x = Math.max(0, Math.min(Math.floor(x), this.maxScrollX)); |
| 3886 |
y = Math.max(0, Math.min(Math.floor(y), this.maxScrollY)); |
| 3887 |
|
| 3888 |
// Synchronize the scroll bar values. |
| 3889 |
this._hScrollBar.value = x; |
| 3890 |
this._vScrollBar.value = y; |
| 3891 |
|
| 3892 |
// Compute the delta scroll amount. |
| 3893 |
let dx = x - this._scrollX; |
| 3894 |
let dy = y - this._scrollY; |
| 3895 |
|
| 3896 |
// Bail early if there is no effective scroll. |
| 3897 |
if (dx === 0 && dy === 0) { |
| 3898 |
return; |
| 3899 |
} |
| 3900 |
|
| 3901 |
// Bail early if the viewport is not visible. |
| 3902 |
if (!this._viewport.isVisible) { |
| 3903 |
this._scrollX = x; |
| 3904 |
this._scrollY = y; |
| 3905 |
return; |
| 3906 |
} |
| 3907 |
|
| 3908 |
// Get the current size of the viewport. |
| 3909 |
let width = this._viewportWidth; |
| 3910 |
let height = this._viewportHeight; |
| 3911 |
|
| 3912 |
// Bail early if the viewport is empty. |
| 3913 |
if (width === 0 || height === 0) { |
| 3914 |
this._scrollX = x; |
| 3915 |
this._scrollY = y; |
| 3916 |
return; |
| 3917 |
} |
| 3918 |
|
| 3919 |
// Get the visible content origin. |
| 3920 |
let contentX = this.headerWidth; |
| 3921 |
let contentY = this.headerHeight; |
| 3922 |
|
| 3923 |
// Get the visible content dimensions. |
| 3924 |
let contentWidth = width - contentX; |
| 3925 |
let contentHeight = height - contentY; |
| 3926 |
|
| 3927 |
// Bail early if there is no content to draw. |
| 3928 |
if (contentWidth <= 0 && contentHeight <= 0) { |
| 3929 |
this._scrollX = x; |
| 3930 |
this._scrollY = y; |
| 3931 |
return; |
| 3932 |
} |
| 3933 |
|
| 3934 |
// Compute the area which needs painting for the `dx` scroll. |
| 3935 |
let dxArea = 0; |
| 3936 |
if (dx !== 0 && contentWidth > 0) { |
| 3937 |
if (Math.abs(dx) >= contentWidth) { |
| 3938 |
dxArea = contentWidth * height; |
| 3939 |
} else { |
| 3940 |
dxArea = Math.abs(dx) * height; |
| 3941 |
} |
| 3942 |
} |
| 3943 |
|
| 3944 |
// Compute the area which needs painting for the `dy` scroll. |
| 3945 |
let dyArea = 0; |
| 3946 |
if (dy !== 0 && contentHeight > 0) { |
| 3947 |
if (Math.abs(dy) >= contentHeight) { |
| 3948 |
dyArea = width * contentHeight; |
| 3949 |
} else { |
| 3950 |
dyArea = width * Math.abs(dy); |
| 3951 |
} |
| 3952 |
} |
| 3953 |
|
| 3954 |
// If the area sum is larger than the total, paint everything. |
| 3955 |
if (dxArea + dyArea >= width * height) { |
| 3956 |
this._scrollX = x; |
| 3957 |
this._scrollY = y; |
| 3958 |
this.paintContent(0, 0, width, height); |
| 3959 |
this._paintOverlay(); |
| 3960 |
return; |
| 3961 |
} |
| 3962 |
|
| 3963 |
// Update the internal Y scroll position. |
| 3964 |
this._scrollY = y; |
| 3965 |
|
| 3966 |
// Scroll the Y axis if needed. If the scroll distance exceeds |
| 3967 |
// the visible height, paint everything. Otherwise, blit the |
| 3968 |
// valid content and paint the dirty region. |
| 3969 |
if (dy !== 0 && contentHeight > 0) { |
| 3970 |
if (Math.abs(dy) >= contentHeight) { |
| 3971 |
this.paintContent(0, contentY, width, contentHeight); |
| 3972 |
} else { |
| 3973 |
const x = 0; |
| 3974 |
const y = dy < 0 ? contentY : contentY + dy; |
| 3975 |
const w = width; |
| 3976 |
const h = contentHeight - Math.abs(dy); |
| 3977 |
this._blitContent(this._canvas, x, y, w, h, x, y - dy); |
| 3978 |
this.paintContent( |
| 3979 |
0, |
| 3980 |
dy < 0 ? contentY : height - dy, |
| 3981 |
width, |
| 3982 |
Math.abs(dy) |
| 3983 |
); |
| 3984 |
|
| 3985 |
// Repaint merged cells that are intersected by the scroll level |
| 3986 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 3987 |
for (const rgn of ['body', 'row-header'] as DataModel.CellRegion[]) { |
| 3988 |
const cellgroups = CellGroup.getCellGroupsAtRegion( |
| 3989 |
this.dataModel, |
| 3990 |
rgn |
| 3991 |
); |
| 3992 |
|
| 3993 |
let paintRgn = { |
| 3994 |
region: rgn, |
| 3995 |
xMin: 0, |
| 3996 |
xMax: 0, |
| 3997 |
yMin: 0, |
| 3998 |
yMax: 0 |
| 3999 |
}; |
| 4000 |
|
| 4001 |
let backgroundColor = undefined; |
| 4002 |
|
| 4003 |
switch (rgn) { |
| 4004 |
case 'body': |
| 4005 |
paintRgn.xMin = this.headerWidth; |
| 4006 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 4007 |
paintRgn.yMin = this.headerHeight; |
| 4008 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 4009 |
|
| 4010 |
backgroundColor = this._style.backgroundColor; |
| 4011 |
break; |
| 4012 |
case 'row-header': |
| 4013 |
paintRgn.xMin = 0; |
| 4014 |
paintRgn.xMax = this.headerWidth; |
| 4015 |
paintRgn.yMin = this.headerHeight; |
| 4016 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 4017 |
|
| 4018 |
backgroundColor = this._style.headerBackgroundColor; |
| 4019 |
break; |
| 4020 |
} |
| 4021 |
|
| 4022 |
this._paintMergedCells(cellgroups, paintRgn, backgroundColor); |
| 4023 |
} |
| 4024 |
} |
| 4025 |
} |
| 4026 |
|
| 4027 |
// Update the internal X scroll position. |
| 4028 |
this._scrollX = x; |
| 4029 |
|
| 4030 |
// Scroll the X axis if needed. If the scroll distance exceeds |
| 4031 |
// the visible width, paint everything. Otherwise, blit the |
| 4032 |
// valid content and paint the dirty region. |
| 4033 |
if (dx !== 0 && contentWidth > 0) { |
| 4034 |
if (Math.abs(dx) >= contentWidth) { |
| 4035 |
this.paintContent(contentX, 0, contentWidth, height); |
| 4036 |
} else { |
| 4037 |
const x = dx < 0 ? contentX : contentX + dx; |
| 4038 |
const y = 0; |
| 4039 |
const w = contentWidth - Math.abs(dx); |
| 4040 |
const h = height; |
| 4041 |
this._blitContent(this._canvas, x, y, w, h, x - dx, y); |
| 4042 |
this.paintContent( |
| 4043 |
dx < 0 ? contentX : width - dx, |
| 4044 |
0, |
| 4045 |
Math.abs(dx), |
| 4046 |
height |
| 4047 |
); |
| 4048 |
|
| 4049 |
// Repaint merged cells that are intersected by the scroll level |
| 4050 |
// Otherwise it will be cut in two by the valid content, and drawn incorrectly |
| 4051 |
for (const rgn of ['body', 'column-header'] as DataModel.CellRegion[]) { |
| 4052 |
const cellGroups = CellGroup.getCellGroupsAtRegion( |
| 4053 |
this.dataModel, |
| 4054 |
rgn |
| 4055 |
); |
| 4056 |
|
| 4057 |
let paintRgn = { |
| 4058 |
region: rgn, |
| 4059 |
xMin: 0, |
| 4060 |
xMax: 0, |
| 4061 |
yMin: 0, |
| 4062 |
yMax: 0 |
| 4063 |
}; |
| 4064 |
|
| 4065 |
let backgroundColor = undefined; |
| 4066 |
|
| 4067 |
switch (rgn) { |
| 4068 |
case 'body': |
| 4069 |
paintRgn.xMin = this.headerWidth; |
| 4070 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 4071 |
paintRgn.yMin = this.headerHeight; |
| 4072 |
paintRgn.yMax = this.headerHeight + this.bodyHeight; |
| 4073 |
|
| 4074 |
backgroundColor = this._style.backgroundColor; |
| 4075 |
break; |
| 4076 |
case 'column-header': |
| 4077 |
paintRgn.xMin = this.headerWidth; |
| 4078 |
paintRgn.xMax = this.headerWidth + this.bodyWidth; |
| 4079 |
paintRgn.yMin = 0; |
| 4080 |
paintRgn.yMax = this.headerHeight; |
| 4081 |
|
| 4082 |
backgroundColor = this._style.headerBackgroundColor; |
| 4083 |
break; |
| 4084 |
} |
| 4085 |
|
| 4086 |
this._paintMergedCells(cellGroups, paintRgn, backgroundColor); |
| 4087 |
} |
| 4088 |
} |
| 4089 |
} |
| 4090 |
|
| 4091 |
// Paint the overlay. |
| 4092 |
this._paintOverlay(); |
| 4093 |
} |
| 4094 |
/** |
| 4095 |
* Blit content into the on-screen grid canvas. |
| 4096 |
* |
| 4097 |
* The rect should be expressed in viewport coordinates. |
| 4098 |
* |
| 4099 |
* This automatically accounts for the dpi ratio. |
| 4100 |
*/ |
| 4101 |
private _blitContent( |
| 4102 |
source: HTMLCanvasElement, |
| 4103 |
x: number, |
| 4104 |
y: number, |
| 4105 |
w: number, |
| 4106 |
h: number, |
| 4107 |
dx: number, |
| 4108 |
dy: number |
| 4109 |
): void { |
| 4110 |
// Scale the blit coordinates by the dpi ratio. |
| 4111 |
x *= this._dpiRatio; |
| 4112 |
y *= this._dpiRatio; |
| 4113 |
w *= this._dpiRatio; |
| 4114 |
h *= this._dpiRatio; |
| 4115 |
dx *= this._dpiRatio; |
| 4116 |
dy *= this._dpiRatio; |
| 4117 |
|
| 4118 |
// Save the current gc state. |
| 4119 |
this._canvasGC.save(); |
| 4120 |
|
| 4121 |
// Set the transform to the identity matrix. |
| 4122 |
this._canvasGC.setTransform(1, 0, 0, 1, 0, 0); |
| 4123 |
|
| 4124 |
// Draw the specified content. |
| 4125 |
this._canvasGC.drawImage(source, x, y, w, h, dx, dy, w, h); |
| 4126 |
|
| 4127 |
// Restore the gc state. |
| 4128 |
this._canvasGC.restore(); |
| 4129 |
} |
| 4130 |
|
| 4131 |
/** |
| 4132 |
* Paint the grid content for the given dirty rect. |
| 4133 |
* |
| 4134 |
* The rect should be expressed in valid viewport coordinates. |
| 4135 |
* |
| 4136 |
* This is the primary paint entry point. The individual `_draw*` |
| 4137 |
* methods should not be invoked directly. This method dispatches |
| 4138 |
* to the drawing methods in the correct order. |
| 4139 |
*/ |
| 4140 |
protected paintContent(rx: number, ry: number, rw: number, rh: number): void { |
| 4141 |
// Scale the canvas and buffer GC for the dpi ratio. |
| 4142 |
this._canvasGC.setTransform(this._dpiRatio, 0, 0, this._dpiRatio, 0, 0); |
| 4143 |
this._bufferGC.setTransform(this._dpiRatio, 0, 0, this._dpiRatio, 0, 0); |
| 4144 |
|
| 4145 |
// Clear the dirty rect of all content. |
| 4146 |
this._canvasGC.clearRect(rx, ry, rw, rh); |
| 4147 |
|
| 4148 |
// Draw the void region. |
| 4149 |
this._drawVoidRegion(rx, ry, rw, rh); |
| 4150 |
|
| 4151 |
// Draw the body region. |
| 4152 |
this._drawBodyRegion(rx, ry, rw, rh); |
| 4153 |
|
| 4154 |
// Draw the row header region. |
| 4155 |
this._drawRowHeaderRegion(rx, ry, rw, rh); |
| 4156 |
|
| 4157 |
// Draw the column header region. |
| 4158 |
this._drawColumnHeaderRegion(rx, ry, rw, rh); |
| 4159 |
|
| 4160 |
// Draw the corner header region. |
| 4161 |
this.drawCornerHeaderRegion(rx, ry, rw, rh); |
| 4162 |
} |
| 4163 |
|
| 4164 |
/** |
| 4165 |
* Resizes body column headers so their text fits |
| 4166 |
* without clipping or wrapping. |
| 4167 |
* @param dataModel |
| 4168 |
*/ |
| 4169 |
private _fitBodyColumnHeaders( |
| 4170 |
dataModel: DataModel, |
| 4171 |
padding: number, |
| 4172 |
numCols?: number |
| 4173 |
): void { |
| 4174 |
// Get the body column count |
| 4175 |
const bodyColumnCount = |
| 4176 |
numCols === undefined ? dataModel.columnCount('body') : numCols; |
| 4177 |
|
| 4178 |
for (let i = 0; i < bodyColumnCount; i++) { |
| 4179 |
/* |
| 4180 |
if we're working with nested column headers, |
| 4181 |
retrieve the nested levels and iterate on them. |
| 4182 |
*/ |
| 4183 |
const numRows = dataModel.rowCount('column-header'); |
| 4184 |
|
| 4185 |
/* |
| 4186 |
Calculate the maximum text width, across |
| 4187 |
all nested rows under a given column number. |
| 4188 |
*/ |
| 4189 |
let maxWidth = 0; |
| 4190 |
for (let j = 0; j < numRows; j++) { |
| 4191 |
const config = DataGrid._getConfig(dataModel, j, i, 'column-header'); |
| 4192 |
const textWidth = this._getCellTextWidth(config); |
| 4193 |
// Update the maximum width for that column. |
| 4194 |
maxWidth = Math.max(maxWidth, textWidth); |
| 4195 |
} |
| 4196 |
|
| 4197 |
/* |
| 4198 |
Send a resize message with new width for the given column. |
| 4199 |
Using a padding of 15 pixels to leave some room. |
| 4200 |
*/ |
| 4201 |
this.resizeColumn('body', i, maxWidth + padding); |
| 4202 |
} |
| 4203 |
} |
| 4204 |
|
| 4205 |
/** |
| 4206 |
* Resizes row header columns so their text fits |
| 4207 |
* without clipping or wrapping. |
| 4208 |
* @param dataModel |
| 4209 |
*/ |
| 4210 |
private _fitRowColumnHeaders( |
| 4211 |
dataModel: DataModel, |
| 4212 |
padding: number, |
| 4213 |
numCols?: number |
| 4214 |
): void { |
| 4215 |
/* |
| 4216 |
if we're working with nested row headers, |
| 4217 |
retrieve the nested levels and iterate on them. |
| 4218 |
*/ |
| 4219 |
const rowColumnCount = |
| 4220 |
numCols === undefined ? dataModel.columnCount('row-header') : numCols; |
| 4221 |
|
| 4222 |
for (let i = 0; i < rowColumnCount; i++) { |
| 4223 |
const numCols = dataModel.rowCount('column-header'); |
| 4224 |
/* |
| 4225 |
Calculate the maximum text width, across |
| 4226 |
all nested columns under a given row index. |
| 4227 |
*/ |
| 4228 |
let maxWidth = 0; |
| 4229 |
for (let j = 0; j < numCols; j++) { |
| 4230 |
const config = DataGrid._getConfig(dataModel, j, i, 'corner-header'); |
| 4231 |
const textWidth = this._getCellTextWidth(config); |
| 4232 |
maxWidth = Math.max(maxWidth, textWidth); |
| 4233 |
} |
| 4234 |
|
| 4235 |
/* |
| 4236 |
Send a resize message with new width for the given column. |
| 4237 |
Using a padding of 15 pixels to leave some room. |
| 4238 |
*/ |
| 4239 |
this.resizeColumn('row-header', i, maxWidth + padding); |
| 4240 |
} |
| 4241 |
} |
| 4242 |
|
| 4243 |
/** |
| 4244 |
* Paint the overlay content for the entire grid. |
| 4245 |
* |
| 4246 |
* This is the primary overlay paint entry point. The individual |
| 4247 |
* `_draw*` methods should not be invoked directly. This method |
| 4248 |
* dispatches to the drawing methods in the correct order. |
| 4249 |
*/ |
| 4250 |
private _paintOverlay(): void { |
| 4251 |
// Scale the overlay GC for the dpi ratio. |
| 4252 |
this._overlayGC.setTransform(this._dpiRatio, 0, 0, this._dpiRatio, 0, 0); |
| 4253 |
|
| 4254 |
// Clear the overlay of all content. |
| 4255 |
this._overlayGC.clearRect(0, 0, this._overlay.width, this._overlay.height); |
| 4256 |
|
| 4257 |
// Draw the body selections. |
| 4258 |
this._drawBodySelections(); |
| 4259 |
|
| 4260 |
// Draw the row header selections. |
| 4261 |
this._drawRowHeaderSelections(); |
| 4262 |
|
| 4263 |
// Draw the column header selections. |
| 4264 |
this._drawColumnHeaderSelections(); |
| 4265 |
|
| 4266 |
// Draw the cursor. |
| 4267 |
this._drawCursor(); |
| 4268 |
|
| 4269 |
// Draw the shadows. |
| 4270 |
this._drawShadows(); |
| 4271 |
} |
| 4272 |
|
| 4273 |
/** |
| 4274 |
* Draw the void region for the dirty rect. |
| 4275 |
*/ |
| 4276 |
private _drawVoidRegion( |
| 4277 |
rx: number, |
| 4278 |
ry: number, |
| 4279 |
rw: number, |
| 4280 |
rh: number |
| 4281 |
): void { |
| 4282 |
// Look up the void color. |
| 4283 |
let color = this._style.voidColor; |
| 4284 |
|
| 4285 |
// Bail if there is no void color. |
| 4286 |
if (!color) { |
| 4287 |
return; |
| 4288 |
} |
| 4289 |
|
| 4290 |
// Fill the dirty rect with the void color. |
| 4291 |
this._canvasGC.fillStyle = color; |
| 4292 |
this._canvasGC.fillRect(rx, ry, rw, rh); |
| 4293 |
} |
| 4294 |
|
| 4295 |
/** |
| 4296 |
* Draw the body region which intersects the dirty rect. |
| 4297 |
*/ |
| 4298 |
private _drawBodyRegion( |
| 4299 |
rx: number, |
| 4300 |
ry: number, |
| 4301 |
rw: number, |
| 4302 |
rh: number |
| 4303 |
): void { |
| 4304 |
// Get the visible content dimensions. |
| 4305 |
let contentW = this._columnSections.length - this._scrollX; |
| 4306 |
let contentH = this._rowSections.length - this._scrollY; |
| 4307 |
|
| 4308 |
// Bail if there is no content to draw. |
| 4309 |
if (contentW <= 0 || contentH <= 0) { |
| 4310 |
return; |
| 4311 |
} |
| 4312 |
|
| 4313 |
// Get the visible content origin. |
| 4314 |
let contentX = this.headerWidth; |
| 4315 |
let contentY = this.headerHeight; |
| 4316 |
|
| 4317 |
// Bail if the dirty rect does not intersect the content area. |
| 4318 |
if (rx + rw <= contentX) { |
| 4319 |
return; |
| 4320 |
} |
| 4321 |
if (ry + rh <= contentY) { |
| 4322 |
return; |
| 4323 |
} |
| 4324 |
if (rx >= contentX + contentW) { |
| 4325 |
return; |
| 4326 |
} |
| 4327 |
if (ry >= contentY + contentH) { |
| 4328 |
return; |
| 4329 |
} |
| 4330 |
|
| 4331 |
// Fetch the geometry. |
| 4332 |
let bh = this.bodyHeight; |
| 4333 |
let bw = this.bodyWidth; |
| 4334 |
let ph = this.pageHeight; |
| 4335 |
let pw = this.pageWidth; |
| 4336 |
|
| 4337 |
// Get the upper and lower bounds of the dirty content area. |
| 4338 |
let x1 = Math.max(rx, contentX); |
| 4339 |
let y1 = Math.max(ry, contentY); |
| 4340 |
let x2 = Math.min(rx + rw - 1, contentX + contentW - 1); |
| 4341 |
let y2 = Math.min(ry + rh - 1, contentY + contentH - 1); |
| 4342 |
|
| 4343 |
// Convert the dirty content bounds into cell bounds. |
| 4344 |
let r1 = this._rowSections.indexOf(y1 - contentY + this._scrollY); |
| 4345 |
let c1 = this._columnSections.indexOf(x1 - contentX + this._scrollX); |
| 4346 |
let r2 = this._rowSections.indexOf(y2 - contentY + this._scrollY); |
| 4347 |
let c2 = this._columnSections.indexOf(x2 - contentX + this._scrollX); |
| 4348 |
|
| 4349 |
// Fetch the max row and column. |
| 4350 |
let maxRow = this._rowSections.count - 1; |
| 4351 |
let maxColumn = this._columnSections.count - 1; |
| 4352 |
|
| 4353 |
// Handle a dirty content area larger than the cell count. |
| 4354 |
if (r2 < 0) { |
| 4355 |
r2 = maxRow; |
| 4356 |
} |
| 4357 |
if (c2 < 0) { |
| 4358 |
c2 = maxColumn; |
| 4359 |
} |
| 4360 |
|
| 4361 |
// Convert the cell bounds back to visible coordinates. |
| 4362 |
let x = this._columnSections.offsetOf(c1) + contentX - this._scrollX; |
| 4363 |
let y = this._rowSections.offsetOf(r1) + contentY - this._scrollY; |
| 4364 |
|
| 4365 |
// Set up the paint region size variables. |
| 4366 |
let width = 0; |
| 4367 |
let height = 0; |
| 4368 |
|
| 4369 |
// Allocate the section sizes arrays. |
| 4370 |
let rowSizes = new Array<number>(r2 - r1 + 1); |
| 4371 |
let columnSizes = new Array<number>(c2 - c1 + 1); |
| 4372 |
|
| 4373 |
// Get the row sizes for the region. |
| 4374 |
for (let j = r1; j <= r2; ++j) { |
| 4375 |
let size = this._rowSections.sizeOf(j); |
| 4376 |
rowSizes[j - r1] = size; |
| 4377 |
height += size; |
| 4378 |
} |
| 4379 |
|
| 4380 |
// Get the column sizes for the region. |
| 4381 |
for (let i = c1; i <= c2; ++i) { |
| 4382 |
let size = this._columnSections.sizeOf(i); |
| 4383 |
columnSizes[i - c1] = size; |
| 4384 |
width += size; |
| 4385 |
} |
| 4386 |
|
| 4387 |
// Adjust the geometry if the last row is streched. |
| 4388 |
if (this._stretchLastRow && ph > bh && r2 === maxRow) { |
| 4389 |
let dh = this.pageHeight - this.bodyHeight; |
| 4390 |
rowSizes[rowSizes.length - 1] += dh; |
| 4391 |
height += dh; |
| 4392 |
y2 += dh; |
| 4393 |
} |
| 4394 |
|
| 4395 |
// Adjust the geometry if the last column is streched. |
| 4396 |
if (this._stretchLastColumn && pw > bw && c2 === maxColumn) { |
| 4397 |
let dw = this.pageWidth - this.bodyWidth; |
| 4398 |
columnSizes[columnSizes.length - 1] += dw; |
| 4399 |
width += dw; |
| 4400 |
x2 += dw; |
| 4401 |
} |
| 4402 |
|
| 4403 |
// Create the paint region object. |
| 4404 |
let rgn: Private.PaintRegion = { |
| 4405 |
region: 'body', |
| 4406 |
xMin: x1, |
| 4407 |
yMin: y1, |
| 4408 |
xMax: x2, |
| 4409 |
yMax: y2, |
| 4410 |
x, |
| 4411 |
y, |
| 4412 |
width, |
| 4413 |
height, |
| 4414 |
row: r1, |
| 4415 |
column: c1, |
| 4416 |
rowSizes, |
| 4417 |
columnSizes |
| 4418 |
}; |
| 4419 |
|
| 4420 |
// Draw the background. |
| 4421 |
this._drawBackground(rgn, this._style.backgroundColor); |
| 4422 |
|
| 4423 |
// Draw the row background. |
| 4424 |
this._drawRowBackground(rgn, this._style.rowBackgroundColor); |
| 4425 |
|
| 4426 |
// Draw the column background. |
| 4427 |
this._drawColumnBackground(rgn, this._style.columnBackgroundColor); |
| 4428 |
|
| 4429 |
// Draw the cell content for the paint region. |
| 4430 |
this._drawCells(rgn); |
| 4431 |
|
| 4432 |
// Draw the horizontal grid lines. |
| 4433 |
this._drawHorizontalGridLines( |
| 4434 |
rgn, |
| 4435 |
this._style.horizontalGridLineColor || this._style.gridLineColor |
| 4436 |
); |
| 4437 |
|
| 4438 |
// Draw the vertical grid lines. |
| 4439 |
this._drawVerticalGridLines( |
| 4440 |
rgn, |
| 4441 |
this._style.verticalGridLineColor || this._style.gridLineColor |
| 4442 |
); |
| 4443 |
|
| 4444 |
// Get the cellgroups from the cell-region that intersects with the paint region |
| 4445 |
const cellGroups = CellGroup.getCellGroupsAtRegion( |
| 4446 |
this.dataModel!, |
| 4447 |
rgn.region |
| 4448 |
).filter(group => { |
| 4449 |
return this.cellGroupInteresectsRegion(group, rgn); |
| 4450 |
}); |
| 4451 |
|
| 4452 |
// Draw merged cells |
| 4453 |
this._paintMergedCells(cellGroups, rgn, this._style.backgroundColor); |
| 4454 |
} |
| 4455 |
|
| 4456 |
/** |
| 4457 |
* Draw the row header region which intersects the dirty rect. |
| 4458 |
*/ |
| 4459 |
private _drawRowHeaderRegion( |
| 4460 |
rx: number, |
| 4461 |
ry: number, |
| 4462 |
rw: number, |
| 4463 |
rh: number |
| 4464 |
): void { |
| 4465 |
// Get the visible content dimensions. |
| 4466 |
let contentW = this.headerWidth; |
| 4467 |
let contentH = this.bodyHeight - this._scrollY; |
| 4468 |
|
| 4469 |
// Bail if there is no content to draw. |
| 4470 |
if (contentW <= 0 || contentH <= 0) { |
| 4471 |
return; |
| 4472 |
} |
| 4473 |
|
| 4474 |
// Get the visible content origin. |
| 4475 |
let contentX = 0; |
| 4476 |
let contentY = this.headerHeight; |
| 4477 |
|
| 4478 |
// Bail if the dirty rect does not intersect the content area. |
| 4479 |
if (rx + rw <= contentX) { |
| 4480 |
return; |
| 4481 |
} |
| 4482 |
if (ry + rh <= contentY) { |
| 4483 |
return; |
| 4484 |
} |
| 4485 |
if (rx >= contentX + contentW) { |
| 4486 |
return; |
| 4487 |
} |
| 4488 |
if (ry >= contentY + contentH) { |
| 4489 |
return; |
| 4490 |
} |
| 4491 |
|
| 4492 |
// Fetch the geometry. |
| 4493 |
let bh = this.bodyHeight; |
| 4494 |
let ph = this.pageHeight; |
| 4495 |
|
| 4496 |
// Get the upper and lower bounds of the dirty content area. |
| 4497 |
let x1 = rx; |
| 4498 |
let y1 = Math.max(ry, contentY); |
| 4499 |
let x2 = Math.min(rx + rw - 1, contentX + contentW - 1); |
| 4500 |
let y2 = Math.min(ry + rh - 1, contentY + contentH - 1); |
| 4501 |
|
| 4502 |
// Convert the dirty content bounds into cell bounds. |
| 4503 |
let r1 = this._rowSections.indexOf(y1 - contentY + this._scrollY); |
| 4504 |
let c1 = this._rowHeaderSections.indexOf(x1); |
| 4505 |
let r2 = this._rowSections.indexOf(y2 - contentY + this._scrollY); |
| 4506 |
let c2 = this._rowHeaderSections.indexOf(x2); |
| 4507 |
|
| 4508 |
// Fetch max row and column. |
| 4509 |
let maxRow = this._rowSections.count - 1; |
| 4510 |
let maxColumn = this._rowHeaderSections.count - 1; |
| 4511 |
|
| 4512 |
// Handle a dirty content area larger than the cell count. |
| 4513 |
if (r2 < 0) { |
| 4514 |
r2 = maxRow; |
| 4515 |
} |
| 4516 |
if (c2 < 0) { |
| 4517 |
c2 = maxColumn; |
| 4518 |
} |
| 4519 |
|
| 4520 |
// Convert the cell bounds back to visible coordinates. |
| 4521 |
let x = this._rowHeaderSections.offsetOf(c1); |
| 4522 |
let y = this._rowSections.offsetOf(r1) + contentY - this._scrollY; |
| 4523 |
|
| 4524 |
// Set up the paint region size variables. |
| 4525 |
let width = 0; |
| 4526 |
let height = 0; |
| 4527 |
|
| 4528 |
// Allocate the section sizes arrays. |
| 4529 |
let rowSizes = new Array<number>(r2 - r1 + 1); |
| 4530 |
let columnSizes = new Array<number>(c2 - c1 + 1); |
| 4531 |
|
| 4532 |
// Get the row sizes for the region. |
| 4533 |
for (let j = r1; j <= r2; ++j) { |
| 4534 |
let size = this._rowSections.sizeOf(j); |
| 4535 |
rowSizes[j - r1] = size; |
| 4536 |
height += size; |
| 4537 |
} |
| 4538 |
|
| 4539 |
// Get the column sizes for the region. |
| 4540 |
for (let i = c1; i <= c2; ++i) { |
| 4541 |
let size = this._rowHeaderSections.sizeOf(i); |
| 4542 |
columnSizes[i - c1] = size; |
| 4543 |
width += size; |
| 4544 |
} |
| 4545 |
|
| 4546 |
// Adjust the geometry if the last row is stretched. |
| 4547 |
if (this._stretchLastRow && ph > bh && r2 === maxRow) { |
| 4548 |
let dh = this.pageHeight - this.bodyHeight; |
| 4549 |
rowSizes[rowSizes.length - 1] += dh; |
| 4550 |
height += dh; |
| 4551 |
y2 += dh; |
| 4552 |
} |
| 4553 |
|
| 4554 |
// Create the paint region object. |
| 4555 |
let rgn: Private.PaintRegion = { |
| 4556 |
region: 'row-header', |
| 4557 |
xMin: x1, |
| 4558 |
yMin: y1, |
| 4559 |
xMax: x2, |
| 4560 |
yMax: y2, |
| 4561 |
x, |
| 4562 |
y, |
| 4563 |
width, |
| 4564 |
height, |
| 4565 |
row: r1, |
| 4566 |
column: c1, |
| 4567 |
rowSizes, |
| 4568 |
columnSizes |
| 4569 |
}; |
| 4570 |
|
| 4571 |
// Draw the background. |
| 4572 |
this._drawBackground(rgn, this._style.headerBackgroundColor); |
| 4573 |
|
| 4574 |
// Draw the cell content for the paint region. |
| 4575 |
this._drawCells(rgn); |
| 4576 |
|
| 4577 |
// Draw the horizontal grid lines. |
| 4578 |
this._drawHorizontalGridLines( |
| 4579 |
rgn, |
| 4580 |
this._style.headerHorizontalGridLineColor || |
| 4581 |
this._style.headerGridLineColor |
| 4582 |
); |
| 4583 |
|
| 4584 |
// Draw the vertical grid lines. |
| 4585 |
this._drawVerticalGridLines( |
| 4586 |
rgn, |
| 4587 |
this._style.headerVerticalGridLineColor || this._style.headerGridLineColor |
| 4588 |
); |
| 4589 |
|
| 4590 |
// Get the cellgroups from the cell-region that intersects with the paint region |
| 4591 |
const cellGroups = CellGroup.getCellGroupsAtRegion( |
| 4592 |
this.dataModel!, |
| 4593 |
rgn.region |
| 4594 |
).filter(group => { |
| 4595 |
return this.cellGroupInteresectsRegion(group, rgn); |
| 4596 |
}); |
| 4597 |
|
| 4598 |
// Draw merged cells |
| 4599 |
this._paintMergedCells(cellGroups, rgn, this._style.headerBackgroundColor); |
| 4600 |
} |
| 4601 |
|
| 4602 |
/** |
| 4603 |
* Draw the column header region which intersects the dirty rect. |
| 4604 |
*/ |
| 4605 |
private _drawColumnHeaderRegion( |
| 4606 |
rx: number, |
| 4607 |
ry: number, |
| 4608 |
rw: number, |
| 4609 |
rh: number |
| 4610 |
): void { |
| 4611 |
// Get the visible content dimensions. |
| 4612 |
let contentW = this.bodyWidth - this._scrollX; |
| 4613 |
let contentH = this.headerHeight; |
| 4614 |
|
| 4615 |
// Bail if there is no content to draw. |
| 4616 |
if (contentW <= 0 || contentH <= 0) { |
| 4617 |
return; |
| 4618 |
} |
| 4619 |
|
| 4620 |
// Get the visible content origin. |
| 4621 |
let contentX = this.headerWidth; |
| 4622 |
let contentY = 0; |
| 4623 |
|
| 4624 |
// Bail if the dirty rect does not intersect the content area. |
| 4625 |
if (rx + rw <= contentX) { |
| 4626 |
return; |
| 4627 |
} |
| 4628 |
if (ry + rh <= contentY) { |
| 4629 |
return; |
| 4630 |
} |
| 4631 |
if (rx >= contentX + contentW) { |
| 4632 |
return; |
| 4633 |
} |
| 4634 |
if (ry >= contentY + contentH) { |
| 4635 |
return; |
| 4636 |
} |
| 4637 |
|
| 4638 |
// Fetch the geometry. |
| 4639 |
let bw = this.bodyWidth; |
| 4640 |
let pw = this.pageWidth; |
| 4641 |
|
| 4642 |
// Get the upper and lower bounds of the dirty content area. |
| 4643 |
let x1 = Math.max(rx, contentX); |
| 4644 |
let y1 = ry; |
| 4645 |
let x2 = Math.min(rx + rw - 1, contentX + contentW - 1); |
| 4646 |
let y2 = Math.min(ry + rh - 1, contentY + contentH - 1); |
| 4647 |
|
| 4648 |
// Convert the dirty content bounds into cell bounds. |
| 4649 |
let r1 = this._columnHeaderSections.indexOf(y1); |
| 4650 |
let c1 = this._columnSections.indexOf(x1 - contentX + this._scrollX); |
| 4651 |
let r2 = this._columnHeaderSections.indexOf(y2); |
| 4652 |
let c2 = this._columnSections.indexOf(x2 - contentX + this._scrollX); |
| 4653 |
|
| 4654 |
// Fetch the max row and column. |
| 4655 |
let maxRow = this._columnHeaderSections.count - 1; |
| 4656 |
let maxColumn = this._columnSections.count - 1; |
| 4657 |
|
| 4658 |
// Handle a dirty content area larger than the cell count. |
| 4659 |
if (r2 < 0) { |
| 4660 |
r2 = maxRow; |
| 4661 |
} |
| 4662 |
if (c2 < 0) { |
| 4663 |
c2 = maxColumn; |
| 4664 |
} |
| 4665 |
|
| 4666 |
// Convert the cell bounds back to visible coordinates. |
| 4667 |
let x = this._columnSections.offsetOf(c1) + contentX - this._scrollX; |
| 4668 |
let y = this._columnHeaderSections.offsetOf(r1); |
| 4669 |
|
| 4670 |
// Set up the paint region size variables. |
| 4671 |
let width = 0; |
| 4672 |
let height = 0; |
| 4673 |
|
| 4674 |
// Allocate the section sizes arrays. |
| 4675 |
let rowSizes = new Array<number>(r2 - r1 + 1); |
| 4676 |
let columnSizes = new Array<number>(c2 - c1 + 1); |
| 4677 |
|
| 4678 |
// Get the row sizes for the region. |
| 4679 |
for (let j = r1; j <= r2; ++j) { |
| 4680 |
let size = this._columnHeaderSections.sizeOf(j); |
| 4681 |
rowSizes[j - r1] = size; |
| 4682 |
height += size; |
| 4683 |
} |
| 4684 |
|
| 4685 |
// Get the column sizes for the region. |
| 4686 |
for (let i = c1; i <= c2; ++i) { |
| 4687 |
let size = this._columnSections.sizeOf(i); |
| 4688 |
columnSizes[i - c1] = size; |
| 4689 |
width += size; |
| 4690 |
} |
| 4691 |
|
| 4692 |
// Adjust the geometry if the last column is stretched. |
| 4693 |
if (this._stretchLastColumn && pw > bw && c2 === maxColumn) { |
| 4694 |
let dw = this.pageWidth - this.bodyWidth; |
| 4695 |
columnSizes[columnSizes.length - 1] += dw; |
| 4696 |
width += dw; |
| 4697 |
x2 += dw; |
| 4698 |
} |
| 4699 |
|
| 4700 |
// Create the paint region object. |
| 4701 |
let rgn: Private.PaintRegion = { |
| 4702 |
region: 'column-header', |
| 4703 |
xMin: x1, |
| 4704 |
yMin: y1, |
| 4705 |
xMax: x2, |
| 4706 |
yMax: y2, |
| 4707 |
x, |
| 4708 |
y, |
| 4709 |
width, |
| 4710 |
height, |
| 4711 |
row: r1, |
| 4712 |
column: c1, |
| 4713 |
rowSizes, |
| 4714 |
columnSizes |
| 4715 |
}; |
| 4716 |
|
| 4717 |
// Draw the background. |
| 4718 |
this._drawBackground(rgn, this._style.headerBackgroundColor); |
| 4719 |
|
| 4720 |
// Draw the cell content for the paint region. |
| 4721 |
this._drawCells(rgn); |
| 4722 |
|
| 4723 |
// Draw the horizontal grid lines. |
| 4724 |
this._drawHorizontalGridLines( |
| 4725 |
rgn, |
| 4726 |
this._style.headerHorizontalGridLineColor || |
| 4727 |
this._style.headerGridLineColor |
| 4728 |
); |
| 4729 |
|
| 4730 |
// Draw the vertical grid lines. |
| 4731 |
this._drawVerticalGridLines( |
| 4732 |
rgn, |
| 4733 |
this._style.headerVerticalGridLineColor || this._style.headerGridLineColor |
| 4734 |
); |
| 4735 |
|
| 4736 |
// Get the cellgroups from the cell-region that intersects with the paint region |
| 4737 |
const cellGroups = CellGroup.getCellGroupsAtRegion( |
| 4738 |
this.dataModel!, |
| 4739 |
rgn.region |
| 4740 |
).filter(group => { |
| 4741 |
return this.cellGroupInteresectsRegion(group, rgn); |
| 4742 |
}); |
| 4743 |
|
| 4744 |
// Draw merged cells |
| 4745 |
this._paintMergedCells(cellGroups, rgn, this._style.headerBackgroundColor); |
| 4746 |
} |
| 4747 |
|
| 4748 |
/** |
| 4749 |
* Draw the corner header region which intersects the dirty rect. |
| 4750 |
*/ |
| 4751 |
protected drawCornerHeaderRegion( |
| 4752 |
rx: number, |
| 4753 |
ry: number, |
| 4754 |
rw: number, |
| 4755 |
rh: number |
| 4756 |
): void { |
| 4757 |
// Get the visible content dimensions. |
| 4758 |
let contentW = this.headerWidth; |
| 4759 |
let contentH = this.headerHeight; |
| 4760 |
|
| 4761 |
// Bail if there is no content to draw. |
| 4762 |
if (contentW <= 0 || contentH <= 0) { |
| 4763 |
return; |
| 4764 |
} |
| 4765 |
|
| 4766 |
// Get the visible content origin. |
| 4767 |
let contentX = 0; |
| 4768 |
let contentY = 0; |
| 4769 |
|
| 4770 |
// Bail if the dirty rect does not intersect the content area. |
| 4771 |
if (rx + rw <= contentX) { |
| 4772 |
return; |
| 4773 |
} |
| 4774 |
if (ry + rh <= contentY) { |
| 4775 |
return; |
| 4776 |
} |
| 4777 |
if (rx >= contentX + contentW) { |
| 4778 |
return; |
| 4779 |
} |
| 4780 |
if (ry >= contentY + contentH) { |
| 4781 |
return; |
| 4782 |
} |
| 4783 |
|
| 4784 |
// Get the upper and lower bounds of the dirty content area. |
| 4785 |
let x1 = rx; |
| 4786 |
let y1 = ry; |
| 4787 |
let x2 = Math.min(rx + rw - 1, contentX + contentW - 1); |
| 4788 |
let y2 = Math.min(ry + rh - 1, contentY + contentH - 1); |
| 4789 |
|
| 4790 |
// Convert the dirty content bounds into cell bounds. |
| 4791 |
let r1 = this._columnHeaderSections.indexOf(y1); |
| 4792 |
let c1 = this._rowHeaderSections.indexOf(x1); |
| 4793 |
let r2 = this._columnHeaderSections.indexOf(y2); |
| 4794 |
let c2 = this._rowHeaderSections.indexOf(x2); |
| 4795 |
|
| 4796 |
// Handle a dirty content area larger than the cell count. |
| 4797 |
if (r2 < 0) { |
| 4798 |
r2 = this._columnHeaderSections.count - 1; |
| 4799 |
} |
| 4800 |
if (c2 < 0) { |
| 4801 |
c2 = this._rowHeaderSections.count - 1; |
| 4802 |
} |
| 4803 |
|
| 4804 |
// Convert the cell bounds back to visible coordinates. |
| 4805 |
let x = this._rowHeaderSections.offsetOf(c1); |
| 4806 |
let y = this._columnHeaderSections.offsetOf(r1); |
| 4807 |
|
| 4808 |
// Set up the paint region size variables. |
| 4809 |
let width = 0; |
| 4810 |
let height = 0; |
| 4811 |
|
| 4812 |
// Allocate the section sizes arrays. |
| 4813 |
let rowSizes = new Array<number>(r2 - r1 + 1); |
| 4814 |
let columnSizes = new Array<number>(c2 - c1 + 1); |
| 4815 |
|
| 4816 |
// Get the row sizes for the region. |
| 4817 |
for (let j = r1; j <= r2; ++j) { |
| 4818 |
let size = this._columnHeaderSections.sizeOf(j); |
| 4819 |
rowSizes[j - r1] = size; |
| 4820 |
height += size; |
| 4821 |
} |
| 4822 |
|
| 4823 |
// Get the column sizes for the region. |
| 4824 |
for (let i = c1; i <= c2; ++i) { |
| 4825 |
let size = this._rowHeaderSections.sizeOf(i); |
| 4826 |
columnSizes[i - c1] = size; |
| 4827 |
width += size; |
| 4828 |
} |
| 4829 |
|
| 4830 |
// Create the paint region object. |
| 4831 |
let rgn: Private.PaintRegion = { |
| 4832 |
region: 'corner-header', |
| 4833 |
xMin: x1, |
| 4834 |
yMin: y1, |
| 4835 |
xMax: x2, |
| 4836 |
yMax: y2, |
| 4837 |
x, |
| 4838 |
y, |
| 4839 |
width, |
| 4840 |
height, |
| 4841 |
row: r1, |
| 4842 |
column: c1, |
| 4843 |
rowSizes, |
| 4844 |
columnSizes |
| 4845 |
}; |
| 4846 |
|
| 4847 |
// Draw the background. |
| 4848 |
this._drawBackground(rgn, this._style.headerBackgroundColor); |
| 4849 |
|
| 4850 |
// Draw the cell content for the paint region. |
| 4851 |
this._drawCells(rgn); |
| 4852 |
|
| 4853 |
// Draw the horizontal grid lines. |
| 4854 |
this._drawHorizontalGridLines( |
| 4855 |
rgn, |
| 4856 |
this._style.headerHorizontalGridLineColor || |
| 4857 |
this._style.headerGridLineColor |
| 4858 |
); |
| 4859 |
|
| 4860 |
// Draw the vertical grid lines. |
| 4861 |
this._drawVerticalGridLines( |
| 4862 |
rgn, |
| 4863 |
this._style.headerVerticalGridLineColor || this._style.headerGridLineColor |
| 4864 |
); |
| 4865 |
|
| 4866 |
// Get the cellgroups from the cell-region that intersects with the paint region |
| 4867 |
const cellGroups = CellGroup.getCellGroupsAtRegion( |
| 4868 |
this.dataModel!, |
| 4869 |
rgn.region |
| 4870 |
).filter(group => { |
| 4871 |
return this.cellGroupInteresectsRegion(group, rgn); |
| 4872 |
}); |
| 4873 |
|
| 4874 |
// Draw merged cells |
| 4875 |
this._paintMergedCells(cellGroups, rgn, this._style.headerBackgroundColor); |
| 4876 |
} |
| 4877 |
|
| 4878 |
/** |
| 4879 |
* Draw the background for the given paint region. |
| 4880 |
*/ |
| 4881 |
private _drawBackground( |
| 4882 |
rgn: Private.PaintRegion, |
| 4883 |
color: string | undefined |
| 4884 |
): void { |
| 4885 |
// Bail if there is no color to draw. |
| 4886 |
if (!color) { |
| 4887 |
return; |
| 4888 |
} |
| 4889 |
|
| 4890 |
// Unpack the region. |
| 4891 |
let { xMin, yMin, xMax, yMax } = rgn; |
| 4892 |
|
| 4893 |
// Fill the region with the specified color. |
| 4894 |
this._canvasGC.fillStyle = color; |
| 4895 |
this._canvasGC.fillRect(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1); |
| 4896 |
} |
| 4897 |
|
| 4898 |
/** |
| 4899 |
* Draw the row background for the given paint region. |
| 4900 |
*/ |
| 4901 |
private _drawRowBackground( |
| 4902 |
rgn: Private.PaintRegion, |
| 4903 |
colorFn: ((i: number) => string) | undefined |
| 4904 |
): void { |
| 4905 |
// Bail if there is no color function. |
| 4906 |
if (!colorFn) { |
| 4907 |
return; |
| 4908 |
} |
| 4909 |
|
| 4910 |
// Compute the X bounds for the row. |
| 4911 |
let x1 = Math.max(rgn.xMin, rgn.x); |
| 4912 |
let x2 = Math.min(rgn.x + rgn.width - 1, rgn.xMax); |
| 4913 |
|
| 4914 |
// Draw the background for the rows in the region. |
| 4915 |
for (let y = rgn.y, j = 0, n = rgn.rowSizes.length; j < n; ++j) { |
| 4916 |
// Fetch the size of the row. |
| 4917 |
let size = rgn.rowSizes[j]; |
| 4918 |
|
| 4919 |
// Skip zero sized rows. |
| 4920 |
if (size === 0) { |
| 4921 |
continue; |
| 4922 |
} |
| 4923 |
|
| 4924 |
// Get the background color for the row. |
| 4925 |
let color = colorFn(rgn.row + j); |
| 4926 |
|
| 4927 |
// Fill the row with the background color if needed. |
| 4928 |
if (color) { |
| 4929 |
let y1 = Math.max(rgn.yMin, y); |
| 4930 |
let y2 = Math.min(y + size - 1, rgn.yMax); |
| 4931 |
this._canvasGC.fillStyle = color; |
| 4932 |
this._canvasGC.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 4933 |
} |
| 4934 |
|
| 4935 |
// Increment the running Y coordinate. |
| 4936 |
y += size; |
| 4937 |
} |
| 4938 |
} |
| 4939 |
|
| 4940 |
/** |
| 4941 |
* Draw the column background for the given paint region. |
| 4942 |
*/ |
| 4943 |
private _drawColumnBackground( |
| 4944 |
rgn: Private.PaintRegion, |
| 4945 |
colorFn: ((i: number) => string) | undefined |
| 4946 |
): void { |
| 4947 |
// Bail if there is no color function. |
| 4948 |
if (!colorFn) { |
| 4949 |
return; |
| 4950 |
} |
| 4951 |
|
| 4952 |
// Compute the Y bounds for the column. |
| 4953 |
let y1 = Math.max(rgn.yMin, rgn.y); |
| 4954 |
let y2 = Math.min(rgn.y + rgn.height - 1, rgn.yMax); |
| 4955 |
|
| 4956 |
// Draw the background for the columns in the region. |
| 4957 |
for (let x = rgn.x, i = 0, n = rgn.columnSizes.length; i < n; ++i) { |
| 4958 |
// Fetch the size of the column. |
| 4959 |
let size = rgn.columnSizes[i]; |
| 4960 |
|
| 4961 |
// Skip zero sized columns. |
| 4962 |
if (size === 0) { |
| 4963 |
continue; |
| 4964 |
} |
| 4965 |
|
| 4966 |
// Get the background color for the column. |
| 4967 |
let color = colorFn(rgn.column + i); |
| 4968 |
|
| 4969 |
// Fill the column with the background color if needed. |
| 4970 |
if (color) { |
| 4971 |
let x1 = Math.max(rgn.xMin, x); |
| 4972 |
let x2 = Math.min(x + size - 1, rgn.xMax); |
| 4973 |
this._canvasGC.fillStyle = color; |
| 4974 |
this._canvasGC.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 4975 |
} |
| 4976 |
|
| 4977 |
// Increment the running X coordinate. |
| 4978 |
x += size; |
| 4979 |
} |
| 4980 |
} |
| 4981 |
|
| 4982 |
/** |
| 4983 |
* Returns column size |
| 4984 |
* @param region |
| 4985 |
* @param index |
| 4986 |
*/ |
| 4987 |
private _getColumnSize(region: DataModel.CellRegion, index: number): number { |
| 4988 |
if (region === 'corner-header') { |
| 4989 |
return this._rowHeaderSections.sizeOf(index); |
| 4990 |
} |
| 4991 |
return this.columnSize(region as DataModel.ColumnRegion, index); |
| 4992 |
} |
| 4993 |
|
| 4994 |
/** |
| 4995 |
* Returns row size |
| 4996 |
* @param region |
| 4997 |
* @param index |
| 4998 |
*/ |
| 4999 |
private _getRowSize(region: DataModel.CellRegion, index: number): number { |
| 5000 |
if (region === 'corner-header') { |
| 5001 |
return this._columnHeaderSections.sizeOf(index); |
| 5002 |
} |
| 5003 |
return this.rowSize(region as DataModel.RowRegion, index); |
| 5004 |
} |
| 5005 |
|
| 5006 |
/** |
| 5007 |
* Draw the cells for the given paint region. |
| 5008 |
*/ |
| 5009 |
private _drawCells(rgn: Private.PaintRegion): void { |
| 5010 |
// Bail if there is no data model. |
| 5011 |
if (!this._dataModel) { |
| 5012 |
return; |
| 5013 |
} |
| 5014 |
|
| 5015 |
// Set up the cell config object for rendering. |
| 5016 |
let config = { |
| 5017 |
x: 0, |
| 5018 |
y: 0, |
| 5019 |
width: 0, |
| 5020 |
height: 0, |
| 5021 |
region: rgn.region, |
| 5022 |
row: 0, |
| 5023 |
column: 0, |
| 5024 |
value: null as any, |
| 5025 |
metadata: DataModel.emptyMetadata |
| 5026 |
}; |
| 5027 |
|
| 5028 |
let groupIndex = -1; |
| 5029 |
|
| 5030 |
// Save the buffer gc before wrapping. |
| 5031 |
this._bufferGC.save(); |
| 5032 |
|
| 5033 |
// Wrap the buffer gc for painting the cells. |
| 5034 |
let gc = new GraphicsContext(this._bufferGC); |
| 5035 |
|
| 5036 |
let height = 0; |
| 5037 |
|
| 5038 |
// Loop over the columns in the region. |
| 5039 |
for (let x = rgn.x, i = 0, n = rgn.columnSizes.length; i < n; ++i) { |
| 5040 |
// Fetch the size of the column. |
| 5041 |
let width = rgn.columnSizes[i]; |
| 5042 |
|
| 5043 |
// Skip zero sized columns. |
| 5044 |
if (width === 0) { |
| 5045 |
continue; |
| 5046 |
} |
| 5047 |
|
| 5048 |
// Compute the column index. |
| 5049 |
let column = rgn.column + i; |
| 5050 |
|
| 5051 |
// Update the config for the current column. |
| 5052 |
config.x = x; |
| 5053 |
config.width = width; |
| 5054 |
config.column = column; |
| 5055 |
|
| 5056 |
// Loop over the rows in the column. |
| 5057 |
for (let y = rgn.y, j = 0, n = rgn.rowSizes.length; j < n; ++j) { |
| 5058 |
// Fetch the size of the row. |
| 5059 |
height = rgn.rowSizes[j]; |
| 5060 |
|
| 5061 |
// Skip zero sized rows. |
| 5062 |
if (height === 0) { |
| 5063 |
continue; |
| 5064 |
} |
| 5065 |
|
| 5066 |
// Compute the row index. |
| 5067 |
let row = rgn.row + j; |
| 5068 |
|
| 5069 |
groupIndex = CellGroup.getGroupIndex( |
| 5070 |
this.dataModel!, |
| 5071 |
config.region, |
| 5072 |
row, |
| 5073 |
column |
| 5074 |
); |
| 5075 |
|
| 5076 |
// For merged cell regions, don't do anything, we draw merged regions later. |
| 5077 |
if (groupIndex !== -1) { |
| 5078 |
y += height; |
| 5079 |
continue; |
| 5080 |
} |
| 5081 |
|
| 5082 |
// Clear the buffer rect for the cell. |
| 5083 |
gc.clearRect(x, y, width, height); |
| 5084 |
|
| 5085 |
let value = DataGrid._getCellValue( |
| 5086 |
this.dataModel as DataModel, |
| 5087 |
rgn.region, |
| 5088 |
row, |
| 5089 |
column |
| 5090 |
); |
| 5091 |
let metadata = DataGrid._getCellMetadata( |
| 5092 |
this.dataModel as DataModel, |
| 5093 |
rgn.region, |
| 5094 |
row, |
| 5095 |
column |
| 5096 |
); |
| 5097 |
|
| 5098 |
// Update the config for the current cell. |
| 5099 |
config.y = y; |
| 5100 |
config.height = height; |
| 5101 |
config.width = width; |
| 5102 |
config.row = row; |
| 5103 |
config.value = value; |
| 5104 |
config.metadata = metadata; |
| 5105 |
|
| 5106 |
// Get the renderer for the cell. |
| 5107 |
let renderer = this._cellRenderers.get(config); |
| 5108 |
|
| 5109 |
// Save the GC state. |
| 5110 |
gc.save(); |
| 5111 |
|
| 5112 |
// Paint the cell into the off-screen buffer. |
| 5113 |
try { |
| 5114 |
if (renderer instanceof AsyncCellRenderer) { |
| 5115 |
if (renderer.isReady(config)) { |
| 5116 |
renderer.paint(gc, config); |
| 5117 |
} else { |
| 5118 |
renderer.paintPlaceholder(gc, config); |
| 5119 |
renderer.load(config).then(() => { |
| 5120 |
const r1 = row; |
| 5121 |
const r2 = row + 1; |
| 5122 |
|
| 5123 |
const c1 = column; |
| 5124 |
const c2 = column + 1; |
| 5125 |
|
| 5126 |
this.repaintRegion(rgn.region, r1, c1, r2, c2); |
| 5127 |
}); |
| 5128 |
} |
| 5129 |
} else { |
| 5130 |
renderer.paint(gc, config); |
| 5131 |
} |
| 5132 |
} catch (err) { |
| 5133 |
console.error(err); |
| 5134 |
} |
| 5135 |
|
| 5136 |
// Restore the GC state. |
| 5137 |
gc.restore(); |
| 5138 |
|
| 5139 |
// Compute the actual X bounds for the cell. |
| 5140 |
let x1 = Math.max(rgn.xMin, config.x); |
| 5141 |
let x2 = Math.min(config.x + config.width - 1, rgn.xMax); |
| 5142 |
|
| 5143 |
// Compute the actual Y bounds for the cell. |
| 5144 |
let y1 = Math.max(rgn.yMin, config.y); |
| 5145 |
let y2 = Math.min(config.y + config.height - 1, rgn.yMax); |
| 5146 |
|
| 5147 |
this._blitContent( |
| 5148 |
this._buffer, |
| 5149 |
x1, |
| 5150 |
y1, |
| 5151 |
x2 - x1 + 1, |
| 5152 |
y2 - y1 + 1, |
| 5153 |
x1, |
| 5154 |
y1 |
| 5155 |
); |
| 5156 |
|
| 5157 |
// Increment the running Y coordinate. |
| 5158 |
y += height; |
| 5159 |
} |
| 5160 |
|
| 5161 |
// Restore the GC state. |
| 5162 |
gc.restore(); |
| 5163 |
|
| 5164 |
// Increment the running X coordinate. |
| 5165 |
x += width; |
| 5166 |
} |
| 5167 |
|
| 5168 |
// Dispose of the wrapped gc. |
| 5169 |
gc.dispose(); |
| 5170 |
|
| 5171 |
// Restore the final buffer gc state. |
| 5172 |
this._bufferGC.restore(); |
| 5173 |
} |
| 5174 |
|
| 5175 |
// TODO Move this in the utils file (but we need the PaintRegion typing) |
| 5176 |
private cellGroupInteresectsRegion( |
| 5177 |
group: CellGroup, |
| 5178 |
rgn: Private.PaintRegion |
| 5179 |
) { |
| 5180 |
const rgnR1 = rgn.row; |
| 5181 |
const rgnR2 = rgn.row + rgn.rowSizes.length; |
| 5182 |
|
| 5183 |
const rgnC1 = rgn.column; |
| 5184 |
const rgnC2 = rgn.column + rgn.columnSizes.length; |
| 5185 |
|
| 5186 |
const dx = Math.min(group.r2, rgnR2) - Math.max(group.r1, rgnR1); |
| 5187 |
const dy = Math.min(group.c2, rgnC2) - Math.max(group.c1, rgnC1); |
| 5188 |
|
| 5189 |
return dx >= 0 && dy >= 0; |
| 5190 |
} |
| 5191 |
|
| 5192 |
private static _getCellValue( |
| 5193 |
dm: DataModel, |
| 5194 |
region: DataModel.CellRegion, |
| 5195 |
row: number, |
| 5196 |
col: number |
| 5197 |
) { |
| 5198 |
// Get the value for the cell. |
| 5199 |
try { |
| 5200 |
return dm.data(region, row, col); |
| 5201 |
} catch (err) { |
| 5202 |
console.error(err); |
| 5203 |
return null; |
| 5204 |
} |
| 5205 |
} |
| 5206 |
|
| 5207 |
private static _getCellMetadata( |
| 5208 |
dm: DataModel, |
| 5209 |
region: DataModel.CellRegion, |
| 5210 |
row: number, |
| 5211 |
col: number |
| 5212 |
) { |
| 5213 |
// Get the metadata for the cell. |
| 5214 |
try { |
| 5215 |
return dm.metadata(region, row, col); |
| 5216 |
} catch (err) { |
| 5217 |
console.error(err); |
| 5218 |
return DataModel.emptyMetadata; |
| 5219 |
} |
| 5220 |
} |
| 5221 |
|
| 5222 |
/** |
| 5223 |
* Paint group cells. |
| 5224 |
*/ |
| 5225 |
private _paintMergedCells( |
| 5226 |
cellGroups: CellGroup[], |
| 5227 |
rgn: Private.PixelRegion, |
| 5228 |
backgroundColor: string | undefined |
| 5229 |
): void { |
| 5230 |
// Bail if there is no data model. |
| 5231 |
if (!this._dataModel) { |
| 5232 |
return; |
| 5233 |
} |
| 5234 |
|
| 5235 |
// Set up the cell config object for rendering. |
| 5236 |
let config = { |
| 5237 |
x: 0, |
| 5238 |
y: 0, |
| 5239 |
width: 0, |
| 5240 |
height: 0, |
| 5241 |
region: rgn.region, |
| 5242 |
row: 0, |
| 5243 |
column: 0, |
| 5244 |
value: null as any, |
| 5245 |
metadata: DataModel.emptyMetadata |
| 5246 |
}; |
| 5247 |
|
| 5248 |
if (backgroundColor) { |
| 5249 |
this._canvasGC.fillStyle = backgroundColor; |
| 5250 |
} |
| 5251 |
// Set the line width for the grid lines. |
| 5252 |
this._canvasGC.lineWidth = 1; |
| 5253 |
|
| 5254 |
// Save the buffer gc before wrapping. |
| 5255 |
this._bufferGC.save(); |
| 5256 |
|
| 5257 |
// Wrap the buffer gc for painting the cells. |
| 5258 |
let gc = new GraphicsContext(this._bufferGC); |
| 5259 |
|
| 5260 |
for (const group of cellGroups) { |
| 5261 |
let width = 0; |
| 5262 |
for (let c = group.c1; c <= group.c2; c++) { |
| 5263 |
width += this._getColumnSize(rgn.region, c); |
| 5264 |
} |
| 5265 |
|
| 5266 |
let height = 0; |
| 5267 |
for (let r = group.r1; r <= group.r2; r++) { |
| 5268 |
height += this._getRowSize(rgn.region, r); |
| 5269 |
} |
| 5270 |
|
| 5271 |
let value = DataGrid._getCellValue( |
| 5272 |
this.dataModel as DataModel, |
| 5273 |
rgn.region, |
| 5274 |
group.r1, |
| 5275 |
group.c1 |
| 5276 |
); |
| 5277 |
let metadata = DataGrid._getCellMetadata( |
| 5278 |
this.dataModel as DataModel, |
| 5279 |
rgn.region, |
| 5280 |
group.r1, |
| 5281 |
group.c2 |
| 5282 |
); |
| 5283 |
|
| 5284 |
let x = 0; |
| 5285 |
let y = 0; |
| 5286 |
switch (rgn.region) { |
| 5287 |
case 'body': |
| 5288 |
x = |
| 5289 |
this._columnSections.offsetOf(group.c1) + |
| 5290 |
this.headerWidth - |
| 5291 |
this._scrollX; |
| 5292 |
y = |
| 5293 |
this._rowSections.offsetOf(group.r1) + |
| 5294 |
this.headerHeight - |
| 5295 |
this._scrollY; |
| 5296 |
break; |
| 5297 |
case 'column-header': |
| 5298 |
x = |
| 5299 |
this._columnSections.offsetOf(group.c1) + |
| 5300 |
this.headerWidth - |
| 5301 |
this._scrollX; |
| 5302 |
y = this._rowSections.offsetOf(group.r1); |
| 5303 |
break; |
| 5304 |
case 'row-header': |
| 5305 |
x = this._columnSections.offsetOf(group.c1); |
| 5306 |
y = |
| 5307 |
this._rowSections.offsetOf(group.r1) + |
| 5308 |
this.headerHeight - |
| 5309 |
this._scrollY; |
| 5310 |
break; |
| 5311 |
case 'corner-header': |
| 5312 |
x = this._columnSections.offsetOf(group.c1); |
| 5313 |
y = this._rowSections.offsetOf(group.r1); |
| 5314 |
break; |
| 5315 |
} |
| 5316 |
|
| 5317 |
config.x = x; |
| 5318 |
config.y = y; |
| 5319 |
config.width = width; |
| 5320 |
config.height = height; |
| 5321 |
config.region = rgn.region; |
| 5322 |
config.row = group.r1; |
| 5323 |
config.column = group.c1; |
| 5324 |
config.value = value; |
| 5325 |
config.metadata = metadata; |
| 5326 |
|
| 5327 |
// Compute the actual X bounds for the cell. |
| 5328 |
const x1 = Math.max(rgn.xMin, x); |
| 5329 |
const x2 = Math.min(x + width - 2, rgn.xMax); |
| 5330 |
|
| 5331 |
// Compute the actual Y bounds for the cell. |
| 5332 |
const y1 = Math.max(rgn.yMin, y); |
| 5333 |
const y2 = Math.min(y + height - 2, rgn.yMax); |
| 5334 |
|
| 5335 |
if (x2 <= x1 || y2 <= y1) { |
| 5336 |
continue; |
| 5337 |
} |
| 5338 |
|
| 5339 |
// Draw the background. |
| 5340 |
if (backgroundColor) { |
| 5341 |
this._canvasGC.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 5342 |
} |
| 5343 |
|
| 5344 |
// Get the renderer for the cell. |
| 5345 |
let renderer = this._cellRenderers.get(config); |
| 5346 |
|
| 5347 |
// Clear the buffer rect for the cell. |
| 5348 |
gc.clearRect(config.x, config.y, width, height); |
| 5349 |
|
| 5350 |
// Save the GC state. |
| 5351 |
gc.save(); |
| 5352 |
|
| 5353 |
// Paint the cell into the off-screen buffer. |
| 5354 |
try { |
| 5355 |
if (renderer instanceof AsyncCellRenderer) { |
| 5356 |
if (renderer.isReady(config)) { |
| 5357 |
renderer.paint(gc, config); |
| 5358 |
} else { |
| 5359 |
renderer.paintPlaceholder(gc, config); |
| 5360 |
|
| 5361 |
const r1 = group.r1; |
| 5362 |
const r2 = group.r2; |
| 5363 |
|
| 5364 |
const c1 = group.c1; |
| 5365 |
const c2 = group.c2; |
| 5366 |
|
| 5367 |
renderer.load(config).then(() => { |
| 5368 |
this.repaintRegion(rgn.region, r1, c1, r2, c2); |
| 5369 |
}); |
| 5370 |
} |
| 5371 |
} else { |
| 5372 |
renderer.paint(gc, config); |
| 5373 |
} |
| 5374 |
} catch (err) { |
| 5375 |
console.error(err); |
| 5376 |
} |
| 5377 |
|
| 5378 |
// Restore the GC state. |
| 5379 |
gc.restore(); |
| 5380 |
|
| 5381 |
this._blitContent(this._buffer, x1, y1, x2 - x1 + 1, y2 - y1 + 1, x1, y1); |
| 5382 |
} |
| 5383 |
|
| 5384 |
// Dispose of the wrapped gc. |
| 5385 |
gc.dispose(); |
| 5386 |
|
| 5387 |
// Restore the final buffer gc state. |
| 5388 |
this._bufferGC.restore(); |
| 5389 |
} |
| 5390 |
|
| 5391 |
/** |
| 5392 |
* Draw the horizontal grid lines for the given paint region. |
| 5393 |
*/ |
| 5394 |
private _drawHorizontalGridLines( |
| 5395 |
rgn: Private.PaintRegion, |
| 5396 |
color: string | undefined |
| 5397 |
): void { |
| 5398 |
// Bail if there is no color to draw. |
| 5399 |
if (!color) { |
| 5400 |
return; |
| 5401 |
} |
| 5402 |
|
| 5403 |
// Compute the X bounds for the horizontal lines. |
| 5404 |
const x1 = Math.max(rgn.xMin, rgn.x); |
| 5405 |
const x2 = Math.min(rgn.x + rgn.width, rgn.xMax + 1); |
| 5406 |
|
| 5407 |
// Begin the path for the grid lines. |
| 5408 |
this._canvasGC.beginPath(); |
| 5409 |
|
| 5410 |
// Set the line width for the grid lines. |
| 5411 |
this._canvasGC.lineWidth = 1; |
| 5412 |
|
| 5413 |
// Fetch the geometry. |
| 5414 |
const bh = this.bodyHeight; |
| 5415 |
const ph = this.pageHeight; |
| 5416 |
|
| 5417 |
// Fetch the number of grid lines to be drawn. |
| 5418 |
let n = rgn.rowSizes.length; |
| 5419 |
|
| 5420 |
// Adjust the count down if the last line shouldn't be drawn. |
| 5421 |
if (this._stretchLastRow && ph > bh) { |
| 5422 |
if (rgn.row + n === this._rowSections.count) { |
| 5423 |
n -= 1; |
| 5424 |
} |
| 5425 |
} |
| 5426 |
|
| 5427 |
// Draw the horizontal grid lines. |
| 5428 |
for (let y = rgn.y, j = 0; j < n; ++j) { |
| 5429 |
// Fetch the size of the row. |
| 5430 |
let size = rgn.rowSizes[j]; |
| 5431 |
|
| 5432 |
// Skip zero sized rows. |
| 5433 |
if (size === 0) { |
| 5434 |
continue; |
| 5435 |
} |
| 5436 |
|
| 5437 |
// Compute the Y position of the line. |
| 5438 |
let pos = y + size - 1; |
| 5439 |
|
| 5440 |
// Draw the line if it's in range of the dirty rect. |
| 5441 |
if (pos >= rgn.yMin && pos <= rgn.yMax) { |
| 5442 |
this._canvasGC.moveTo(x1, pos + 0.5); |
| 5443 |
this._canvasGC.lineTo(x2, pos + 0.5); |
| 5444 |
} |
| 5445 |
|
| 5446 |
// Increment the running Y coordinate. |
| 5447 |
y += size; |
| 5448 |
} |
| 5449 |
|
| 5450 |
// Stroke the lines with the specified color. |
| 5451 |
this._canvasGC.strokeStyle = color; |
| 5452 |
this._canvasGC.stroke(); |
| 5453 |
} |
| 5454 |
|
| 5455 |
/** |
| 5456 |
* Draw the vertical grid lines for the given paint region. |
| 5457 |
*/ |
| 5458 |
private _drawVerticalGridLines( |
| 5459 |
rgn: Private.PaintRegion, |
| 5460 |
color: string | undefined |
| 5461 |
): void { |
| 5462 |
// Bail if there is no color to draw. |
| 5463 |
if (!color) { |
| 5464 |
return; |
| 5465 |
} |
| 5466 |
|
| 5467 |
// Compute the Y bounds for the vertical lines. |
| 5468 |
const y1 = Math.max(rgn.yMin, rgn.y); |
| 5469 |
const y2 = Math.min(rgn.y + rgn.height, rgn.yMax + 1); |
| 5470 |
|
| 5471 |
// Begin the path for the grid lines |
| 5472 |
this._canvasGC.beginPath(); |
| 5473 |
|
| 5474 |
// Set the line width for the grid lines. |
| 5475 |
this._canvasGC.lineWidth = 1; |
| 5476 |
|
| 5477 |
// Fetch the geometry. |
| 5478 |
const bw = this.bodyWidth; |
| 5479 |
const pw = this.pageWidth; |
| 5480 |
|
| 5481 |
// Fetch the number of grid lines to be drawn. |
| 5482 |
let n = rgn.columnSizes.length; |
| 5483 |
|
| 5484 |
// Adjust the count down if the last line shouldn't be drawn. |
| 5485 |
if (this._stretchLastColumn && pw > bw) { |
| 5486 |
if (rgn.column + n === this._columnSections.count) { |
| 5487 |
n -= 1; |
| 5488 |
} |
| 5489 |
} |
| 5490 |
|
| 5491 |
// Draw the vertical grid lines. |
| 5492 |
for (let x = rgn.x, i = 0; i < n; ++i) { |
| 5493 |
// Fetch the size of the column. |
| 5494 |
let size = rgn.columnSizes[i]; |
| 5495 |
|
| 5496 |
// Skip zero sized columns. |
| 5497 |
if (size === 0) { |
| 5498 |
continue; |
| 5499 |
} |
| 5500 |
|
| 5501 |
// Compute the X position of the line. |
| 5502 |
let pos = x + size - 1; |
| 5503 |
|
| 5504 |
// Draw the line if it's in range of the dirty rect. |
| 5505 |
if (pos >= rgn.xMin && pos <= rgn.xMax) { |
| 5506 |
this._canvasGC.moveTo(pos + 0.5, y1); |
| 5507 |
this._canvasGC.lineTo(pos + 0.5, y2); |
| 5508 |
} |
| 5509 |
|
| 5510 |
// Increment the running X coordinate. |
| 5511 |
x += size; |
| 5512 |
} |
| 5513 |
|
| 5514 |
// Stroke the lines with the specified color. |
| 5515 |
this._canvasGC.strokeStyle = color; |
| 5516 |
this._canvasGC.stroke(); |
| 5517 |
} |
| 5518 |
|
| 5519 |
/** |
| 5520 |
* Draw the body selections for the data grid. |
| 5521 |
*/ |
| 5522 |
private _drawBodySelections(): void { |
| 5523 |
// Fetch the selection model. |
| 5524 |
let model = this._selectionModel; |
| 5525 |
|
| 5526 |
// Bail early if there are no selections. |
| 5527 |
if (!model || model.isEmpty) { |
| 5528 |
return; |
| 5529 |
} |
| 5530 |
|
| 5531 |
// Fetch the selection colors. |
| 5532 |
let fill = this._style.selectionFillColor; |
| 5533 |
let stroke = this._style.selectionBorderColor; |
| 5534 |
|
| 5535 |
// Bail early if there is nothing to draw. |
| 5536 |
if (!fill && !stroke) { |
| 5537 |
return; |
| 5538 |
} |
| 5539 |
|
| 5540 |
// Fetch the scroll geometry. |
| 5541 |
let sx = this._scrollX; |
| 5542 |
let sy = this._scrollY; |
| 5543 |
|
| 5544 |
// Get the first visible cell of the grid. |
| 5545 |
let r1 = this._rowSections.indexOf(sy); |
| 5546 |
let c1 = this._columnSections.indexOf(sx); |
| 5547 |
|
| 5548 |
// Bail early if there are no visible cells. |
| 5549 |
if (r1 < 0 || c1 < 0) { |
| 5550 |
return; |
| 5551 |
} |
| 5552 |
|
| 5553 |
// Fetch the extra geometry. |
| 5554 |
let bw = this.bodyWidth; |
| 5555 |
let bh = this.bodyHeight; |
| 5556 |
let pw = this.pageWidth; |
| 5557 |
let ph = this.pageHeight; |
| 5558 |
let hw = this.headerWidth; |
| 5559 |
let hh = this.headerHeight; |
| 5560 |
|
| 5561 |
// Get the last visible cell of the grid. |
| 5562 |
let r2 = this._rowSections.indexOf(sy + ph); |
| 5563 |
let c2 = this._columnSections.indexOf(sx + pw); |
| 5564 |
|
| 5565 |
// Fetch the max row and column. |
| 5566 |
let maxRow = this._rowSections.count - 1; |
| 5567 |
let maxColumn = this._columnSections.count - 1; |
| 5568 |
|
| 5569 |
// Clamp the last cell if the void space is visible. |
| 5570 |
r2 = r2 < 0 ? maxRow : r2; |
| 5571 |
c2 = c2 < 0 ? maxColumn : c2; |
| 5572 |
|
| 5573 |
// Fetch the overlay gc. |
| 5574 |
let gc = this._overlayGC; |
| 5575 |
|
| 5576 |
// Save the gc state. |
| 5577 |
gc.save(); |
| 5578 |
|
| 5579 |
// Set up the body clipping rect. |
| 5580 |
gc.beginPath(); |
| 5581 |
gc.rect(hw, hh, pw, ph); |
| 5582 |
gc.clip(); |
| 5583 |
|
| 5584 |
// Set up the gc style. |
| 5585 |
if (fill) { |
| 5586 |
gc.fillStyle = fill; |
| 5587 |
} |
| 5588 |
if (stroke) { |
| 5589 |
gc.strokeStyle = stroke; |
| 5590 |
gc.lineWidth = 1; |
| 5591 |
} |
| 5592 |
|
| 5593 |
// Iterate over the selections. |
| 5594 |
for (let s of model.selections()) { |
| 5595 |
// Skip the section if it's not visible. |
| 5596 |
if (s.r1 < r1 && s.r2 < r1) { |
| 5597 |
continue; |
| 5598 |
} |
| 5599 |
if (s.r1 > r2 && s.r2 > r2) { |
| 5600 |
continue; |
| 5601 |
} |
| 5602 |
if (s.c1 < c1 && s.c2 < c1) { |
| 5603 |
continue; |
| 5604 |
} |
| 5605 |
if (s.c1 > c2 && s.c2 > c2) { |
| 5606 |
continue; |
| 5607 |
} |
| 5608 |
|
| 5609 |
// Clamp the cell to the model bounds. |
| 5610 |
let sr1 = Math.max(0, Math.min(s.r1, maxRow)); |
| 5611 |
let sc1 = Math.max(0, Math.min(s.c1, maxColumn)); |
| 5612 |
let sr2 = Math.max(0, Math.min(s.r2, maxRow)); |
| 5613 |
let sc2 = Math.max(0, Math.min(s.c2, maxColumn)); |
| 5614 |
|
| 5615 |
// Swap index order if needed. |
| 5616 |
let tmp: number; |
| 5617 |
if (sr1 > sr2) { |
| 5618 |
tmp = sr1; |
| 5619 |
sr1 = sr2; |
| 5620 |
sr2 = tmp; |
| 5621 |
} |
| 5622 |
if (sc1 > sc2) { |
| 5623 |
tmp = sc1; |
| 5624 |
sc1 = sc2; |
| 5625 |
sc2 = tmp; |
| 5626 |
} |
| 5627 |
|
| 5628 |
const joinedGroup = CellGroup.joinCellGroupWithMergedCellGroups( |
| 5629 |
this.dataModel!, |
| 5630 |
{ r1: sr1, r2: sr2, c1: sc1, c2: sc2 }, |
| 5631 |
'body' |
| 5632 |
); |
| 5633 |
|
| 5634 |
sr1 = joinedGroup.r1; |
| 5635 |
sr2 = joinedGroup.r2; |
| 5636 |
sc1 = joinedGroup.c1; |
| 5637 |
sc2 = joinedGroup.c2; |
| 5638 |
|
| 5639 |
// Convert to pixel coordinates. |
| 5640 |
let x1 = this._columnSections.offsetOf(sc1) - sx + hw; |
| 5641 |
let y1 = this._rowSections.offsetOf(sr1) - sy + hh; |
| 5642 |
let x2 = this._columnSections.extentOf(sc2) - sx + hw; |
| 5643 |
let y2 = this._rowSections.extentOf(sr2) - sy + hh; |
| 5644 |
|
| 5645 |
// Adjust the trailing X coordinate for column stretch. |
| 5646 |
if (this._stretchLastColumn && pw > bw && sc2 === maxColumn) { |
| 5647 |
x2 = hw + pw - 1; |
| 5648 |
} |
| 5649 |
|
| 5650 |
// Adjust the trailing Y coordinate for row stretch. |
| 5651 |
if (this._stretchLastRow && ph > bh && sr2 === maxRow) { |
| 5652 |
y2 = hh + ph - 1; |
| 5653 |
} |
| 5654 |
|
| 5655 |
// Clamp the bounds to just outside of the clipping rect. |
| 5656 |
x1 = Math.max(hw - 1, x1); |
| 5657 |
y1 = Math.max(hh - 1, y1); |
| 5658 |
x2 = Math.min(hw + pw + 1, x2); |
| 5659 |
y2 = Math.min(hh + ph + 1, y2); |
| 5660 |
|
| 5661 |
// Skip zero sized ranges. |
| 5662 |
if (x2 < x1 || y2 < y1) { |
| 5663 |
continue; |
| 5664 |
} |
| 5665 |
|
| 5666 |
// Fill the rect if needed. |
| 5667 |
if (fill) { |
| 5668 |
gc.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 5669 |
} |
| 5670 |
|
| 5671 |
// Stroke the rect if needed. |
| 5672 |
if (stroke) { |
| 5673 |
gc.strokeRect(x1 - 0.5, y1 - 0.5, x2 - x1 + 1, y2 - y1 + 1); |
| 5674 |
} |
| 5675 |
} |
| 5676 |
|
| 5677 |
// Restore the gc state. |
| 5678 |
gc.restore(); |
| 5679 |
} |
| 5680 |
|
| 5681 |
/** |
| 5682 |
* Draw the row header selections for the data grid. |
| 5683 |
*/ |
| 5684 |
private _drawRowHeaderSelections(): void { |
| 5685 |
// Fetch the selection model. |
| 5686 |
let model = this._selectionModel; |
| 5687 |
|
| 5688 |
// Bail early if there are no selections or if the selectionMode is the entire column. |
| 5689 |
if (!model || model.isEmpty || model.selectionMode == 'column') { |
| 5690 |
return; |
| 5691 |
} |
| 5692 |
|
| 5693 |
// Bail early if the row headers are not visible. |
| 5694 |
if (this.headerWidth === 0 || this.pageHeight === 0) { |
| 5695 |
return; |
| 5696 |
} |
| 5697 |
|
| 5698 |
// Fetch the selection colors. |
| 5699 |
let fill = this._style.headerSelectionFillColor; |
| 5700 |
let stroke = this._style.headerSelectionBorderColor; |
| 5701 |
|
| 5702 |
// Bail early if there is nothing to draw. |
| 5703 |
if (!fill && !stroke) { |
| 5704 |
return; |
| 5705 |
} |
| 5706 |
|
| 5707 |
// Fetch common geometry. |
| 5708 |
let sy = this._scrollY; |
| 5709 |
let bh = this.bodyHeight; |
| 5710 |
let ph = this.pageHeight; |
| 5711 |
let hw = this.headerWidth; |
| 5712 |
let hh = this.headerHeight; |
| 5713 |
let rs = this._rowSections; |
| 5714 |
|
| 5715 |
// Fetch the overlay gc. |
| 5716 |
let gc = this._overlayGC; |
| 5717 |
|
| 5718 |
// Save the gc state. |
| 5719 |
gc.save(); |
| 5720 |
|
| 5721 |
// Set up the header clipping rect. |
| 5722 |
gc.beginPath(); |
| 5723 |
gc.rect(0, hh, hw, ph); |
| 5724 |
gc.clip(); |
| 5725 |
|
| 5726 |
// Set up the gc style. |
| 5727 |
if (fill) { |
| 5728 |
gc.fillStyle = fill; |
| 5729 |
} |
| 5730 |
if (stroke) { |
| 5731 |
gc.strokeStyle = stroke; |
| 5732 |
gc.lineWidth = 1; |
| 5733 |
} |
| 5734 |
|
| 5735 |
// Fetch the max row. |
| 5736 |
let maxRow = rs.count - 1; |
| 5737 |
|
| 5738 |
// Fetch the visible rows. |
| 5739 |
let r1 = rs.indexOf(sy); |
| 5740 |
let r2 = rs.indexOf(sy + ph - 1); |
| 5741 |
r2 = r2 < 0 ? maxRow : r2; |
| 5742 |
|
| 5743 |
// Iterate over the visible rows. |
| 5744 |
for (let j = r1; j <= r2; ++j) { |
| 5745 |
// Skip rows which aren't selected. |
| 5746 |
if (!model.isRowSelected(j)) { |
| 5747 |
continue; |
| 5748 |
} |
| 5749 |
|
| 5750 |
// Get the dimensions of the row. |
| 5751 |
let y = rs.offsetOf(j) - sy + hh; |
| 5752 |
let h = rs.sizeOf(j); |
| 5753 |
|
| 5754 |
// Adjust the height for row stretch. |
| 5755 |
if (this._stretchLastRow && ph > bh && j === maxRow) { |
| 5756 |
h = hh + ph - y; |
| 5757 |
} |
| 5758 |
|
| 5759 |
// Skip zero sized rows. |
| 5760 |
if (h === 0) { |
| 5761 |
continue; |
| 5762 |
} |
| 5763 |
|
| 5764 |
// Fill the rect if needed. |
| 5765 |
if (fill) { |
| 5766 |
gc.fillRect(0, y, hw, h); |
| 5767 |
} |
| 5768 |
|
| 5769 |
// Draw the border if needed. |
| 5770 |
if (stroke) { |
| 5771 |
gc.beginPath(); |
| 5772 |
gc.moveTo(hw - 0.5, y - 1); |
| 5773 |
gc.lineTo(hw - 0.5, y + h); |
| 5774 |
gc.stroke(); |
| 5775 |
} |
| 5776 |
} |
| 5777 |
|
| 5778 |
// Restore the gc state. |
| 5779 |
gc.restore(); |
| 5780 |
} |
| 5781 |
|
| 5782 |
/** |
| 5783 |
* Draw the column header selections for the data grid. |
| 5784 |
*/ |
| 5785 |
private _drawColumnHeaderSelections(): void { |
| 5786 |
// Fetch the selection model. |
| 5787 |
let model = this._selectionModel; |
| 5788 |
|
| 5789 |
// Bail early if there are no selections or if the selectionMode is the entire row |
| 5790 |
if (!model || model.isEmpty || model.selectionMode == 'row') { |
| 5791 |
return; |
| 5792 |
} |
| 5793 |
|
| 5794 |
// Bail early if the column headers are not visible. |
| 5795 |
if (this.headerHeight === 0 || this.pageWidth === 0) { |
| 5796 |
return; |
| 5797 |
} |
| 5798 |
|
| 5799 |
// Fetch the selection colors. |
| 5800 |
let fill = this._style.headerSelectionFillColor; |
| 5801 |
let stroke = this._style.headerSelectionBorderColor; |
| 5802 |
|
| 5803 |
// Bail early if there is nothing to draw. |
| 5804 |
if (!fill && !stroke) { |
| 5805 |
return; |
| 5806 |
} |
| 5807 |
|
| 5808 |
// Fetch common geometry. |
| 5809 |
let sx = this._scrollX; |
| 5810 |
let bw = this.bodyWidth; |
| 5811 |
let pw = this.pageWidth; |
| 5812 |
let hw = this.headerWidth; |
| 5813 |
let hh = this.headerHeight; |
| 5814 |
let cs = this._columnSections; |
| 5815 |
|
| 5816 |
// Fetch the overlay gc. |
| 5817 |
let gc = this._overlayGC; |
| 5818 |
|
| 5819 |
// Save the gc state. |
| 5820 |
gc.save(); |
| 5821 |
|
| 5822 |
// Set up the header clipping rect. |
| 5823 |
gc.beginPath(); |
| 5824 |
gc.rect(hw, 0, pw, hh); |
| 5825 |
gc.clip(); |
| 5826 |
|
| 5827 |
// Set up the gc style. |
| 5828 |
if (fill) { |
| 5829 |
gc.fillStyle = fill; |
| 5830 |
} |
| 5831 |
if (stroke) { |
| 5832 |
gc.strokeStyle = stroke; |
| 5833 |
gc.lineWidth = 1; |
| 5834 |
} |
| 5835 |
|
| 5836 |
// Fetch the max column. |
| 5837 |
let maxCol = cs.count - 1; |
| 5838 |
|
| 5839 |
// Fetch the visible columns. |
| 5840 |
let c1 = cs.indexOf(sx); |
| 5841 |
let c2 = cs.indexOf(sx + pw - 1); |
| 5842 |
c2 = c2 < 0 ? maxCol : c2; |
| 5843 |
|
| 5844 |
// Iterate over the visible columns. |
| 5845 |
for (let i = c1; i <= c2; ++i) { |
| 5846 |
// Skip columns which aren't selected. |
| 5847 |
if (!model.isColumnSelected(i)) { |
| 5848 |
continue; |
| 5849 |
} |
| 5850 |
|
| 5851 |
// Get the dimensions of the column. |
| 5852 |
let x = cs.offsetOf(i) - sx + hw; |
| 5853 |
let w = cs.sizeOf(i); |
| 5854 |
|
| 5855 |
// Adjust the width for column stretch. |
| 5856 |
if (this._stretchLastColumn && pw > bw && i === maxCol) { |
| 5857 |
w = hw + pw - x; |
| 5858 |
} |
| 5859 |
|
| 5860 |
// Skip zero sized columns. |
| 5861 |
if (w === 0) { |
| 5862 |
continue; |
| 5863 |
} |
| 5864 |
|
| 5865 |
// Fill the rect if needed. |
| 5866 |
if (fill) { |
| 5867 |
gc.fillRect(x, 0, w, hh); |
| 5868 |
} |
| 5869 |
|
| 5870 |
// Draw the border if needed. |
| 5871 |
if (stroke) { |
| 5872 |
gc.beginPath(); |
| 5873 |
gc.moveTo(x - 1, hh - 0.5); |
| 5874 |
gc.lineTo(x + w, hh - 0.5); |
| 5875 |
gc.stroke(); |
| 5876 |
} |
| 5877 |
} |
| 5878 |
|
| 5879 |
// Restore the gc state. |
| 5880 |
gc.restore(); |
| 5881 |
} |
| 5882 |
|
| 5883 |
/** |
| 5884 |
* Draw the overlay cursor for the data grid. |
| 5885 |
*/ |
| 5886 |
private _drawCursor(): void { |
| 5887 |
// Fetch the selection model. |
| 5888 |
let model = this._selectionModel; |
| 5889 |
|
| 5890 |
// Bail early if there is no cursor. |
| 5891 |
if (!model || model.isEmpty || model.selectionMode !== 'cell') { |
| 5892 |
return; |
| 5893 |
} |
| 5894 |
|
| 5895 |
// Extract the style information. |
| 5896 |
let fill = this._style.cursorFillColor; |
| 5897 |
let stroke = this._style.cursorBorderColor; |
| 5898 |
|
| 5899 |
// Bail early if there is nothing to draw. |
| 5900 |
if (!fill && !stroke) { |
| 5901 |
return; |
| 5902 |
} |
| 5903 |
|
| 5904 |
// Fetch the cursor location. |
| 5905 |
let startRow = model.cursorRow; |
| 5906 |
let startColumn = model.cursorColumn; |
| 5907 |
|
| 5908 |
// Fetch the max row and column. |
| 5909 |
let maxRow = this._rowSections.count - 1; |
| 5910 |
let maxColumn = this._columnSections.count - 1; |
| 5911 |
|
| 5912 |
// Bail early if the cursor is out of bounds. |
| 5913 |
if (startRow < 0 || startRow > maxRow) { |
| 5914 |
return; |
| 5915 |
} |
| 5916 |
if (startColumn < 0 || startColumn > maxColumn) { |
| 5917 |
return; |
| 5918 |
} |
| 5919 |
|
| 5920 |
let endRow = startRow; |
| 5921 |
let endColumn = startColumn; |
| 5922 |
|
| 5923 |
const joinedGroup = CellGroup.joinCellGroupWithMergedCellGroups( |
| 5924 |
this.dataModel!, |
| 5925 |
{ r1: startRow, r2: endRow, c1: startColumn, c2: endColumn }, |
| 5926 |
'body' |
| 5927 |
); |
| 5928 |
|
| 5929 |
startRow = joinedGroup.r1; |
| 5930 |
endRow = joinedGroup.r2; |
| 5931 |
startColumn = joinedGroup.c1; |
| 5932 |
endColumn = joinedGroup.c2; |
| 5933 |
|
| 5934 |
// Fetch geometry. |
| 5935 |
let sx = this._scrollX; |
| 5936 |
let sy = this._scrollY; |
| 5937 |
let bw = this.bodyWidth; |
| 5938 |
let bh = this.bodyHeight; |
| 5939 |
let pw = this.pageWidth; |
| 5940 |
let ph = this.pageHeight; |
| 5941 |
let hw = this.headerWidth; |
| 5942 |
let hh = this.headerHeight; |
| 5943 |
let vw = this._viewportWidth; |
| 5944 |
let vh = this._viewportHeight; |
| 5945 |
|
| 5946 |
// Get the cursor bounds in viewport coordinates. |
| 5947 |
let x1 = this._columnSections.offsetOf(startColumn) - sx + hw; |
| 5948 |
let x2 = this._columnSections.extentOf(endColumn) - sx + hw; |
| 5949 |
let y1 = this._rowSections.offsetOf(startRow) - sy + hh; |
| 5950 |
let y2 = this._rowSections.extentOf(endRow) - sy + hh; |
| 5951 |
|
| 5952 |
// Adjust the trailing X coordinate for column stretch. |
| 5953 |
if (this._stretchLastColumn && pw > bw && startColumn === maxColumn) { |
| 5954 |
x2 = vw - 1; |
| 5955 |
} |
| 5956 |
|
| 5957 |
// Adjust the trailing Y coordinate for row stretch. |
| 5958 |
if (this._stretchLastRow && ph > bh && startRow === maxRow) { |
| 5959 |
y2 = vh - 1; |
| 5960 |
} |
| 5961 |
|
| 5962 |
// Skip zero sized cursors. |
| 5963 |
if (x2 < x1 || y2 < y1) { |
| 5964 |
return; |
| 5965 |
} |
| 5966 |
|
| 5967 |
// Bail early if the cursor is off the screen. |
| 5968 |
if (x1 - 1 >= vw || y1 - 1 >= vh || x2 + 1 < hw || y2 + 1 < hh) { |
| 5969 |
return; |
| 5970 |
} |
| 5971 |
|
| 5972 |
// Fetch the overlay gc. |
| 5973 |
let gc = this._overlayGC; |
| 5974 |
|
| 5975 |
// Save the gc state. |
| 5976 |
gc.save(); |
| 5977 |
|
| 5978 |
// Set up the body clipping rect. |
| 5979 |
gc.beginPath(); |
| 5980 |
gc.rect(hw, hh, pw, ph); |
| 5981 |
gc.clip(); |
| 5982 |
|
| 5983 |
// Clear any existing overlay content. |
| 5984 |
gc.clearRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 5985 |
|
| 5986 |
// Fill the cursor rect if needed. |
| 5987 |
if (fill) { |
| 5988 |
// Set up the fill style. |
| 5989 |
gc.fillStyle = fill; |
| 5990 |
|
| 5991 |
// Fill the cursor rect. |
| 5992 |
gc.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1); |
| 5993 |
} |
| 5994 |
|
| 5995 |
// Stroke the cursor border if needed. |
| 5996 |
if (stroke) { |
| 5997 |
// Set up the stroke style. |
| 5998 |
gc.strokeStyle = stroke; |
| 5999 |
gc.lineWidth = 2; |
| 6000 |
|
| 6001 |
// Stroke the cursor rect. |
| 6002 |
gc.strokeRect(x1, y1, x2 - x1, y2 - y1); |
| 6003 |
} |
| 6004 |
|
| 6005 |
// Restore the gc state. |
| 6006 |
gc.restore(); |
| 6007 |
} |
| 6008 |
|
| 6009 |
/** |
| 6010 |
* Draw the overlay shadows for the data grid. |
| 6011 |
*/ |
| 6012 |
private _drawShadows(): void { |
| 6013 |
// Fetch the scroll shadow from the style. |
| 6014 |
let shadow = this._style.scrollShadow; |
| 6015 |
|
| 6016 |
// Bail early if there is no shadow to draw. |
| 6017 |
if (!shadow) { |
| 6018 |
return; |
| 6019 |
} |
| 6020 |
|
| 6021 |
// Fetch the scroll position. |
| 6022 |
let sx = this._scrollX; |
| 6023 |
let sy = this._scrollY; |
| 6024 |
|
| 6025 |
// Fetch maximum scroll position. |
| 6026 |
let sxMax = this.maxScrollX; |
| 6027 |
let syMax = this.maxScrollY; |
| 6028 |
|
| 6029 |
// Fetch the header width and height. |
| 6030 |
let hw = this.headerWidth; |
| 6031 |
let hh = this.headerHeight; |
| 6032 |
|
| 6033 |
// Fetch the page width and height. |
| 6034 |
let pw = this.pageWidth; |
| 6035 |
let ph = this.pageHeight; |
| 6036 |
|
| 6037 |
// Fetch the viewport width and height. |
| 6038 |
let vw = this._viewportWidth; |
| 6039 |
let vh = this._viewportHeight; |
| 6040 |
|
| 6041 |
// Fetch the body width and height. |
| 6042 |
let bw = this.bodyWidth; |
| 6043 |
let bh = this.bodyHeight; |
| 6044 |
|
| 6045 |
// Adjust the body size for row and column stretch. |
| 6046 |
if (this._stretchLastRow && ph > bh) { |
| 6047 |
bh = ph; |
| 6048 |
} |
| 6049 |
if (this._stretchLastColumn && pw > bw) { |
| 6050 |
bw = pw; |
| 6051 |
} |
| 6052 |
|
| 6053 |
// Fetch the gc object. |
| 6054 |
let gc = this._overlayGC; |
| 6055 |
|
| 6056 |
// Save the gc state. |
| 6057 |
gc.save(); |
| 6058 |
|
| 6059 |
// Draw the column header shadow if needed. |
| 6060 |
if (sy > 0) { |
| 6061 |
// Set up the gradient coordinates. |
| 6062 |
let x0 = 0; |
| 6063 |
let y0 = hh; |
| 6064 |
let x1 = 0; |
| 6065 |
let y1 = y0 + shadow.size; |
| 6066 |
|
| 6067 |
// Create the gradient object. |
| 6068 |
let grad = gc.createLinearGradient(x0, y0, x1, y1); |
| 6069 |
|
| 6070 |
// Set the gradient stops. |
| 6071 |
grad.addColorStop(0, shadow.color1); |
| 6072 |
grad.addColorStop(0.5, shadow.color2); |
| 6073 |
grad.addColorStop(1, shadow.color3); |
| 6074 |
|
| 6075 |
// Set up the rect coordinates. |
| 6076 |
let x = 0; |
| 6077 |
let y = hh; |
| 6078 |
let w = hw + Math.min(pw, bw - sx); |
| 6079 |
let h = shadow.size; |
| 6080 |
|
| 6081 |
// Fill the shadow rect with the fill style. |
| 6082 |
gc.fillStyle = grad; |
| 6083 |
gc.fillRect(x, y, w, h); |
| 6084 |
} |
| 6085 |
|
| 6086 |
// Draw the row header shadow if needed. |
| 6087 |
if (sx > 0) { |
| 6088 |
// Set up the gradient coordinates. |
| 6089 |
let x0 = hw; |
| 6090 |
let y0 = 0; |
| 6091 |
let x1 = x0 + shadow.size; |
| 6092 |
let y1 = 0; |
| 6093 |
|
| 6094 |
// Create the gradient object. |
| 6095 |
let grad = gc.createLinearGradient(x0, y0, x1, y1); |
| 6096 |
|
| 6097 |
// Set the gradient stops. |
| 6098 |
grad.addColorStop(0, shadow.color1); |
| 6099 |
grad.addColorStop(0.5, shadow.color2); |
| 6100 |
grad.addColorStop(1, shadow.color3); |
| 6101 |
|
| 6102 |
// Set up the rect coordinates. |
| 6103 |
let x = hw; |
| 6104 |
let y = 0; |
| 6105 |
let w = shadow.size; |
| 6106 |
let h = hh + Math.min(ph, bh - sy); |
| 6107 |
|
| 6108 |
// Fill the shadow rect with the fill style. |
| 6109 |
gc.fillStyle = grad; |
| 6110 |
gc.fillRect(x, y, w, h); |
| 6111 |
} |
| 6112 |
|
| 6113 |
// Draw the column footer shadow if needed. |
| 6114 |
if (sy < syMax) { |
| 6115 |
// Set up the gradient coordinates. |
| 6116 |
let x0 = 0; |
| 6117 |
let y0 = vh; |
| 6118 |
let x1 = 0; |
| 6119 |
let y1 = vh - shadow.size; |
| 6120 |
|
| 6121 |
// Create the gradient object. |
| 6122 |
let grad = gc.createLinearGradient(x0, y0, x1, y1); |
| 6123 |
|
| 6124 |
// Set the gradient stops. |
| 6125 |
grad.addColorStop(0, shadow.color1); |
| 6126 |
grad.addColorStop(0.5, shadow.color2); |
| 6127 |
grad.addColorStop(1, shadow.color3); |
| 6128 |
|
| 6129 |
// Set up the rect coordinates. |
| 6130 |
let x = 0; |
| 6131 |
let y = vh - shadow.size; |
| 6132 |
let w = hw + Math.min(pw, bw - sx); |
| 6133 |
let h = shadow.size; |
| 6134 |
|
| 6135 |
// Fill the shadow rect with the fill style. |
| 6136 |
gc.fillStyle = grad; |
| 6137 |
gc.fillRect(x, y, w, h); |
| 6138 |
} |
| 6139 |
|
| 6140 |
// Draw the row footer shadow if needed. |
| 6141 |
if (sx < sxMax) { |
| 6142 |
// Set up the gradient coordinates. |
| 6143 |
let x0 = vw; |
| 6144 |
let y0 = 0; |
| 6145 |
let x1 = vw - shadow.size; |
| 6146 |
let y1 = 0; |
| 6147 |
|
| 6148 |
// Create the gradient object. |
| 6149 |
let grad = gc.createLinearGradient(x0, y0, x1, y1); |
| 6150 |
|
| 6151 |
// Set the gradient stops. |
| 6152 |
grad.addColorStop(0, shadow.color1); |
| 6153 |
grad.addColorStop(0.5, shadow.color2); |
| 6154 |
grad.addColorStop(1, shadow.color3); |
| 6155 |
|
| 6156 |
// Set up the rect coordinates. |
| 6157 |
let x = vw - shadow.size; |
| 6158 |
let y = 0; |
| 6159 |
let w = shadow.size; |
| 6160 |
let h = hh + Math.min(ph, bh - sy); |
| 6161 |
|
| 6162 |
// Fill the shadow rect with the fill style. |
| 6163 |
gc.fillStyle = grad; |
| 6164 |
gc.fillRect(x, y, w, h); |
| 6165 |
} |
| 6166 |
|
| 6167 |
// Restore the gc state. |
| 6168 |
gc.restore(); |
| 6169 |
} |
| 6170 |
|
| 6171 |
private _viewport: Widget; |
| 6172 |
private _vScrollBar: ScrollBar; |
| 6173 |
private _hScrollBar: ScrollBar; |
| 6174 |
private _scrollCorner: Widget; |
| 6175 |
|
| 6176 |
private _scrollX = 0; |
| 6177 |
private _scrollY = 0; |
| 6178 |
private _viewportWidth = 0; |
| 6179 |
private _viewportHeight = 0; |
| 6180 |
|
| 6181 |
private _mousedown = false; |
| 6182 |
private _keyHandler: DataGrid.IKeyHandler | null = null; |
| 6183 |
private _mouseHandler: DataGrid.IMouseHandler | null = null; |
| 6184 |
|
| 6185 |
private _vScrollBarMinWidth = 0; |
| 6186 |
private _hScrollBarMinHeight = 0; |
| 6187 |
private _dpiRatio = Math.ceil(window.devicePixelRatio); |
| 6188 |
|
| 6189 |
private _canvas: HTMLCanvasElement; |
| 6190 |
private _buffer: HTMLCanvasElement; |
| 6191 |
private _overlay: HTMLCanvasElement; |
| 6192 |
private _canvasGC: CanvasRenderingContext2D; |
| 6193 |
private _bufferGC: CanvasRenderingContext2D; |
| 6194 |
private _overlayGC: CanvasRenderingContext2D; |
| 6195 |
|
| 6196 |
private _rowSections: SectionList; |
| 6197 |
private _columnSections: SectionList; |
| 6198 |
private _rowHeaderSections: SectionList; |
| 6199 |
private _columnHeaderSections: SectionList; |
| 6200 |
|
| 6201 |
private _dataModel: DataModel | null = null; |
| 6202 |
private _selectionModel: SelectionModel | null = null; |
| 6203 |
|
| 6204 |
private _stretchLastRow: boolean; |
| 6205 |
private _stretchLastColumn: boolean; |
| 6206 |
|
| 6207 |
private _style: DataGrid.Style; |
| 6208 |
private _cellRenderers: RendererMap; |
| 6209 |
private _copyConfig: DataGrid.CopyConfig; |
| 6210 |
private _headerVisibility: DataGrid.HeaderVisibility; |
| 6211 |
private _editorController: ICellEditorController | null; |
| 6212 |
private _editingEnabled: boolean = false; |
| 6213 |
} |
| 6214 |
|
| 6215 |
/** |
| 6216 |
* The namespace for the `DataGrid` class statics. |
| 6217 |
*/ |
| 6218 |
export namespace DataGrid { |
| 6219 |
/** |
| 6220 |
* An object which defines the style for a data grid. |
| 6221 |
* |
| 6222 |
* #### Notes |
| 6223 |
* All style colors support the full CSS color syntax. |
| 6224 |
*/ |
| 6225 |
export type Style = { |
| 6226 |
/** |
| 6227 |
* The void color for the data grid. |
| 6228 |
* |
| 6229 |
* This is the base fill color for the entire data grid. |
| 6230 |
*/ |
| 6231 |
readonly voidColor?: string; |
| 6232 |
|
| 6233 |
/** |
| 6234 |
* The background color for the body cells. |
| 6235 |
* |
| 6236 |
* This color is layered on top of the `voidColor`. |
| 6237 |
*/ |
| 6238 |
readonly backgroundColor?: string; |
| 6239 |
|
| 6240 |
/** |
| 6241 |
* A function which returns the background color for a row. |
| 6242 |
* |
| 6243 |
* This color is layered on top of the `backgroundColor` and can |
| 6244 |
* be used to implement "zebra striping" of the grid rows. |
| 6245 |
*/ |
| 6246 |
readonly rowBackgroundColor?: (index: number) => string; |
| 6247 |
|
| 6248 |
/** |
| 6249 |
* A function which returns the background color for a column. |
| 6250 |
* |
| 6251 |
* This color is layered on top of the `backgroundColor` and can |
| 6252 |
* be used to implement "zebra striping" of the grid columns. |
| 6253 |
*/ |
| 6254 |
readonly columnBackgroundColor?: (index: number) => string; |
| 6255 |
|
| 6256 |
/** |
| 6257 |
* The color for the grid lines of the body cells. |
| 6258 |
* |
| 6259 |
* The grid lines are draw on top of the cell contents. |
| 6260 |
*/ |
| 6261 |
readonly gridLineColor?: string; |
| 6262 |
|
| 6263 |
/** |
| 6264 |
* The color for the vertical grid lines of the body cells. |
| 6265 |
* |
| 6266 |
* This overrides the `gridLineColor` option. |
| 6267 |
*/ |
| 6268 |
readonly verticalGridLineColor?: string; |
| 6269 |
|
| 6270 |
/** |
| 6271 |
* The color for the horizontal grid lines of the body cells. |
| 6272 |
* |
| 6273 |
* This overrides the `gridLineColor` option. |
| 6274 |
*/ |
| 6275 |
readonly horizontalGridLineColor?: string; |
| 6276 |
|
| 6277 |
/** |
| 6278 |
* The background color for the header cells. |
| 6279 |
* |
| 6280 |
* This color is layered on top of the `voidColor`. |
| 6281 |
*/ |
| 6282 |
readonly headerBackgroundColor?: string; |
| 6283 |
|
| 6284 |
/** |
| 6285 |
* The color for the grid lines of the header cells. |
| 6286 |
* |
| 6287 |
* The grid lines are draw on top of the cell contents. |
| 6288 |
*/ |
| 6289 |
readonly headerGridLineColor?: string; |
| 6290 |
|
| 6291 |
/** |
| 6292 |
* The color for the vertical grid lines of the header cells. |
| 6293 |
* |
| 6294 |
* This overrides the `headerGridLineColor` option. |
| 6295 |
*/ |
| 6296 |
readonly headerVerticalGridLineColor?: string; |
| 6297 |
|
| 6298 |
/** |
| 6299 |
* The color for the horizontal grid lines of the header cells. |
| 6300 |
* |
| 6301 |
* This overrides the `headerGridLineColor` option. |
| 6302 |
*/ |
| 6303 |
readonly headerHorizontalGridLineColor?: string; |
| 6304 |
|
| 6305 |
/** |
| 6306 |
* The fill color for a selection. |
| 6307 |
*/ |
| 6308 |
readonly selectionFillColor?: string; |
| 6309 |
|
| 6310 |
/** |
| 6311 |
* The border color for a selection. |
| 6312 |
*/ |
| 6313 |
readonly selectionBorderColor?: string; |
| 6314 |
|
| 6315 |
/** |
| 6316 |
* The fill color for the cursor. |
| 6317 |
*/ |
| 6318 |
readonly cursorFillColor?: string; |
| 6319 |
|
| 6320 |
/** |
| 6321 |
* The border color for the cursor. |
| 6322 |
*/ |
| 6323 |
readonly cursorBorderColor?: string; |
| 6324 |
|
| 6325 |
/** |
| 6326 |
* The fill color for a header selection. |
| 6327 |
*/ |
| 6328 |
readonly headerSelectionFillColor?: string; |
| 6329 |
|
| 6330 |
/** |
| 6331 |
* The border color for a header selection. |
| 6332 |
*/ |
| 6333 |
readonly headerSelectionBorderColor?: string; |
| 6334 |
|
| 6335 |
/** |
| 6336 |
* The drop shadow effect when the grid is scrolled. |
| 6337 |
*/ |
| 6338 |
readonly scrollShadow?: { |
| 6339 |
/** |
| 6340 |
* The size of the shadow, in pixels. |
| 6341 |
*/ |
| 6342 |
readonly size: number; |
| 6343 |
|
| 6344 |
/** |
| 6345 |
* The first color stop for the shadow. |
| 6346 |
*/ |
| 6347 |
readonly color1: string; |
| 6348 |
|
| 6349 |
/** |
| 6350 |
* The second color stop for the shadow. |
| 6351 |
*/ |
| 6352 |
readonly color2: string; |
| 6353 |
|
| 6354 |
/** |
| 6355 |
* The third color stop for the shadow. |
| 6356 |
*/ |
| 6357 |
readonly color3: string; |
| 6358 |
}; |
| 6359 |
}; |
| 6360 |
|
| 6361 |
/** |
| 6362 |
* An object which defines the default sizes for a data grid. |
| 6363 |
*/ |
| 6364 |
export type DefaultSizes = { |
| 6365 |
/** |
| 6366 |
* The default height of a row. |
| 6367 |
*/ |
| 6368 |
readonly rowHeight: number; |
| 6369 |
|
| 6370 |
/** |
| 6371 |
* The default width of a column. |
| 6372 |
*/ |
| 6373 |
readonly columnWidth: number; |
| 6374 |
|
| 6375 |
/** |
| 6376 |
* The default width of a row header. |
| 6377 |
*/ |
| 6378 |
readonly rowHeaderWidth: number; |
| 6379 |
|
| 6380 |
/** |
| 6381 |
* The default height of a column header. |
| 6382 |
*/ |
| 6383 |
readonly columnHeaderHeight: number; |
| 6384 |
}; |
| 6385 |
|
| 6386 |
/** |
| 6387 |
* An object which defines the minimum sizes for a data grid. |
| 6388 |
*/ |
| 6389 |
export type MinimumSizes = { |
| 6390 |
/** |
| 6391 |
* The minimum height of a row. |
| 6392 |
*/ |
| 6393 |
readonly rowHeight: number; |
| 6394 |
|
| 6395 |
/** |
| 6396 |
* The minimum width of a column. |
| 6397 |
*/ |
| 6398 |
readonly columnWidth: number; |
| 6399 |
|
| 6400 |
/** |
| 6401 |
* The minimum width of a row header. |
| 6402 |
*/ |
| 6403 |
readonly rowHeaderWidth: number; |
| 6404 |
|
| 6405 |
/** |
| 6406 |
* The minimum height of a column header. |
| 6407 |
*/ |
| 6408 |
readonly columnHeaderHeight: number; |
| 6409 |
}; |
| 6410 |
|
| 6411 |
/** |
| 6412 |
* A type alias for the supported header visibility modes. |
| 6413 |
*/ |
| 6414 |
export type HeaderVisibility = 'all' | 'row' | 'column' | 'none'; |
| 6415 |
|
| 6416 |
/** |
| 6417 |
* A type alias for the supported column auto resize modes. |
| 6418 |
*/ |
| 6419 |
export type ColumnFitType = 'all' | 'row-header' | 'body'; |
| 6420 |
|
| 6421 |
/** |
| 6422 |
* A type alias for the arguments to a copy format function. |
| 6423 |
*/ |
| 6424 |
export type CopyFormatArgs = { |
| 6425 |
/** |
| 6426 |
* The cell region for the value. |
| 6427 |
*/ |
| 6428 |
region: DataModel.CellRegion; |
| 6429 |
|
| 6430 |
/** |
| 6431 |
* The row index of the value. |
| 6432 |
*/ |
| 6433 |
row: number; |
| 6434 |
|
| 6435 |
/** |
| 6436 |
* The column index of the value. |
| 6437 |
*/ |
| 6438 |
column: number; |
| 6439 |
|
| 6440 |
/** |
| 6441 |
* The value for the cell. |
| 6442 |
*/ |
| 6443 |
value: any; |
| 6444 |
|
| 6445 |
/** |
| 6446 |
* The metadata for the cell. |
| 6447 |
*/ |
| 6448 |
metadata: DataModel.Metadata; |
| 6449 |
}; |
| 6450 |
|
| 6451 |
/** |
| 6452 |
* A type alias for a copy format function. |
| 6453 |
*/ |
| 6454 |
export type CopyFormatFunc = (args: CopyFormatArgs) => string; |
| 6455 |
|
| 6456 |
/** |
| 6457 |
* A type alias for the data grid copy config. |
| 6458 |
*/ |
| 6459 |
export type CopyConfig = { |
| 6460 |
/** |
| 6461 |
* The separator to use between values. |
| 6462 |
*/ |
| 6463 |
readonly separator: string; |
| 6464 |
|
| 6465 |
/** |
| 6466 |
* The headers to included in the copied output. |
| 6467 |
*/ |
| 6468 |
readonly headers: 'none' | 'row' | 'column' | 'all'; |
| 6469 |
|
| 6470 |
/** |
| 6471 |
* The function for formatting the data values. |
| 6472 |
*/ |
| 6473 |
readonly format: CopyFormatFunc; |
| 6474 |
|
| 6475 |
/** |
| 6476 |
* The cell count threshold for a copy to be considered "large". |
| 6477 |
*/ |
| 6478 |
readonly warningThreshold: number; |
| 6479 |
}; |
| 6480 |
|
| 6481 |
/** |
| 6482 |
* An options object for initializing a data grid. |
| 6483 |
*/ |
| 6484 |
export interface IOptions { |
| 6485 |
/** |
| 6486 |
* The style for the data grid. |
| 6487 |
* |
| 6488 |
* The default is `DataGrid.defaultStyle`. |
| 6489 |
*/ |
| 6490 |
style?: Style; |
| 6491 |
|
| 6492 |
/** |
| 6493 |
* The default sizes for the data grid. |
| 6494 |
* |
| 6495 |
* The default is `DataGrid.defaultSizes`. |
| 6496 |
*/ |
| 6497 |
defaultSizes?: DefaultSizes; |
| 6498 |
|
| 6499 |
/** |
| 6500 |
* The minimum sizes for the data grid. |
| 6501 |
* |
| 6502 |
* The default is `DataGrid.minimumSizes`. |
| 6503 |
*/ |
| 6504 |
minimumSizes?: MinimumSizes; |
| 6505 |
|
| 6506 |
/** |
| 6507 |
* The header visibility for the data grid. |
| 6508 |
* |
| 6509 |
* The default is `'all'`. |
| 6510 |
*/ |
| 6511 |
headerVisibility?: HeaderVisibility; |
| 6512 |
|
| 6513 |
/** |
| 6514 |
* The cell renderer map for the data grid. |
| 6515 |
* |
| 6516 |
* The default is an empty renderer map. |
| 6517 |
*/ |
| 6518 |
cellRenderers?: RendererMap; |
| 6519 |
|
| 6520 |
/** |
| 6521 |
* The default cell renderer for the data grid. |
| 6522 |
* |
| 6523 |
* The default is a new `TextRenderer`. |
| 6524 |
*/ |
| 6525 |
defaultRenderer?: CellRenderer; |
| 6526 |
|
| 6527 |
/** |
| 6528 |
* The copy configuration data for the grid. |
| 6529 |
* |
| 6530 |
* The default is `DataGrid.defaultCopyConfig`. |
| 6531 |
*/ |
| 6532 |
copyConfig?: CopyConfig; |
| 6533 |
|
| 6534 |
/** |
| 6535 |
* Whether to stretch the last row of the grid. |
| 6536 |
* |
| 6537 |
* The default is `false`. |
| 6538 |
*/ |
| 6539 |
stretchLastRow?: boolean; |
| 6540 |
|
| 6541 |
/** |
| 6542 |
* Whether to stretch the last column of the grid. |
| 6543 |
* |
| 6544 |
* The default is `false`. |
| 6545 |
*/ |
| 6546 |
stretchLastColumn?: boolean; |
| 6547 |
} |
| 6548 |
|
| 6549 |
/** |
| 6550 |
* An object which handles keydown events for the data grid. |
| 6551 |
*/ |
| 6552 |
export interface IKeyHandler extends IDisposable { |
| 6553 |
/** |
| 6554 |
* Handle the key down event for the data grid. |
| 6555 |
* |
| 6556 |
* @param grid - The data grid of interest. |
| 6557 |
* |
| 6558 |
* @param event - The keydown event of interest. |
| 6559 |
* |
| 6560 |
* #### Notes |
| 6561 |
* This will not be called if the mouse button is pressed. |
| 6562 |
*/ |
| 6563 |
onKeyDown(grid: DataGrid, event: KeyboardEvent): void; |
| 6564 |
} |
| 6565 |
|
| 6566 |
/** |
| 6567 |
* An object which handles mouse events for the data grid. |
| 6568 |
*/ |
| 6569 |
export interface IMouseHandler extends IDisposable { |
| 6570 |
/** |
| 6571 |
* Release any resources acquired during a mouse press. |
| 6572 |
* |
| 6573 |
* #### Notes |
| 6574 |
* This method is called when the mouse should be released |
| 6575 |
* independent of a mouseup event, such as an early detach. |
| 6576 |
*/ |
| 6577 |
release(): void; |
| 6578 |
|
| 6579 |
/** |
| 6580 |
* Handle the mouse hover event for the data grid. |
| 6581 |
* |
| 6582 |
* @param grid - The data grid of interest. |
| 6583 |
* |
| 6584 |
* @param event - The mouse hover event of interest. |
| 6585 |
*/ |
| 6586 |
onMouseHover(grid: DataGrid, event: MouseEvent): void; |
| 6587 |
|
| 6588 |
/** |
| 6589 |
* Handle the mouse leave event for the data grid. |
| 6590 |
* |
| 6591 |
* @param grid - The data grid of interest. |
| 6592 |
* |
| 6593 |
* @param event - The mouse hover event of interest. |
| 6594 |
*/ |
| 6595 |
onMouseLeave(grid: DataGrid, event: MouseEvent): void; |
| 6596 |
|
| 6597 |
/** |
| 6598 |
* Handle the mouse down event for the data grid. |
| 6599 |
* |
| 6600 |
* @param grid - The data grid of interest. |
| 6601 |
* |
| 6602 |
* @param event - The mouse down event of interest. |
| 6603 |
*/ |
| 6604 |
onMouseDown(grid: DataGrid, event: MouseEvent): void; |
| 6605 |
|
| 6606 |
/** |
| 6607 |
* Handle the mouse move event for the data grid. |
| 6608 |
* |
| 6609 |
* @param grid - The data grid of interest. |
| 6610 |
* |
| 6611 |
* @param event - The mouse move event of interest. |
| 6612 |
*/ |
| 6613 |
onMouseMove(grid: DataGrid, event: MouseEvent): void; |
| 6614 |
|
| 6615 |
/** |
| 6616 |
* Handle the mouse up event for the data grid. |
| 6617 |
* |
| 6618 |
* @param grid - The data grid of interest. |
| 6619 |
* |
| 6620 |
* @param event - The mouse up event of interest. |
| 6621 |
*/ |
| 6622 |
onMouseUp(grid: DataGrid, event: MouseEvent): void; |
| 6623 |
|
| 6624 |
/** |
| 6625 |
* Handle the mouse double click event for the data grid. |
| 6626 |
* |
| 6627 |
* @param grid - The data grid of interest. |
| 6628 |
* |
| 6629 |
* @param event - The mouse double click event of interest. |
| 6630 |
*/ |
| 6631 |
onMouseDoubleClick(grid: DataGrid, event: MouseEvent): void; |
| 6632 |
|
| 6633 |
/** |
| 6634 |
* Handle the context menu event for the data grid. |
| 6635 |
* |
| 6636 |
* @param grid - The data grid of interest. |
| 6637 |
* |
| 6638 |
* @param event - The context menu event of interest. |
| 6639 |
*/ |
| 6640 |
onContextMenu(grid: DataGrid, event: MouseEvent): void; |
| 6641 |
|
| 6642 |
/** |
| 6643 |
* Handle the wheel event for the data grid. |
| 6644 |
* |
| 6645 |
* @param grid - The data grid of interest. |
| 6646 |
* |
| 6647 |
* @param event - The wheel event of interest. |
| 6648 |
*/ |
| 6649 |
onWheel(grid: DataGrid, event: WheelEvent): void; |
| 6650 |
} |
| 6651 |
|
| 6652 |
/** |
| 6653 |
* An object which holds the result of a grid hit test. |
| 6654 |
*/ |
| 6655 |
export type HitTestResult = { |
| 6656 |
/** |
| 6657 |
* The region of the data grid that was hit. |
| 6658 |
*/ |
| 6659 |
readonly region: DataModel.CellRegion | 'void'; |
| 6660 |
|
| 6661 |
/** |
| 6662 |
* The row index of the cell that was hit. |
| 6663 |
* |
| 6664 |
* This is `-1` for the `void` region. |
| 6665 |
*/ |
| 6666 |
readonly row: number; |
| 6667 |
|
| 6668 |
/** |
| 6669 |
* The column index of the cell that was hit. |
| 6670 |
* |
| 6671 |
* This is `-1` for the `void` region. |
| 6672 |
*/ |
| 6673 |
readonly column: number; |
| 6674 |
|
| 6675 |
/** |
| 6676 |
* The X coordinate of the mouse in cell coordinates. |
| 6677 |
* |
| 6678 |
* This is `-1` for the `void` region. |
| 6679 |
*/ |
| 6680 |
readonly x: number; |
| 6681 |
|
| 6682 |
/** |
| 6683 |
* The Y coordinate of the mouse in cell coordinates. |
| 6684 |
* |
| 6685 |
* This is `-1` for the `void` region. |
| 6686 |
*/ |
| 6687 |
readonly y: number; |
| 6688 |
|
| 6689 |
/** |
| 6690 |
* The width of the cell. |
| 6691 |
* |
| 6692 |
* This is `-1` for the `void` region. |
| 6693 |
*/ |
| 6694 |
readonly width: number; |
| 6695 |
|
| 6696 |
/** |
| 6697 |
* The height of the cell. |
| 6698 |
* |
| 6699 |
* This is `-1` for the `void` region. |
| 6700 |
*/ |
| 6701 |
readonly height: number; |
| 6702 |
}; |
| 6703 |
|
| 6704 |
/** |
| 6705 |
* A generic format function for the copy handler. |
| 6706 |
* |
| 6707 |
* @param args - The format args for the function. |
| 6708 |
* |
| 6709 |
* @returns The string representation of the value. |
| 6710 |
* |
| 6711 |
* #### Notes |
| 6712 |
* This function uses `String()` to coerce a value to a string. |
| 6713 |
*/ |
| 6714 |
export function copyFormatGeneric(args: CopyFormatArgs): string { |
| 6715 |
if (args.value === null || args.value === undefined) { |
| 6716 |
return ''; |
| 6717 |
} |
| 6718 |
return String(args.value); |
| 6719 |
} |
| 6720 |
|
| 6721 |
/** |
| 6722 |
* The default theme for a data grid. |
| 6723 |
*/ |
| 6724 |
export const defaultStyle: Style = { |
| 6725 |
voidColor: '#F3F3F3', |
| 6726 |
backgroundColor: '#FFFFFF', |
| 6727 |
gridLineColor: 'rgba(20, 20, 20, 0.15)', |
| 6728 |
headerBackgroundColor: '#F3F3F3', |
| 6729 |
headerGridLineColor: 'rgba(20, 20, 20, 0.25)', |
| 6730 |
selectionFillColor: 'rgba(49, 119, 229, 0.2)', |
| 6731 |
selectionBorderColor: 'rgba(0, 107, 247, 1.0)', |
| 6732 |
cursorBorderColor: 'rgba(0, 107, 247, 1.0)', |
| 6733 |
headerSelectionFillColor: 'rgba(20, 20, 20, 0.1)', |
| 6734 |
headerSelectionBorderColor: 'rgba(0, 107, 247, 1.0)', |
| 6735 |
scrollShadow: { |
| 6736 |
size: 10, |
| 6737 |
color1: 'rgba(0, 0, 0, 0.20)', |
| 6738 |
color2: 'rgba(0, 0, 0, 0.05)', |
| 6739 |
color3: 'rgba(0, 0, 0, 0.00)' |
| 6740 |
} |
| 6741 |
}; |
| 6742 |
|
| 6743 |
/** |
| 6744 |
* The default sizes for a data grid. |
| 6745 |
*/ |
| 6746 |
export const defaultSizes: DefaultSizes = { |
| 6747 |
rowHeight: 20, |
| 6748 |
columnWidth: 64, |
| 6749 |
rowHeaderWidth: 64, |
| 6750 |
columnHeaderHeight: 20 |
| 6751 |
}; |
| 6752 |
|
| 6753 |
/** |
| 6754 |
* The default minimum sizes for a data grid. |
| 6755 |
*/ |
| 6756 |
export const minimumSizes: MinimumSizes = { |
| 6757 |
rowHeight: 20, |
| 6758 |
columnWidth: 10, |
| 6759 |
rowHeaderWidth: 10, |
| 6760 |
columnHeaderHeight: 20 |
| 6761 |
}; |
| 6762 |
|
| 6763 |
/** |
| 6764 |
* The default copy config for a data grid. |
| 6765 |
*/ |
| 6766 |
export const defaultCopyConfig: CopyConfig = { |
| 6767 |
separator: '\t', |
| 6768 |
format: copyFormatGeneric, |
| 6769 |
headers: 'none', |
| 6770 |
warningThreshold: 1e6 |
| 6771 |
}; |
| 6772 |
|
| 6773 |
/** |
| 6774 |
* A body region defined by the cells' boundaries using row/column coordinates. |
| 6775 |
*/ |
| 6776 |
export interface IBodyRegion { |
| 6777 |
/** |
| 6778 |
* First row of the region |
| 6779 |
*/ |
| 6780 |
firstRow: number; |
| 6781 |
|
| 6782 |
/** |
| 6783 |
* Last row of the region |
| 6784 |
*/ |
| 6785 |
lastRow: number; |
| 6786 |
|
| 6787 |
/** |
| 6788 |
* First column of the region |
| 6789 |
*/ |
| 6790 |
firstColumn: number; |
| 6791 |
|
| 6792 |
/** |
| 6793 |
* Last column of the region |
| 6794 |
*/ |
| 6795 |
lastColumn: number; |
| 6796 |
} |
| 6797 |
} |
| 6798 |
|
| 6799 |
/** |
| 6800 |
* The namespace for the module implementation details. |
| 6801 |
*/ |
| 6802 |
namespace Private { |
| 6803 |
/** |
| 6804 |
* A singleton `scroll-request` conflatable message. |
| 6805 |
*/ |
| 6806 |
export const ScrollRequest = new ConflatableMessage('scroll-request'); |
| 6807 |
|
| 6808 |
/** |
| 6809 |
* A singleton `overlay-paint-request` conflatable message. |
| 6810 |
*/ |
| 6811 |
export const OverlayPaintRequest = new ConflatableMessage( |
| 6812 |
'overlay-paint-request' |
| 6813 |
); |
| 6814 |
|
| 6815 |
/** |
| 6816 |
* Create a new zero-sized canvas element. |
| 6817 |
*/ |
| 6818 |
export function createCanvas(): HTMLCanvasElement { |
| 6819 |
let canvas = document.createElement('canvas'); |
| 6820 |
canvas.width = 0; |
| 6821 |
canvas.height = 0; |
| 6822 |
return canvas; |
| 6823 |
} |
| 6824 |
|
| 6825 |
/** |
| 6826 |
* Checks whether a given regions has merged cells in it. |
| 6827 |
* @param dataModel grid's data model. |
| 6828 |
* @param region the paint region to be checked. |
| 6829 |
* @returns boolean. |
| 6830 |
*/ |
| 6831 |
export function regionHasMergedCells( |
| 6832 |
dataModel: DataModel, |
| 6833 |
region: DataModel.CellRegion |
| 6834 |
): boolean { |
| 6835 |
const regionGroups = CellGroup.getCellGroupsAtRegion(dataModel!, region); |
| 6836 |
return regionGroups.length > 0; |
| 6837 |
} |
| 6838 |
|
| 6839 |
/** |
| 6840 |
* An object which represents a canvas region in pixels. |
| 6841 |
*/ |
| 6842 |
export type PixelRegion = { |
| 6843 |
/** |
| 6844 |
* The min X coordinate the of the dirty viewport rect. |
| 6845 |
* |
| 6846 |
* #### Notes |
| 6847 |
* The data grid must not draw outside of this boundary. |
| 6848 |
*/ |
| 6849 |
xMin: number; |
| 6850 |
|
| 6851 |
/** |
| 6852 |
* The min Y coordinate the of the dirty viewport rect. |
| 6853 |
* |
| 6854 |
* #### Notes |
| 6855 |
* The data grid must not draw outside of this boundary. |
| 6856 |
*/ |
| 6857 |
yMin: number; |
| 6858 |
|
| 6859 |
/** |
| 6860 |
* The max X coordinate the of the dirty viewport rect. |
| 6861 |
* |
| 6862 |
* #### Notes |
| 6863 |
* The data grid must not draw outside of this boundary. |
| 6864 |
*/ |
| 6865 |
xMax: number; |
| 6866 |
|
| 6867 |
/** |
| 6868 |
* The max Y coordinate the of the dirty viewport rect. |
| 6869 |
* |
| 6870 |
* #### Notes |
| 6871 |
* The data grid must not draw outside of this boundary. |
| 6872 |
*/ |
| 6873 |
yMax: number; |
| 6874 |
|
| 6875 |
/** |
| 6876 |
* The cell region. |
| 6877 |
*/ |
| 6878 |
region: DataModel.CellRegion; |
| 6879 |
}; |
| 6880 |
|
| 6881 |
/** |
| 6882 |
* An object which represents a region to be painted. |
| 6883 |
*/ |
| 6884 |
export type PaintRegion = PixelRegion & { |
| 6885 |
/** |
| 6886 |
* The X coordinate the of the region, in viewport coordinates. |
| 6887 |
* |
| 6888 |
* #### Notes |
| 6889 |
* This is aligned to the first cell boundary. |
| 6890 |
*/ |
| 6891 |
x: number; |
| 6892 |
|
| 6893 |
/** |
| 6894 |
* The Y coordinate the of the region, in viewport coordinates. |
| 6895 |
* |
| 6896 |
* #### Notes |
| 6897 |
* This is aligned to the first cell boundary. |
| 6898 |
*/ |
| 6899 |
y: number; |
| 6900 |
|
| 6901 |
/** |
| 6902 |
* The total width of the region. |
| 6903 |
* |
| 6904 |
* #### Notes |
| 6905 |
* This is aligned to the cell boundaries. |
| 6906 |
*/ |
| 6907 |
width: number; |
| 6908 |
|
| 6909 |
/** |
| 6910 |
* The total height of the region. |
| 6911 |
* |
| 6912 |
* #### Notes |
| 6913 |
* This is aligned to the cell boundaries. |
| 6914 |
*/ |
| 6915 |
height: number; |
| 6916 |
|
| 6917 |
/** |
| 6918 |
* The row index of the first cell in the region. |
| 6919 |
*/ |
| 6920 |
row: number; |
| 6921 |
|
| 6922 |
/** |
| 6923 |
* The column index of the first cell in the region. |
| 6924 |
*/ |
| 6925 |
column: number; |
| 6926 |
|
| 6927 |
/** |
| 6928 |
* The row sizes for the rows in the region. |
| 6929 |
*/ |
| 6930 |
rowSizes: number[]; |
| 6931 |
|
| 6932 |
/** |
| 6933 |
* The column sizes for the columns in the region. |
| 6934 |
*/ |
| 6935 |
columnSizes: number[]; |
| 6936 |
}; |
| 6937 |
|
| 6938 |
/** |
| 6939 |
* A conflatable message which merges dirty paint regions. |
| 6940 |
*/ |
| 6941 |
export class PaintRequest extends ConflatableMessage { |
| 6942 |
/** |
| 6943 |
* Construct a new paint request messages. |
| 6944 |
* |
| 6945 |
* @param region - The cell region for the paint. |
| 6946 |
* |
| 6947 |
* @param r1 - The top-left row of the dirty region. |
| 6948 |
* |
| 6949 |
* @param c1 - The top-left column of the dirty region. |
| 6950 |
* |
| 6951 |
* @param r2 - The bottom-right row of the dirty region. |
| 6952 |
* |
| 6953 |
* @param c2 - The bottom-right column of the dirty region. |
| 6954 |
*/ |
| 6955 |
constructor( |
| 6956 |
region: DataModel.CellRegion | 'all', |
| 6957 |
r1: number, |
| 6958 |
c1: number, |
| 6959 |
r2: number, |
| 6960 |
c2: number |
| 6961 |
) { |
| 6962 |
super('paint-request'); |
| 6963 |
this._region = region; |
| 6964 |
this._r1 = r1; |
| 6965 |
this._c1 = c1; |
| 6966 |
this._r2 = r2; |
| 6967 |
this._c2 = c2; |
| 6968 |
} |
| 6969 |
|
| 6970 |
/** |
| 6971 |
* The cell region for the paint. |
| 6972 |
*/ |
| 6973 |
get region(): DataModel.CellRegion | 'all' { |
| 6974 |
return this._region; |
| 6975 |
} |
| 6976 |
|
| 6977 |
/** |
| 6978 |
* The top-left row of the dirty region. |
| 6979 |
*/ |
| 6980 |
get r1(): number { |
| 6981 |
return this._r1; |
| 6982 |
} |
| 6983 |
|
| 6984 |
/** |
| 6985 |
* The top-left column of the dirty region. |
| 6986 |
*/ |
| 6987 |
get c1(): number { |
| 6988 |
return this._c1; |
| 6989 |
} |
| 6990 |
|
| 6991 |
/** |
| 6992 |
* The bottom-right row of the dirty region. |
| 6993 |
*/ |
| 6994 |
get r2(): number { |
| 6995 |
return this._r2; |
| 6996 |
} |
| 6997 |
|
| 6998 |
/** |
| 6999 |
* The bottom-right column of the dirty region. |
| 7000 |
*/ |
| 7001 |
get c2(): number { |
| 7002 |
return this._c2; |
| 7003 |
} |
| 7004 |
|
| 7005 |
/** |
| 7006 |
* Conflate this message with another paint request. |
| 7007 |
*/ |
| 7008 |
conflate(other: PaintRequest): boolean { |
| 7009 |
// Bail early if the request is already painting everything. |
| 7010 |
if (this._region === 'all') { |
| 7011 |
return true; |
| 7012 |
} |
| 7013 |
|
| 7014 |
// Any region can conflate with the `'all'` region. |
| 7015 |
if (other._region === 'all') { |
| 7016 |
this._region = 'all'; |
| 7017 |
return true; |
| 7018 |
} |
| 7019 |
|
| 7020 |
// Otherwise, do not conflate with a different region. |
| 7021 |
if (this._region !== other._region) { |
| 7022 |
return false; |
| 7023 |
} |
| 7024 |
|
| 7025 |
// Conflate the region to the total boundary. |
| 7026 |
this._r1 = Math.min(this._r1, other._r1); |
| 7027 |
this._c1 = Math.min(this._c1, other._c1); |
| 7028 |
this._r2 = Math.max(this._r2, other._r2); |
| 7029 |
this._c2 = Math.max(this._c2, other._c2); |
| 7030 |
return true; |
| 7031 |
} |
| 7032 |
|
| 7033 |
private _region: DataModel.CellRegion | 'all'; |
| 7034 |
private _r1: number; |
| 7035 |
private _c1: number; |
| 7036 |
private _r2: number; |
| 7037 |
private _c2: number; |
| 7038 |
} |
| 7039 |
|
| 7040 |
/** |
| 7041 |
* A conflatable message for resizing rows. |
| 7042 |
*/ |
| 7043 |
export class RowResizeRequest extends ConflatableMessage { |
| 7044 |
/** |
| 7045 |
* Construct a new row resize request. |
| 7046 |
* |
| 7047 |
* @param region - The row region which holds the section. |
| 7048 |
* |
| 7049 |
* @param index - The index of row in the region. |
| 7050 |
* |
| 7051 |
* @param size - The target size of the section. |
| 7052 |
*/ |
| 7053 |
constructor(region: DataModel.RowRegion, index: number, size: number) { |
| 7054 |
super('row-resize-request'); |
| 7055 |
this._region = region; |
| 7056 |
this._index = index; |
| 7057 |
this._size = size; |
| 7058 |
} |
| 7059 |
|
| 7060 |
/** |
| 7061 |
* The row region which holds the section. |
| 7062 |
*/ |
| 7063 |
get region(): DataModel.RowRegion { |
| 7064 |
return this._region; |
| 7065 |
} |
| 7066 |
|
| 7067 |
/** |
| 7068 |
* The index of the row in the region. |
| 7069 |
*/ |
| 7070 |
get index(): number { |
| 7071 |
return this._index; |
| 7072 |
} |
| 7073 |
|
| 7074 |
/** |
| 7075 |
* The target size of the section. |
| 7076 |
*/ |
| 7077 |
get size(): number { |
| 7078 |
return this._size; |
| 7079 |
} |
| 7080 |
|
| 7081 |
/** |
| 7082 |
* Conflate this message with another row resize request. |
| 7083 |
*/ |
| 7084 |
conflate(other: RowResizeRequest): boolean { |
| 7085 |
if (this._region !== other._region || this._index !== other._index) { |
| 7086 |
return false; |
| 7087 |
} |
| 7088 |
this._size = other._size; |
| 7089 |
return true; |
| 7090 |
} |
| 7091 |
|
| 7092 |
private _region: DataModel.RowRegion; |
| 7093 |
private _index: number; |
| 7094 |
private _size: number; |
| 7095 |
} |
| 7096 |
|
| 7097 |
/** |
| 7098 |
* A conflatable message for resizing columns. |
| 7099 |
*/ |
| 7100 |
export class ColumnResizeRequest extends ConflatableMessage { |
| 7101 |
/** |
| 7102 |
* Construct a new column resize request. |
| 7103 |
* |
| 7104 |
* @param region - The column region which holds the section. |
| 7105 |
* |
| 7106 |
* @param index - The index of column in the region. |
| 7107 |
* |
| 7108 |
* @param size - The target size of the section. |
| 7109 |
* If null, then infer the size to fit. |
| 7110 |
*/ |
| 7111 |
constructor( |
| 7112 |
region: DataModel.ColumnRegion, |
| 7113 |
index: number, |
| 7114 |
size: number | null |
| 7115 |
) { |
| 7116 |
super('column-resize-request'); |
| 7117 |
this._region = region; |
| 7118 |
this._index = index; |
| 7119 |
this._size = size; |
| 7120 |
} |
| 7121 |
|
| 7122 |
/** |
| 7123 |
* The column region which holds the section. |
| 7124 |
*/ |
| 7125 |
get region(): DataModel.ColumnRegion { |
| 7126 |
return this._region; |
| 7127 |
} |
| 7128 |
|
| 7129 |
/** |
| 7130 |
* The index of the column in the region. |
| 7131 |
*/ |
| 7132 |
get index(): number { |
| 7133 |
return this._index; |
| 7134 |
} |
| 7135 |
|
| 7136 |
/** |
| 7137 |
* The target size of the section. |
| 7138 |
*/ |
| 7139 |
get size(): number | null { |
| 7140 |
return this._size; |
| 7141 |
} |
| 7142 |
|
| 7143 |
/** |
| 7144 |
* Conflate this message with another column resize request. |
| 7145 |
*/ |
| 7146 |
conflate(other: ColumnResizeRequest): boolean { |
| 7147 |
if (this._region !== other._region || this._index !== other._index) { |
| 7148 |
return false; |
| 7149 |
} |
| 7150 |
this._size = other._size; |
| 7151 |
return true; |
| 7152 |
} |
| 7153 |
|
| 7154 |
private _region: DataModel.ColumnRegion; |
| 7155 |
private _index: number; |
| 7156 |
private _size: number | null; |
| 7157 |
} |
| 7158 |
} |