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 this._provider = provider; | |
| 18 this.element.addEventListener("scroll", this.refresh.bind(this), false); | |
|
dgozman
2016/09/28 20:56:18
Should this be innerElement?
einbinder
2016/09/29 01:04:21
The .element scrolls, the ._innerElement is just v
| |
| 19 | |
| 20 this._firstActiveIndex = 0; | |
| 21 this._lastActiveIndex = -1; | |
| 22 this._itemCount = 0; | |
| 23 } | |
| 24 WebInspector.StaticViewportControl._insertedAt = Symbol("WebInspector.StaticView portControl._insertedAt"); | |
| 25 WebInspector.StaticViewportControl.prototype = { | |
|
dgozman
2016/09/28 20:56:18
nit: blank lines, please.
einbinder
2016/09/29 01:04:20
Done.
| |
| 26 invalidateContent: function() | |
| 27 { | |
| 28 this._itemCount = this._provider.itemCount(); | |
| 29 this._innerElement.removeChildren(); | |
| 30 this.refresh(); | |
| 31 }, | |
| 32 | |
| 33 refresh: function() | |
| 34 { | |
| 35 if (!this._visibleHeight()) | |
| 36 return; // Do nothing for invisible controls. | |
| 37 | |
| 38 var height = 0; | |
| 39 this._cumulativeHeights = new Int32Array(this._itemCount); | |
| 40 for (var i = 0; i < this._itemCount; ++i) { | |
| 41 height += this._provider.fastHeight(i); | |
| 42 this._cumulativeHeights[i] = height; | |
| 43 } | |
| 44 this._innerElement.style.height = height + "px"; | |
| 45 | |
| 46 this._update(); | |
| 47 }, | |
| 48 | |
| 49 _update: function() | |
| 50 { | |
| 51 if (!this._cumulativeHeights) { | |
| 52 this.refresh(); | |
| 53 return; | |
| 54 } | |
| 55 | |
| 56 var visibleHeight = this._visibleHeight(); | |
| 57 var visibleFrom = this.element.scrollTop; | |
| 58 var activeHeight = visibleHeight * 2; | |
| 59 this._firstActiveIndex = Math.max(Array.prototype.lowerBound.call(this._ cumulativeHeights, visibleFrom + 1 - (activeHeight - visibleHeight) / 2), 0); | |
| 60 this._lastActiveIndex = Math.min(Array.prototype.lowerBound.call(this._c umulativeHeights, visibleFrom + visibleHeight + (activeHeight - visibleHeight) / 2), this._itemCount - 1); | |
| 61 | |
| 62 for (var i = this._innerElement.children.length - 1; i >= 0; --i) { | |
| 63 var element = this._innerElement.children[i]; | |
| 64 if (element[WebInspector.StaticViewportControl._insertedAt] < this._ firstActiveIndex || element[WebInspector.StaticViewportControl._insertedAt] > th is._lastActiveIndex) | |
| 65 element.remove(); | |
| 66 } | |
| 67 | |
| 68 for (var i = this._firstActiveIndex; i <= this._lastActiveIndex; ++i) | |
| 69 this._insertElement(i); | |
| 70 }, | |
| 71 | |
| 72 /** | |
| 73 * @param {number} index | |
| 74 */ | |
| 75 _insertElement: function(index) | |
| 76 { | |
| 77 var element = this._provider.itemElement(index); | |
| 78 if (!element || element.parentElement) | |
| 79 return; | |
| 80 | |
| 81 element.style.position = "absolute"; | |
| 82 element.style.top = (this._cumulativeHeights[index - 1] || 0) + "px"; | |
| 83 element.style.left = "0"; | |
| 84 element.style.right = "0"; | |
| 85 element[WebInspector.StaticViewportControl._insertedAt] = index; | |
| 86 this._innerElement.appendChild(element); | |
| 87 }, | |
| 88 | |
| 89 /** | |
| 90 * @return {number} | |
| 91 */ | |
| 92 firstVisibleIndex: function() | |
| 93 { | |
| 94 var firstVisibleIndex = Math.max(Array.prototype.lowerBound.call(this._c umulativeHeights, this.element.scrollTop + 1), 0); | |
| 95 return Math.max(firstVisibleIndex, this._firstActiveIndex); | |
| 96 }, | |
| 97 | |
| 98 /** | |
| 99 * @return {number} | |
| 100 */ | |
| 101 lastVisibleIndex: function() | |
| 102 { | |
| 103 var lastVisibleIndex = Math.max(Array.prototype.lowerBound.call(this._cu mulativeHeights, this.element.scrollTop + this._visibleHeight()), 0); | |
| 104 return Math.min(lastVisibleIndex, this._lastActiveIndex); | |
| 105 }, | |
| 106 | |
| 107 /** | |
| 108 * @param {number} index | |
| 109 * @param {boolean=} makeLast | |
| 110 */ | |
| 111 scrollItemIntoView: function(index, makeLast) | |
| 112 { | |
| 113 var firstVisibleIndex = this.firstVisibleIndex(); | |
| 114 var lastVisibleIndex = this.lastVisibleIndex(); | |
| 115 if (index > firstVisibleIndex && index < lastVisibleIndex) | |
| 116 return; | |
| 117 if (makeLast) | |
| 118 this.forceScrollItemToBeLast(index); | |
| 119 else if (index <= firstVisibleIndex) | |
| 120 this.forceScrollItemToBeFirst(index); | |
| 121 else if (index >= lastVisibleIndex) | |
| 122 this.forceScrollItemToBeLast(index); | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * @param {number} index | |
| 127 */ | |
| 128 forceScrollItemToBeFirst: function(index) | |
| 129 { | |
| 130 this.element.scrollTop = index > 0 ? this._cumulativeHeights[index - 1] : 0; | |
| 131 this._update(); | |
| 132 }, | |
| 133 | |
| 134 /** | |
| 135 * @param {number} index | |
| 136 */ | |
| 137 forceScrollItemToBeLast: function(index) | |
| 138 { | |
| 139 this.element.scrollTop = this._cumulativeHeights[index] - this._visibleH eight(); | |
| 140 this._update(); | |
| 141 }, | |
| 142 | |
| 143 /** | |
| 144 * @return {number} | |
| 145 */ | |
| 146 _visibleHeight: function() | |
| 147 { | |
| 148 return this.element.offsetHeight; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 /** | |
| 153 * @interface | |
| 154 */ | |
| 155 WebInspector.StaticViewportControl.Provider = function() | |
| 156 { | |
| 157 } | |
| 158 | |
| 159 WebInspector.StaticViewportControl.Provider.prototype = { | |
| 160 /** | |
| 161 * @param {number} index | |
| 162 * @return {number} | |
| 163 */ | |
| 164 fastHeight: function(index) { return 0; }, | |
|
dgozman
2016/09/28 20:56:18
itemHeight?
einbinder
2016/09/29 01:04:20
fastItemHeight
| |
| 165 | |
| 166 /** | |
| 167 * @return {number} | |
| 168 */ | |
| 169 itemCount: function() { return 0; }, | |
| 170 | |
| 171 /** | |
| 172 * @param {number} index | |
| 173 * @return {?Element} | |
| 174 */ | |
| 175 itemElement: function(index) { return null; } | |
| 176 } | |
| OLD | NEW |