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 /** | 9 /** |
10 * The maximum gap from the edge of the scrolling area which will display | 10 * The maximum gap from the edge of the scrolling area which will display |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 this.reset(); | 71 this.reset(); |
72 }, | 72 }, |
73 | 73 |
74 /** | 74 /** |
75 * Resets the tile DOM. | 75 * Resets the tile DOM. |
76 */ | 76 */ |
77 reset: function() { | 77 reset: function() { |
78 }, | 78 }, |
79 | 79 |
80 /** | 80 /** |
81 * Update the appearance of this tile according to |data|. | 81 * The data for this Tile. |
82 * @param {Object} data A dictionary of relevant data for the page. | 82 * @param {Object} data A dictionary of relevant data for the page. |
83 */ | 83 */ |
84 setData: function(data) { | 84 set data(data) { |
85 // TODO(pedrosimonetti): Remove data.filler usage everywhere. | 85 // TODO(pedrosimonetti): Remove data.filler usage everywhere. |
86 if (!data || data.filler) { | 86 if (!data || data.filler) { |
87 if (this.data_) | 87 if (this.data_) |
88 this.reset(); | 88 this.reset(); |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 this.data_ = data; | 92 this.data_ = data; |
93 } | 93 }, |
94 }; | 94 }; |
95 | 95 |
96 var TileGetters = { | 96 var TileGetters = { |
97 /** | 97 /** |
98 * The TileCell associated to this Tile. | 98 * The TileCell associated to this Tile. |
99 * @type {TileCell} | 99 * @type {TileCell} |
100 */ | 100 */ |
101 'tileCell': function() { | 101 'tileCell': function() { |
102 return findAncestorByClass(this, 'tile-cell'); | 102 return findAncestorByClass(this, 'tile-cell'); |
103 }, | 103 }, |
104 | 104 |
105 /** | 105 /** |
106 * The index of the Tile. | 106 * The index of the Tile. |
107 * @type {number} | 107 * @type {number} |
108 */ | 108 */ |
109 'index': function() { | 109 'index': function() { |
110 assert(this.tileCell); | 110 assert(this.tileCell); |
111 return this.tileCell.index; | 111 return this.tileCell.index; |
112 }, | 112 }, |
113 | |
114 /** | |
115 * Tile data object. | |
116 * @type {Object} | |
117 */ | |
118 'data': function() { | |
119 return this.data_; | |
120 } | |
121 }; | 113 }; |
122 | 114 |
123 //---------------------------------------------------------------------------- | 115 //---------------------------------------------------------------------------- |
124 // TileCell | 116 // TileCell |
125 //---------------------------------------------------------------------------- | 117 //---------------------------------------------------------------------------- |
126 | 118 |
127 /** | 119 /** |
128 * Creates a new TileCell object. A TileCell represents a cell in the | 120 * Creates a new TileCell object. A TileCell represents a cell in the |
129 * TilePage's grid. A TilePage uses TileCells to position Tiles in the proper | 121 * TilePage's grid. A TilePage uses TileCells to position Tiles in the proper |
130 * place and to animate them individually. Each TileCell is associated to | 122 * place and to animate them individually. Each TileCell is associated to |
(...skipping 28 matching lines...) Expand all Loading... | |
159 this.className = 'tile-cell'; | 151 this.className = 'tile-cell'; |
160 this.assign(tile); | 152 this.assign(tile); |
161 }, | 153 }, |
162 | 154 |
163 /** | 155 /** |
164 * The index of the TileCell. | 156 * The index of the TileCell. |
165 * @type {number} | 157 * @type {number} |
166 */ | 158 */ |
167 get index() { | 159 get index() { |
168 return Array.prototype.indexOf.call(this.tilePage.tiles_, | 160 return Array.prototype.indexOf.call(this.tilePage.tiles_, |
169 this.firstChild); | 161 this.tile); |
170 }, | 162 }, |
171 | 163 |
172 /** | 164 /** |
165 * The Tile associated to this TileCell. | |
166 * @type {Tile} | |
Dan Beam
2012/12/07 05:05:31
nit: you should assert(this.firstElementChild) and
pedro (no code reviews)
2012/12/07 21:54:55
We cannot do that because sometimes we use |if (ce
| |
167 */ | |
168 get tile() { | |
169 return this.firstElementChild; | |
170 }, | |
171 | |
172 /** | |
173 * The TilePage associated to this TileCell. | 173 * The TilePage associated to this TileCell. |
174 * @type {TilePage} | 174 * @type {TilePage} |
175 */ | 175 */ |
176 get tilePage() { | 176 get tilePage() { |
177 return findAncestorByClass(this, 'tile-page'); | 177 return findAncestorByClass(this, 'tile-page'); |
178 }, | 178 }, |
179 | 179 |
180 /** | 180 /** |
181 * Assigns a Tile to the this TileCell. | 181 * Assigns a Tile to the this TileCell. |
182 * @type {TilePage} | 182 * @type {TilePage} |
183 */ | 183 */ |
184 assign: function(tile) { | 184 assign: function(tile) { |
185 if (this.firstChild) | 185 if (this.tile) |
186 this.replaceChild(tile, this.firstChild); | 186 this.replaceChild(tile, this.tile); |
187 else | 187 else |
188 this.appendChild(tile); | 188 this.appendChild(tile); |
189 }, | 189 }, |
190 | 190 |
191 /** | 191 /** |
192 * Called when an app is removed from Chrome. Animates its disappearance. | 192 * Called when an app is removed from Chrome. Animates its disappearance. |
193 * @param {boolean=} opt_animate Whether the animation should be animated. | 193 * @param {boolean=} opt_animate Whether the animation should be animated. |
194 */ | 194 */ |
195 doRemove: function(opt_animate) { | 195 doRemove: function(opt_animate) { |
196 if (opt_animate) | 196 this.tilePage.removeTile(this.tile, false); |
197 this.firstChild.classList.add('removing-tile-contents'); | |
198 else | |
199 this.tilePage.removeTile(this, false); | |
200 }, | 197 }, |
201 }; | 198 }; |
202 | 199 |
203 //---------------------------------------------------------------------------- | 200 //---------------------------------------------------------------------------- |
204 // TilePage | 201 // TilePage |
205 //---------------------------------------------------------------------------- | 202 //---------------------------------------------------------------------------- |
206 | 203 |
207 /** | 204 /** |
208 * Creates a new TilePage object. This object contains tiles and controls | 205 * Creates a new TilePage object. This object contains tiles and controls |
209 * their layout. | 206 * their layout. |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
474 var data = dataList[i]; | 471 var data = dataList[i]; |
475 var tile = tiles[i]; | 472 var tile = tiles[i]; |
476 | 473 |
477 // TODO(pedrosimonetti): What do we do when there's no tile here? | 474 // TODO(pedrosimonetti): What do we do when there's no tile here? |
478 if (!tile) | 475 if (!tile) |
479 return; | 476 return; |
480 | 477 |
481 if (i >= dataList.length) | 478 if (i >= dataList.length) |
482 tile.reset(); | 479 tile.reset(); |
483 else | 480 else |
484 tile.setData(data); | 481 tile.data = data; |
485 } | 482 } |
486 }, | 483 }, |
487 | 484 |
488 /** | 485 /** |
489 * Sets the dataList that will be used to create Tiles. This method will | 486 * Sets the dataList that will be used to create Tiles. This method will |
490 * create/remove necessary/unnecessary tiles, render the grid when the | 487 * create/remove necessary/unnecessary tiles, render the grid when the |
491 * number of tiles has changed, and finally will call |updateTiles_| which | 488 * number of tiles has changed, and finally will call |updateTiles_| which |
492 * will in turn render the individual tiles. | 489 * will in turn render the individual tiles. |
493 * @param {Array} dataList The array of data. | 490 * @param {Array} dataList The array of data. |
494 */ | 491 */ |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
632 tileCell = tileRowTiles[col]; | 629 tileCell = tileRowTiles[col]; |
633 } else { | 630 } else { |
634 var span = cr.doc.createElement('span'); | 631 var span = cr.doc.createElement('span'); |
635 tileCell = new TileCell(span, this.config); | 632 tileCell = new TileCell(span, this.config); |
636 } | 633 } |
637 | 634 |
638 // Render Tiles. | 635 // Render Tiles. |
639 if (tile < tileCount) { | 636 if (tile < tileCount) { |
640 tileCell.classList.remove('filler'); | 637 tileCell.classList.remove('filler'); |
641 tileElement = tiles[tile]; | 638 tileElement = tiles[tile]; |
642 if (!tileCell.firstChild) | 639 if (!tileCell.tile) |
643 tileCell.appendChild(tileElement); | 640 tileCell.appendChild(tileElement); |
644 else if (tileElement != tileCell.firstChild) | 641 else if (tileElement != tileCell.tile) |
645 tileCell.replaceChild(tileElement, tileCell.firstChild); | 642 tileCell.replaceChild(tileElement, tileCell.tile); |
646 } else if (!tileCell.classList.contains('filler')) { | 643 } else if (!tileCell.classList.contains('filler')) { |
647 tileCell.classList.add('filler'); | 644 tileCell.classList.add('filler'); |
648 tileElement = cr.doc.createElement('span'); | 645 tileElement = cr.doc.createElement('span'); |
649 tileElement.className = 'tile'; | 646 tileElement.className = 'tile'; |
650 if (tileCell.firstChild) | 647 if (tileCell.tile) |
651 tileCell.replaceChild(tileElement, tileCell.firstChild); | 648 tileCell.replaceChild(tileElement, tileCell.tile); |
652 else | 649 else |
653 tileCell.appendChild(tileElement); | 650 tileCell.appendChild(tileElement); |
654 } | 651 } |
655 | 652 |
656 if (!tileRowTiles[col]) | 653 if (!tileRowTiles[col]) |
657 tileRow.appendChild(tileCell); | 654 tileRow.appendChild(tileCell); |
658 } | 655 } |
659 } | 656 } |
660 | 657 |
661 // Remove excessive tile rows from the tile grid. | 658 // Remove excessive tile rows from the tile grid. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
850 | 847 |
851 var repositioningStartIndex = index; | 848 var repositioningStartIndex = index; |
852 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); | 849 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); |
853 | 850 |
854 this.initializeRepositioningAnimation_(index, repositioningEndIndex); | 851 this.initializeRepositioningAnimation_(index, repositioningEndIndex); |
855 | 852 |
856 var restoredData = newDataList[index]; | 853 var restoredData = newDataList[index]; |
857 var tileBeingRestored = createTile(this, restoredData); | 854 var tileBeingRestored = createTile(this, restoredData); |
858 tileCells[index].appendChild(tileBeingRestored); | 855 tileCells[index].appendChild(tileBeingRestored); |
859 | 856 |
860 var extraTile = extraCell.firstChild; | 857 var extraTile = extraCell.tile; |
861 | 858 |
862 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, | 859 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, |
863 repositioningStartIndex, repositioningEndIndex, false); | 860 repositioningStartIndex, repositioningEndIndex, false); |
864 | 861 |
865 // Cleans up the animation. | 862 // Cleans up the animation. |
866 var onPositioningTransitionEnd = function(e) { | 863 var onPositioningTransitionEnd = function(e) { |
867 var propertyName = e.propertyName; | 864 var propertyName = e.propertyName; |
868 if (!(propertyName == '-webkit-transform' || | 865 if (!(propertyName == '-webkit-transform' || |
869 propertyName == 'opacity')) { | 866 propertyName == 'opacity')) { |
870 return; | 867 return; |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1068 * a tile filler will be created. | 1065 * a tile filler will be created. |
1069 * @param {TilePage} tilePage A TilePage. | 1066 * @param {TilePage} tilePage A TilePage. |
1070 * @param {Object=} opt_data The data that will be used to create the tile. | 1067 * @param {Object=} opt_data The data that will be used to create the tile. |
1071 * @return {Tile} The new tile. | 1068 * @return {Tile} The new tile. |
1072 */ | 1069 */ |
1073 function createTile(tilePage, opt_data) { | 1070 function createTile(tilePage, opt_data) { |
1074 var tile; | 1071 var tile; |
1075 if (opt_data) { | 1072 if (opt_data) { |
1076 // If there's data, the new tile will be a real one (not a filler). | 1073 // If there's data, the new tile will be a real one (not a filler). |
1077 tile = new tilePage.TileClass(tilePage.config); | 1074 tile = new tilePage.TileClass(tilePage.config); |
1078 tile.setData(opt_data); | 1075 tile.data = opt_data; |
1079 } else { | 1076 } else { |
1080 // Otherwise, it will be a fake filler tile. | 1077 // Otherwise, it will be a fake filler tile. |
1081 tile = cr.doc.createElement('span'); | 1078 tile = cr.doc.createElement('span'); |
1082 tile.className = 'tile'; | 1079 tile.className = 'tile'; |
1083 } | 1080 } |
1084 return tile; | 1081 return tile; |
1085 } | 1082 } |
1086 | 1083 |
1087 /** | 1084 /** |
1088 * Fades a tile. | 1085 * Fades a tile. |
1089 * @param {Tile} tile A Tile. | 1086 * @param {Tile} tile A Tile. |
1090 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is | 1087 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is |
1091 * false, then the tile is going to fade-out. | 1088 * false, then the tile is going to fade-out. |
1092 */ | 1089 */ |
1093 function fadeTile(tile, isFadeIn) { | 1090 function fadeTile(tile, isFadeIn) { |
1094 var className = 'animate-hide-tile'; | 1091 var className = 'animate-hide-tile'; |
1095 tile.classList.add(className); | 1092 tile.classList.add(className); |
1096 if (isFadeIn) { | 1093 if (isFadeIn) { |
1097 // Forces a reflow to ensure that the fade-out animation will work. | 1094 // Forces a reflow to ensure that the fade-out animation will work. |
1098 tile.scrollTop; | 1095 tile.scrollTop; |
1099 tile.classList.remove(className); | 1096 tile.classList.remove(className); |
1100 } | 1097 } |
1101 } | 1098 } |
1102 | 1099 |
1103 return { | 1100 return { |
1104 Tile: Tile, | 1101 Tile: Tile, |
1105 TilePage: TilePage, | 1102 TilePage: TilePage, |
1106 }; | 1103 }; |
1107 }); | 1104 }); |
OLD | NEW |