1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
|
<!DOCTYPE HTML>
<html id="t" jsvalues="dir:textdirection;firstview:firstview;bookmarkbarattached:bookmarkbarattached;hasattribution:hasattribution;anim:anim">
<!--
This page is optimized for perceived performance. Our enemies are the time
taken for the backend to generate our data, and the time taken to parse
and render the starting HTML/CSS content of the page. This page is
designed to let Chrome do both of those things in parallel.
1. Defines temporary content callback functions
2. Fires off requests for content (these can come back 20-150ms later)
3. Defines basic functions (handlers)
4. Renders a fast-parse hard-coded version of itself (this can take 20-50ms)
5. Defines the full content-rendering functions
If the requests for content come back before the content-rendering functions
are defined, the data is held until those functions are defined.
-->
<script src="local_strings.js"></script>
<script>
/* Logging info for benchmarking purposes. */
var log = [];
function logEvent(name) {
log.push([name, Date.now()]);
}
// Basic functions to send, receive, store and process the data from our
// backend.
var unprocessedData = {
mostVisitedPages : false,
searchURLs : false,
recentlyBookmarked : false,
recentlyClosedTabs : false
}
var renderFunctionsDefined = false;
var localStrings;
// The list of URLs that have been blacklisted (so that thumbnails are not
// shown) in the last set of change. Used to revert changes when the Cancel
// button is pressed.
var blacklistedURLs = [];
function $(o) {return document.getElementById(o);}
/**
* If the functions that can render content are defined, render
* the content for any data we've received so far.
*/
function processData() {
if (renderFunctionsDefined) {
if (unprocessedData.mostVisitedPages) {
renderMostVisitedPages(unprocessedData.mostVisitedPages);
unprocessedData.mostVisitedPages = false;
}
if (unprocessedData.searchURLs) {
renderSearchURLs(unprocessedData.searchURLs);
unprocessedData.searchURLs = false;
}
if (unprocessedData.recentlyBookmarked) {
renderRecentlyBookmarked(unprocessedData.recentlyBookmarked);
unprocessedData.recentlyBookmarked = false;
}
if (unprocessedData.recentlyClosedTabs) {
renderRecentlyClosedTabs(unprocessedData.recentlyClosedTabs);
unprocessedData.recentlyClosedTabs = false;
}
}
}
function mostVisitedPages(data) {
logEvent('received most visited pages');
unprocessedData.mostVisitedPages = data;
processData();
}
function searchURLs(data) {
logEvent('received search URLs');
unprocessedData.searchURLs = data;
processData();
}
function recentlyBookmarked(data) {
logEvent('received recently bookmarked data');
unprocessedData.recentlyBookmarked = data;
processData();
}
function recentlyClosedTabs(data) {
logEvent('received recently closed tabs');
unprocessedData.recentlyClosedTabs = data;
processData();
}
function resizeP13N(new_height) {
var childf = document.getElementById('p13n');
if (new_height < 1) {
childf.style.display = "none";
return;
}
childf.height = new_height;
childf.style.display = "block";
}
chrome.send('getMostVisited');
chrome.send('getMostSearched');
chrome.send('getRecentlyBookmarked');
chrome.send('getRecentlyClosedTabs');
function handleWindowResize() {
if (!document.body || document.body.clientWidth < 10) {
// We're probably a background tab, so don't do anything.
return;
}
if (document.body.className == 'small' && document.body.clientWidth >= 885) {
document.body.className = '';
} else if (document.body.className == '' && document.body.clientWidth <= 865) {
document.body.className = 'small';
}
}
function handleDOMContentLoaded() {
logEvent('domcontentloaded fired');
}
logEvent('log start');
</script>
<head>
<meta charset="utf-8">
<title jscontent="title"></title>
<style>
html {
height:100%;
}
body {
margin:0px;
}
html[firstview='true'] #main {
opacity:0.0;
-webkit-transition:all 0.4s;
}
html[firstview='true'] #main.visible {
opacity:1.0;
}
html[anim='false'] * {
-webkit-transition-property: none !important
}
#main {
margin-left:auto;
margin-right:auto;
margin-top:10px;
}
form {
padding: 0;
margin: 0;
}
.section {
padding:3px 0px 5px 0px;
margin-bottom:30px;
}
.section-title {
line-height:19pt;
font-size:110%;
font-weight:bold;
margin-bottom:4px;
}
#mostvisitedsection {
margin:0px 5px 0px 0px;
}
#mostvisited td {
padding:0px 10px 10px 0px;
}
html[dir='rtl'] #mostvisited td {
padding:0px 0px 10px 10px;
}
.most-visited-text {
width:548px; /* thumbnail + td * 3 - 2*padding - 2*margin */
padding:20px;
margin:15px;
background-color:white;
-webkit-box-shadow: 5px 5px 10px #ccc;
-webkit-transition:all 0.12s;
}
.thumbnail-title {
background-image:url(chrome://favicon/);
display:block;
background-repeat:no-repeat;
background-size:16px;
background-position:0px 1px;
width:172px; /* thumbnail - padding */
margin-top:6px; /* line up favicons with search favicons */
padding:1px 0px 4px 22px;
overflow: hidden;
text-overflow: ellipsis;
text-decoration:none;
-webkit-transition:all 0.12s;
}
html[dir='rtl'] .thumbnail-title {
background-position:right;
padding-left:0px;
padding-right:22px;
text-align:right;
}
.thumbnail {
width:195px;
height:136px;
border:1px solid #ccc;
background-color:#eee;
-webkit-transition:all 0.12s;
}
a.thumbnail {
border:1px solid #abe;
}
.small .thumbnail-title {
width:127px;
}
.small .thumbnail {
width:150px;
height:113px;
}
.small .most-visited-text {
width:430px;
padding:15px;
margin:12px;
}
.recent-bookmark {
display:block;
background-repeat:no-repeat;
background-size:16px;
background-position:0px 1px;
padding:1px 0px 0px 22px;
margin:3px 0px 3px 0px;
min-height:16pt;
line-height:16px;
overflow: hidden;
text-overflow: ellipsis;
text-decoration:underline;
}
.recent-window-container {
line-height:16px;
display:block;
margin:3px 0px 3px 0px;
padding-left:22px;
}
.recent-window-container img {
margin:0 3px -2px 3px;
}
.recent-window-hover-container {
position:absolute;
border:1px solid #999;
-webkit-box-shadow: 5px 5px 10px #ccc;
background-color:white;
width: 157px;
left: 20px;
white-space:nowrap;
opacity:.9;
}
.recent-window-hover-container .recent-bookmark {
text-decoration:none;
text-overflow:ellipsis;
overflow:hidden;
margin: 3px 0 0 5px;
}
.recently-closed-window-link {
'text-decoration:none';
}
.recently-closed-window-link:hover {
cursor:pointer;
}
.recently-close-window-text {
text-decoration:underline;
}
html[dir='rtl'] .recent-bookmark {
background-position:right;
padding-left:0px;
padding-right:22px;
}
a {
color:#0000cc;
text-decoration:underline;
white-space: nowrap;
}
a.manage {
color:#77c;
margin-left: 5px;
margin-right: 5px;
line-height:19pt;
text-decoration:underline;
}
html[dir='rtl'] #managesearcheslink {
float: left;
}
.sidebar {
width: 207px;
padding:3px 10px 3px 9px;
-webkit-border-radius:5px 5px;
margin-bottom:10px;
}
html[dir='rtl'] #recentlyBookmarkedContainer {
text-align:right;
}
#recentlyClosedContainer {
position:relative;
}
html[dir='rtl'] #recentlyClosedContainer {
text-align:right;
}
#searches input {
border:1px solid #7f9db9;
background-repeat: no-repeat;
background-position:4px center;
padding-left: 23px;
min-height:24px;
width:182px;
margin-bottom:8px;
display:block;
}
html[dir='rtl'] #searches input {
background-position: right;
padding-left:0px;
padding-right: 23px;
}
#searches input:-webkit-input-placeholder-mode {
color: #aaa;
}
html[hasattribution='false'] .attribution {
display:none;
}
.footer {
border-top:1px solid #ccc;
padding-top:4px;
font-size:8pt;
}
.edit-visible {
display: none;
}
.edit-mode .edit-visible {
display: inline;
}
.non-edit-visible {
display: inline;
}
.edit-mode .non-edit-visible {
display: none;
}
.most-visited-container {
position: relative;
left: 0px;
top: 0px;
}
.edit-mode .disabled-on-edit {
opacity: 0.5;
pointer-events: none; /* Disable clicks */
}
</style>
<link id="themecss" rel="stylesheet" href="chrome://theme/css/newtab.css" />
</head>
<body onload="updateAttribution(); logEvent('body onload fired');"
jsvalues=".style.fontFamily:fontfamily;.style.fontSize:fontsize">
<div id="l10n" style="display:none;">
<span id="closedwindowsingle" jscontent="closedwindowsingle">1 Tab</span>
<span id="closedwindowmultiple" jscontent="closedwindowmultiple">% Tabs</span>
</div>
<script>
// We apply the size class here so that we don't trigger layout animations onload.
handleWindowResize();
window.addEventListener('resize', handleWindowResize, true);
document.addEventListener('DOMContentLoaded', handleDOMContentLoaded);
</script>
<table id="main" cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="top">
<div id="mostvisitedsection" class="section">
<div id="mostvisited" style="position:relative;">
<div>
<span class="section-title non-edit-visible" jscontent="mostvisited"></span>
<span class="section-title edit-visible" jseval="this.innerHTML = $this.editmodeheading;"></span>
</div>
<div id="mostvisitedintro" style="display:none;">
<div class="most-visited-text" style="position:absolute;" jseval="this.innerHTML = $this.mostvisitedintro;"></div>
<table>
<tr>
<td><div class="thumbnail"> </div></td>
<td><div class="thumbnail"></div></td>
<td><div class="thumbnail"> </div></td>
</tr>
<tr>
<td><div class="thumbnail"> </div></td>
<td><div class="thumbnail"></div></td>
<td><div class="thumbnail"> </div></td>
</tr>
</table>
</div>
<table id="mostvisitedtable">
<!-- This content forces the view to the correct width and provides a
preview of what's to load to reduce white-flash. Users who get
the mostvisitedintro will see a brief flash of this content. We
only use one row so that we may avoid flashing extra rows when
the user has only one row of items -->
<tr>
<td>
<div class="thumbnail-title"> </div>
<div class="thumbnail"></div>
</td>
<td>
<div class="thumbnail-title"> </div>
<div class="thumbnail"></div>
</td>
<td>
<div class="thumbnail-title"> </div>
<div class="thumbnail"></div>
</td>
</tr>
</table>
</div>
<a href="#"
class="manage non-edit-visible"
onclick="enterEditMode(); return false"
id="editthumbnails">
<span jscontent="editthumbnails"></span></a>
<button type="button" class="edit-visible" onclick="exitEditMode();"
jscontent="doneediting"></button>
<button type="button" class="edit-visible" onclick="cancelEdits();"
jscontent="cancelediting"></button>
<a href="#"
class="manage edit-visible"
onclick="restoreThumbnails(); return false">
<span jscontent="restorethumbnails"></span></a>
<a href="#"
jsvalues="href:showhistoryurl"
class="manage non-edit-visible">
<span jscontent="showhistory"></span> »</a>
</div>
</td>
<td valign="top" width="230">
<div align="right">
<img src="../../app/theme/%DISTRIBUTION%/product_logo.png"
width="145" height="52" style="padding-bottom:8px;" />
</div>
<iframe id="p13n" frameborder="0" width="100%" scrolling="no" height="0"
jsdisplay="p13nsrc" style="display:none;"
jsvalues="src:p13nsrc"></iframe>
<div id="searches" class="sidebar themed">
<div class="section-title" jscontent="searches"></div>
<form onsubmit="chrome.send('searchHistoryPage', [this.search.value]); return false;">
<input type="text" class="hint"
name="search"
style="background-image:url(chrome://favicon/);"
jsvalues="placeholder:searchhistory">
</form>
<div id='searches-entries'></div>
</div>
<div id="recentlyBookmarked" class="sidebar themed" style="display:none">
<span class="section-title" jscontent="bookmarks"></span>
<div id="recentlyBookmarkedContainer"></div>
</div>
<div id="recentlyClosedTabs" class="sidebar" style="display:none">
<div class="section-title" jscontent="recentlyclosed"></div>
<div id="recentlyClosedContainer"></div>
</div>
<div id="attribution" class="sidebar attribution">
<span jscontent="attributionintro"></span><br />
<img id="attribution-img" />
</div>
</td>
</tr>
</table>
<script>
logEvent('start of second script block');
/* Return a DOM element with tag name |elem| and attributes |attrs|. */
function DOM(elem, attrs) {
var elem = document.createElement(elem);
for (var attr in attrs) {
elem[attr] = attrs[attr];
}
return elem;
}
/**
* Partially applies this function to a particular 'this object' and zero or
* more arguments. The result is a new function with some arguments of the first
* function pre-filled and the value of |this| 'pre-specified'.<br><br>
*
* Remaining arguments specified at call-time are appended to the pre-
* specified ones.<br><br>
*
* @param {Function} fn A function to partially apply.
* @param {Object} selfObj Specifies the object which |this| should point to
* when the function is run.
* @param {Object} var_args Additional arguments that are partially
* applied to the function.
*
* @return {!Function} A partially-applied form of the function bind() was
* invoked as a method of.
*/
function bind(fn, selfObj, var_args) {
var boundArgs = Array.prototype.slice.call(arguments, 2);
return function() {
var args = Array.prototype.slice.call(arguments);
args.unshift.apply(args, boundArgs);
return fn.apply(selfObj, args);
}
}
/* Return the DOM element for a "most visited" entry.
|page| should be an object with "title", "url", and "direction" fields. */
function makeMostVisitedDOM(page, number) {
/* The HTML we want looks like this:
<a class="disabled-on-edit" href="URL" title="gmail.com">
<div class="thumbnail-title disabled-on-edit"
style="background-image:url(faviconurl);direction:ltr">gmail.com</div>
<img class="thumbnail disabled-on-edit"
style="background-image:url(thumbnailurl);" />
</a>
*/
var root;
if (page.url) {
root = DOM('a', {className:'disabled-on-edit',
href:page.url,
title:page.title});
root.addEventListener("mousedown", function(event) {
chrome.send("metrics", ["NTP_MostVisited" + number])
}, false);
} else {
// Something went wrong; don't make it clickable.
root = DOM('span');
}
/* Create the thumbnail */
var img_thumbnail = DOM('img', {className:'thumbnail disabled-on-edit'});
img_thumbnail.setAttribute('onload', "logEvent('image loaded');");
img_thumbnail.src = 'chrome://thumb/' + page.url;
/* Create the title */
var div_title = DOM('div', {className:'thumbnail-title disabled-on-edit'});
div_title.style.backgroundImage =
'url("chrome://favicon/' + page.url + '")';
/* Set the title's directionality independently of the overall page
directionality. We need to do this since a purely LTR title should always
have it's direction set as ltr. We only set the title direction to rtl if
it contains a strong RTL character. Please refer to http://crbug.com/5926
for more information.
*/
div_title.style.direction = page.direction;
if (page.title) {
div_title.appendChild(document.createTextNode(page.title));
} else {
// Make the empty title at least push down the icon.
div_title.innerHTML = ' ';
}
root.appendChild(div_title);
root.appendChild(img_thumbnail);
return root;
}
/* Return the DOM element for the cross that should be displayed in edit-mode
over a "most visited" entry. */
function makeCrossImageDOM(url) {
var cross = DOM('div', {className:'edit-cross'});
cross.addEventListener("mousedown",
function(event) {
if (event.which == 1) // Left click only.
blacklistURL(url); },
false);
return cross;
}
/* This function is called by the browser with the most visited pages list.
|pages| is a list of page objects, which have url, title, and direction
attributes. */
function renderMostVisitedPages(pages) {
logEvent('renderMostVisitedPages called: ' + pages.length);
var table = document.getElementById("main");
// If we were in edit-mode, stay in that mode as this means this is a
// refresh triggered by thumbnails editing.
if (table.className.indexOf('edit-mode') != -1)
table.className = 'visible edit-mode';
else
table.className = 'visible';
var table = document.getElementById("mostvisitedtable");
table.innerHTML = '';
// Show the most visited helptext if most visited is still useless. This is
// a crappy heuristic.
if (pages.length < 3) {
$('mostvisitedintro').style.display = 'block';
$('editthumbnails').style.display = 'none';
return;
}
$('mostvisitedintro').style.display = 'none';
// Create the items and add them to rows.
var rows = [];
var rowNum = -1;
for (var i = 0, page; page = pages[i]; ++i) {
if (i % 3 == 0) {
rowNum += 1;
rows[rowNum] = DOM('tr', {});
}
var cell = DOM('td');
var container = DOM('div', { className: "most-visited-container"});
container.appendChild(makeCrossImageDOM(page.url));
container.appendChild(makeMostVisitedDOM(page, i));
cell.appendChild(container);
rows[rowNum].appendChild(cell);
logEvent('mostVisitedPage : ' + i);
}
// Add the rows to the table.
for (var i = 0, row; row = rows[i]; i++) {
table.appendChild(row);
}
logEvent('renderMostVisitedPages done');
}
function makeSearchURL(url) {
/* The HTML we want looks like this:
<form>
<input type="text" class="hint"
style="background-image:url(chrome://favicon/"+url+");"
placeholder="Search Wikipedia">
</form>
*/
var input = DOM('input', {type:'text',
className: 'hint'});
// There is no DOM property for placeholder.
input.setAttribute('placeholder', url.short_name);
input.keyword = url.keyword;
if (url.favIconURL) {
input.style.backgroundImage =
'url("chrome://favicon/iconurl/' + url.favIconURL + '")';
} else {
input.style.backgroundImage =
'url("chrome://favicon/http://' + url.short_name + '")';
}
var form = DOM('form');
form.onsubmit = function() {
chrome.send('doSearch', [input.keyword, input.value]);
return false;
};
form.appendChild(input);
return form;
}
/* This function is called by the browser when the list of search URLs is
available. |urls| is a list of objects with |name| attributes. */
function renderSearchURLs(urls) {
logEvent('renderSearchURLs called: ' + urls.length);
var container = document.getElementById('searches-entries');
container.innerHTML = ''; // Clear out any previous contents.
if (urls.length > 0) {
document.getElementById('searches').style.display = 'block';
for (var i = 0; i < urls.length; ++i) {
container.appendChild(makeSearchURL(urls[i]));
}
}
logEvent('renderSearchURLs done');
}
/* This function is called by the browser when the list of recently bookmarked
URLs is available. |entries| is a list of objects with title, url, and
direction attributes. */
function renderRecentlyBookmarked(entries) {
logEvent('renderRecentlyBookmarked called: ' + entries.length);
var section = document.getElementById('recentlyBookmarked');
var container = document.getElementById('recentlyBookmarkedContainer');
/* recentlyBookmarked is called any time the bookmarks change. Remove existing
entries before adding new ones. */
section.style.display = 'none';
container.innerHTML = '';
if (entries.length > 0) {
section.style.display = 'block';
for (var i = 0, entry = entries[0]; entry = entries[i]; ++i) {
var link = DOM('a', {href: entry.url,
className:'recent-bookmark',
title:entry.title});
link.addEventListener("mousedown", function(event) {
chrome.send("metrics", ["NTP_Bookmark" + i])
}, false);
link.style.backgroundImage =
'url("chrome://favicon/' + entry.url + '")';
/* Set the bookmark title's directionality independently of the page, see
comment about setting div_title.style.direction above for details.
*/
link.style.direction = entry.direction;
link.appendChild(document.createTextNode(entry.title));
container.appendChild(link);
}
}
logEvent('renderRecentlyBookmarked done');
}
/* This function adds incoming information about tabs to the new tab UI. */
function renderRecentlyClosedTabs(entries) {
logEvent('renderRecentlyClosedTabs begin');
var section = document.getElementById('recentlyClosedTabs');
var container = document.getElementById('recentlyClosedContainer');
/* recentlyClosedTabs is called on every internal event which
affects tab history to make sure things are up to
date. Therefore, reset the recentlyClosedTabs state on every
call. */
section.style.display = 'none';
container.innerHTML = '';
if (entries.length > 0) {
section.style.display = 'block';
for (var i = 0; entry = entries[i]; ++i) {
var link;
if (entry.type == "tab") {
// Closed tab.
link = createRecentBookmark('a', entry);
container.appendChild(link);
} else {
// Closed window.
var linkSpanContainer = DOM('div', { className: 'recent-window-container'});
var linkSpan = DOM('span');
linkSpan.appendChild(document.createTextNode(" "));
for (var windowIndex = 0; windowIndex < entry.tabs.length; windowIndex++) {
var tab = entry.tabs[windowIndex];
var tabImg = DOM('img', {
src:'url("chrome://favicon/' + tab.url + '")',
width:16,
height:16});
tabImg.onmousedown = function() { return false; }
linkSpan.appendChild(tabImg);
}
link = DOM('span', { className: 'recently-closed-window-link' } );
windowSpan = DOM('span', {className: 'recently-close-window-text'});
windowSpan.appendChild(document.createTextNode(
recentlyClosedWindowText(entry.tabs.length)));
link.appendChild(windowSpan);
link.appendChild(linkSpan);
linkSpanContainer.appendChild(link);
container.appendChild(linkSpanContainer);
// The card takes care of appending itself to the DOM, so no need to
// keep a reference to it.
new RecentlyClosedHoverCard(linkSpanContainer, entry);
}
link.onclick = function(sessionId) {
return function() {
chrome.send("metrics", ["NTP_TabRestored" + i]);
/* This is a hack because chrome.send is hardcoded to only
accept arrays of strings. */
chrome.send('reopenTab', [sessionId.toString()]);
return false;
}
}(entry.sessionId);
}
}
logEvent('renderRecentlyClosedTabs done');
}
/**
* Returns the text used for a recently closed window.
*
* @param numTabs number of tabs in the window
*
* @return the text to use
*/
function recentlyClosedWindowText(numTabs) {
if (numTabs == 1)
return localStrings.getString('closedwindowsingle');
return localStrings.getString('closedwindowmultiple').replace('%', numTabs);
}
/**
* Creates an item to go in the recent bookmarks or recently closed lists.
*
* @param {String} tagName Tagname for the DOM element to create.
* @param {Object} data Object with title, url, and direction to popuplate the element.
*
* @return {Node} The element containing the bookmark.
*/
function createRecentBookmark(tagName, data) {
var link = DOM(tagName, {className:'recent-bookmark', title:data.title});
if (tagName == 'a')
link.href = data.url;
link.style.backgroundImage = 'url("chrome://favicon/' + data.url + '")';
/* Set the title's directionality independently of the page, see comment
about setting div_title.style.direction above for details.
*/
link.style.direction = data.direction;
link.appendChild(document.createTextNode(data.title));
return link;
}
/**
* A hover card for windows in the recently closed list to show more details.
*
* @param {Node} target The element the hover card is for.
* @param {Object} data Object containing all the data for the card.
*/
function RecentlyClosedHoverCard(target, data) {
this.target_ = target;
this.data_ = data;
this.target_.onmouseover = bind(this.setShowTimeout_, this);
this.target_.onmouseout = bind(this.setHideTimeout_, this);
}
/** Timeout set when closing the card. */
RecentlyClosedHoverCard.closeTimeout_;
/** Timeout set when opening the card. */
RecentlyClosedHoverCard.openTimeout_;
/**
* Clears the timer for hiding the card.
*/
RecentlyClosedHoverCard.clearHideTimeout_ = function() {
clearTimeout(RecentlyClosedHoverCard.closeTimeout_);
};
/**
* Clears the timer for opening the card.
*/
RecentlyClosedHoverCard.clearOpenTimeout_ = function() {
clearTimeout(RecentlyClosedHoverCard.openTimeout_);
};
/**
* Creates and shows the card.
*/
RecentlyClosedHoverCard.prototype.show_ = function() {
if (!this.container_) {
this.container_ = DOM('div', {className: 'recent-window-hover-container'});
for (var i = 0; i < this.data_.tabs.length; i++) {
var tab = this.data_.tabs[i];
var item = createRecentBookmark('span', tab);
this.container_.appendChild(item);
}
this.target_.parentNode.insertBefore(this.container_,
this.target_.nextSibling);
this.container_.onmouseover = RecentlyClosedHoverCard.clearHideTimeout_;
this.container_.onmouseout = bind(this.setHideTimeout_, this);
}
this.container_.style.display = '';
};
/**
* Hides the card.
*/
RecentlyClosedHoverCard.prototype.hide_ = function() {
this.container_.style.display = 'none';
};
/**
* Clears any open timers and sets the open timer.
* If the card is already showing then we only need to clear
* the hide timer.
*/
RecentlyClosedHoverCard.prototype.setShowTimeout_ = function() {
if (this.container && this.container_.style.display != 'none') {
// If we're already showing the hovercard, make sure we don't hide it again
// onmouseover.
RecentlyClosedHoverCard.clearHideTimeout_();
return;
}
RecentlyClosedHoverCard.clearOpenTimeout_();
RecentlyClosedHoverCard.openTimeout_ =
setTimeout(bind(this.show_, this), 200);
};
/**
* Clears the open timer and sets the close one.
*/
RecentlyClosedHoverCard.prototype.setHideTimeout_ = function() {
RecentlyClosedHoverCard.clearOpenTimeout_();
RecentlyClosedHoverCard.closeTimeout_ =
setTimeout(bind(this.hide_, this), 200);
};
/**
* Switches to thumbnails editing mode.
*/
function enterEditMode() {
// If the cross-image in the heading has not been added yet, do it.
// Note that we have to insert the image node explicitly because the
// heading is localized and therefore set at run-time, and we need
// the image to be static so that it can be inlined at build-time.
var crossImageDiv = document.getElementById('cross-image-container');
if (crossImageDiv && !crossImageDiv.hasChildNodes()) {
var image = document.getElementById('small-cross-image');
image.parentNode.removeChild(image);
crossImageDiv.appendChild(image);
image.style.display = 'inline';
crossImageDiv.style.display = 'inline';
}
document.getElementById('main').className = 'visible edit-mode';
window.onunload = cancelEdits;
}
function exitEditMode() {
document.getElementById('main').className = 'visible';
blacklistedURLs = [];
window.onunload = null;
}
function cancelEdits() {
if (blacklistedURLs.length > 0) {
chrome.send('removeURLsFromMostVisitedBlacklist', blacklistedURLs);
// NOTE(arv): According to evanm the order of messages should be the order
// they are called in. However, he wasn't sure and glen wasn't sure either.
// We should keep our eyes open for weird issues and if the order is not
// guaranteed we can call 'getMostVisited' in a timeout.
chrome.send('getMostVisited');
}
exitEditMode();
}
function blacklistURL(url) {
blacklistedURLs.push(url);
chrome.send('blacklistURLFromMostVisited', [url]);
// NOTE(arv): See note in cancelEdits.
chrome.send('getMostVisited');
}
function restoreThumbnails() {
exitEditMode();
chrome.send('clearMostVisitedURLsBlacklist');
// NOTE(arv): See note in cancelEdits.
chrome.send('getMostVisited');
}
function themeChanged() {
$('themecss').href = 'chrome://theme/css/newtab.css?' + Date.now();
updateAttribution();
}
function updateAttribution() {
$('attribution-img').src = 'chrome://theme/theme_ntp_attribution?' +
Date.now();
}
function bookmarkBarAttached() {
document.documentElement.setAttribute("bookmarkbarattached", "true");
}
function bookmarkBarDetached() {
document.documentElement.setAttribute("bookmarkbarattached", "false");
}
function viewLog() {
var lines = [];
var start = log[0][1];
for (var i = 0; i < log.length; i++) {
lines.push((log[i][1] - start) + ': ' + log[i][0]);
}
var lognode = document.createElement('pre');
lognode.appendChild(document.createTextNode(lines.join("\n")));
document.body.appendChild(lognode);
}
logEvent('end of second script block');
localStrings = new LocalStrings($('l10n'));
// We've got all the JS we need, render any unprocessed data.
renderFunctionsDefined = true;
processData();
// In case renderMostVisitedItems doesn't come back quickly enough, begin
// the first-run fade-in. If it has started or if this is not a first
// run new tab, this will be a no-op.
setTimeout(function(){document.getElementById('main').className = 'visible'},
1000);
</script>
<img id="small-cross-image" style="display: none; vertical-align:middle;" alt="X"
src="chrome://theme/newtab_remove_icon"/>
</body>
<style>
/* This CSS code is located at the end of file so it does not slow-down the page
loading, as it contains inlined images.
*/
.edit-mode div.edit-cross {
position: absolute;
z-index: 10;
width: 81px;
height: 81px;
left: 60px;
top: 47px;
background: url(chrome://theme/newtab_remove_thumbnail);
}
.edit-mode div.edit-cross:hover {
background: url(chrome://theme/newtab_remove_thumbnail_hover);
}
.edit-mode div.edit-cross:active {
background: url(chrome://theme/newtab_remove_thumbnail_active);
}
.recent-window-container {
background: url(chrome://theme/newtab_closed_window);
background-repeat: no-repeat;
}
html[dir='rtl'] .recent-window-container {
background-position: right;
padding-right: 22px;
}
</style>
</html>
|