OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('ntp', function() { | 5 cr.define('ntp', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * The maximum gap from the edge of the scrolling area which will display | 9 * The maximum gap from the edge of the scrolling area which will display |
10 * the shadow with transparency. After this point the shadow will become | 10 * the shadow with transparency. After this point the shadow will become |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 * place and to animate them individually. Each TileCell is associated to | 119 * place and to animate them individually. Each TileCell is associated to |
120 * one Tile at a time (or none if it is a filler object), and that association | 120 * one Tile at a time (or none if it is a filler object), and that association |
121 * might change when the grid is resized. When that happens, the grid is | 121 * might change when the grid is resized. When that happens, the grid is |
122 * updated and the Tiles are moved to the proper TileCell. We cannot move the | 122 * updated and the Tiles are moved to the proper TileCell. We cannot move the |
123 * the TileCell itself during the resize because this transition is animated | 123 * the TileCell itself during the resize because this transition is animated |
124 * with CSS and there's no way to stop CSS animations, and we really want to | 124 * with CSS and there's no way to stop CSS animations, and we really want to |
125 * animate with CSS to take advantage of hardware acceleration. | 125 * animate with CSS to take advantage of hardware acceleration. |
126 * @constructor | 126 * @constructor |
127 * @extends {HTMLDivElement} | 127 * @extends {HTMLDivElement} |
128 * @param {HTMLElement} tile Tile element that will be associated to the cell. | 128 * @param {HTMLElement} tile Tile element that will be associated to the cell. |
129 * @param {Object} config TilePage configuration object. | |
130 */ | 129 */ |
131 function TileCell(tile, config) { | 130 function TileCell(tile) { |
132 var tileCell = cr.doc.createElement('div'); | 131 var tileCell = cr.doc.createElement('div'); |
133 tileCell.__proto__ = TileCell.prototype; | 132 tileCell.__proto__ = TileCell.prototype; |
134 tileCell.initialize(tile, config); | 133 tileCell.initialize(tile); |
135 | 134 |
136 return tileCell; | 135 return tileCell; |
137 } | 136 } |
138 | 137 |
139 TileCell.prototype = { | 138 TileCell.prototype = { |
140 __proto__: HTMLDivElement.prototype, | 139 __proto__: HTMLDivElement.prototype, |
141 | 140 |
142 /** | 141 /** |
143 * Initializes a TileCell. | 142 * Initializes a TileCell. |
144 * @param {Tile} tile The Tile that will be assigned to this TileCell. | 143 * @param {Tile} tile The Tile that will be assigned to this TileCell. |
145 * @param {Object} config TilePage configuration object. | |
146 */ | 144 */ |
147 initialize: function(tile, config) { | 145 initialize: function(tile) { |
148 this.className = 'tile-cell'; | 146 this.className = 'tile-cell'; |
149 this.assign(tile); | 147 this.assign(tile); |
150 }, | 148 }, |
151 | 149 |
152 /** | 150 /** |
153 * The index of the TileCell. | 151 * The index of the TileCell. |
154 * @type {number} | 152 * @type {number} |
155 */ | 153 */ |
156 get index() { | 154 get index() { |
157 return Array.prototype.indexOf.call(this.tilePage.tiles_, | 155 return Array.prototype.indexOf.call(this.tilePage.tiles_, |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 this.tiles_.splice(index, 0, tile); | 442 this.tiles_.splice(index, 0, tile); |
445 this.fireAddedEvent(tile, index, false); | 443 this.fireAddedEvent(tile, index, false); |
446 this.renderGrid(); | 444 this.renderGrid(); |
447 }, | 445 }, |
448 | 446 |
449 /** | 447 /** |
450 * Create a blank tile. | 448 * Create a blank tile. |
451 * @protected | 449 * @protected |
452 */ | 450 */ |
453 createTile_: function() { | 451 createTile_: function() { |
454 return new this.TileClass(this.config); | 452 return new this.TileClass(); |
455 }, | 453 }, |
456 | 454 |
457 /** | 455 /** |
458 * Create blank tiles. | 456 * Create blank tiles. |
459 * @param {number} count The desired number of Tiles to be created. If this | 457 * @param {number} count The desired number of Tiles to be created. If this |
460 * value the maximum value defined in |config.maxTileCount|, the maximum | 458 * value the maximum value defined in |config.maxTileCount|, the maximum |
461 * value will be used instead. | 459 * value will be used instead. |
462 * @protected | 460 * @protected |
463 */ | 461 */ |
464 createTiles_: function(count) { | 462 createTiles_: function(count) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
589 * Renders the tile grid, and the individual tiles. Rendering the grid | 587 * Renders the tile grid, and the individual tiles. Rendering the grid |
590 * consists of adding/removing tile rows and tile cells according to the | 588 * consists of adding/removing tile rows and tile cells according to the |
591 * specified size (defined by the number of columns in the grid). While | 589 * specified size (defined by the number of columns in the grid). While |
592 * rendering the grid, the tiles are rendered in order in their respective | 590 * rendering the grid, the tiles are rendered in order in their respective |
593 * cells and tile fillers are rendered when needed. This method sets the | 591 * cells and tile fillers are rendered when needed. This method sets the |
594 * private properties colCount_ and rowCount_. | 592 * private properties colCount_ and rowCount_. |
595 * | 593 * |
596 * This method should be called every time the contents of the grid changes, | 594 * This method should be called every time the contents of the grid changes, |
597 * that is, when the number, contents or order of the tiles has changed. | 595 * that is, when the number, contents or order of the tiles has changed. |
598 * @param {number=} opt_colCount The number of columns. | 596 * @param {number=} opt_colCount The number of columns. |
597 * @param {boolean=} opt_doNotScroll Do not fire onScroll event. | |
599 * @protected | 598 * @protected |
600 */ | 599 */ |
601 renderGrid: function(opt_colCount) { | 600 renderGrid: function(opt_colCount, opt_doNotScroll) { |
602 var colCount = opt_colCount || this.colCount_; | 601 var colCount = opt_colCount || this.colCount_; |
603 | 602 |
604 var tileGridContent = this.tileGridContent_; | 603 var tileGridContent = this.tileGridContent_; |
605 var tiles = this.tiles_; | 604 var tiles = this.tiles_; |
606 var tileCount = tiles.length; | 605 var tileCount = tiles.length; |
607 | 606 |
608 var rowCount = Math.ceil(tileCount / colCount); | 607 var rowCount = Math.ceil(tileCount / colCount); |
609 var tileRows = tileGridContent.getElementsByClassName('tile-row'); | 608 var tileRows = tileGridContent.getElementsByClassName('tile-row'); |
610 | 609 |
611 for (var tile = 0, row = 0; row < rowCount; row++) { | 610 for (var tile = 0, row = 0; row < rowCount; row++) { |
(...skipping 17 matching lines...) Expand all Loading... | |
629 } | 628 } |
630 | 629 |
631 // For each column in the current row. | 630 // For each column in the current row. |
632 for (var col = 0; col < colCount; col++, tile++) { | 631 for (var col = 0; col < colCount; col++, tile++) { |
633 var tileCell; | 632 var tileCell; |
634 var tileElement; | 633 var tileElement; |
635 if (tileRowTiles[col]) { | 634 if (tileRowTiles[col]) { |
636 tileCell = tileRowTiles[col]; | 635 tileCell = tileRowTiles[col]; |
637 } else { | 636 } else { |
638 var span = cr.doc.createElement('span'); | 637 var span = cr.doc.createElement('span'); |
639 tileCell = new TileCell(span, this.config); | 638 tileCell = new TileCell(span); |
640 } | 639 } |
641 | 640 |
642 // Render Tiles. | 641 // Render Tiles. |
643 if (tile < tileCount) { | 642 if (tile < tileCount) { |
644 tileCell.classList.remove('filler'); | 643 tileCell.classList.remove('filler'); |
645 tileElement = tiles[tile]; | 644 tileElement = tiles[tile]; |
646 if (!tileCell.tile) | 645 if (!tileCell.tile) |
647 tileCell.appendChild(tileElement); | 646 tileCell.appendChild(tileElement); |
648 else if (tileElement != tileCell.tile) | 647 else if (tileElement != tileCell.tile) |
649 tileCell.replaceChild(tileElement, tileCell.tile); | 648 tileCell.replaceChild(tileElement, tileCell.tile); |
(...skipping 13 matching lines...) Expand all Loading... | |
663 } | 662 } |
664 | 663 |
665 // Remove excessive tile rows from the tile grid. | 664 // Remove excessive tile rows from the tile grid. |
666 while (tileRows.length > rowCount) { | 665 while (tileRows.length > rowCount) { |
667 tileGridContent.removeChild(tileGridContent.lastElementChild); | 666 tileGridContent.removeChild(tileGridContent.lastElementChild); |
668 } | 667 } |
669 | 668 |
670 this.colCount_ = colCount; | 669 this.colCount_ = colCount; |
671 this.rowCount_ = rowCount; | 670 this.rowCount_ = rowCount; |
672 | 671 |
673 this.onScroll(); | 672 if (!opt_doNotScroll) |
673 this.onScroll(); | |
674 }, | 674 }, |
675 | 675 |
676 // layout | 676 // layout |
677 // ------------------------------------------------------------------------- | 677 // ------------------------------------------------------------------------- |
678 | 678 |
679 /** | 679 /** |
680 * Calculates the layout of the tile page according to the current Bottom | 680 * Calculates the layout of the tile page according to the current Bottom |
681 * Panel's size. This method will resize the containers of the tile page, | 681 * Panel's size. This method will resize the containers of the tile page, |
682 * and re-render the grid when its dimension changes (number of columns or | 682 * and re-render the grid when its dimension changes (number of columns or |
683 * visible rows changes). This method also sets the private properties | 683 * visible rows changes). This method also sets the private properties |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
839 | 839 |
840 /** | 840 /** |
841 * Animates a tile restoration. | 841 * Animates a tile restoration. |
842 * @param {number} index The index of the tile to be restored. | 842 * @param {number} index The index of the tile to be restored. |
843 * @param {Object} newDataList The new data list. | 843 * @param {Object} newDataList The new data list. |
844 */ | 844 */ |
845 animateTileRestoration: function(index, newDataList) { | 845 animateTileRestoration: function(index, newDataList) { |
846 var tiles = this.tiles_; | 846 var tiles = this.tiles_; |
847 var tileCount = tiles.length; | 847 var tileCount = tiles.length; |
848 | 848 |
849 var tileCells = this.querySelectorAll('.tile-cell'); | 849 var tileCells = this.querySelectorAll('.tile-cell'); |
Dan Beam
2012/12/14 22:49:46
FYI: if you use getElementsByClassName() you won't
pedro (no code reviews)
2012/12/14 23:42:57
Done. Thanks for the explanation. I wonder why que
Dan Beam
2012/12/17 19:08:56
This is by design in the new API so mutations even
| |
850 | |
851 // If the desired position is outside the grid, then the grid must be | |
852 // expanded so there will be a cell in the desired position. | |
853 if (index >= tileCells.length) { | |
854 // Create a dummy empty tile in order to expand the grid. | |
855 this.tiles_.push(createTile(this)); | |
856 this.renderGrid(null, true); | |
857 tileCells = this.querySelectorAll('.tile-cell'); | |
858 } | |
859 | |
850 var extraTileIndex = Math.min(tileCount, this.config.maxTileCount - 1); | 860 var extraTileIndex = Math.min(tileCount, this.config.maxTileCount - 1); |
851 var extraCell = tileCells[extraTileIndex]; | 861 var extraCell = tileCells[extraTileIndex]; |
852 var extraTileData = newDataList[extraTileIndex + 1]; | 862 var extraTileData = newDataList[extraTileIndex + 1]; |
853 | 863 |
854 var repositioningStartIndex = index; | 864 var repositioningStartIndex = index; |
855 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); | 865 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); |
856 | 866 |
857 this.initializeRepositioningAnimation_(index, repositioningEndIndex); | 867 this.initializeRepositioningAnimation_(index, repositioningEndIndex); |
858 | 868 |
859 var restoredData = newDataList[index]; | 869 var restoredData = newDataList[index]; |
860 var tileBeingRestored = createTile(this, restoredData); | 870 var tileBeingRestored = createTile(this, restoredData); |
871 | |
872 // Temporarily assume the |index| cell so the tile can be animated in | |
873 // the right spot. | |
861 tileCells[index].appendChild(tileBeingRestored); | 874 tileCells[index].appendChild(tileBeingRestored); |
862 | 875 |
863 if (this.config.scrollable) | 876 if (this.config.scrollable) |
864 this.content_.scrollTop = tileCells[index].offsetTop; | 877 this.content_.scrollTop = tileCells[index].offsetTop; |
865 | 878 |
866 var extraTile; | 879 var extraTile; |
867 if (extraCell) | 880 if (extraCell) |
868 extraTile = extraCell.tile; | 881 extraTile = extraCell.tile; |
869 | 882 |
870 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, | 883 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1077 * Creates a new tile given a particular data. If there's no data, then | 1090 * Creates a new tile given a particular data. If there's no data, then |
1078 * a tile filler will be created. | 1091 * a tile filler will be created. |
1079 * @param {TilePage} tilePage A TilePage. | 1092 * @param {TilePage} tilePage A TilePage. |
1080 * @param {Object=} opt_data The data that will be used to create the tile. | 1093 * @param {Object=} opt_data The data that will be used to create the tile. |
1081 * @return {Tile} The new tile. | 1094 * @return {Tile} The new tile. |
1082 */ | 1095 */ |
1083 function createTile(tilePage, opt_data) { | 1096 function createTile(tilePage, opt_data) { |
1084 var tile; | 1097 var tile; |
1085 if (opt_data) { | 1098 if (opt_data) { |
1086 // If there's data, the new tile will be a real one (not a filler). | 1099 // If there's data, the new tile will be a real one (not a filler). |
1087 tile = new tilePage.TileClass(tilePage.config); | 1100 tile = new tilePage.TileClass(opt_data); |
1088 tile.data = opt_data; | |
1089 } else { | 1101 } else { |
1090 // Otherwise, it will be a fake filler tile. | 1102 // Otherwise, it will be a fake filler tile. |
1091 tile = cr.doc.createElement('span'); | 1103 tile = cr.doc.createElement('span'); |
1092 tile.className = 'tile'; | 1104 tile.className = 'tile'; |
1093 } | 1105 } |
1094 return tile; | 1106 return tile; |
1095 } | 1107 } |
1096 | 1108 |
1097 /** | 1109 /** |
1098 * Fades a tile. | 1110 * Fades a tile. |
1099 * @param {Tile} tile A Tile. | 1111 * @param {Tile} tile A Tile. |
1100 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is | 1112 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is |
1101 * false, then the tile is going to fade-out. | 1113 * false, then the tile is going to fade-out. |
1102 */ | 1114 */ |
1103 function fadeTile(tile, isFadeIn) { | 1115 function fadeTile(tile, isFadeIn) { |
1104 var className = 'animate-hide-tile'; | 1116 var className = 'animate-hide-tile'; |
1105 tile.classList.add(className); | 1117 tile.classList.add(className); |
1106 if (isFadeIn) { | 1118 if (isFadeIn) { |
1107 // Forces a reflow to ensure that the fade-out animation will work. | 1119 // Forces a reflow to ensure that the fade-out animation will work. |
1108 tile.scrollTop; | 1120 tile.scrollTop; |
1109 tile.classList.remove(className); | 1121 tile.classList.remove(className); |
1110 } | 1122 } |
1111 } | 1123 } |
1112 | 1124 |
1113 return { | 1125 return { |
1114 Tile: Tile, | 1126 Tile: Tile, |
1115 TilePage: TilePage, | 1127 TilePage: TilePage, |
1116 }; | 1128 }; |
1117 }); | 1129 }); |
OLD | NEW |