| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * @param {Element} container Content container. | 8 * @param {Element} container Content container. |
| 9 * @param {cr.ui.ArrayDataModel} dataModel Data model. | 9 * @param {cr.ui.ArrayDataModel} dataModel Data model. |
| 10 * @param {cr.ui.ListSelectionModel} selectionModel Selection model. | 10 * @param {cr.ui.ListSelectionModel} selectionModel Selection model. |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 this.scheduleLayout(Mosaic.LAYOUT_DELAY); | 430 this.scheduleLayout(Mosaic.LAYOUT_DELAY); |
| 431 } | 431 } |
| 432 | 432 |
| 433 if (event.added.length) { | 433 if (event.added.length) { |
| 434 var newTiles = []; | 434 var newTiles = []; |
| 435 for (var t = 0; t !== event.added.length; t++) | 435 for (var t = 0; t !== event.added.length; t++) |
| 436 newTiles.push(new Mosaic.Tile(this, this.dataModel_.item(index + t))); | 436 newTiles.push(new Mosaic.Tile(this, this.dataModel_.item(index + t))); |
| 437 | 437 |
| 438 this.tiles_.splice.apply(this.tiles_, [index, 0].concat(newTiles)); | 438 this.tiles_.splice.apply(this.tiles_, [index, 0].concat(newTiles)); |
| 439 this.initTiles_(newTiles); | 439 this.initTiles_(newTiles); |
| 440 this.scheduleLayout(Mosaic.LAYOUT_DELAY); |
| 440 } | 441 } |
| 441 | 442 |
| 442 if (this.tiles_.length !== this.dataModel_.length) | 443 if (this.tiles_.length !== this.dataModel_.length) |
| 443 console.error('Mosaic is out of sync'); | 444 console.error('Mosaic is out of sync'); |
| 444 }; | 445 }; |
| 445 | 446 |
| 446 /** | 447 /** |
| 447 * Content change handler. | 448 * Content change handler. |
| 448 * | 449 * |
| 449 * @param {Event} event Event. | 450 * @param {Event} event Event. |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 Mosaic.Layout.prototype.getHeight = function() { | 770 Mosaic.Layout.prototype.getHeight = function() { |
| 770 var firstColumn = this.columns_[0]; | 771 var firstColumn = this.columns_[0]; |
| 771 return firstColumn ? firstColumn.getHeight() : 0; | 772 return firstColumn ? firstColumn.getHeight() : 0; |
| 772 }; | 773 }; |
| 773 | 774 |
| 774 /** | 775 /** |
| 775 * @return {Array.<Mosaic.Tile>} All tiles in the layout. | 776 * @return {Array.<Mosaic.Tile>} All tiles in the layout. |
| 776 */ | 777 */ |
| 777 Mosaic.Layout.prototype.getTiles = function() { | 778 Mosaic.Layout.prototype.getTiles = function() { |
| 778 return Array.prototype.concat.apply([], | 779 return Array.prototype.concat.apply([], |
| 779 this.columns_.map(function(c) { return c.getTiles() })); | 780 this.columns_.map(function(c) { return c.getTiles(); })); |
| 780 }; | 781 }; |
| 781 | 782 |
| 782 /** | 783 /** |
| 783 * @return {number} Total number of tiles added to the layout. | 784 * @return {number} Total number of tiles added to the layout. |
| 784 */ | 785 */ |
| 785 Mosaic.Layout.prototype.getTileCount = function() { | 786 Mosaic.Layout.prototype.getTileCount = function() { |
| 786 return this.getLaidOutTileCount() + | 787 return this.getLaidOutTileCount() + |
| 787 (this.newColumn_ ? this.newColumn_.getTileCount() : 0); | 788 (this.newColumn_ ? this.newColumn_.getTileCount() : 0); |
| 788 }; | 789 }; |
| 789 | 790 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 845 | 846 |
| 846 if (this.newColumn_.isSuboptimal()) { | 847 if (this.newColumn_.isSuboptimal()) { |
| 847 layoutQueue = this.newColumn_.getTiles().concat(layoutQueue); | 848 layoutQueue = this.newColumn_.getTiles().concat(layoutQueue); |
| 848 this.newColumn_.retryWithLowerDensity(); | 849 this.newColumn_.retryWithLowerDensity(); |
| 849 continue; | 850 continue; |
| 850 } | 851 } |
| 851 | 852 |
| 852 this.columns_.push(this.newColumn_); | 853 this.columns_.push(this.newColumn_); |
| 853 this.newColumn_ = null; | 854 this.newColumn_ = null; |
| 854 | 855 |
| 855 if (this.mode_ === Mosaic.Layout.MODE_FINAL) { | 856 if (this.mode_ === Mosaic.Layout.MODE_FINAL && isFinalColumn) { |
| 856 this.getLastColumn_().layout(); | 857 this.commit_(); |
| 857 continue; | 858 continue; |
| 858 } | 859 } |
| 859 | 860 |
| 860 if (this.getWidth() > this.viewportWidth_) { | 861 if (this.getWidth() > this.viewportWidth_) { |
| 861 // Viewport completely filled. | 862 // Viewport completely filled. |
| 862 if (this.density_.equals(this.maxDensity_)) { | 863 if (this.density_.equals(this.maxDensity_)) { |
| 863 // Max density reached, commit if tentative, just continue if dry run. | 864 // Max density reached, commit if tentative, just continue if dry run. |
| 864 if (this.mode_ === Mosaic.Layout.MODE_TENTATIVE) | 865 if (this.mode_ === Mosaic.Layout.MODE_TENTATIVE) |
| 865 this.commit_(); | 866 this.commit_(); |
| 866 continue; | 867 continue; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 886 }; | 887 }; |
| 887 | 888 |
| 888 /** | 889 /** |
| 889 * Commits the tentative layout. | 890 * Commits the tentative layout. |
| 890 * | 891 * |
| 891 * @param {number=} opt_offsetX Horizontal offset. | 892 * @param {number=} opt_offsetX Horizontal offset. |
| 892 * @param {number=} opt_offsetY Vertical offset. | 893 * @param {number=} opt_offsetY Vertical offset. |
| 893 * @private | 894 * @private |
| 894 */ | 895 */ |
| 895 Mosaic.Layout.prototype.commit_ = function(opt_offsetX, opt_offsetY) { | 896 Mosaic.Layout.prototype.commit_ = function(opt_offsetX, opt_offsetY) { |
| 896 console.assert(this.mode_ !== Mosaic.Layout.MODE_FINAL, | |
| 897 'Did not expect final layout'); | |
| 898 for (var i = 0; i !== this.columns_.length; i++) { | 897 for (var i = 0; i !== this.columns_.length; i++) { |
| 899 this.columns_[i].layout(opt_offsetX, opt_offsetY); | 898 this.columns_[i].layout(opt_offsetX, opt_offsetY); |
| 900 } | 899 } |
| 901 this.mode_ = Mosaic.Layout.MODE_FINAL; | 900 this.mode_ = Mosaic.Layout.MODE_FINAL; |
| 902 }; | 901 }; |
| 903 | 902 |
| 904 /** | 903 /** |
| 905 * Finds the most horizontally stretched layout built from the same tiles. | 904 * Finds the most horizontally stretched layout built from the same tiles. |
| 906 * | 905 * |
| 907 * The main layout algorithm fills the entire available viewport height. | 906 * The main layout algorithm fills the entire available viewport height. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 918 // If the layout aspect ratio is not dramatically different from | 917 // If the layout aspect ratio is not dramatically different from |
| 919 // the viewport aspect ratio then there is no need to optimize. | 918 // the viewport aspect ratio then there is no need to optimize. |
| 920 if (this.getWidth() / this.getHeight() > | 919 if (this.getWidth() / this.getHeight() > |
| 921 this.viewportWidth_ / this.viewportHeight_ * 0.9) | 920 this.viewportWidth_ / this.viewportHeight_ * 0.9) |
| 922 return null; | 921 return null; |
| 923 | 922 |
| 924 var tiles = this.getTiles(); | 923 var tiles = this.getTiles(); |
| 925 if (tiles.length === 1) | 924 if (tiles.length === 1) |
| 926 return null; // Single tile layout is always the same. | 925 return null; // Single tile layout is always the same. |
| 927 | 926 |
| 928 var tileHeights = tiles.map(function(t) { return t.getMaxContentHeight() }); | 927 var tileHeights = tiles.map(function(t) { return t.getMaxContentHeight(); }); |
| 929 var minTileHeight = Math.min.apply(null, tileHeights); | 928 var minTileHeight = Math.min.apply(null, tileHeights); |
| 930 | 929 |
| 931 for (var h = minTileHeight; h < this.viewportHeight_; h += minTileHeight) { | 930 for (var h = minTileHeight; h < this.viewportHeight_; h += minTileHeight) { |
| 932 var layout = new Mosaic.Layout( | 931 var layout = new Mosaic.Layout( |
| 933 Mosaic.Layout.MODE_DRY_RUN, this.density_.clone()); | 932 Mosaic.Layout.MODE_DRY_RUN, this.density_.clone()); |
| 934 layout.setViewportSize(this.viewportWidth_, h); | 933 layout.setViewportSize(this.viewportWidth_, h); |
| 935 for (var t = 0; t !== tiles.length; t++) | 934 for (var t = 0; t !== tiles.length; t++) |
| 936 layout.add(tiles[t], t + 1 === tiles.length); | 935 layout.add(tiles[t], t + 1 === tiles.length); |
| 937 | 936 |
| 938 if (layout.getWidth() <= this.viewportWidth_) | 937 if (layout.getWidth() <= this.viewportWidth_) |
| (...skipping 1040 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1979 return new Rect(this.left_ - this.container_.scrollLeft, this.top_, | 1978 return new Rect(this.left_ - this.container_.scrollLeft, this.top_, |
| 1980 this.width_, this.height_).inflate(-margin, -margin); | 1979 this.width_, this.height_).inflate(-margin, -margin); |
| 1981 }; | 1980 }; |
| 1982 | 1981 |
| 1983 /** | 1982 /** |
| 1984 * @return {number} X coordinate of the tile center. | 1983 * @return {number} X coordinate of the tile center. |
| 1985 */ | 1984 */ |
| 1986 Mosaic.Tile.prototype.getCenterX = function() { | 1985 Mosaic.Tile.prototype.getCenterX = function() { |
| 1987 return this.left_ + Math.round(this.width_ / 2); | 1986 return this.left_ + Math.round(this.width_ / 2); |
| 1988 }; | 1987 }; |
| OLD | NEW |