Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @constructor | |
| 7 * @param {!WebInspector.StaticViewportControl.Provider} provider | |
| 8 */ | |
| 9 WebInspector.StaticViewportControl = function(provider) | |
| 10 { | |
| 11 this.element = createElement("div"); | |
| 12 this.element.style.overflow = "auto"; | |
| 13 this._innerElement = this.element.createChild("div"); | |
| 14 this._innerElement.style.height = "0px"; | |
| 15 this._innerElement.style.position = "relative"; | |
| 16 this._innerElement.style.overflow = "hidden"; | |
| 17 | |
| 18 this._provider = provider; | |
| 19 this.element.addEventListener("scroll", this._update.bind(this), false); | |
|
dgozman
2016/09/29 20:35:50
Did you look into making this passive for performa
einbinder
2016/09/29 22:04:00
We are already prepopulating elements. I'd rather
| |
| 20 this._itemCount = 0; | |
| 21 this._insertedAtSymbol = Symbol("WebInspector.StaticViewportControl._inserte dAtSymbol"); | |
| 22 } | |
| 23 | |
| 24 WebInspector.StaticViewportControl.prototype = { | |
| 25 refresh: function() | |
| 26 { | |
| 27 this._itemCount = this._provider.itemCount(); | |
| 28 this._innerElement.removeChildren(); | |
| 29 | |
| 30 var height = 0; | |
| 31 this._cumulativeHeights = new Int32Array(this._itemCount); | |
| 32 for (var i = 0; i < this._itemCount; ++i) { | |
| 33 height += this._provider.fastItemHeight(i); | |
| 34 this._cumulativeHeights[i] = height; | |
| 35 } | |
| 36 this._innerElement.style.height = height + "px"; | |
| 37 | |
| 38 this._update(); | |
| 39 }, | |
| 40 | |
| 41 _update: function() | |
| 42 { | |
| 43 if (!this._cumulativeHeights) { | |
| 44 this.refresh(); | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 var visibleHeight = this._visibleHeight(); | |
| 49 var visibleFrom = this.element.scrollTop; | |
| 50 var activeHeight = visibleHeight * 2; | |
| 51 var firstActiveIndex = Math.max(Array.prototype.lowerBound.call(this._cu mulativeHeights, visibleFrom + 1 - (activeHeight - visibleHeight) / 2), 0); | |
|
dgozman
2016/09/29 20:35:50
I feel like inlining activeHeight will make this m
einbinder
2016/09/29 22:04:00
The value of activeHeight here is somewhat arbitra
| |
| 52 var lastActiveIndex = Math.min(Array.prototype.lowerBound.call(this._cum ulativeHeights, visibleFrom + visibleHeight + (activeHeight - visibleHeight) / 2 ), this._itemCount - 1); | |
| 53 | |
| 54 for (var i = this._innerElement.children.length - 1; i >= 0; --i) { | |
|
dgozman
2016/09/29 20:35:50
I think that saving children into local variable m
einbinder
2016/09/29 22:04:00
Done.
| |
| 55 var element = this._innerElement.children[i]; | |
| 56 if (element[this._insertedAtSymbol] < firstActiveIndex || element[th is._insertedAtSymbol] > lastActiveIndex) | |
| 57 element.remove(); | |
| 58 } | |
| 59 | |
| 60 for (var i = firstActiveIndex; i <= lastActiveIndex; ++i) | |
| 61 this._insertElement(i); | |
| 62 }, | |
| 63 | |
| 64 /** | |
| 65 * @param {number} index | |
| 66 */ | |
| 67 _insertElement: function(index) | |
| 68 { | |
| 69 var element = this._provider.itemElement(index); | |
| 70 if (!element || element.parentElement) | |
|
dgozman
2016/09/29 20:35:50
Let's maybe check that parentElement === this._inn
einbinder
2016/09/29 22:04:00
Done.
| |
| 71 return; | |
| 72 | |
| 73 element.style.position = "absolute"; | |
| 74 element.style.top = (this._cumulativeHeights[index - 1] || 0) + "px"; | |
| 75 element.style.left = "0"; | |
| 76 element.style.right = "0"; | |
| 77 element[this._insertedAtSymbol] = index; | |
| 78 this._innerElement.appendChild(element); | |
| 79 }, | |
| 80 | |
| 81 /** | |
| 82 * @return {number} | |
| 83 */ | |
| 84 firstVisibleIndex: function() | |
| 85 { | |
| 86 return Math.max(Array.prototype.lowerBound.call(this._cumulativeHeights, this.element.scrollTop + 1), 0); | |
| 87 }, | |
| 88 | |
| 89 /** | |
| 90 * @return {number} | |
| 91 */ | |
| 92 lastVisibleIndex: function() | |
| 93 { | |
| 94 var lastVisibleIndex = Math.max(Array.prototype.lowerBound.call(this._cu mulativeHeights, this.element.scrollTop + this._visibleHeight()), 0); | |
| 95 return Math.min(lastVisibleIndex, this._itemCount); | |
| 96 }, | |
| 97 | |
| 98 /** | |
| 99 * @param {number} index | |
| 100 * @param {boolean=} makeLast | |
| 101 */ | |
| 102 scrollItemIntoView: function(index, makeLast) | |
| 103 { | |
| 104 var firstVisibleIndex = this.firstVisibleIndex(); | |
| 105 var lastVisibleIndex = this.lastVisibleIndex(); | |
| 106 if (index > firstVisibleIndex && index < lastVisibleIndex) | |
| 107 return; | |
| 108 if (makeLast) | |
| 109 this.forceScrollItemToBeLast(index); | |
| 110 else if (index <= firstVisibleIndex) | |
| 111 this.forceScrollItemToBeFirst(index); | |
| 112 else if (index >= lastVisibleIndex) | |
| 113 this.forceScrollItemToBeLast(index); | |
| 114 }, | |
| 115 | |
| 116 /** | |
| 117 * @param {number} index | |
| 118 */ | |
| 119 forceScrollItemToBeFirst: function(index) | |
| 120 { | |
| 121 this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; | |
| 122 this._update(); | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * @param {number} index | |
| 127 */ | |
| 128 forceScrollItemToBeLast: function(index) | |
| 129 { | |
| 130 this.element.scrollTop = this._cumulativeHeights[index] - this._visibleH eight(); | |
| 131 this._update(); | |
| 132 }, | |
| 133 | |
| 134 /** | |
| 135 * @return {number} | |
| 136 */ | |
| 137 _visibleHeight: function() | |
| 138 { | |
| 139 return this.element.offsetHeight; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * @interface | |
| 145 */ | |
| 146 WebInspector.StaticViewportControl.Provider = function() | |
| 147 { | |
| 148 } | |
| 149 | |
| 150 WebInspector.StaticViewportControl.Provider.prototype = { | |
| 151 /** | |
| 152 * @param {number} index | |
| 153 * @return {number} | |
| 154 */ | |
| 155 fastItemHeight: function(index) { return 0; }, | |
| 156 | |
| 157 /** | |
| 158 * @return {number} | |
| 159 */ | |
| 160 itemCount: function() { return 0; }, | |
| 161 | |
| 162 /** | |
| 163 * @param {number} index | |
| 164 * @return {?Element} | |
| 165 */ | |
| 166 itemElement: function(index) { return null; } | |
| 167 } | |
| OLD | NEW |