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

Side by Side Diff: chrome/browser/resources/local_ntp/local_ntp.js

Issue 2684593002: [Local NTP] Cleanup: Don't create HTML elements dynamically (Closed)
Patch Set: tests Created 3 years, 10 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 5
6 /** 6 /**
7 * @fileoverview The local InstantExtended NTP. 7 * @fileoverview The local InstantExtended NTP.
8 */ 8 */
9 9
10 10
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 FAKEBOX: 'fakebox', 117 FAKEBOX: 'fakebox',
118 FAKEBOX_INPUT: 'fakebox-input', 118 FAKEBOX_INPUT: 'fakebox-input',
119 FAKEBOX_TEXT: 'fakebox-text', 119 FAKEBOX_TEXT: 'fakebox-text',
120 LOGO: 'logo', 120 LOGO: 'logo',
121 NOTIFICATION: 'mv-notice', 121 NOTIFICATION: 'mv-notice',
122 NOTIFICATION_CLOSE_BUTTON: 'mv-notice-x', 122 NOTIFICATION_CLOSE_BUTTON: 'mv-notice-x',
123 NOTIFICATION_MESSAGE: 'mv-msg', 123 NOTIFICATION_MESSAGE: 'mv-msg',
124 NTP_CONTENTS: 'ntp-contents', 124 NTP_CONTENTS: 'ntp-contents',
125 RESTORE_ALL_LINK: 'mv-restore', 125 RESTORE_ALL_LINK: 'mv-restore',
126 TILES: 'mv-tiles', 126 TILES: 'mv-tiles',
127 TILES_IFRAME: 'mv-single',
127 UNDO_LINK: 'mv-undo' 128 UNDO_LINK: 'mv-undo'
128 }; 129 };
129 130
130 131
131 /** 132 /**
132 * Enum for keycodes. 133 * Enum for keycodes.
133 * @enum {number} 134 * @enum {number}
134 * @const 135 * @const
135 */ 136 */
136 var KEYCODE = { 137 var KEYCODE = {
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 var luminance = 0.3 * rgba[0] + 0.59 * rgba[1] + 0.11 * rgba[2]; 238 var luminance = 0.3 * rgba[0] + 0.59 * rgba[1] + 0.11 * rgba[2];
238 return luminance >= 128; 239 return luminance >= 128;
239 } 240 }
240 241
241 242
242 /** 243 /**
243 * Updates the NTP based on the current theme. 244 * Updates the NTP based on the current theme.
244 * @private 245 * @private
245 */ 246 */
246 function renderTheme() { 247 function renderTheme() {
247 var fakeboxText = $(IDS.FAKEBOX_TEXT);
248 if (fakeboxText) {
249 fakeboxText.innerHTML = '';
250 if (configData.translatedStrings.searchboxPlaceholder) {
251 fakeboxText.textContent =
252 configData.translatedStrings.searchboxPlaceholder;
253 }
254 }
Marc Treib 2017/02/07 14:37:11 This block moved into init() where it belongs - it
255
256 var info = ntpApiHandle.themeBackgroundInfo; 248 var info = ntpApiHandle.themeBackgroundInfo;
257 var isThemeDark = getIsThemeDark(info); 249 var isThemeDark = getIsThemeDark(info);
258 ntpContents.classList.toggle(CLASSES.DARK, isThemeDark); 250 ntpContents.classList.toggle(CLASSES.DARK, isThemeDark);
259 if (!info) { 251 if (!info) {
260 return; 252 return;
261 } 253 }
262 254
263 var background = [convertToRGBAColor(info.backgroundColorRgba), 255 var background = [convertToRGBAColor(info.backgroundColorRgba),
264 info.imageUrl, 256 info.imageUrl,
265 info.imageTiling, 257 info.imageTiling,
266 info.imageHorizontalAlignment, 258 info.imageHorizontalAlignment,
267 info.imageVerticalAlignment].join(' ').trim(); 259 info.imageVerticalAlignment].join(' ').trim();
268 260
269 document.body.style.background = background; 261 document.body.style.background = background;
270 document.body.classList.toggle(CLASSES.ALTERNATE_LOGO, info.alternateLogo); 262 document.body.classList.toggle(CLASSES.ALTERNATE_LOGO, info.alternateLogo);
271 updateThemeAttribution(info.attributionUrl, info.imageHorizontalAlignment); 263 updateThemeAttribution(info.attributionUrl, info.imageHorizontalAlignment);
272 setCustomThemeStyle(info); 264 setCustomThemeStyle(info);
273 265
266 // Inform the most visited iframe of the new theme.
274 var themeinfo = {cmd: 'updateTheme'}; 267 var themeinfo = {cmd: 'updateTheme'};
275 if (!info.usingDefaultTheme) { 268 if (!info.usingDefaultTheme) {
276 themeinfo.tileBorderColor = convertToRGBAColor(info.sectionBorderColorRgba); 269 themeinfo.tileBorderColor = convertToRGBAColor(info.sectionBorderColorRgba);
277 themeinfo.tileHoverBorderColor = convertToRGBAColor(info.headerColorRgba); 270 themeinfo.tileHoverBorderColor = convertToRGBAColor(info.headerColorRgba);
278 } 271 }
279 themeinfo.isThemeDark = isThemeDark; 272 themeinfo.isThemeDark = isThemeDark;
280 273
281 var titleColor = NTP_DESIGN.titleColor; 274 var titleColor = NTP_DESIGN.titleColor;
282 if (!info.usingDefaultTheme && info.textColorRgba) { 275 if (!info.usingDefaultTheme && info.textColorRgba) {
283 titleColor = info.textColorRgba; 276 titleColor = info.textColorRgba;
284 } else if (isThemeDark) { 277 } else if (isThemeDark) {
285 titleColor = NTP_DESIGN.titleColorAgainstDark; 278 titleColor = NTP_DESIGN.titleColorAgainstDark;
286 } 279 }
287 themeinfo.tileTitleColor = convertToRGBAColor(titleColor); 280 themeinfo.tileTitleColor = convertToRGBAColor(titleColor);
288 281
289 $('mv-single').contentWindow.postMessage(themeinfo, '*'); 282 $(IDS.TILES_IFRAME).contentWindow.postMessage(themeinfo, '*');
290 } 283 }
291 284
292 285
293 /** 286 /**
294 * Updates the NTP based on the current theme, then rerenders all tiles. 287 * Updates the NTP based on the current theme, then rerenders all tiles.
295 * @private 288 * @private
296 */ 289 */
297 function onThemeChange() { 290 function onThemeChange() {
298 renderTheme(); 291 renderTheme();
299 } 292 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 * Fetches new data, creates, and renders tiles. 409 * Fetches new data, creates, and renders tiles.
417 */ 410 */
418 function reloadTiles() { 411 function reloadTiles() {
419 var pages = ntpApiHandle.mostVisited; 412 var pages = ntpApiHandle.mostVisited;
420 var cmds = []; 413 var cmds = [];
421 for (var i = 0; i < Math.min(MAX_NUM_TILES_TO_SHOW, pages.length); ++i) { 414 for (var i = 0; i < Math.min(MAX_NUM_TILES_TO_SHOW, pages.length); ++i) {
422 cmds.push({cmd: 'tile', rid: pages[i].rid}); 415 cmds.push({cmd: 'tile', rid: pages[i].rid});
423 } 416 }
424 cmds.push({cmd: 'show', maxVisible: numColumnsShown * NUM_ROWS}); 417 cmds.push({cmd: 'show', maxVisible: numColumnsShown * NUM_ROWS});
425 418
426 $('mv-single').contentWindow.postMessage(cmds, '*'); 419 $(IDS.TILES_IFRAME).contentWindow.postMessage(cmds, '*');
427 } 420 }
428 421
429 422
430 /** 423 /**
431 * Shows the blacklist notification and triggers a delay to hide it. 424 * Shows the blacklist notification and triggers a delay to hide it.
432 */ 425 */
433 function showNotification() { 426 function showNotification() {
434 notification.classList.remove(CLASSES.HIDE_NOTIFICATION); 427 notification.classList.remove(CLASSES.HIDE_NOTIFICATION);
435 notification.classList.remove(CLASSES.DELAYED_HIDE_NOTIFICATION); 428 notification.classList.remove(CLASSES.DELAYED_HIDE_NOTIFICATION);
436 notification.scrollTop; 429 notification.scrollTop;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 } 500 }
508 501
509 502
510 /** 503 /**
511 * Resizes elements because the number of tile columns may need to change in 504 * Resizes elements because the number of tile columns may need to change in
512 * response to resizing. Also shows or hides extra tiles tiles according to the 505 * response to resizing. Also shows or hides extra tiles tiles according to the
513 * new width of the page. 506 * new width of the page.
514 */ 507 */
515 function onResize() { 508 function onResize() {
516 updateContentWidth(); 509 updateContentWidth();
517 $('mv-single').contentWindow.postMessage( 510 $(IDS.TILES_IFRAME).contentWindow.postMessage(
518 {cmd: 'tilesVisible', maxVisible: numColumnsShown * NUM_ROWS}, '*'); 511 {cmd: 'tilesVisible', maxVisible: numColumnsShown * NUM_ROWS}, '*');
519 } 512 }
520 513
521 514
522 /** 515 /**
523 * Handles new input by disposing the NTP, according to where the input was 516 * Handles new input by disposing the NTP, according to where the input was
524 * entered. 517 * entered.
525 */ 518 */
526 function onInputStart() { 519 function onInputStart() {
527 if (fakebox && isFakeboxFocused()) { 520 if (fakebox && isFakeboxFocused()) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 /** 620 /**
628 * Prepares the New Tab Page by adding listeners, the most visited pages 621 * Prepares the New Tab Page by adding listeners, the most visited pages
629 * section, and Google-specific elements for a Google-provided page. 622 * section, and Google-specific elements for a Google-provided page.
630 */ 623 */
631 function init() { 624 function init() {
632 notification = $(IDS.NOTIFICATION); 625 notification = $(IDS.NOTIFICATION);
633 attribution = $(IDS.ATTRIBUTION); 626 attribution = $(IDS.ATTRIBUTION);
634 ntpContents = $(IDS.NTP_CONTENTS); 627 ntpContents = $(IDS.NTP_CONTENTS);
635 628
636 if (configData.isGooglePage) { 629 if (configData.isGooglePage) {
637 var logo = document.createElement('div'); 630 fakebox = $(IDS.FAKEBOX);
638 logo.id = IDS.LOGO;
639 logo.title = 'Google';
640
641 fakebox = document.createElement('div');
642 fakebox.id = IDS.FAKEBOX;
643 var fakeboxHtml = [];
644 fakeboxHtml.push('<div id="' + IDS.FAKEBOX_TEXT + '"></div>');
645 fakeboxHtml.push('<input id="' + IDS.FAKEBOX_INPUT +
646 '" autocomplete="off" tabindex="-1" type="url" aria-hidden="true">');
647 fakeboxHtml.push('<div id="cursor"></div>');
648 fakebox.innerHTML = fakeboxHtml.join('');
649
650 ntpContents.insertBefore(fakebox, ntpContents.firstChild);
651 ntpContents.insertBefore(logo, ntpContents.firstChild);
652 } else { 631 } else {
653 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE); 632 document.body.classList.add(CLASSES.NON_GOOGLE_PAGE);
654 } 633 }
655 634
656 // Hide notifications after fade out, so we can't focus on links via keyboard. 635 // Hide notifications after fade out, so we can't focus on links via keyboard.
657 notification.addEventListener('webkitTransitionEnd', hideNotification); 636 notification.addEventListener('webkitTransitionEnd', hideNotification);
658 637
659 var notificationMessage = $(IDS.NOTIFICATION_MESSAGE); 638 $(IDS.NOTIFICATION_MESSAGE).textContent =
660 notificationMessage.textContent =
661 configData.translatedStrings.thumbnailRemovedNotification; 639 configData.translatedStrings.thumbnailRemovedNotification;
662 640
663 var undoLink = $(IDS.UNDO_LINK); 641 var undoLink = $(IDS.UNDO_LINK);
664 undoLink.addEventListener('click', onUndo); 642 undoLink.addEventListener('click', onUndo);
665 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo); 643 registerKeyHandler(undoLink, KEYCODE.ENTER, onUndo);
666 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove; 644 undoLink.textContent = configData.translatedStrings.undoThumbnailRemove;
667 645
668 var restoreAllLink = $(IDS.RESTORE_ALL_LINK); 646 var restoreAllLink = $(IDS.RESTORE_ALL_LINK);
669 restoreAllLink.addEventListener('click', onRestoreAll); 647 restoreAllLink.addEventListener('click', onRestoreAll);
670 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onRestoreAll); 648 registerKeyHandler(restoreAllLink, KEYCODE.ENTER, onRestoreAll);
(...skipping 16 matching lines...) Expand all
687 665
688 ntpApiHandle.oninputstart = onInputStart; 666 ntpApiHandle.oninputstart = onInputStart;
689 ntpApiHandle.oninputcancel = onInputCancel; 667 ntpApiHandle.oninputcancel = onInputCancel;
690 668
691 if (ntpApiHandle.isInputInProgress) 669 if (ntpApiHandle.isInputInProgress)
692 onInputStart(); 670 onInputStart();
693 671
694 searchboxApiHandle = topLevelHandle.searchBox; 672 searchboxApiHandle = topLevelHandle.searchBox;
695 673
696 if (fakebox) { 674 if (fakebox) {
675 $(IDS.FAKEBOX_TEXT).textContent =
676 configData.translatedStrings.searchboxPlaceholder;
677
697 // Listener for updating the key capture state. 678 // Listener for updating the key capture state.
698 document.body.onmousedown = function(event) { 679 document.body.onmousedown = function(event) {
699 if (isFakeboxClick(event)) 680 if (isFakeboxClick(event))
700 searchboxApiHandle.startCapturingKeyStrokes(); 681 searchboxApiHandle.startCapturingKeyStrokes();
701 else if (isFakeboxFocused()) 682 else if (isFakeboxFocused())
702 searchboxApiHandle.stopCapturingKeyStrokes(); 683 searchboxApiHandle.stopCapturingKeyStrokes();
703 }; 684 };
704 searchboxApiHandle.onkeycapturechange = function() { 685 searchboxApiHandle.onkeycapturechange = function() {
705 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled); 686 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled);
706 }; 687 };
707 var inputbox = $(IDS.FAKEBOX_INPUT); 688 var inputbox = $(IDS.FAKEBOX_INPUT);
708 if (inputbox) { 689 inputbox.onpaste = function(event) {
709 inputbox.onpaste = function(event) { 690 event.preventDefault();
710 event.preventDefault(); 691 // Send pasted text to Omnibox.
711 // Send pasted text to Omnibox. 692 var text = event.clipboardData.getData('text/plain');
712 var text = event.clipboardData.getData('text/plain'); 693 if (text)
713 if (text) 694 searchboxApiHandle.paste(text);
714 searchboxApiHandle.paste(text); 695 };
715 }; 696 inputbox.ondrop = function(event) {
716 inputbox.ondrop = function(event) { 697 event.preventDefault();
717 event.preventDefault(); 698 var text = event.dataTransfer.getData('text/plain');
718 var text = event.dataTransfer.getData('text/plain'); 699 if (text) {
719 if (text) { 700 searchboxApiHandle.paste(text);
720 searchboxApiHandle.paste(text); 701 }
721 } 702 setFakeboxDragFocus(false);
722 setFakeboxDragFocus(false); 703 };
723 }; 704 inputbox.ondragenter = function() {
724 inputbox.ondragenter = function() { 705 setFakeboxDragFocus(true);
725 setFakeboxDragFocus(true); 706 };
726 }; 707 inputbox.ondragleave = function() {
727 inputbox.ondragleave = function() { 708 setFakeboxDragFocus(false);
728 setFakeboxDragFocus(false); 709 };
729 };
730 }
731 710
732 // Update the fakebox style to match the current key capturing state. 711 // Update the fakebox style to match the current key capturing state.
733 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled); 712 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled);
734 } 713 }
735 714
736 if (searchboxApiHandle.rtl) { 715 if (searchboxApiHandle.rtl) {
737 $(IDS.NOTIFICATION).dir = 'rtl'; 716 $(IDS.NOTIFICATION).dir = 'rtl';
738 // Grabbing the root HTML element. 717 // Grabbing the root HTML element.
739 document.documentElement.setAttribute('dir', 'rtl'); 718 document.documentElement.setAttribute('dir', 'rtl');
740 // Add class for setting alignments based on language directionality. 719 // Add class for setting alignments based on language directionality.
741 document.documentElement.classList.add(CLASSES.RTL); 720 document.documentElement.classList.add(CLASSES.RTL);
742 } 721 }
743 722
744 var iframe = document.createElement('iframe'); 723 // Pass arguments to the most visited iframe.
745 // Change the order of tabbing the page to start with NTP tiles.
746 iframe.setAttribute('tabindex', '1');
747 iframe.id = 'mv-single';
748
749 var args = []; 724 var args = [];
750 725
751 if (searchboxApiHandle.rtl) 726 if (searchboxApiHandle.rtl)
752 args.push('rtl=1'); 727 args.push('rtl=1');
753 if (NTP_DESIGN.numTitleLines > 1) 728 if (NTP_DESIGN.numTitleLines > 1)
754 args.push('ntl=' + NTP_DESIGN.numTitleLines); 729 args.push('ntl=' + NTP_DESIGN.numTitleLines);
755 730
756 args.push('removeTooltip=' + 731 args.push('removeTooltip=' +
757 encodeURIComponent(configData.translatedStrings.removeThumbnailTooltip)); 732 encodeURIComponent(configData.translatedStrings.removeThumbnailTooltip));
758 733
734 var iframe = $(IDS.TILES_IFRAME);
759 iframe.src = '//most-visited/single.html?' + args.join('&'); 735 iframe.src = '//most-visited/single.html?' + args.join('&');
760 $(IDS.TILES).appendChild(iframe);
761
762 iframe.onload = function() { 736 iframe.onload = function() {
763 reloadTiles(); 737 reloadTiles();
764 renderTheme(); 738 renderTheme();
765 }; 739 };
766 740
767 window.addEventListener('message', handlePostMessage); 741 window.addEventListener('message', handlePostMessage);
768 } 742 }
769 743
770 744
771 /** 745 /**
772 * Binds event listeners. 746 * Binds event listeners.
773 */ 747 */
774 function listen() { 748 function listen() {
775 document.addEventListener('DOMContentLoaded', init); 749 document.addEventListener('DOMContentLoaded', init);
776 } 750 }
777 751
778 return { 752 return {
779 init: init, 753 init: init,
780 listen: listen 754 listen: listen
781 }; 755 };
782 } 756 }
783 757
784 if (!window.localNTPUnitTest) { 758 if (!window.localNTPUnitTest) {
785 LocalNTP().listen(); 759 LocalNTP().listen();
786 } 760 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698