| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 (function() { | 5 (function() { |
| 6 <include src="../../../../ui/webui/resources/js/assert.js"> | 6 <include src="../../../../ui/webui/resources/js/assert.js"> |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * True if this a Google page and not some other search provider. Used to | 9 * True if this a Google page and not some other search provider. Used to |
| 10 * determine whether to show the logo and fakebox. | 10 * determine whether to show the logo and fakebox. |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 document.body.classList.add(CLASSES.FAKEBOX_ANIMATE); | 500 document.body.classList.add(CLASSES.FAKEBOX_ANIMATE); |
| 501 } else if (!fakebox || | 501 } else if (!fakebox || |
| 502 !document.body.classList.contains(CLASSES.FAKEBOX_ANIMATE)) { | 502 !document.body.classList.contains(CLASSES.FAKEBOX_ANIMATE)) { |
| 503 // The user has typed in the omnibox - hide the NTP immediately. | 503 // The user has typed in the omnibox - hide the NTP immediately. |
| 504 document.body.classList.add(CLASSES.HIDE_NTP); | 504 document.body.classList.add(CLASSES.HIDE_NTP); |
| 505 clearCustomTheme(); | 505 clearCustomTheme(); |
| 506 } | 506 } |
| 507 } | 507 } |
| 508 | 508 |
| 509 /** | 509 /** |
| 510 * Shows the NTP (destroys the activeBox if exists and reloads the custom |
| 511 * theme). |
| 512 */ |
| 513 function showNtp() { |
| 514 hideActiveSuggestions(); |
| 515 document.body.classList.remove(CLASSES.HIDE_NTP); |
| 516 onThemeChange(); |
| 517 } |
| 518 |
| 519 /** |
| 510 * Clears the custom theme (if any). | 520 * Clears the custom theme (if any). |
| 511 */ | 521 */ |
| 512 function clearCustomTheme() { | 522 function clearCustomTheme() { |
| 513 document.body.style.background = ''; | 523 document.body.style.background = ''; |
| 514 document.body.classList.remove(CLASSES.CUSTOM_THEME); | 524 document.body.classList.remove(CLASSES.CUSTOM_THEME); |
| 515 } | 525 } |
| 516 | 526 |
| 517 /** | 527 /** |
| 518 * @return {boolean} True if the NTP is visible. | 528 * @return {boolean} True if the NTP is visible. |
| 519 */ | 529 */ |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 var KEY_UP_ARROW = 38; | 651 var KEY_UP_ARROW = 38; |
| 642 | 652 |
| 643 /** | 653 /** |
| 644 * "Down" arrow keycode. | 654 * "Down" arrow keycode. |
| 645 * @type {number} | 655 * @type {number} |
| 646 * @const | 656 * @const |
| 647 */ | 657 */ |
| 648 var KEY_DOWN_ARROW = 40; | 658 var KEY_DOWN_ARROW = 40; |
| 649 | 659 |
| 650 /** | 660 /** |
| 661 * "Esc" keycode. |
| 662 * @type {number} |
| 663 * @const |
| 664 */ |
| 665 var KEY_ESC = 27; |
| 666 |
| 667 /** |
| 651 * Pixels of padding inside a suggestion div for displaying its icon. | 668 * Pixels of padding inside a suggestion div for displaying its icon. |
| 652 * @type {number} | 669 * @type {number} |
| 653 * @const | 670 * @const |
| 654 */ | 671 */ |
| 655 var SUGGESTION_ICON_PADDING = 26; | 672 var SUGGESTION_ICON_PADDING = 26; |
| 656 | 673 |
| 657 /** | 674 /** |
| 658 * Pixels by which iframes should be moved down relative to their wrapping | 675 * Pixels by which iframes should be moved down relative to their wrapping |
| 659 * suggestion div. | 676 * suggestion div. |
| 660 */ | 677 */ |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1067 }, | 1084 }, |
| 1068 | 1085 |
| 1069 /** | 1086 /** |
| 1070 * Selects the suggestion after the current selection. | 1087 * Selects the suggestion after the current selection. |
| 1071 */ | 1088 */ |
| 1072 selectNext: function() { | 1089 selectNext: function() { |
| 1073 this.changeSelection_(this.selectedIndex_ + 1); | 1090 this.changeSelection_(this.selectedIndex_ + 1); |
| 1074 }, | 1091 }, |
| 1075 | 1092 |
| 1076 /** | 1093 /** |
| 1094 * @return {boolean} True if a suggestion is selected by default or because |
| 1095 * the user arrowed down to it. |
| 1096 */ |
| 1097 hasSelectedSuggestion: function() { |
| 1098 return this.selectedIndex_ != -1; |
| 1099 }, |
| 1100 |
| 1101 /** |
| 1102 * Clears the selected suggestion. |
| 1103 */ |
| 1104 clearSelection: function() { |
| 1105 this.selectedIndex_ = -1; |
| 1106 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); |
| 1107 if (oldSelection) |
| 1108 oldSelection.classList.remove(CLASSES.SELECTED); |
| 1109 }, |
| 1110 |
| 1111 /** |
| 1077 * Changes the current selected suggestion index. | 1112 * Changes the current selected suggestion index. |
| 1078 * @param {number} index The new selection to suggest. | 1113 * @param {number} index The new selection to suggest. |
| 1079 * @private | 1114 * @private |
| 1080 */ | 1115 */ |
| 1081 changeSelection_: function(index) { | 1116 changeSelection_: function(index) { |
| 1082 var numSuggestions = this.suggestions_.length; | 1117 var numSuggestions = this.suggestions_.length; |
| 1083 this.selectedIndex_ = Math.min(numSuggestions - 1, Math.max(-1, index)); | 1118 this.selectedIndex_ = Math.min(numSuggestions - 1, Math.max(-1, index)); |
| 1084 | 1119 |
| 1085 this.redrawSelection_(); | 1120 this.redrawSelection_(); |
| 1086 this.redrawHover_(); | 1121 this.redrawHover_(); |
| 1087 }, | 1122 }, |
| 1088 | 1123 |
| 1089 /** | 1124 /** |
| 1090 * Redraws the selected suggestion. | 1125 * Redraws the selected suggestion. |
| 1091 * @private | 1126 * @private |
| 1092 */ | 1127 */ |
| 1093 redrawSelection_: function() { | 1128 redrawSelection_: function() { |
| 1094 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); | 1129 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); |
| 1095 if (oldSelection) | 1130 if (oldSelection) |
| 1096 oldSelection.classList.remove(CLASSES.SELECTED); | 1131 oldSelection.classList.remove(CLASSES.SELECTED); |
| 1097 if (this.selectedIndex_ == -1) { | 1132 if (!this.hasSelectedSuggestion()) { |
| 1098 searchboxApiHandle.setValue(this.inputValue_); | 1133 searchboxApiHandle.setValue(this.inputValue_); |
| 1099 } else { | 1134 } else { |
| 1100 this.suggestions_[this.selectedIndex_].select(true); | 1135 this.suggestions_[this.selectedIndex_].select(true); |
| 1101 searchboxApiHandle.setRestrictedValue( | 1136 searchboxApiHandle.setRestrictedValue( |
| 1102 this.suggestions_[this.selectedIndex_].restrictedId); | 1137 this.suggestions_[this.selectedIndex_].restrictedId); |
| 1103 } | 1138 } |
| 1104 }, | 1139 }, |
| 1105 | 1140 |
| 1106 /** | 1141 /** |
| 1107 * @param {!Window} iframeWindow The window of the iframe that was clicked. | 1142 * @param {!Window} iframeWindow The window of the iframe that was clicked. |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1300 | 1335 |
| 1301 if (activeBox) | 1336 if (activeBox) |
| 1302 activeBox.repositionSuggestions(); | 1337 activeBox.repositionSuggestions(); |
| 1303 | 1338 |
| 1304 // This is bound only for initialization. Afterwards this style should only | 1339 // This is bound only for initialization. Afterwards this style should only |
| 1305 // change onmarginchange. | 1340 // change onmarginchange. |
| 1306 window.removeEventListener('resize', setSuggestionStyles); | 1341 window.removeEventListener('resize', setSuggestionStyles); |
| 1307 } | 1342 } |
| 1308 | 1343 |
| 1309 /** | 1344 /** |
| 1310 * Makes keys navigate through suggestions. | 1345 * Handles key press events. |
| 1311 * @param {Object} e The key being pressed. | 1346 * @param {Object} e The key being pressed. |
| 1312 */ | 1347 */ |
| 1313 function handleKeyPress(e) { | 1348 function handleKeyPress(e) { |
| 1314 if (!activeBox) | |
| 1315 return; | |
| 1316 | |
| 1317 switch (e.keyCode) { | 1349 switch (e.keyCode) { |
| 1318 case KEY_UP_ARROW: | 1350 case KEY_UP_ARROW: |
| 1319 activeBox.selectPrevious(); | 1351 if (activeBox) |
| 1352 activeBox.selectPrevious(); |
| 1320 break; | 1353 break; |
| 1321 case KEY_DOWN_ARROW: | 1354 case KEY_DOWN_ARROW: |
| 1322 activeBox.selectNext(); | 1355 if (activeBox) |
| 1356 activeBox.selectNext(); |
| 1357 break; |
| 1358 case KEY_ESC: |
| 1359 if (activeBox && activeBox.hasSelectedSuggestion()) |
| 1360 activeBox.clearSelection(); |
| 1361 else |
| 1362 showNtp(); |
| 1323 break; | 1363 break; |
| 1324 } | 1364 } |
| 1325 } | 1365 } |
| 1326 | 1366 |
| 1327 /** | 1367 /** |
| 1328 * Handles postMessage calls from suggestion iframes. | 1368 * Handles postMessage calls from suggestion iframes. |
| 1329 * @param {Object} message A notification that all iframes are done loading or | 1369 * @param {Object} message A notification that all iframes are done loading or |
| 1330 * that an iframe was clicked. | 1370 * that an iframe was clicked. |
| 1331 */ | 1371 */ |
| 1332 function handleMessage(message) { | 1372 function handleMessage(message) { |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1526 } | 1566 } |
| 1527 } | 1567 } |
| 1528 | 1568 |
| 1529 document.addEventListener('DOMContentLoaded', init); | 1569 document.addEventListener('DOMContentLoaded', init); |
| 1530 window.addEventListener('message', handleMessage, false); | 1570 window.addEventListener('message', handleMessage, false); |
| 1531 window.addEventListener('blur', function() { | 1571 window.addEventListener('blur', function() { |
| 1532 if (activeBox) | 1572 if (activeBox) |
| 1533 activeBox.clearHover(); | 1573 activeBox.clearHover(); |
| 1534 }, false); | 1574 }, false); |
| 1535 })(); | 1575 })(); |
| OLD | NEW |