| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 * @extends {DataGrid.DataGrid<!NODE_TYPE>} | 6 * @extends {DataGrid.DataGrid<!NODE_TYPE>} |
| 7 * @template NODE_TYPE | 7 * @template NODE_TYPE |
| 8 */ | 8 */ |
| 9 DataGrid.ViewportDataGrid = class extends DataGrid.DataGrid { | 9 DataGrid.ViewportDataGrid = class extends DataGrid.DataGrid { |
| 10 /** | 10 /** |
| 11 * @param {!Array.<!DataGrid.DataGrid.ColumnDescriptor>} columnsArray | 11 * @param {!Array.<!DataGrid.DataGrid.ColumnDescriptor>} columnsArray |
| 12 * @param {function(!NODE_TYPE, string, string, string)=} editCallback | 12 * @param {function(!NODE_TYPE, string, string, string)=} editCallback |
| 13 * @param {function(!NODE_TYPE)=} deleteCallback | 13 * @param {function(!NODE_TYPE)=} deleteCallback |
| 14 * @param {function()=} refreshCallback | 14 * @param {function()=} refreshCallback |
| 15 */ | 15 */ |
| 16 constructor(columnsArray, editCallback, deleteCallback, refreshCallback) { | 16 constructor(columnsArray, editCallback, deleteCallback, refreshCallback) { |
| 17 super(columnsArray, editCallback, deleteCallback, refreshCallback); | 17 super(columnsArray, editCallback, deleteCallback, refreshCallback); |
| 18 | 18 |
| 19 this._onScrollBound = this._onScroll.bind(this); | 19 this._onScrollBound = this._onScroll.bind(this); |
| 20 this._scrollContainer.addEventListener('scroll', this._onScrollBound, true); | 20 this._scrollContainer.addEventListener('scroll', this._onScrollBound, true); |
| 21 | 21 |
| 22 /** @type {!Array.<!DataGrid.ViewportDataGridNode>} */ | 22 /** @type {!Array.<!DataGrid.ViewportDataGridNode>} */ |
| 23 this._visibleNodes = []; | 23 this._visibleNodes = []; |
| 24 this._inline = false; | 24 this._inline = false; |
| 25 | 25 |
| 26 this._stickToBottom = false; | 26 this._stickToBottom = false; |
| 27 this._updateIsFromUser = false; | 27 this._updateIsFromUser = false; |
| 28 this._atBottom = true; | |
| 29 this._lastScrollTop = 0; | 28 this._lastScrollTop = 0; |
| 30 this._firstVisibleIsStriped = false; | 29 this._firstVisibleIsStriped = false; |
| 31 | 30 |
| 32 this.setRootNode(new DataGrid.ViewportDataGridNode()); | 31 this.setRootNode(new DataGrid.ViewportDataGridNode()); |
| 33 } | 32 } |
| 34 | 33 |
| 35 /** | 34 /** |
| 36 * @param {!Element} scrollContainer | 35 * @param {!Element} scrollContainer |
| 37 */ | 36 */ |
| 38 setScrollContainer(scrollContainer) { | 37 setScrollContainer(scrollContainer) { |
| 39 this._scrollContainer.removeEventListener('scroll', this._onScrollBound, tru
e); | 38 this._scrollContainer.removeEventListener('scroll', this._onScrollBound, tru
e); |
| 40 this._scrollContainer = scrollContainer; | 39 this._scrollContainer = scrollContainer; |
| 41 this._scrollContainer.addEventListener('scroll', this._onScrollBound, true); | 40 this._scrollContainer.addEventListener('scroll', this._onScrollBound, true); |
| 42 } | 41 } |
| 43 | 42 |
| 44 /** | 43 /** |
| 45 * @override | 44 * @override |
| 46 */ | 45 */ |
| 47 onResize() { | 46 onResize() { |
| 48 if (this._stickToBottom && this._atBottom) | 47 if (this._stickToBottom) |
| 49 this._scrollContainer.scrollTop = this._scrollContainer.scrollHeight - thi
s._scrollContainer.clientHeight; | 48 this._scrollContainer.scrollTop = this._scrollContainer.scrollHeight - thi
s._scrollContainer.clientHeight; |
| 50 this.scheduleUpdate(); | 49 this.scheduleUpdate(); |
| 51 super.onResize(); | 50 super.onResize(); |
| 52 } | 51 } |
| 53 | 52 |
| 54 /** | 53 /** |
| 55 * @param {boolean} stick | 54 * @param {boolean} stick |
| 56 */ | 55 */ |
| 57 setStickToBottom(stick) { | 56 setStickToBottom(stick) { |
| 58 this._stickToBottom = stick; | 57 this._stickToBottom = stick; |
| 59 } | 58 } |
| 60 | 59 |
| 61 /** | 60 /** |
| 62 * @param {?Event} event | 61 * @param {?Event} event |
| 63 */ | 62 */ |
| 64 _onScroll(event) { | 63 _onScroll(event) { |
| 65 this._atBottom = this._scrollContainer.isScrolledToBottom(); | 64 this._stickToBottom = this._scrollContainer.isScrolledToBottom(); |
| 66 if (this._lastScrollTop !== this._scrollContainer.scrollTop) | 65 if (this._lastScrollTop !== this._scrollContainer.scrollTop) |
| 67 this.scheduleUpdate(true); | 66 this.scheduleUpdate(true); |
| 68 } | 67 } |
| 69 | 68 |
| 70 /** | 69 /** |
| 71 * @protected | 70 * @protected |
| 72 */ | 71 */ |
| 73 scheduleUpdateStructure() { | 72 scheduleUpdateStructure() { |
| 74 this.scheduleUpdate(); | 73 this.scheduleUpdate(); |
| 75 } | 74 } |
| 76 | 75 |
| 77 /** | 76 /** |
| 78 * @param {boolean=} isFromUser | 77 * @param {boolean=} isFromUser |
| 79 */ | 78 */ |
| 80 scheduleUpdate(isFromUser) { | 79 scheduleUpdate(isFromUser) { |
| 80 if (this._stickToBottom && isFromUser) |
| 81 this._stickToBottom = this._scrollContainer.isScrolledToBottom(); |
| 81 this._updateIsFromUser = this._updateIsFromUser || isFromUser; | 82 this._updateIsFromUser = this._updateIsFromUser || isFromUser; |
| 82 if (this._updateAnimationFrameId) | 83 if (this._updateAnimationFrameId) |
| 83 return; | 84 return; |
| 84 this._updateAnimationFrameId = this.element.window().requestAnimationFrame(t
his._update.bind(this)); | 85 this._updateAnimationFrameId = this.element.window().requestAnimationFrame(t
his._update.bind(this)); |
| 85 } | 86 } |
| 86 | 87 |
| 87 // TODO(allada) This should be fixed to never be needed. It is needed right no
w for network because removing | 88 // TODO(allada) This should be fixed to never be needed. It is needed right no
w for network because removing |
| 88 // elements happens followed by a scheduleRefresh() which causes white space t
o be visible, but the waterfall | 89 // elements happens followed by a scheduleRefresh() which causes white space t
o be visible, but the waterfall |
| 89 // updates instantly. | 90 // updates instantly. |
| 90 updateInstantly() { | 91 updateInstantly() { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 _update() { | 151 _update() { |
| 151 if (this._updateAnimationFrameId) { | 152 if (this._updateAnimationFrameId) { |
| 152 this.element.window().cancelAnimationFrame(this._updateAnimationFrameId); | 153 this.element.window().cancelAnimationFrame(this._updateAnimationFrameId); |
| 153 delete this._updateAnimationFrameId; | 154 delete this._updateAnimationFrameId; |
| 154 } | 155 } |
| 155 | 156 |
| 156 var clientHeight = this._scrollContainer.clientHeight; | 157 var clientHeight = this._scrollContainer.clientHeight; |
| 157 var scrollTop = this._scrollContainer.scrollTop; | 158 var scrollTop = this._scrollContainer.scrollTop; |
| 158 var currentScrollTop = scrollTop; | 159 var currentScrollTop = scrollTop; |
| 159 var maxScrollTop = Math.max(0, this._contentHeight() - clientHeight); | 160 var maxScrollTop = Math.max(0, this._contentHeight() - clientHeight); |
| 160 if (!this._updateIsFromUser && this._stickToBottom && this._atBottom) | 161 if (!this._updateIsFromUser && this._stickToBottom) |
| 161 scrollTop = maxScrollTop; | 162 scrollTop = maxScrollTop; |
| 162 this._updateIsFromUser = false; | 163 this._updateIsFromUser = false; |
| 163 scrollTop = Math.min(maxScrollTop, scrollTop); | 164 scrollTop = Math.min(maxScrollTop, scrollTop); |
| 164 this._atBottom = scrollTop === maxScrollTop; | |
| 165 | 165 |
| 166 var viewportState = this._calculateVisibleNodes(clientHeight, scrollTop); | 166 var viewportState = this._calculateVisibleNodes(clientHeight, scrollTop); |
| 167 var visibleNodes = viewportState.visibleNodes; | 167 var visibleNodes = viewportState.visibleNodes; |
| 168 var visibleNodesSet = new Set(visibleNodes); | 168 var visibleNodesSet = new Set(visibleNodes); |
| 169 | 169 |
| 170 for (var i = 0; i < this._visibleNodes.length; ++i) { | 170 for (var i = 0; i < this._visibleNodes.length; ++i) { |
| 171 var oldNode = this._visibleNodes[i]; | 171 var oldNode = this._visibleNodes[i]; |
| 172 if (!visibleNodesSet.has(oldNode) && oldNode.attached()) { | 172 if (!visibleNodesSet.has(oldNode) && oldNode.attached()) { |
| 173 var element = oldNode.existingElement(); | 173 var element = oldNode.existingElement(); |
| 174 element.remove(); | 174 element.remove(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 if (index === -1) | 221 if (index === -1) |
| 222 return; | 222 return; |
| 223 var fromY = 0; | 223 var fromY = 0; |
| 224 for (var i = 0; i < index; ++i) | 224 for (var i = 0; i < index; ++i) |
| 225 fromY += nodes[i].nodeSelfHeight(); | 225 fromY += nodes[i].nodeSelfHeight(); |
| 226 var toY = fromY + node.nodeSelfHeight(); | 226 var toY = fromY + node.nodeSelfHeight(); |
| 227 | 227 |
| 228 var scrollTop = this._scrollContainer.scrollTop; | 228 var scrollTop = this._scrollContainer.scrollTop; |
| 229 if (scrollTop > fromY) { | 229 if (scrollTop > fromY) { |
| 230 scrollTop = fromY; | 230 scrollTop = fromY; |
| 231 this._atBottom = false; | 231 this._stickToBottom = false; |
| 232 } else if (scrollTop + this._scrollContainer.offsetHeight < toY) { | 232 } else if (scrollTop + this._scrollContainer.offsetHeight < toY) { |
| 233 scrollTop = toY - this._scrollContainer.offsetHeight; | 233 scrollTop = toY - this._scrollContainer.offsetHeight; |
| 234 } | 234 } |
| 235 this._scrollContainer.scrollTop = scrollTop; | 235 this._scrollContainer.scrollTop = scrollTop; |
| 236 } | 236 } |
| 237 }; | 237 }; |
| 238 | 238 |
| 239 DataGrid.ViewportDataGrid.Events = { | 239 DataGrid.ViewportDataGrid.Events = { |
| 240 ViewportCalculated: Symbol('ViewportCalculated') | 240 ViewportCalculated: Symbol('ViewportCalculated') |
| 241 }; | 241 }; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 this.existingElement().classList.remove('expanded'); | 413 this.existingElement().classList.remove('expanded'); |
| 414 this.dataGrid.scheduleUpdateStructure(); | 414 this.dataGrid.scheduleUpdateStructure(); |
| 415 } | 415 } |
| 416 | 416 |
| 417 /** | 417 /** |
| 418 * @override | 418 * @override |
| 419 */ | 419 */ |
| 420 expand() { | 420 expand() { |
| 421 if (this._expanded) | 421 if (this._expanded) |
| 422 return; | 422 return; |
| 423 this.dataGrid._stickToBottom = false; |
| 423 this.clearFlatNodes(); | 424 this.clearFlatNodes(); |
| 424 super.expand(); | 425 super.expand(); |
| 425 this.dataGrid.scheduleUpdateStructure(); | 426 this.dataGrid.scheduleUpdateStructure(); |
| 426 } | 427 } |
| 427 | 428 |
| 428 /** | 429 /** |
| 429 * @protected | 430 * @protected |
| 430 * @return {boolean} | 431 * @return {boolean} |
| 431 */ | 432 */ |
| 432 attached() { | 433 attached() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 454 | 455 |
| 455 /** | 456 /** |
| 456 * @override | 457 * @override |
| 457 * @param {number} index | 458 * @param {number} index |
| 458 */ | 459 */ |
| 459 recalculateSiblings(index) { | 460 recalculateSiblings(index) { |
| 460 this.clearFlatNodes(); | 461 this.clearFlatNodes(); |
| 461 super.recalculateSiblings(index); | 462 super.recalculateSiblings(index); |
| 462 } | 463 } |
| 463 }; | 464 }; |
| OLD | NEW |