2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @param {!WebInspector.ViewportControl.Provider} provider
35 WebInspector.ViewportControl = function(provider)
37 this.element = document.createElement("div");
38 this.element.className = "fill";
39 this.element.style.overflow = "auto";
40 this._topGapElement = this.element.createChild("div");
41 this._contentElement = this.element.createChild("div");
42 this._bottomGapElement = this.element.createChild("div");
44 this._provider = provider;
45 this.element.addEventListener("scroll", this._onScroll.bind(this), false);
46 this._firstVisibleIndex = 0;
47 this._lastVisibleIndex = -1;
53 WebInspector.ViewportControl.Provider = function()
57 WebInspector.ViewportControl.Provider.prototype = {
61 itemCount: function() { return 0; },
64 * @param {number} index
67 itemElement: function(index) { return null; }
70 WebInspector.ViewportControl.prototype = {
74 contentElement: function()
76 return this._contentElement;
81 if (!this.element.clientHeight)
82 return; // Do nothing for invisible controls.
85 this._contentElement.style.setProperty("height", "100000px");
86 this._contentElement.removeChildren();
87 var itemCount = this._provider.itemCount();
89 this._firstVisibleIndex = -1;
90 this._lastVisibleIndex = -1;
94 if (!this._rowHeight) {
95 var firstElement = this._provider.itemElement(0);
96 this._rowHeight = firstElement.measurePreferredSize(this._contentElement).height;
99 var visibleFrom = this.element.scrollTop;
100 var visibleTo = visibleFrom + this.element.clientHeight;
102 this._firstVisibleIndex = Math.floor(visibleFrom / this._rowHeight);
103 this._lastVisibleIndex = Math.min(Math.ceil(visibleTo / this._rowHeight), itemCount) - 1;
105 this._topGapElement.style.height = (this._rowHeight * this._firstVisibleIndex) + "px";
106 this._bottomGapElement.style.height = (this._rowHeight * (itemCount - this._lastVisibleIndex - 1)) + "px";
108 for (var i = this._firstVisibleIndex; i <= this._lastVisibleIndex; ++i)
109 this._contentElement.appendChild(this._provider.itemElement(i));
110 // Release scroller protection.
111 this._contentElement.style.removeProperty("height");
115 * @param {?Event} event
117 _onScroll: function(event)
125 rowsPerViewport: function()
127 return Math.floor(this.element.clientHeight / this._rowHeight);
133 firstVisibleIndex: function()
135 return this._firstVisibleIndex;
141 lastVisibleIndex: function()
143 return this._lastVisibleIndex;
149 renderedElementAt: function(index)
151 if (index < this._firstVisibleIndex)
153 if (index > this._lastVisibleIndex)
155 return this._contentElement.childNodes[index - this._firstVisibleIndex];
159 * @param {number} index
160 * @param {boolean=} makeLast
162 scrollItemIntoView: function(index, makeLast)
164 if (index > this._firstVisibleIndex && index < this._lastVisibleIndex)
168 this.element.scrollTop = this._rowHeight * (index + 1) - this.element.clientHeight;
170 this.element.scrollTop = this._rowHeight * index;