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

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

Issue 8637001: [NTP4] Auto-deletion of empty apps panes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more fixes and typos from reviewing myself Created 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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('ntp4', function() { 5 cr.define('ntp4', function() {
6 'use strict'; 6 'use strict';
7 7
8 // We can't pass the currently dragging tile via dataTransfer because of 8 // We can't pass the currently dragging tile via dataTransfer because of
9 // http://crbug.com/31037 9 // http://crbug.com/31037
10 var currentlyDraggingTile = null; 10 var currentlyDraggingTile = null;
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 */ 283 */
284 onDragCloneTransitionEnd_: function(e) { 284 onDragCloneTransitionEnd_: function(e) {
285 if (this.classList.contains('dragging') && 285 if (this.classList.contains('dragging') &&
286 (e.propertyName == 'left' || e.propertyName == 'top' || 286 (e.propertyName == 'left' || e.propertyName == 'top' ||
287 e.propertyName == '-webkit-transform')) { 287 e.propertyName == '-webkit-transform')) {
288 this.finalizeDrag_(); 288 this.finalizeDrag_();
289 } 289 }
290 }, 290 },
291 291
292 /** 292 /**
293 * Called when an app is removed from Chrome. Animates its disappearance.
294 */
295 doRemove: function() {
296 this.firstChild.classList.add('removing-tile-contents');
297 },
298
299 /**
300 * Callback for the webkitAnimationEnd event on the tile's contents. 293 * Callback for the webkitAnimationEnd event on the tile's contents.
301 * @param {Event} e The event object. 294 * @param {Event} e The event object.
302 */ 295 */
303 onContentsAnimationEnd_: function(e) { 296 onContentsAnimationEnd_: function(e) {
304 if (this.firstChild.classList.contains('new-tile-contents')) 297 if (this.firstChild.classList.contains('new-tile-contents'))
305 this.firstChild.classList.remove('new-tile-contents'); 298 this.firstChild.classList.remove('new-tile-contents');
306 if (this.firstChild.classList.contains('removing-tile-contents')) 299 if (this.firstChild.classList.contains('removing-tile-contents'))
307 this.tilePage.removeTile(this, true); 300 this.tilePage.removeTile(this, true);
308 }, 301 },
309 }; 302 };
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 function TilePage(gridValues) { 349 function TilePage(gridValues) {
357 var el = cr.doc.createElement('div'); 350 var el = cr.doc.createElement('div');
358 el.gridValues_ = gridValues; 351 el.gridValues_ = gridValues;
359 el.__proto__ = TilePage.prototype; 352 el.__proto__ = TilePage.prototype;
360 el.initialize(); 353 el.initialize();
361 354
362 return el; 355 return el;
363 } 356 }
364 357
365 /** 358 /**
359 * The type of events TilePage dispatches.
360 * @public
361 * @static
362 * @memberOf TilePage
363 */
364 TilePage.EventType = {
365 TILE_REMOVED: 'tilePage:tile_removed'
366 };
367
368 /**
366 * Takes a collection of grid layout pixel values and updates them with 369 * Takes a collection of grid layout pixel values and updates them with
367 * additional tiling values that are calculated from TilePage constants. 370 * additional tiling values that are calculated from TilePage constants.
368 * @param {Object} grid The grid layout pixel values to update. 371 * @param {Object} grid The grid layout pixel values to update.
369 */ 372 */
370 TilePage.initGridValues = function(grid) { 373 TilePage.initGridValues = function(grid) {
371 // The amount of space we need to display a narrow grid (all narrow grids 374 // The amount of space we need to display a narrow grid (all narrow grids
372 // are this size). 375 // are this size).
373 grid.narrowWidth = 376 grid.narrowWidth =
374 grid.minTileWidth * tileWidthFraction(grid.minColCount, 377 grid.minTileWidth * tileWidthFraction(grid.minColCount,
375 grid.tileSpacingFraction); 378 grid.tileSpacingFraction);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 /** 494 /**
492 * Returns any extra padding to insert to the bottom of a tile page. By 495 * Returns any extra padding to insert to the bottom of a tile page. By
493 * default there is none, but subclasses can override. 496 * default there is none, but subclasses can override.
494 * @type {number} 497 * @type {number}
495 */ 498 */
496 get extraBottomPadding() { 499 get extraBottomPadding() {
497 return 0; 500 return 0;
498 }, 501 },
499 502
500 /** 503 /**
504 * Removes the tilePage from the DOM and cleans up event handlers.
505 */
506 remove: function() {
507 // This checks arguments.length as most remove functions have a boolean
508 // |opt_animate| argument, but that's not necesarilly applicable to
509 // removing a tilePage. Selecting a different card in an animated way and
510 // deleting the card afterward is probably a better choice.
511 assert(typeof arguments[0] != 'boolean',
512 'This function takes no |opt_animate| argument.');
513 assert(this.tileCount == 0, 'Only removal of an empty page is allowed.');
514 this.tearDown_();
515 this.parentNode.removeChild(this);
516 },
517
518 /**
501 * Cleans up resources that are no longer needed after this TilePage 519 * Cleans up resources that are no longer needed after this TilePage
502 * instance is removed from the DOM. 520 * instance is removed from the DOM.
521 * @private
503 */ 522 */
504 tearDown: function() { 523 tearDown_: function() {
505 this.eventTracker.removeAll(); 524 this.eventTracker.removeAll();
506 }, 525 },
507 526
508 /** 527 /**
509 * Appends a tile to the end of the tile grid. 528 * Appends a tile to the end of the tile grid.
510 * @param {HTMLElement} tileElement The contents of the tile. 529 * @param {HTMLElement} tileElement The contents of the tile.
511 * @param {?boolean} animate If true, the append will be animated. 530 * @param {?boolean} animate If true, the append will be animated.
512 * @protected 531 * @protected
513 */ 532 */
514 appendTile: function(tileElement, animate) { 533 appendTile: function(tileElement, animate) {
515 this.addTileAt(tileElement, this.tileElements_.length, animate); 534 this.addTileAt(tileElement, this.tileElements_.length, animate);
516 }, 535 },
517 536
518 /** 537 /**
519 * Adds the given element to the tile grid. 538 * Adds the given element to the tile grid.
520 * @param {Node} tileElement The tile object/node to insert. 539 * @param {Node} tileElement The tile object/node to insert.
521 * @param {number} index The location in the tile grid to insert it at. 540 * @param {number} index The location in the tile grid to insert it at.
522 * @param {?boolean} animate If true, the tile in question will be animated 541 * @param {boolean=} opt_animate If true, the tile in question will be
523 * (other tiles, if they must reposition, do not animate). 542 * animated (other tiles, if they must reposition, do not animate).
524 * @protected 543 * @protected
525 */ 544 */
526 addTileAt: function(tileElement, index, animate) { 545 addTileAt: function(tileElement, index, opt_animate) {
527 this.classList.remove('animating-tile-page'); 546 this.classList.remove('animating-tile-page');
528 if (animate) 547 if (opt_animate)
529 tileElement.classList.add('new-tile-contents'); 548 tileElement.classList.add('new-tile-contents');
549
550 // Make sure the index is positive and either in the the bounds of
551 // this.tileElements_ or at the end (meaning append).
552 assert(index >= 0 && index <= this.tileElements_.length);
553
530 var wrapperDiv = new Tile(tileElement); 554 var wrapperDiv = new Tile(tileElement);
531 if (index == this.tileElements_.length) { 555 // If is out of the bounds of the tile element list, .insertBefore() will
532 this.tileGrid_.appendChild(wrapperDiv); 556 // act just like appendChild().
533 } else { 557 this.tileGrid_.insertBefore(wrapperDiv, this.tileElements_[index]);
534 this.tileGrid_.insertBefore(wrapperDiv,
535 this.tileElements_[index]);
536 }
537 this.calculateLayoutValues_(); 558 this.calculateLayoutValues_();
538 this.heightChanged_(); 559 this.heightChanged_();
539 560
540 this.positionTile_(index); 561 this.positionTile_(index);
541 }, 562 },
542 563
543 /** 564 /**
544 * Removes the given tile and animates the respositioning of the other 565 * Removes the given tile and animates the repositioning of the other tiles.
545 * tiles. 566 * @param {boolean=} opt_animate Whether the removal should be animated.
546 * @param {HTMLElement} tile The tile to remove from |tileGrid_|. 567 * @param {boolean=} opt_dontNotify Whether a page should be removed if the
547 * @param {?boolean} animate If true, remaining tiles will animate. 568 * last tile is removed from it.
548 */ 569 */
549 removeTile: function(tile, animate) { 570 removeTile: function(tile, opt_animate, opt_dontNotify) {
550 if (animate) 571 if (opt_animate)
551 this.classList.add('animating-tile-page'); 572 this.classList.add('animating-tile-page');
552 var index = tile.index;
553 tile.parentNode.removeChild(tile); 573 tile.parentNode.removeChild(tile);
554 this.calculateLayoutValues_(); 574 this.calculateLayoutValues_();
555 this.cleanupDrag(); 575 this.cleanupDrag();
576
577 if (!opt_dontNotify)
578 this.fireRemovedEvent(!!opt_animate);
556 }, 579 },
557 580
558 /** 581 /**
582 * Notify interested subscribers that a tile has been removed from this
583 * page.
584 * @param {boolean} wasAnimated Whether the removal was animated.
585 */
586 fireRemovedEvent: function(wasAnimated) {
587 var e = document.createEvent('Event');
588 e.initEvent(TilePage.EventType.TILE_REMOVED, true, true);
589 e.tilePage = this;
590 e.wasAnimated = wasAnimated;
591 this.dispatchEvent(e);
592 },
593
594 /**
559 * Removes all tiles from the page. 595 * Removes all tiles from the page.
560 */ 596 */
561 removeAllTiles: function() { 597 removeAllTiles: function() {
562 this.tileGrid_.innerHTML = ''; 598 this.tileGrid_.innerHTML = '';
563 }, 599 },
564 600
565 /** 601 /**
566 * Called when the page is selected (in the card selector). 602 * Called when the page is selected (in the card selector).
567 * @param {Event} e A custom cardselected event. 603 * @param {Event} e A custom cardselected event.
568 * @private 604 * @private
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 /** 1171 /**
1136 * Appends the currently dragged tile to the end of the page. Called 1172 * Appends the currently dragged tile to the end of the page. Called
1137 * from outside the page, e.g. when dropping on a nav dot. 1173 * from outside the page, e.g. when dropping on a nav dot.
1138 */ 1174 */
1139 appendDraggingTile: function() { 1175 appendDraggingTile: function() {
1140 var originalPage = currentlyDraggingTile.tilePage; 1176 var originalPage = currentlyDraggingTile.tilePage;
1141 if (originalPage == this) 1177 if (originalPage == this)
1142 return; 1178 return;
1143 1179
1144 this.addDragData(null, this.tileElements_.length); 1180 this.addDragData(null, this.tileElements_.length);
1145 originalPage.cleanupDrag(); 1181 if (originalPage)
1182 originalPage.cleanupDrag();
1146 }, 1183 },
1147 1184
1148 /** 1185 /**
1149 * Makes sure all the tiles are in the right place after a drag is over. 1186 * Makes sure all the tiles are in the right place after a drag is over.
1150 */ 1187 */
1151 cleanupDrag: function() { 1188 cleanupDrag: function() {
1152 this.repositionTiles_(currentlyDraggingTile); 1189 this.repositionTiles_(currentlyDraggingTile);
1153 // Remove the drag mask. 1190 // Remove the drag mask.
1154 this.updateMask_(); 1191 this.updateMask_();
1155 }, 1192 },
1156 1193
1157 /** 1194 /**
1158 * Reposition all the tiles (possibly ignoring one). 1195 * Reposition all the tiles (possibly ignoring one).
1159 * @param {Node} ignoreNode An optional node to ignore. 1196 * @param {?Node} ignoreNode An optional node to ignore.
1160 * @private 1197 * @private
1161 */ 1198 */
1162 repositionTiles_: function(ignoreNode) { 1199 repositionTiles_: function(ignoreNode) {
1163 for (var i = 0; i < this.tileElements_.length; i++) { 1200 for (var i = 0; i < this.tileElements_.length; i++) {
1164 if (!ignoreNode || ignoreNode !== this.tileElements_[i]) 1201 if (!ignoreNode || ignoreNode !== this.tileElements_[i])
1165 this.positionTile_(i); 1202 this.positionTile_(i);
1166 } 1203 }
1167 }, 1204 },
1168 1205
1169 /** 1206 /**
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1230 assert(false); 1267 assert(false);
1231 }, 1268 },
1232 }; 1269 };
1233 1270
1234 return { 1271 return {
1235 getCurrentlyDraggingTile: getCurrentlyDraggingTile, 1272 getCurrentlyDraggingTile: getCurrentlyDraggingTile,
1236 setCurrentDropEffect: setCurrentDropEffect, 1273 setCurrentDropEffect: setCurrentDropEffect,
1237 TilePage: TilePage, 1274 TilePage: TilePage,
1238 }; 1275 };
1239 }); 1276 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698