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

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

Issue 9116037: [NTP4] Make TilePage and CardSlider evented to simplify code and fix page switcher bug (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: found one more bug when you leave a tab while mousing over page switcher Created 8 years, 11 months 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('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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 this.finalizeDrag_(); 193 this.finalizeDrag_();
194 } else { 194 } else {
195 // The CSS3 transitions spec intentionally leaves it up to individual 195 // The CSS3 transitions spec intentionally leaves it up to individual
196 // user agents to determine when styles should be applied. On some 196 // user agents to determine when styles should be applied. On some
197 // platforms (at the moment, Windows), when you apply both classes 197 // platforms (at the moment, Windows), when you apply both classes
198 // immediately a transition may not occur correctly. That's why we're 198 // immediately a transition may not occur correctly. That's why we're
199 // using a setTimeout here to queue adding the class until the 199 // using a setTimeout here to queue adding the class until the
200 // previous class (currently: .placing) sets up a transition. 200 // previous class (currently: .placing) sets up a transition.
201 // http://dev.w3.org/csswg/css3-transitions/#starting 201 // http://dev.w3.org/csswg/css3-transitions/#starting
202 window.setTimeout(function() { 202 window.setTimeout(function() {
203 this.dragClone.classList.add('dropped-on-other-page'); 203 if (this.dragClone)
204 this.dragClone.classList.add('dropped-on-other-page');
204 }.bind(this), 0); 205 }.bind(this), 0);
205 } 206 }
206 } 207 }
207 208
208 delete this.lastDropEffect; 209 delete this.lastDropEffect;
209 this.landedOnTrash = false; 210 this.landedOnTrash = false;
210 }, 211 },
211 212
212 /** 213 /**
213 * Creates a clone of this node offset by the coordinates. Used for the 214 * Creates a clone of this node offset by the coordinates. Used for the
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 onDragCloneTransitionEnd_: function(e) { 285 onDragCloneTransitionEnd_: function(e) {
285 if (this.classList.contains('dragging') && 286 if (this.classList.contains('dragging') &&
286 (e.propertyName == 'left' || e.propertyName == 'top' || 287 (e.propertyName == 'left' || e.propertyName == 'top' ||
287 e.propertyName == '-webkit-transform')) { 288 e.propertyName == '-webkit-transform')) {
288 this.finalizeDrag_(); 289 this.finalizeDrag_();
289 } 290 }
290 }, 291 },
291 292
292 /** 293 /**
293 * Called when an app is removed from Chrome. Animates its disappearance. 294 * Called when an app is removed from Chrome. Animates its disappearance.
295 * @param {boolean=} opt_animate Whether the animation should be animated.
294 */ 296 */
295 doRemove: function() { 297 doRemove: function(opt_animate) {
296 this.firstChild.classList.add('removing-tile-contents'); 298 if (opt_animate)
299 this.firstChild.classList.add('removing-tile-contents');
300 else
301 this.parentNode.removeChild(this);
297 }, 302 },
298 303
299 /** 304 /**
300 * Callback for the webkitAnimationEnd event on the tile's contents. 305 * Callback for the webkitAnimationEnd event on the tile's contents.
301 * @param {Event} e The event object. 306 * @param {Event} e The event object.
302 */ 307 */
303 onContentsAnimationEnd_: function(e) { 308 onContentsAnimationEnd_: function(e) {
304 if (this.firstChild.classList.contains('new-tile-contents')) 309 if (this.firstChild.classList.contains('new-tile-contents'))
305 this.firstChild.classList.remove('new-tile-contents'); 310 this.firstChild.classList.remove('new-tile-contents');
306 if (this.firstChild.classList.contains('removing-tile-contents')) 311 if (this.firstChild.classList.contains('removing-tile-contents'))
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 /** 496 /**
492 * Returns any extra padding to insert to the bottom of a tile page. By 497 * Returns any extra padding to insert to the bottom of a tile page. By
493 * default there is none, but subclasses can override. 498 * default there is none, but subclasses can override.
494 * @type {number} 499 * @type {number}
495 */ 500 */
496 get extraBottomPadding() { 501 get extraBottomPadding() {
497 return 0; 502 return 0;
498 }, 503 },
499 504
500 /** 505 /**
506 * Removes the tilePage from the DOM and cleans up event handlers.
507 */
508 remove: function() {
509 // This checks arguments.length as most remove functions have a boolean
510 // |opt_animate| argument, but that's not necesarilly applicable to
511 // removing a tilePage. Selecting a different card in an animated way and
512 // deleting the card afterward is probably a better choice.
513 assert(typeof arguments[0] != 'boolean',
514 'This function takes no |opt_animate| argument.');
515 this.tearDown_();
516 this.parentNode.removeChild(this);
517 },
518
519 /**
501 * Cleans up resources that are no longer needed after this TilePage 520 * Cleans up resources that are no longer needed after this TilePage
502 * instance is removed from the DOM. 521 * instance is removed from the DOM.
522 * @private
503 */ 523 */
504 tearDown: function() { 524 tearDown_: function() {
505 this.eventTracker.removeAll(); 525 this.eventTracker.removeAll();
506 }, 526 },
507 527
508 /** 528 /**
509 * Appends a tile to the end of the tile grid. 529 * Appends a tile to the end of the tile grid.
510 * @param {HTMLElement} tileElement The contents of the tile. 530 * @param {HTMLElement} tileElement The contents of the tile.
511 * @param {?boolean} animate If true, the append will be animated. 531 * @param {?boolean} animate If true, the append will be animated.
512 * @protected 532 * @protected
513 */ 533 */
514 appendTile: function(tileElement, animate) { 534 appendTile: function(tileElement, animate) {
515 this.addTileAt(tileElement, this.tileElements_.length, animate); 535 this.addTileAt(tileElement, this.tileElements_.length, animate);
516 }, 536 },
517 537
518 /** 538 /**
519 * Adds the given element to the tile grid. 539 * Adds the given element to the tile grid.
520 * @param {Node} tileElement The tile object/node to insert. 540 * @param {Node} tileElement The tile object/node to insert.
521 * @param {number} index The location in the tile grid to insert it at. 541 * @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 542 * @param {boolean=} opt_animate If true, the tile in question will be
523 * (other tiles, if they must reposition, do not animate). 543 * animated (other tiles, if they must reposition, do not animate).
524 * @protected 544 * @protected
525 */ 545 */
526 addTileAt: function(tileElement, index, animate) { 546 addTileAt: function(tileElement, index, opt_animate) {
527 this.classList.remove('animating-tile-page'); 547 this.classList.remove('animating-tile-page');
528 if (animate) 548 if (opt_animate)
529 tileElement.classList.add('new-tile-contents'); 549 tileElement.classList.add('new-tile-contents');
550
551 // Make sure the index is positive and either in the the bounds of
552 // this.tileElements_ or at the end (meaning append).
553 assert(index >= 0 && index <= this.tileElements_.length);
554
530 var wrapperDiv = new Tile(tileElement); 555 var wrapperDiv = new Tile(tileElement);
531 if (index == this.tileElements_.length) { 556 // If is out of the bounds of the tile element list, .insertBefore() will
532 this.tileGrid_.appendChild(wrapperDiv); 557 // act just like appendChild().
533 } else { 558 this.tileGrid_.insertBefore(wrapperDiv, this.tileElements_[index]);
534 this.tileGrid_.insertBefore(wrapperDiv,
535 this.tileElements_[index]);
536 }
537 this.calculateLayoutValues_(); 559 this.calculateLayoutValues_();
538 this.heightChanged_(); 560 this.heightChanged_();
539 561
540 this.positionTile_(index); 562 this.positionTile_(index);
563 this.fireAddedEvent(wrapperDiv, index, !!opt_animate);
541 }, 564 },
542 565
543 /** 566 /**
544 * Removes the given tile and animates the respositioning of the other 567 * Notify interested subscribers that a tile has been removed from this
545 * tiles. 568 * page.
546 * @param {HTMLElement} tile The tile to remove from |tileGrid_|. 569 * @param {Tile} tile The newly added tile.
547 * @param {?boolean} animate If true, remaining tiles will animate. 570 * @param {number} index The index of the tile that was added.
571 * @param {boolean} wasAnimated Whether the removal was animated.
548 */ 572 */
549 removeTile: function(tile, animate) { 573 fireAddedEvent: function(tile, index, wasAnimated) {
550 if (animate) 574 var e = document.createEvent('Event');
575 e.initEvent('tilePage:tile_added', true, true);
576 e.addedIndex = index;
577 e.addedTile = tile;
578 e.wasAnimated = wasAnimated;
579 this.dispatchEvent(e);
580 },
581
582 /**
583 * Removes the given tile and animates the repositioning of the other tiles.
584 * @param {boolean=} opt_animate Whether the removal should be animated.
585 * @param {boolean=} opt_dontNotify Whether a page should be removed if the
586 * last tile is removed from it.
587 */
588 removeTile: function(tile, opt_animate, opt_dontNotify) {
589 if (opt_animate)
551 this.classList.add('animating-tile-page'); 590 this.classList.add('animating-tile-page');
552 var index = tile.index; 591 var index = tile.index;
553 tile.parentNode.removeChild(tile); 592 tile.parentNode.removeChild(tile);
554 this.calculateLayoutValues_(); 593 this.calculateLayoutValues_();
555 this.cleanupDrag(); 594 this.cleanupDrag();
595
596 if (!opt_dontNotify)
597 this.fireRemovedEvent(tile, index, !!opt_animate);
556 }, 598 },
557 599
558 /** 600 /**
601 * Notify interested subscribers that a tile has been removed from this
602 * page.
603 * @param {Tile} tile The tile that was removed.
604 * @param {number} oldIndex Where the tile was positioned before removal.
605 * @param {boolean} wasAnimated Whether the removal was animated.
606 */
607 fireRemovedEvent: function(tile, oldIndex, wasAnimated) {
608 var e = document.createEvent('Event');
609 e.initEvent('tilePage:tile_removed', true, true);
610 e.removedIndex = oldIndex;
611 e.removedTile = tile;
612 e.wasAnimated = wasAnimated;
613 this.dispatchEvent(e);
614 },
615
616 /**
559 * Removes all tiles from the page. 617 * Removes all tiles from the page.
560 */ 618 */
561 removeAllTiles: function() { 619 removeAllTiles: function() {
562 this.tileGrid_.innerHTML = ''; 620 this.tileGrid_.innerHTML = '';
563 }, 621 },
564 622
565 /** 623 /**
566 * Called when the page is selected (in the card selector). 624 * Called when the page is selected (in the card selector).
567 * @param {Event} e A custom cardselected event. 625 * @param {Event} e A custom cardselected event.
568 * @private 626 * @private
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
1135 /** 1193 /**
1136 * Appends the currently dragged tile to the end of the page. Called 1194 * 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. 1195 * from outside the page, e.g. when dropping on a nav dot.
1138 */ 1196 */
1139 appendDraggingTile: function() { 1197 appendDraggingTile: function() {
1140 var originalPage = currentlyDraggingTile.tilePage; 1198 var originalPage = currentlyDraggingTile.tilePage;
1141 if (originalPage == this) 1199 if (originalPage == this)
1142 return; 1200 return;
1143 1201
1144 this.addDragData(null, this.tileElements_.length); 1202 this.addDragData(null, this.tileElements_.length);
1145 originalPage.cleanupDrag(); 1203 if (originalPage)
1204 originalPage.cleanupDrag();
1146 }, 1205 },
1147 1206
1148 /** 1207 /**
1149 * Makes sure all the tiles are in the right place after a drag is over. 1208 * Makes sure all the tiles are in the right place after a drag is over.
1150 */ 1209 */
1151 cleanupDrag: function() { 1210 cleanupDrag: function() {
1152 this.repositionTiles_(currentlyDraggingTile); 1211 this.repositionTiles_(currentlyDraggingTile);
1153 // Remove the drag mask. 1212 // Remove the drag mask.
1154 this.updateMask_(); 1213 this.updateMask_();
1155 }, 1214 },
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1230 assert(false); 1289 assert(false);
1231 }, 1290 },
1232 }; 1291 };
1233 1292
1234 return { 1293 return {
1235 getCurrentlyDraggingTile: getCurrentlyDraggingTile, 1294 getCurrentlyDraggingTile: getCurrentlyDraggingTile,
1236 setCurrentDropEffect: setCurrentDropEffect, 1295 setCurrentDropEffect: setCurrentDropEffect,
1237 TilePage: TilePage, 1296 TilePage: TilePage,
1238 }; 1297 };
1239 }); 1298 });
OLDNEW
« no previous file with comments | « chrome/browser/resources/ntp4/page_switcher.js ('k') | chrome/browser/resources/shared/js/cr/ui/card_slider.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698