Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | /** |
| 32 | * @unrestricted |
| 33 | */ |
| 34 | Console.ConsoleViewport = class { |
| 35 | /** |
| 36 | * @param {!Console.ConsoleViewportProvider} provider |
| 37 | */ |
| 38 | constructor(provider) { |
| 39 | this.element = createElement('div'); |
| 40 | this.element.style.overflow = 'auto'; |
| 41 | this._topGapElement = this.element.createChild('div'); |
| 42 | this._topGapElement.style.height = '0px'; |
| 43 | this._topGapElement.style.color = 'transparent'; |
| 44 | this._contentElement = this.element.createChild('div'); |
| 45 | this._bottomGapElement = this.element.createChild('div'); |
| 46 | this._bottomGapElement.style.height = '0px'; |
| 47 | this._bottomGapElement.style.color = 'transparent'; |
| 48 | |
| 49 | // Text content needed for range intersection checks in _updateSelectionModel. |
| 50 | // Use Unicode ZERO WIDTH NO-BREAK SPACE, which avoids contributing any height to the element's layout overflow. |
| 51 | this._topGapElement.textContent = '\uFEFF'; |
| 52 | this._bottomGapElement.textContent = '\uFEFF'; |
| 53 | |
| 54 | this._provider = provider; |
| 55 | this.element.addEventListener('scroll', this._onScroll.bind(this), false); |
| 56 | this.element.addEventListener('copy', this._onCopy.bind(this), false); |
| 57 | this.element.addEventListener('dragstart', this._onDragStart.bind(this), false); |
| 58 | |
| 59 | this._firstActiveIndex = -1; |
| 60 | this._lastActiveIndex = -1; |
| 61 | this._renderedItems = []; |
| 62 | this._anchorSelection = null; |
| 63 | this._headSelection = null; |
| 64 | this._itemCount = 0; |
| 65 | this._cumulativeHeights = new Int32Array(0); |
| 66 | this._muteCopyHandler = false; |
| 67 | |
| 68 | // Listen for any changes to descendants and trigger a refresh. This ensures |
| 69 | // that items updated asynchronously will not break stick-to-bottom behavior |
| 70 | // if they change the scroll height. |
| 71 | this._observer = new MutationObserver(this.refresh.bind(this)); |
| 72 | this._observerConfig = {childList: true, subtree: true}; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return {boolean} |
| 77 | */ |
| 78 | stickToBottom() { |
| 79 | return this._stickToBottom; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param {boolean} value |
| 84 | */ |
| 85 | setStickToBottom(value) { |
| 86 | this._stickToBottom = value; |
| 87 | if (this._stickToBottom) |
| 88 | this._observer.observe(this._contentElement, this._observerConfig); |
| 89 | else |
| 90 | this._observer.disconnect(); |
| 91 | } |
| 92 | |
| 93 | copyWithStyles() { |
| 94 | this._muteCopyHandler = true; |
| 95 | this.element.ownerDocument.execCommand('copy'); |
| 96 | this._muteCopyHandler = false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param {!Event} event |
| 101 | */ |
| 102 | _onCopy(event) { |
| 103 | if (this._muteCopyHandler) |
| 104 | return; |
| 105 | const text = this._selectedText(); |
| 106 | if (!text) |
| 107 | return; |
| 108 | event.preventDefault(); |
| 109 | event.clipboardData.setData('text/plain', text); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param {!Event} event |
| 114 | */ |
| 115 | _onDragStart(event) { |
| 116 | const text = this._selectedText(); |
| 117 | if (!text) |
| 118 | return false; |
| 119 | event.dataTransfer.clearData(); |
| 120 | event.dataTransfer.setData('text/plain', text); |
| 121 | event.dataTransfer.effectAllowed = 'copy'; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @return {!Element} |
| 127 | */ |
| 128 | contentElement() { |
| 129 | return this._contentElement; |
| 130 | } |
| 131 | |
| 132 | invalidate() { |
| 133 | delete this._cachedProviderElements; |
| 134 | this._itemCount = this._provider.itemCount(); |
| 135 | this._rebuildCumulativeHeights(); |
| 136 | this.refresh(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param {number} index |
| 141 | * @return {?Console.ConsoleViewportElement} |
| 142 | */ |
| 143 | _providerElement(index) { |
| 144 | if (!this._cachedProviderElements) |
| 145 | this._cachedProviderElements = new Array(this._itemCount); |
| 146 | let element = this._cachedProviderElements[index]; |
| 147 | if (!element) { |
| 148 | element = this._provider.itemElement(index); |
| 149 | this._cachedProviderElements[index] = element; |
| 150 | } |
| 151 | return element; |
| 152 | } |
| 153 | |
| 154 | _rebuildCumulativeHeights() { |
| 155 | const firstActiveIndex = this._firstActiveIndex; |
| 156 | const lastActiveIndex = this._lastActiveIndex; |
| 157 | let height = 0; |
| 158 | this._cumulativeHeights = new Int32Array(this._itemCount); |
| 159 | for (let i = 0; i < this._itemCount; ++i) { |
| 160 | if (firstActiveIndex <= i && i - firstActiveIndex < this._renderedItems.length && i <= lastActiveIndex) |
| 161 | height += this._renderedItems[i - firstActiveIndex].element().offsetHeight; |
| 162 | else |
| 163 | height += this._provider.fastHeight(i); |
| 164 | this._cumulativeHeights[i] = height; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | _rebuildCumulativeHeightsIfNeeded() { |
| 169 | // Check whether current items in DOM have changed heights. Tolerate 1-pixel |
| 170 | // error due to double-to-integer rounding errors. |
| 171 | for (let i = 0; i < this._renderedItems.length; ++i) { |
| 172 | const cachedItemHeight = this._cachedItemHeight(this._firstActiveIndex + i); |
| 173 | if (Math.abs(cachedItemHeight - this._renderedItems[i].element().offsetHeight) > 1) { |
| 174 | this._rebuildCumulativeHeights(); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @param {number} index |
| 182 | * @return {number} |
| 183 | */ |
| 184 | _cachedItemHeight(index) { |
| 185 | return index === 0 ? this._cumulativeHeights[0] : |
| 186 | this._cumulativeHeights[index] - this._cumulativeHeights[index - 1]; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @param {?Selection} selection |
| 191 | * @suppressGlobalPropertiesCheck |
| 192 | */ |
| 193 | _isSelectionBackwards(selection) { |
| 194 | if (!selection || !selection.rangeCount) |
| 195 | return false; |
| 196 | const range = document.createRange(); |
| 197 | range.setStart(selection.anchorNode, selection.anchorOffset); |
| 198 | range.setEnd(selection.focusNode, selection.focusOffset); |
| 199 | return range.collapsed; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param {number} itemIndex |
| 204 | * @param {!Node} node |
| 205 | * @param {number} offset |
| 206 | * @return {!{item: number, node: !Node, offset: number}} |
| 207 | */ |
| 208 | _createSelectionModel(itemIndex, node, offset) { |
| 209 | return {item: itemIndex, node: node, offset: offset}; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * @param {?Selection} selection |
| 214 | */ |
| 215 | _updateSelectionModel(selection) { |
| 216 | const range = selection && selection.rangeCount ? selection.getRangeAt(0) : null; |
| 217 | if (!range || selection.isCollapsed || !this.element.hasSelection()) { |
| 218 | this._headSelection = null; |
| 219 | this._anchorSelection = null; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | let firstSelected = Number.MAX_VALUE; |
| 224 | let lastSelected = -1; |
| 225 | |
| 226 | let hasVisibleSelection = false; |
| 227 | for (let i = 0; i < this._renderedItems.length; ++i) { |
| 228 | if (range.intersectsNode(this._renderedItems[i].element())) { |
| 229 | const index = i + this._firstActiveIndex; |
| 230 | firstSelected = Math.min(firstSelected, index); |
| 231 | lastSelected = Math.max(lastSelected, index); |
| 232 | hasVisibleSelection = true; |
| 233 | } |
| 234 | } |
| 235 | if (hasVisibleSelection) { |
| 236 | firstSelected = |
| 237 | this._createSelectionModel(firstSelected, /** @type {!Node} */ (range.startContainer), range.startOffset); |
| 238 | lastSelected = |
| 239 | this._createSelectionModel(lastSelected, /** @type {!Node} */ (range.endContainer), range.endOffset); |
| 240 | } |
| 241 | const topOverlap = range.intersectsNode(this._topGapElement) && this._topGapElement._active; |
| 242 | const bottomOverlap = range.intersectsNode(this._bottomGapElement) && this._bottomGapElement._active; |
| 243 | if (!topOverlap && !bottomOverlap && !hasVisibleSelection) { |
| 244 | this._headSelection = null; |
| 245 | this._anchorSelection = null; |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | if (!this._anchorSelection || !this._headSelection) { |
| 250 | this._anchorSelection = this._createSelectionModel(0, this.element, 0); |
| 251 | this._headSelection = this._createSelectionModel(this._itemCount - 1, this.element, this.element.children.length); |
| 252 | this._selectionIsBackward = false; |
| 253 | } |
| 254 | |
| 255 | const isBackward = this._isSelectionBackwards(selection); |
| 256 | const startSelection = this._selectionIsBackward ? this._headSelection : this._anchorSelection; |
| 257 | const endSelection = this._selectionIsBackward ? this._anchorSelection : this._headSelection; |
| 258 | if (topOverlap && bottomOverlap && hasVisibleSelection) { |
| 259 | firstSelected = firstSelected.item < startSelection.item ? firstSelected : startSelection; |
| 260 | lastSelected = lastSelected.item > endSelection.item ? lastSelected : endSelection; |
| 261 | } else if (!hasVisibleSelection) { |
| 262 | firstSelected = startSelection; |
| 263 | lastSelected = endSelection; |
| 264 | } else if (topOverlap) { |
| 265 | firstSelected = isBackward ? this._headSelection : this._anchorSelection; |
| 266 | } else if (bottomOverlap) { |
| 267 | lastSelected = isBackward ? this._anchorSelection : this._headSelection; |
| 268 | } |
| 269 | |
| 270 | if (isBackward) { |
| 271 | this._anchorSelection = lastSelected; |
| 272 | this._headSelection = firstSelected; |
| 273 | } else { |
| 274 | this._anchorSelection = firstSelected; |
| 275 | this._headSelection = lastSelected; |
| 276 | } |
| 277 | this._selectionIsBackward = isBackward; |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @param {?Selection} selection |
| 283 | */ |
| 284 | _restoreSelection(selection) { |
| 285 | let anchorElement = null; |
| 286 | let anchorOffset; |
| 287 | if (this._firstActiveIndex <= this._anchorSelection.item && this._anchorSelection.item <= this._lastActiveIndex) { |
| 288 | anchorElement = this._anchorSelection.node; |
| 289 | anchorOffset = this._anchorSelection.offset; |
| 290 | } else { |
| 291 | if (this._anchorSelection.item < this._firstActiveIndex) |
| 292 | anchorElement = this._topGapElement; |
| 293 | else if (this._anchorSelection.item > this._lastActiveIndex) |
| 294 | anchorElement = this._bottomGapElement; |
| 295 | anchorOffset = this._selectionIsBackward ? 1 : 0; |
| 296 | } |
| 297 | |
| 298 | let headElement = null; |
| 299 | let headOffset; |
| 300 | if (this._firstActiveIndex <= this._headSelection.item && this._headSelection.item <= this._lastActiveIndex) { |
| 301 | headElement = this._headSelection.node; |
| 302 | headOffset = this._headSelection.offset; |
| 303 | } else { |
| 304 | if (this._headSelection.item < this._firstActiveIndex) |
| 305 | headElement = this._topGapElement; |
| 306 | else if (this._headSelection.item > this._lastActiveIndex) |
| 307 | headElement = this._bottomGapElement; |
| 308 | headOffset = this._selectionIsBackward ? 0 : 1; |
| 309 | } |
| 310 | |
| 311 | selection.setBaseAndExtent(anchorElement, anchorOffset, headElement, headOffset); |
| 312 | } |
| 313 | |
| 314 | refresh() { |
| 315 | this._observer.disconnect(); |
| 316 | this._innerRefresh(); |
| 317 | if (this._stickToBottom) |
| 318 | this._observer.observe(this._contentElement, this._observerConfig); |
| 319 | } |
| 320 | |
| 321 | _innerRefresh() { |
| 322 | if (!this._visibleHeight()) |
| 323 | return; // Do nothing for invisible controls. |
| 324 | |
| 325 | if (!this._itemCount) { |
| 326 | for (let i = 0; i < this._renderedItems.length; ++i) |
| 327 | this._renderedItems[i].willHide(); |
| 328 | this._renderedItems = []; |
| 329 | this._contentElement.removeChildren(); |
| 330 | this._topGapElement.style.height = '0px'; |
| 331 | this._bottomGapElement.style.height = '0px'; |
| 332 | this._firstActiveIndex = -1; |
| 333 | this._lastActiveIndex = -1; |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | const selection = this.element.getComponentSelection(); |
| 338 | const shouldRestoreSelection = this._updateSelectionModel(selection); |
| 339 | |
| 340 | const visibleFrom = this.element.scrollTop; |
| 341 | const visibleHeight = this._visibleHeight(); |
| 342 | const activeHeight = visibleHeight * 2; |
| 343 | this._rebuildCumulativeHeightsIfNeeded(); |
| 344 | |
| 345 | // When the viewport is scrolled to the bottom, using the cumulative heights estimate is not |
| 346 | // precise enough to determine next visible indices. This stickToBottom check avoids extra |
| 347 | // calls to refresh in those cases. |
| 348 | if (this._stickToBottom) { |
| 349 | this._firstActiveIndex = |
| 350 | Math.max(this._itemCount - Math.ceil(activeHeight / this._provider.minimumRowHeight()), 0); |
| 351 | this._lastActiveIndex = this._itemCount - 1; |
| 352 | } else { |
| 353 | this._firstActiveIndex = |
| 354 | Math.max(this._cumulativeHeights.lowerBound(visibleFrom + 1 - (activeHeight - visibleHeight) / 2), 0); |
| 355 | // Proactively render more rows in case some of them will be collapsed without triggering refresh. @see crbug.com/390169 |
| 356 | this._lastActiveIndex = this._firstActiveIndex + Math.ceil(activeHeight / this._provider.minimumRowHeight()) - 1; |
| 357 | this._lastActiveIndex = Math.min(this._lastActiveIndex, this._itemCount - 1); |
| 358 | } |
| 359 | |
| 360 | const topGapHeight = this._cumulativeHeights[this._firstActiveIndex - 1] || 0; |
| 361 | const bottomGapHeight = |
| 362 | this._cumulativeHeights[this._cumulativeHeights.length - 1] - this._cumulativeHeights[this._lastActiveIndex]; |
| 363 | |
| 364 | /** |
| 365 | * @this {Console.ConsoleViewport} |
| 366 | */ |
| 367 | function prepare() { |
| 368 | this._topGapElement.style.height = topGapHeight + 'px'; |
| 369 | this._bottomGapElement.style.height = bottomGapHeight + 'px'; |
| 370 | this._topGapElement._active = !!topGapHeight; |
| 371 | this._bottomGapElement._active = !!bottomGapHeight; |
| 372 | this._contentElement.style.setProperty('height', '10000000px'); |
| 373 | } |
| 374 | |
| 375 | this._partialViewportUpdate(prepare.bind(this)); |
| 376 | this._contentElement.style.removeProperty('height'); |
| 377 | // Should be the last call in the method as it might force layout. |
| 378 | if (shouldRestoreSelection) |
| 379 | this._restoreSelection(selection); |
| 380 | if (this._stickToBottom) |
| 381 | this.element.scrollTop = 10000000; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * @param {function()} prepare |
| 386 | */ |
| 387 | _partialViewportUpdate(prepare) { |
| 388 | const itemsToRender = new Set(); |
| 389 | for (let i = this._firstActiveIndex; i <= this._lastActiveIndex; ++i) |
| 390 | itemsToRender.add(this._providerElement(i)); |
| 391 | const willBeHidden = this._renderedItems.filter(item => !itemsToRender.has(item)); |
| 392 | for (let i = 0; i < willBeHidden.length; ++i) |
| 393 | willBeHidden[i].willHide(); |
| 394 | prepare(); |
| 395 | for (let i = 0; i < willBeHidden.length; ++i) |
| 396 | willBeHidden[i].element().remove(); |
| 397 | |
| 398 | const wasShown = []; |
| 399 | let anchor = this._contentElement.firstChild; |
| 400 | for (const viewportElement of itemsToRender) { |
| 401 | const element = viewportElement.element(); |
| 402 | if (element !== anchor) { |
| 403 | const shouldCallWasShown = !element.parentElement; |
| 404 | if (shouldCallWasShown) |
| 405 | wasShown.push(viewportElement); |
| 406 | this._contentElement.insertBefore(element, anchor); |
| 407 | } else { |
| 408 | anchor = anchor.nextSibling; |
| 409 | } |
| 410 | } |
| 411 | for (let i = 0; i < wasShown.length; ++i) |
| 412 | wasShown[i].wasShown(); |
| 413 | this._renderedItems = Array.from(itemsToRender); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @return {?string} |
| 418 | */ |
| 419 | _selectedText() { |
| 420 | this._updateSelectionModel(this.element.getComponentSelection()); |
| 421 | if (!this._headSelection || !this._anchorSelection) |
| 422 | return null; |
| 423 | |
| 424 | let startSelection = null; |
| 425 | let endSelection = null; |
| 426 | if (this._selectionIsBackward) { |
| 427 | startSelection = this._headSelection; |
| 428 | endSelection = this._anchorSelection; |
| 429 | } else { |
| 430 | startSelection = this._anchorSelection; |
| 431 | endSelection = this._headSelection; |
| 432 | } |
| 433 | |
| 434 | const textLines = []; |
| 435 | for (let i = startSelection.item; i <= endSelection.item; ++i) { |
| 436 | const element = this._providerElement(i).element(); |
| 437 | const lineContent = element.childTextNodes().map(Components.Linkifier.untruncatedNodeText).join(''); |
| 438 | textLines.push(lineContent); |
| 439 | } |
| 440 | |
| 441 | const endSelectionElement = this._providerElement(endSelection.item).element(); |
| 442 | if (endSelection.node && endSelection.node.isSelfOrDescendant(endSelectionElement)) { |
| 443 | const itemTextOffset = this._textOffsetInNode(endSelectionElement, endSelection.node, endSelection.offset); |
| 444 | textLines[textLines.length - 1] = textLines.peekLast().substring(0, itemTextOffset); |
| 445 | } |
| 446 | |
| 447 | const startSelectionElement = this._providerElement(startSelection.item).element(); |
| 448 | if (startSelection.node && startSelection.node.isSelfOrDescendant(startSelectionElement)) { |
| 449 | const itemTextOffset = this._textOffsetInNode(startSelectionElement, startSelection.node, startSelection.offset); |
| 450 | textLines[0] = textLines[0].substring(itemTextOffset); |
| 451 | } |
| 452 | |
| 453 | return textLines.join('\n'); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @param {!Element} itemElement |
| 458 | * @param {!Node} selectionNode |
| 459 | * @param {number} offset |
| 460 | * @return {number} |
| 461 | */ |
| 462 | _textOffsetInNode(itemElement, selectionNode, offset) { |
| 463 | // If the selectionNode is not a TextNode, we may need to convert a child offset into a character offset. |
| 464 | if (selectionNode.nodeType !== Node.TEXT_NODE) { |
| 465 | if (offset < selectionNode.childNodes.length) { |
| 466 | selectionNode = /** @type {!Node} */ (selectionNode.childNodes.item(offset)); |
| 467 | offset = 0; |
| 468 | } else { |
| 469 | offset = selectionNode.textContent.length; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | let chars = 0; |
| 474 | let node = itemElement; |
| 475 | while ((node = node.traverseNextNode(itemElement)) && node !== selectionNode) { |
| 476 | if (node.nodeType !== Node.TEXT_NODE || node.parentElement.nodeName === 'STYLE' || |
| 477 | node.parentElement.nodeName === 'SCRIPT') |
| 478 | continue; |
| 479 | chars += Components.Linkifier.untruncatedNodeText(node).length; |
| 480 | } |
| 481 | // If the selected node text was truncated, treat any non-zero offset as the full length. |
| 482 | const untruncatedContainerLength = Components.Linkifier.untruncatedNodeText(selectionNode).length; |
| 483 | if (offset > 0 && untruncatedContainerLength !== selectionNode.textContent.length) |
| 484 | offset = untruncatedContainerLength; |
| 485 | return chars + offset; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @param {!Event} event |
| 490 | */ |
| 491 | _onScroll(event) { |
| 492 | this.refresh(); |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @return {number} |
| 497 | */ |
| 498 | firstVisibleIndex() { |
| 499 | if (!this._cumulativeHeights.length) |
| 500 | return -1; |
| 501 | this._rebuildCumulativeHeightsIfNeeded(); |
| 502 | return this._cumulativeHeights.lowerBound(this.element.scrollTop + 1); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * @return {number} |
| 507 | */ |
| 508 | lastVisibleIndex() { |
| 509 | if (!this._cumulativeHeights.length) |
| 510 | return -1; |
| 511 | this._rebuildCumulativeHeightsIfNeeded(); |
| 512 | const scrollBottom = this.element.scrollTop + this.element.clientHeight; |
| 513 | const right = this._itemCount - 1; |
| 514 | return this._cumulativeHeights.lowerBound(scrollBottom, undefined, undefined, right); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @return {?Element} |
| 519 | */ |
| 520 | renderedElementAt(index) { |
| 521 | if (index < this._firstActiveIndex) |
| 522 | return null; |
| 523 | if (index > this._lastActiveIndex) |
| 524 | return null; |
| 525 | return this._renderedItems[index - this._firstActiveIndex].element(); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * @param {number} index |
| 530 | * @param {boolean=} makeLast |
| 531 | */ |
| 532 | scrollItemIntoView(index, makeLast) { |
| 533 | const firstVisibleIndex = this.firstVisibleIndex(); |
| 534 | const lastVisibleIndex = this.lastVisibleIndex(); |
| 535 | if (index > firstVisibleIndex && index < lastVisibleIndex) |
| 536 | return; |
| 537 | if (makeLast) |
| 538 | this.forceScrollItemToBeLast(index); |
| 539 | else if (index <= firstVisibleIndex) |
| 540 | this.forceScrollItemToBeFirst(index); |
| 541 | else if (index >= lastVisibleIndex) |
| 542 | this.forceScrollItemToBeLast(index); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @param {number} index |
| 547 | */ |
| 548 | forceScrollItemToBeFirst(index) { |
| 549 | console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index'); |
| 550 | this.setStickToBottom(false); |
| 551 | this._rebuildCumulativeHeightsIfNeeded(); |
| 552 | this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; |
| 553 | if (this.element.isScrolledToBottom()) |
| 554 | this.setStickToBottom(true); |
| 555 | this.refresh(); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @param {number} index |
| 560 | */ |
| 561 | forceScrollItemToBeLast(index) { |
| 562 | console.assert(index >= 0 && index < this._itemCount, 'Cannot scroll item at invalid index'); |
| 563 | this.setStickToBottom(false); |
| 564 | this._rebuildCumulativeHeightsIfNeeded(); |
| 565 | this.element.scrollTop = this._cumulativeHeights[index] - this._visibleHeight(); |
| 566 | if (this.element.isScrolledToBottom()) |
| 567 | this.setStickToBottom(true); |
| 568 | this.refresh(); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @return {number} |
| 573 | */ |
| 574 | _visibleHeight() { |
| 575 | // Use offsetHeight instead of clientHeight to avoid being affected by horizontal scroll. |
| 576 | return this.element.offsetHeight; |
| 577 | } |
| 578 | }; |
| 579 | |
| 580 | /** |
| 581 | * @interface |
| 582 | */ |
| 583 | Console.ConsoleViewportProvider = function() {}; |
| 584 | |
| 585 | Console.ConsoleViewportProvider.prototype = { |
| 586 | /** |
| 587 | * @param {number} index |
| 588 | * @return {number} |
| 589 | */ |
| 590 | fastHeight(index) { |
| 591 | return 0; |
| 592 | }, |
| 593 | |
| 594 | /** |
| 595 | * @return {number} |
| 596 | */ |
| 597 | itemCount() { |
| 598 | return 0; |
| 599 | }, |
| 600 | |
| 601 | /** |
| 602 | * @return {number} |
| 603 | */ |
| 604 | minimumRowHeight() { |
| 605 | return 0; |
| 606 | }, |
| 607 | |
| 608 | /** |
| 609 | * @param {number} index |
| 610 | * @return {?Console.ConsoleViewportElement} |
| 611 | */ |
| 612 | itemElement(index) { |
| 613 | return null; |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | /** |
| 618 | * @interface |
| 619 | */ |
| 620 | Console.ConsoleViewportElement = function() {}; |
| 621 | Console.ConsoleViewportElement.prototype = { |
| 622 | willHide() {}, |
| 623 | |
| 624 | wasShown() {}, |
| 625 | |
| 626 | /** |
| 627 | * @return {!Element} |
| 628 | */ |
| 629 | element() {}, |
| 630 | }; |