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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 } | 627 } |
630 | 628 |
631 // For each column in the current row. | 629 // For each column in the current row. |
632 for (var col = 0; col < colCount; col++, tile++) { | 630 for (var col = 0; col < colCount; col++, tile++) { |
633 var tileCell; | 631 var tileCell; |
634 var tileElement; | 632 var tileElement; |
635 if (tileRowTiles[col]) { | 633 if (tileRowTiles[col]) { |
636 tileCell = tileRowTiles[col]; | 634 tileCell = tileRowTiles[col]; |
637 } else { | 635 } else { |
638 var span = cr.doc.createElement('span'); | 636 var span = cr.doc.createElement('span'); |
639 tileCell = new TileCell(span, this.config); | 637 tileCell = new TileCell(span); |
640 } | 638 } |
641 | 639 |
642 // Render Tiles. | 640 // Render Tiles. |
643 if (tile < tileCount) { | 641 if (tile < tileCount) { |
644 tileCell.classList.remove('filler'); | 642 tileCell.classList.remove('filler'); |
645 tileElement = tiles[tile]; | 643 tileElement = tiles[tile]; |
646 if (!tileCell.tile) | 644 if (!tileCell.tile) |
647 tileCell.appendChild(tileElement); | 645 tileCell.appendChild(tileElement); |
648 else if (tileElement != tileCell.tile) | 646 else if (tileElement != tileCell.tile) |
649 tileCell.replaceChild(tileElement, tileCell.tile); | 647 tileCell.replaceChild(tileElement, tileCell.tile); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
844 */ | 842 */ |
845 animateTileRestoration: function(index, newDataList) { | 843 animateTileRestoration: function(index, newDataList) { |
846 var tiles = this.tiles_; | 844 var tiles = this.tiles_; |
847 var tileCount = tiles.length; | 845 var tileCount = tiles.length; |
848 | 846 |
849 var tileCells = this.querySelectorAll('.tile-cell'); | 847 var tileCells = this.querySelectorAll('.tile-cell'); |
850 var extraTileIndex = Math.min(tileCount, this.config.maxTileCount - 1); | 848 var extraTileIndex = Math.min(tileCount, this.config.maxTileCount - 1); |
851 var extraCell = tileCells[extraTileIndex]; | 849 var extraCell = tileCells[extraTileIndex]; |
852 var extraTileData = newDataList[extraTileIndex + 1]; | 850 var extraTileData = newDataList[extraTileIndex + 1]; |
853 | 851 |
852 var restoredData = newDataList[index]; | |
853 var tileBeingRestored = createTile(this, restoredData); | |
854 | |
855 // If the tile being restored/added is on the last position, and the | |
856 // tile before that is on the last column of a particular row, then we | |
857 // need to handle it separately once there will be no cell to put the | |
858 // restored tile temporarily and there will be no rearrangement of tiles. | |
Dan Beam
2012/12/14 18:28:13
I appreciate this comment but I'm still not quite
pedro (no code reviews)
2012/12/14 18:54:10
I've rewritten this comment, let me know if it is
| |
859 if (!tileCells[index]) { | |
860 this.appendTile(tileBeingRestored); | |
861 tileBeingRestored.classList.add('target-tile'); | |
862 fadeTile(tileBeingRestored, true); | |
863 return; | |
864 } | |
865 | |
854 var repositioningStartIndex = index; | 866 var repositioningStartIndex = index; |
855 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); | 867 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); |
856 | 868 |
857 this.initializeRepositioningAnimation_(index, repositioningEndIndex); | 869 this.initializeRepositioningAnimation_(index, repositioningEndIndex); |
858 | 870 |
859 var restoredData = newDataList[index]; | |
860 var tileBeingRestored = createTile(this, restoredData); | |
861 tileCells[index].appendChild(tileBeingRestored); | 871 tileCells[index].appendChild(tileBeingRestored); |
862 | 872 |
863 if (this.config.scrollable) | 873 if (this.config.scrollable) |
864 this.content_.scrollTop = tileCells[index].offsetTop; | 874 this.content_.scrollTop = tileCells[index].offsetTop; |
865 | 875 |
866 var extraTile; | 876 var extraTile; |
867 if (extraCell) | 877 if (extraCell) |
868 extraTile = extraCell.tile; | 878 extraTile = extraCell.tile; |
869 | 879 |
870 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, | 880 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 | 1087 * Creates a new tile given a particular data. If there's no data, then |
1078 * a tile filler will be created. | 1088 * a tile filler will be created. |
1079 * @param {TilePage} tilePage A TilePage. | 1089 * @param {TilePage} tilePage A TilePage. |
1080 * @param {Object=} opt_data The data that will be used to create the tile. | 1090 * @param {Object=} opt_data The data that will be used to create the tile. |
1081 * @return {Tile} The new tile. | 1091 * @return {Tile} The new tile. |
1082 */ | 1092 */ |
1083 function createTile(tilePage, opt_data) { | 1093 function createTile(tilePage, opt_data) { |
1084 var tile; | 1094 var tile; |
1085 if (opt_data) { | 1095 if (opt_data) { |
1086 // If there's data, the new tile will be a real one (not a filler). | 1096 // If there's data, the new tile will be a real one (not a filler). |
1087 tile = new tilePage.TileClass(tilePage.config); | 1097 tile = new tilePage.TileClass(opt_data); |
1088 tile.data = opt_data; | |
1089 } else { | 1098 } else { |
1090 // Otherwise, it will be a fake filler tile. | 1099 // Otherwise, it will be a fake filler tile. |
1091 tile = cr.doc.createElement('span'); | 1100 tile = cr.doc.createElement('span'); |
1092 tile.className = 'tile'; | 1101 tile.className = 'tile'; |
1093 } | 1102 } |
1094 return tile; | 1103 return tile; |
1095 } | 1104 } |
1096 | 1105 |
1097 /** | 1106 /** |
1098 * Fades a tile. | 1107 * Fades a tile. |
1099 * @param {Tile} tile A Tile. | 1108 * @param {Tile} tile A Tile. |
1100 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is | 1109 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is |
1101 * false, then the tile is going to fade-out. | 1110 * false, then the tile is going to fade-out. |
1102 */ | 1111 */ |
1103 function fadeTile(tile, isFadeIn) { | 1112 function fadeTile(tile, isFadeIn) { |
1104 var className = 'animate-hide-tile'; | 1113 var className = 'animate-hide-tile'; |
1105 tile.classList.add(className); | 1114 tile.classList.add(className); |
1106 if (isFadeIn) { | 1115 if (isFadeIn) { |
1107 // Forces a reflow to ensure that the fade-out animation will work. | 1116 // Forces a reflow to ensure that the fade-out animation will work. |
1108 tile.scrollTop; | 1117 tile.scrollTop; |
1109 tile.classList.remove(className); | 1118 tile.classList.remove(className); |
1110 } | 1119 } |
1111 } | 1120 } |
1112 | 1121 |
1113 return { | 1122 return { |
1114 Tile: Tile, | 1123 Tile: Tile, |
1115 TilePage: TilePage, | 1124 TilePage: TilePage, |
1116 }; | 1125 }; |
1117 }); | 1126 }); |
OLD | NEW |