OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 // Handle the bookmark bar and theme change requests from the C++ side. | 5 // Handle the bookmark bar and theme change requests from the C++ side. |
6 var ntp = { | 6 var ntp = { |
7 /** @param {string} attached */ | 7 /** @param {string} attached */ |
8 setBookmarkBarAttached: function(attached) { | 8 setBookmarkBarAttached: function(attached) { |
9 document.documentElement.setAttribute('bookmarkbarattached', attached); | 9 document.documentElement.setAttribute('bookmarkbarattached', attached); |
10 }, | 10 }, |
11 | 11 |
12 /** @param {!{hasCustomBackground: boolean}} themeData */ | 12 /** @param {!{hasCustomBackground: boolean}} themeData */ |
13 themeChanged: function(themeData) { | 13 themeChanged: function(themeData) { |
14 document.documentElement.setAttribute('hascustombackground', | 14 document.documentElement.setAttribute('hascustombackground', |
15 themeData.hasCustomBackground); | 15 themeData.hasCustomBackground); |
16 $('incognitothemecss').href = | 16 $('incognitothemecss').href = |
17 'chrome://theme/css/incognito_new_tab_theme.css?' + Date.now(); | 17 'chrome://theme/css/incognito_new_tab_theme.css?' + Date.now(); |
18 }, | 18 }, |
19 }; | 19 }; |
20 | 20 |
21 // Let the width of two lists of bulletpoints in a horizontal alignment | 21 // Let the width of two lists of bulletpoints in a horizontal alignment |
22 // determine the maximum content width. | 22 // determine the maximum content width. |
23 window.addEventListener('load', function() { | 23 function recomputeLayoutWidth() { |
24 var bulletpoints = document.querySelectorAll('.bulletpoints'); | 24 var bulletpoints = document.querySelectorAll('.bulletpoints'); |
25 var content = document.querySelector('.content'); | 25 var content = document.querySelector('.content'); |
26 | 26 |
27 // Unless this is the first load of the Incognito NTP in this session and | 27 // Unless this is the first load of the Incognito NTP in this session and |
28 // with this font size, we already have the maximum content width determined. | 28 // with this font size, we already have the maximum content width determined. |
29 var fontSize = window.getComputedStyle(document.body).fontSize; | 29 var fontSize = window.getComputedStyle(document.body).fontSize; |
30 var maxWidth = localStorage[fontSize] || | 30 var maxWidth = localStorage[fontSize] || |
31 (bulletpoints[0].offsetWidth + bulletpoints[1].offsetWidth + | 31 (bulletpoints[0].offsetWidth + bulletpoints[1].offsetWidth + |
32 40 /* margin */ + 2 /* offsetWidths may be rounded down */); | 32 40 /* margin */ + 2 /* offsetWidths may be rounded down */); |
33 | 33 |
34 // Save the data for quicker access when the NTP is reloaded. Note that since | 34 // Save the data for quicker access when the NTP is reloaded. Note that since |
35 // we're in the Incognito mode, the local storage is ephemeral and the data | 35 // we're in the Incognito mode, the local storage is ephemeral and the data |
36 // will be discarded when the session ends. | 36 // will be discarded when the session ends. |
37 localStorage[fontSize] = maxWidth; | 37 localStorage[fontSize] = maxWidth; |
38 | 38 |
39 // Limit the maximum width to 600px. That might force the two lists | 39 // Limit the maximum width to 600px. That might force the two lists |
40 // of bulletpoints under each other, in which case we must swap the left | 40 // of bulletpoints under each other, in which case we must swap the left |
41 // and right margin. | 41 // and right margin. |
42 if (maxWidth > 600) { | 42 if (maxWidth > 600) { |
43 maxWidth = 600; | 43 maxWidth = 600; |
44 | 44 |
45 bulletpoints[1].classList.add('tooWide'); | 45 bulletpoints[1].classList.add('tooWide'); |
46 } else { | |
47 bulletpoints[1].classList.remove('tooWide'); | |
Dan Beam
2017/05/24 03:09:19
nit:
var MAX_ALLOWED_WIDTH = 600;
var tooWide =
msramek
2017/05/24 12:56:02
bulletpoints[1].classList.add('tooWide', tooWide)
Dan Beam
2017/05/24 17:43:54
sorry, classList.toggle
msramek
2017/05/24 23:16:37
Ah, right. Done.
| |
46 } | 48 } |
47 | 49 |
48 content.style.maxWidth = maxWidth + "px"; | 50 content.style.maxWidth = maxWidth + "px"; |
51 }; | |
Dan Beam
2017/05/24 03:09:19
no semi
msramek
2017/05/24 12:56:02
Done.
| |
52 | |
53 window.addEventListener('load', function() { | |
54 recomputeLayoutWidth(); | |
55 | |
56 var fontSizeEstimator = $('font-size-estimator'); | |
57 if (ResizeObserver) { | |
Dan Beam
2017/05/24 03:09:19
errr, why do you need to feature detect this?
msramek
2017/05/24 12:56:02
It has existed for a few milestones, but it's stil
| |
58 var observer = new ResizeObserver(recomputeLayoutWidth); | |
59 observer.observe(fontSizeEstimator); | |
Dan Beam
2017/05/24 03:09:19
new ResizeObserver(recomputeLayoutWidth).observe(f
msramek
2017/05/24 12:56:02
Acknowledged, but I removed this code.
| |
60 } else { | |
61 var currentFontSize = window.getComputedStyle(document.body).fontSize; | |
62 | |
63 setInterval(function() { | |
64 if (window.getComputedStyle(document.body).fontSize == currentFontSize) | |
Dan Beam
2017/05/24 03:09:19
why are you calling this so much? this is a mildl
msramek
2017/05/24 12:56:02
Acknowledged, but I removed this code.
| |
65 return; | |
66 | |
67 currentFontSize = window.getComputedStyle(document.body).fontSize; | |
68 recomputeLayoutWidth(); | |
69 }, 1000 /* ms */); | |
70 } | |
49 }); | 71 }); |
OLD | NEW |