Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/resources/ntp_search/tile_page.js

Issue 11438009: NTP5: Refactoring appData to use Tile's data implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing Dan's comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
9 /** 8 /**
10 * 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
11 * the shadow with transparency. After this point the shadow will become 10 * the shadow with transparency. After this point the shadow will become
12 * 100% opaque. 11 * 100% opaque.
13 * @type {number} 12 * @type {number}
14 * @const 13 * @const
15 */ 14 */
16 var MAX_SCROLL_SHADOW_GAP = 16; 15 var MAX_SCROLL_SHADOW_GAP = 16;
17 16
18 /** 17 /**
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 this.reset(); 70 this.reset();
72 }, 71 },
73 72
74 /** 73 /**
75 * Resets the tile DOM. 74 * Resets the tile DOM.
76 */ 75 */
77 reset: function() { 76 reset: function() {
78 }, 77 },
79 78
80 /** 79 /**
81 * Update the appearance of this tile according to |data|. 80 * The data for this Tile.
82 * @param {Object} data A dictionary of relevant data for the page. 81 * @param {Object} data A dictionary of relevant data for the page.
83 */ 82 */
84 setData: function(data) { 83 set data(data) {
85 // TODO(pedrosimonetti): Remove data.filler usage everywhere. 84 // TODO(pedrosimonetti): Remove data.filler usage everywhere.
86 if (!data || data.filler) { 85 if (!data || data.filler) {
87 if (this.data_) 86 if (this.data_)
88 this.reset(); 87 this.reset();
89 return; 88 return;
90 } 89 }
91 90
92 this.data_ = data; 91 this.data_ = data;
93 } 92 },
Dan Beam 2012/12/11 01:49:24 why don't you put `get data` here so you don't hav
pedro (no code reviews) 2012/12/11 07:20:53 As I explained earlier, that is not possible: http
pedro (no code reviews) 2012/12/11 07:35:13 Actually, I cannot include this getter here becaus
94 }; 93 };
95 94
96 var TileGetters = { 95 var TileGetters = {
97 /** 96 /**
98 * The TileCell associated to this Tile. 97 * The TileCell associated to this Tile.
99 * @type {TileCell} 98 * @type {TileCell}
100 */ 99 */
101 'tileCell': function() { 100 'tileCell': function() {
102 return findAncestorByClass(this, 'tile-cell'); 101 return findAncestorByClass(this, 'tile-cell');
103 }, 102 },
104 103
105 /** 104 /**
106 * The index of the Tile. 105 * The index of the Tile.
107 * @type {number} 106 * @type {number}
108 */ 107 */
109 'index': function() { 108 'index': function() {
110 assert(this.tileCell); 109 assert(this.tileCell);
111 return this.tileCell.index; 110 return this.tileCell.index;
112 }, 111 },
113
114 /**
115 * Tile data object.
116 * @type {Object}
117 */
118 'data': function() {
119 return this.data_;
120 }
121 }; 112 };
122 113
123 //---------------------------------------------------------------------------- 114 //----------------------------------------------------------------------------
124 // TileCell 115 // TileCell
125 //---------------------------------------------------------------------------- 116 //----------------------------------------------------------------------------
126 117
127 /** 118 /**
128 * Creates a new TileCell object. A TileCell represents a cell in the 119 * 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 120 * 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 121 * place and to animate them individually. Each TileCell is associated to
(...skipping 28 matching lines...) Expand all
159 this.className = 'tile-cell'; 150 this.className = 'tile-cell';
160 this.assign(tile); 151 this.assign(tile);
161 }, 152 },
162 153
163 /** 154 /**
164 * The index of the TileCell. 155 * The index of the TileCell.
165 * @type {number} 156 * @type {number}
166 */ 157 */
167 get index() { 158 get index() {
168 return Array.prototype.indexOf.call(this.tilePage.tiles_, 159 return Array.prototype.indexOf.call(this.tilePage.tiles_,
169 this.firstChild); 160 this.tile);
170 }, 161 },
171 162
172 /** 163 /**
164 * The Tile associated to this TileCell.
165 * @type {Tile}
166 */
167 get tile() {
168 return this.firstElementChild;
169 },
170
171 /**
173 * The TilePage associated to this TileCell. 172 * The TilePage associated to this TileCell.
174 * @type {TilePage} 173 * @type {TilePage}
175 */ 174 */
176 get tilePage() { 175 get tilePage() {
177 return findAncestorByClass(this, 'tile-page'); 176 return findAncestorByClass(this, 'tile-page');
178 }, 177 },
179 178
180 /** 179 /**
181 * Assigns a Tile to the this TileCell. 180 * Assigns a Tile to the this TileCell.
182 * @type {TilePage} 181 * @type {TilePage}
183 */ 182 */
184 assign: function(tile) { 183 assign: function(tile) {
185 if (this.firstChild) 184 if (this.tile)
186 this.replaceChild(tile, this.firstChild); 185 this.replaceChild(tile, this.tile);
187 else 186 else
188 this.appendChild(tile); 187 this.appendChild(tile);
189 }, 188 },
190 189
191 /** 190 /**
192 * Called when an app is removed from Chrome. Animates its disappearance. 191 * Called when an app is removed from Chrome. Animates its disappearance.
193 * @param {boolean=} opt_animate Whether the animation should be animated. 192 * @param {boolean=} opt_animate Whether the animation should be animated.
194 */ 193 */
195 doRemove: function(opt_animate) { 194 doRemove: function(opt_animate) {
196 if (opt_animate) 195 this.tilePage.removeTile(this.tile, false);
197 this.firstChild.classList.add('removing-tile-contents');
198 else
199 this.tilePage.removeTile(this, false);
200 }, 196 },
201 }; 197 };
202 198
203 //---------------------------------------------------------------------------- 199 //----------------------------------------------------------------------------
204 // TilePage 200 // TilePage
205 //---------------------------------------------------------------------------- 201 //----------------------------------------------------------------------------
206 202
207 /** 203 /**
208 * Creates a new TilePage object. This object contains tiles and controls 204 * Creates a new TilePage object. This object contains tiles and controls
209 * their layout. 205 * their layout.
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 * @param {Tile} tile The tile to be added. 439 * @param {Tile} tile The tile to be added.
444 * @param {number} index The location in the tile grid to insert it at. 440 * @param {number} index The location in the tile grid to insert it at.
445 * @protected 441 * @protected
446 */ 442 */
447 addTileAt: function(tile, index) { 443 addTileAt: function(tile, index) {
448 this.tiles_.splice(index, 0, tile); 444 this.tiles_.splice(index, 0, tile);
449 this.fireAddedEvent(tile, index); 445 this.fireAddedEvent(tile, index);
450 }, 446 },
451 447
452 /** 448 /**
449 * Create a blank tile.
450 * @protected
451 */
452 createTile_: function() {
453 return new this.TileClass(this.config);
454 },
455
456 /**
453 * Create blank tiles. 457 * Create blank tiles.
454 * @private 458 * @param {number} count The desired number of Tiles to be created. If this
455 * @param {number} count The number of Tiles to be created. 459 * value the maximum value defined in |config.maxTileCount|, the maximum
460 * value will be used instead.
461 * @protected
456 */ 462 */
457 createTiles_: function(count) { 463 createTiles_: function(count) {
458 var Class = this.TileClass; 464 count = Math.min(count, this.config.maxTileCount);
459 var config = this.config;
460 count = Math.min(count, config.maxTileCount);
461 for (var i = 0; i < count; i++) { 465 for (var i = 0; i < count; i++) {
462 this.appendTile(new Class(config)); 466 this.appendTile(this.createTile_());
463 } 467 }
464 }, 468 },
465 469
466 /** 470 /**
467 * Update the tiles after a change to |dataList_|. 471 * Update the tiles after a change to |dataList_|.
468 */ 472 */
469 updateTiles_: function() { 473 updateTiles_: function() {
470 var maxTileCount = this.config.maxTileCount; 474 var maxTileCount = this.config.maxTileCount;
471 var dataList = this.dataList_; 475 var dataList = this.dataList_;
472 var tiles = this.tiles; 476 var tiles = this.tiles;
473 for (var i = 0; i < maxTileCount; i++) { 477 for (var i = 0; i < maxTileCount; i++) {
474 var data = dataList[i]; 478 var data = dataList[i];
475 var tile = tiles[i]; 479 var tile = tiles[i];
476 480
477 // TODO(pedrosimonetti): What do we do when there's no tile here? 481 // TODO(pedrosimonetti): What do we do when there's no tile here?
478 if (!tile) 482 if (!tile)
479 return; 483 return;
480 484
481 if (i >= dataList.length) 485 if (i >= dataList.length)
482 tile.reset(); 486 tile.reset();
483 else 487 else
484 tile.setData(data); 488 tile.data = data;
485 } 489 }
486 }, 490 },
487 491
488 /** 492 /**
489 * Sets the dataList that will be used to create Tiles. This method will 493 * Sets the dataList that will be used to create Tiles. This method will
490 * create/remove necessary/unnecessary tiles, render the grid when the 494 * create/remove necessary/unnecessary tiles, render the grid when the
491 * number of tiles has changed, and finally will call |updateTiles_| which 495 * number of tiles has changed, and finally will call |updateTiles_| which
492 * will in turn render the individual tiles. 496 * will in turn render the individual tiles.
493 * @param {Array} dataList The array of data. 497 * @param {Array} dataList The array of data.
494 */ 498 */
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 * @param {number=} opt_colCount The number of columns. 595 * @param {number=} opt_colCount The number of columns.
592 * @private 596 * @private
593 */ 597 */
594 renderGrid_: function(opt_colCount) { 598 renderGrid_: function(opt_colCount) {
595 var colCount = opt_colCount || this.colCount_; 599 var colCount = opt_colCount || this.colCount_;
596 600
597 var tileGridContent = this.tileGridContent_; 601 var tileGridContent = this.tileGridContent_;
598 var tiles = this.tiles_; 602 var tiles = this.tiles_;
599 var tileCount = tiles.length; 603 var tileCount = tiles.length;
600 604
601 var numOfVisibleRows = this.numOfVisibleRows_;
602 var rowCount = Math.ceil(tileCount / colCount); 605 var rowCount = Math.ceil(tileCount / colCount);
603 var tileRows = tileGridContent.getElementsByClassName('tile-row'); 606 var tileRows = tileGridContent.getElementsByClassName('tile-row');
604 607
605 var pageOffset = this.pageOffset_;
606
607 for (var tile = 0, row = 0; row < rowCount; row++) { 608 for (var tile = 0, row = 0; row < rowCount; row++) {
608 var tileRow = tileRows[row]; 609 var tileRow = tileRows[row];
609 610
610 // Create tile row if there's no one yet. 611 // Create tile row if there's no one yet.
611 if (!tileRow) { 612 if (!tileRow) {
612 tileRow = cr.doc.createElement('div'); 613 tileRow = cr.doc.createElement('div');
613 tileRow.className = 'tile-row'; 614 tileRow.className = 'tile-row';
614 tileGridContent.appendChild(tileRow); 615 tileGridContent.appendChild(tileRow);
615 } 616 }
616 617
(...skipping 15 matching lines...) Expand all
632 tileCell = tileRowTiles[col]; 633 tileCell = tileRowTiles[col];
633 } else { 634 } else {
634 var span = cr.doc.createElement('span'); 635 var span = cr.doc.createElement('span');
635 tileCell = new TileCell(span, this.config); 636 tileCell = new TileCell(span, this.config);
636 } 637 }
637 638
638 // Render Tiles. 639 // Render Tiles.
639 if (tile < tileCount) { 640 if (tile < tileCount) {
640 tileCell.classList.remove('filler'); 641 tileCell.classList.remove('filler');
641 tileElement = tiles[tile]; 642 tileElement = tiles[tile];
642 if (!tileCell.firstChild) 643 if (!tileCell.tile)
643 tileCell.appendChild(tileElement); 644 tileCell.appendChild(tileElement);
644 else if (tileElement != tileCell.firstChild) 645 else if (tileElement != tileCell.tile)
645 tileCell.replaceChild(tileElement, tileCell.firstChild); 646 tileCell.replaceChild(tileElement, tileCell.tile);
646 } else if (!tileCell.classList.contains('filler')) { 647 } else if (!tileCell.classList.contains('filler')) {
647 tileCell.classList.add('filler'); 648 tileCell.classList.add('filler');
648 tileElement = cr.doc.createElement('span'); 649 tileElement = cr.doc.createElement('span');
649 tileElement.className = 'tile'; 650 tileElement.className = 'tile';
650 if (tileCell.firstChild) 651 if (tileCell.tile)
651 tileCell.replaceChild(tileElement, tileCell.firstChild); 652 tileCell.replaceChild(tileElement, tileCell.tile);
652 else 653 else
653 tileCell.appendChild(tileElement); 654 tileCell.appendChild(tileElement);
654 } 655 }
655 656
656 if (!tileRowTiles[col]) 657 if (!tileRowTiles[col])
657 tileRow.appendChild(tileCell); 658 tileRow.appendChild(tileCell);
658 } 659 }
659 } 660 }
660 661
661 // Remove excessive tile rows from the tile grid. 662 // Remove excessive tile rows from the tile grid.
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 851
851 var repositioningStartIndex = index; 852 var repositioningStartIndex = index;
852 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0); 853 var repositioningEndIndex = tileCount - (extraTileData ? 1 : 0);
853 854
854 this.initializeRepositioningAnimation_(index, repositioningEndIndex); 855 this.initializeRepositioningAnimation_(index, repositioningEndIndex);
855 856
856 var restoredData = newDataList[index]; 857 var restoredData = newDataList[index];
857 var tileBeingRestored = createTile(this, restoredData); 858 var tileBeingRestored = createTile(this, restoredData);
858 tileCells[index].appendChild(tileBeingRestored); 859 tileCells[index].appendChild(tileBeingRestored);
859 860
860 var extraTile = extraCell.firstChild; 861 var extraTile = extraCell.tile;
861 862
862 this.executeRepositioningAnimation_(tileBeingRestored, extraTile, 863 this.executeRepositioningAnimation_(tileBeingRestored, extraTile,
863 repositioningStartIndex, repositioningEndIndex, false); 864 repositioningStartIndex, repositioningEndIndex, false);
864 865
865 // Cleans up the animation. 866 // Cleans up the animation.
866 var onPositioningTransitionEnd = function(e) { 867 var onPositioningTransitionEnd = function(e) {
867 var propertyName = e.propertyName; 868 var propertyName = e.propertyName;
868 if (!(propertyName == '-webkit-transform' || 869 if (!(propertyName == '-webkit-transform' ||
869 propertyName == 'opacity')) { 870 propertyName == 'opacity')) {
870 return; 871 return;
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1068 * a tile filler will be created. 1069 * a tile filler will be created.
1069 * @param {TilePage} tilePage A TilePage. 1070 * @param {TilePage} tilePage A TilePage.
1070 * @param {Object=} opt_data The data that will be used to create the tile. 1071 * @param {Object=} opt_data The data that will be used to create the tile.
1071 * @return {Tile} The new tile. 1072 * @return {Tile} The new tile.
1072 */ 1073 */
1073 function createTile(tilePage, opt_data) { 1074 function createTile(tilePage, opt_data) {
1074 var tile; 1075 var tile;
1075 if (opt_data) { 1076 if (opt_data) {
1076 // If there's data, the new tile will be a real one (not a filler). 1077 // If there's data, the new tile will be a real one (not a filler).
1077 tile = new tilePage.TileClass(tilePage.config); 1078 tile = new tilePage.TileClass(tilePage.config);
1078 tile.setData(opt_data); 1079 tile.data = opt_data;
1079 } else { 1080 } else {
1080 // Otherwise, it will be a fake filler tile. 1081 // Otherwise, it will be a fake filler tile.
1081 tile = cr.doc.createElement('span'); 1082 tile = cr.doc.createElement('span');
1082 tile.className = 'tile'; 1083 tile.className = 'tile';
1083 } 1084 }
1084 return tile; 1085 return tile;
1085 } 1086 }
1086 1087
1087 /** 1088 /**
1088 * Fades a tile. 1089 * Fades a tile.
1089 * @param {Tile} tile A Tile. 1090 * @param {Tile} tile A Tile.
1090 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is 1091 * @param {boolean} isFadeIn Whether to fade-in the tile. If |isFadeIn| is
1091 * false, then the tile is going to fade-out. 1092 * false, then the tile is going to fade-out.
1092 */ 1093 */
1093 function fadeTile(tile, isFadeIn) { 1094 function fadeTile(tile, isFadeIn) {
1094 var className = 'animate-hide-tile'; 1095 var className = 'animate-hide-tile';
1095 tile.classList.add(className); 1096 tile.classList.add(className);
1096 if (isFadeIn) { 1097 if (isFadeIn) {
1097 // Forces a reflow to ensure that the fade-out animation will work. 1098 // Forces a reflow to ensure that the fade-out animation will work.
1098 tile.scrollTop; 1099 tile.scrollTop;
1099 tile.classList.remove(className); 1100 tile.classList.remove(className);
1100 } 1101 }
1101 } 1102 }
1102 1103
1103 return { 1104 return {
1104 Tile: Tile, 1105 Tile: Tile,
1105 TilePage: TilePage, 1106 TilePage: TilePage,
1106 }; 1107 };
1107 }); 1108 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698