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 // 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 801 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 // leftMargin centers the grid within the avaiable space. | 812 // leftMargin centers the grid within the avaiable space. |
813 var minMargin = wide ? MIN_WIDE_MARGIN : 0; | 813 var minMargin = wide ? MIN_WIDE_MARGIN : 0; |
814 var leftMargin = | 814 var leftMargin = |
815 Math.max(minMargin, | 815 Math.max(minMargin, |
816 (this.tileGrid_.clientWidth - effectiveGridWidth) / 2); | 816 (this.tileGrid_.clientWidth - effectiveGridWidth) / 2); |
817 | 817 |
818 var rowHeight = this.heightForWidth(realTileValues.tileWidth) + | 818 var rowHeight = this.heightForWidth(realTileValues.tileWidth) + |
819 realTileValues.interTileSpacing; | 819 realTileValues.interTileSpacing; |
820 | 820 |
821 this.layoutValues_ = { | 821 this.layoutValues_ = { |
| 822 colWidth: realTileValues.offsetX, |
| 823 gridWidth: effectiveGridWidth, |
| 824 leftMargin: leftMargin, |
822 numRowTiles: numRowTiles, | 825 numRowTiles: numRowTiles, |
823 leftMargin: leftMargin, | |
824 colWidth: realTileValues.offsetX, | |
825 rowHeight: rowHeight, | 826 rowHeight: rowHeight, |
826 tileWidth: realTileValues.tileWidth, | 827 tileWidth: realTileValues.tileWidth, |
827 wide: wide, | 828 wide: wide, |
828 }; | 829 }; |
829 | 830 |
830 // We need to update the top margin as well. | 831 // We need to update the top margin as well. |
831 this.updateTopMargin_(); | 832 this.updateTopMargin_(); |
832 | 833 |
833 this.firePageLayoutEvent_(); | 834 this.firePageLayoutEvent_(); |
834 }, | 835 }, |
835 | 836 |
836 /** | 837 /** |
837 * Dispatches the custom pagelayout event. | 838 * Dispatches the custom pagelayout event. |
838 * @private | 839 * @private |
839 */ | 840 */ |
840 firePageLayoutEvent_: function() { | 841 firePageLayoutEvent_: function() { |
841 cr.dispatchSimpleEvent(this, 'pagelayout', true, true); | 842 cr.dispatchSimpleEvent(this, 'pagelayout', true, true); |
842 }, | 843 }, |
843 | 844 |
844 /** | 845 /** |
| 846 * @return {number} The amount of margin that should be animated (in pixels) |
| 847 * for the current grid layout. |
| 848 */ |
| 849 getAnimatedLeftMargin_: function() { |
| 850 if (this.layoutValues_.wide) |
| 851 return 0; |
| 852 |
| 853 var grid = this.gridValues_; |
| 854 return (grid.minWideWidth - MIN_WIDE_MARGIN - grid.narrowWidth) / 2; |
| 855 }, |
| 856 |
| 857 /** |
845 * Calculates the x/y coordinates for an element and moves it there. | 858 * Calculates the x/y coordinates for an element and moves it there. |
846 * @param {number} index The index of the element to be positioned. | 859 * @param {number} index The index of the element to be positioned. |
847 * @param {number} indexOffset If provided, this is added to |index| when | 860 * @param {number} indexOffset If provided, this is added to |index| when |
848 * positioning the tile. The effect is that the tile will be positioned | 861 * positioning the tile. The effect is that the tile will be positioned |
849 * in a non-default location. | 862 * in a non-default location. |
850 * @private | 863 * @private |
851 */ | 864 */ |
852 positionTile_: function(index, indexOffset) { | 865 positionTile_: function(index, indexOffset) { |
853 var grid = this.gridValues_; | 866 var grid = this.gridValues_; |
854 var layout = this.layoutValues_; | 867 var layout = this.layoutValues_; |
855 | 868 |
856 indexOffset = typeof indexOffset != 'undefined' ? indexOffset : 0; | 869 indexOffset = typeof indexOffset != 'undefined' ? indexOffset : 0; |
857 // Add the offset _after_ the modulus division. We might want to show the | 870 // Add the offset _after_ the modulus division. We might want to show the |
858 // tile off the side of the grid. | 871 // tile off the side of the grid. |
859 var col = index % layout.numRowTiles + indexOffset; | 872 var col = index % layout.numRowTiles + indexOffset; |
860 var row = Math.floor(index / layout.numRowTiles); | 873 var row = Math.floor(index / layout.numRowTiles); |
861 // Calculate the final on-screen position for the tile. | 874 // Calculate the final on-screen position for the tile. |
862 var realX = col * layout.colWidth + layout.leftMargin; | 875 var realX = col * layout.colWidth + layout.leftMargin; |
863 var realY = row * layout.rowHeight; | 876 var realY = row * layout.rowHeight; |
864 | 877 |
865 // Calculate the portion of the tile's position that should be animated. | 878 // Calculate the portion of the tile's position that should be animated. |
866 var animatedTileValues = layout.wide ? | 879 var animatedTileValues = layout.wide ? |
867 grid.wideTileValues : grid.narrowTileValues; | 880 grid.wideTileValues : grid.narrowTileValues; |
868 // Animate the difference between three-wide and six-wide. | 881 // Animate the difference between three-wide and six-wide. |
869 var animatedLeftMargin = layout.wide ? | 882 var animatedLeftMargin = this.getAnimatedLeftMargin_(); |
870 0 : (grid.minWideWidth - MIN_WIDE_MARGIN - grid.narrowWidth) / 2; | |
871 var animatedX = col * animatedTileValues.offsetX + animatedLeftMargin; | 883 var animatedX = col * animatedTileValues.offsetX + animatedLeftMargin; |
872 var animatedY = row * (this.heightForWidth(animatedTileValues.tileWidth) + | 884 var animatedY = row * (this.heightForWidth(animatedTileValues.tileWidth) + |
873 animatedTileValues.interTileSpacing); | 885 animatedTileValues.interTileSpacing); |
874 | 886 |
875 var tile = this.tileElements_[index]; | 887 var tile = this.tileElements_[index]; |
876 tile.setGridPosition(animatedX, animatedY); | 888 tile.setGridPosition(animatedX, animatedY); |
877 tile.firstChild.setBounds(layout.tileWidth, | 889 tile.firstChild.setBounds(layout.tileWidth, |
878 realX - animatedX, | 890 realX - animatedX, |
879 realY - animatedY); | 891 realY - animatedY); |
880 | 892 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1014 this.topMarginIsForWide_ = layout.wide; | 1026 this.topMarginIsForWide_ = layout.wide; |
1015 this.topMarginPx_ = newMargin; | 1027 this.topMarginPx_ = newMargin; |
1016 this.topMargin_.style.marginTop = | 1028 this.topMargin_.style.marginTop = |
1017 toCssPx(this.topMarginPx_ - this.animatedTopMarginPx_); | 1029 toCssPx(this.topMarginPx_ - this.animatedTopMarginPx_); |
1018 }, | 1030 }, |
1019 | 1031 |
1020 /** | 1032 /** |
1021 * Position the notification if there's one showing. | 1033 * Position the notification if there's one showing. |
1022 */ | 1034 */ |
1023 positionNotification_: function() { | 1035 positionNotification_: function() { |
1024 if (this.notification && !this.notification.hidden) { | 1036 var notification = this.notification; |
1025 this.notification.style.margin = | 1037 if (!notification || notification.hidden) |
1026 -this.notification.offsetHeight + 'px ' + | 1038 return; |
1027 this.layoutValues_.leftMargin + 'px 0'; | 1039 |
1028 } | 1040 // Update the horizontal position. |
| 1041 var animatedLeftMargin = this.getAnimatedLeftMargin_(); |
| 1042 notification.style.WebkitMarginStart = animatedLeftMargin + 'px'; |
| 1043 var leftOffset = (this.layoutValues_.leftMargin - animatedLeftMargin) * |
| 1044 (isRTL() ? -1 : 1); |
| 1045 notification.style.WebkitTransform = 'translateX(' + leftOffset + 'px)'; |
| 1046 |
| 1047 // Update the allowable widths of the text. |
| 1048 var buttonWidth = notification.querySelector('button').offsetWidth + 8; |
| 1049 notification.querySelector('span').style.maxWidth = |
| 1050 this.layoutValues_.gridWidth - buttonWidth + 'px'; |
| 1051 |
| 1052 // This makes sure the text doesn't condense smaller than the narrow size |
| 1053 // of the grid (e.g. when a user makes the window really small). |
| 1054 notification.style.minWidth = |
| 1055 this.gridValues_.narrowWidth - buttonWidth + 'px'; |
| 1056 |
| 1057 // Update the top position. |
| 1058 notification.style.marginTop = -notification.offsetHeight + 'px'; |
1029 }, | 1059 }, |
1030 | 1060 |
1031 /** | 1061 /** |
1032 * Handles final setup that can only happen after |this| is inserted into | 1062 * Handles final setup that can only happen after |this| is inserted into |
1033 * the page. | 1063 * the page. |
1034 * @private | 1064 * @private |
1035 */ | 1065 */ |
1036 onNodeInsertedIntoDocument_: function(e) { | 1066 onNodeInsertedIntoDocument_: function(e) { |
1037 this.calculateLayoutValues_(); | 1067 this.calculateLayoutValues_(); |
1038 this.heightChanged_(); | 1068 this.heightChanged_(); |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1338 assert(false); | 1368 assert(false); |
1339 }, | 1369 }, |
1340 }; | 1370 }; |
1341 | 1371 |
1342 return { | 1372 return { |
1343 getCurrentlyDraggingTile: getCurrentlyDraggingTile, | 1373 getCurrentlyDraggingTile: getCurrentlyDraggingTile, |
1344 setCurrentDropEffect: setCurrentDropEffect, | 1374 setCurrentDropEffect: setCurrentDropEffect, |
1345 TilePage: TilePage, | 1375 TilePage: TilePage, |
1346 }; | 1376 }; |
1347 }); | 1377 }); |
OLD | NEW |