Chromium Code Reviews| 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 | 5 |
| 6 /** | 6 /** |
| 7 * @fileoverview The local InstantExtended NTP and suggestions dropdown. | 7 * @fileoverview The local InstantExtended NTP and suggestions dropdown. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 (function() { | 10 (function() { |
| (...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 } else if (!fakebox || | 606 } else if (!fakebox || |
| 607 !document.body.classList.contains(CLASSES.FAKEBOX_ANIMATE)) { | 607 !document.body.classList.contains(CLASSES.FAKEBOX_ANIMATE)) { |
| 608 // The user has typed in the omnibox - hide the NTP immediately. | 608 // The user has typed in the omnibox - hide the NTP immediately. |
| 609 document.body.classList.add(CLASSES.HIDE_NTP); | 609 document.body.classList.add(CLASSES.HIDE_NTP); |
| 610 clearCustomTheme(); | 610 clearCustomTheme(); |
| 611 } | 611 } |
| 612 } | 612 } |
| 613 | 613 |
| 614 | 614 |
| 615 /** | 615 /** |
| 616 * Shows the NTP (destroys the activeBox if exists and reloads the custom | |
| 617 * theme). | |
| 618 */ | |
| 619 function showNtp() { | |
| 620 hideActiveSuggestions(); | |
| 621 document.body.classList.remove(CLASSES.HIDE_NTP); | |
| 622 onThemeChange(); | |
| 623 } | |
| 624 | |
| 625 /** | |
| 616 * Clears the custom theme (if any). | 626 * Clears the custom theme (if any). |
| 617 */ | 627 */ |
| 618 function clearCustomTheme() { | 628 function clearCustomTheme() { |
| 619 document.body.style.background = ''; | 629 document.body.style.background = ''; |
| 620 document.body.classList.remove(CLASSES.CUSTOM_THEME); | 630 document.body.classList.remove(CLASSES.CUSTOM_THEME); |
| 621 } | 631 } |
| 622 | 632 |
| 623 | 633 |
| 624 /** | 634 /** |
| 625 * @return {boolean} True if the NTP is visible. | 635 * @return {boolean} True if the NTP is visible. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 | 782 |
| 773 /** | 783 /** |
| 774 * "Down" arrow keycode. | 784 * "Down" arrow keycode. |
| 775 * @type {number} | 785 * @type {number} |
| 776 * @const | 786 * @const |
| 777 */ | 787 */ |
| 778 var KEY_DOWN_ARROW = 40; | 788 var KEY_DOWN_ARROW = 40; |
| 779 | 789 |
| 780 | 790 |
| 781 /** | 791 /** |
| 792 * "Esc" keycode. | |
| 793 * @type {number} | |
| 794 * @const | |
| 795 */ | |
| 796 var KEY_ESC = 27; | |
| 797 | |
| 798 /** | |
| 782 * Pixels of padding inside a suggestion div for displaying its icon. | 799 * Pixels of padding inside a suggestion div for displaying its icon. |
| 783 * @type {number} | 800 * @type {number} |
| 784 * @const | 801 * @const |
| 785 */ | 802 */ |
| 786 var SUGGESTION_ICON_PADDING = 26; | 803 var SUGGESTION_ICON_PADDING = 26; |
| 787 | 804 |
| 788 | 805 |
| 789 /** | 806 /** |
| 790 * Pixels by which iframes should be moved down relative to their wrapping | 807 * Pixels by which iframes should be moved down relative to their wrapping |
| 791 * suggestion div. | 808 * suggestion div. |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1198 }, | 1215 }, |
| 1199 | 1216 |
| 1200 /** | 1217 /** |
| 1201 * Selects the suggestion after the current selection. | 1218 * Selects the suggestion after the current selection. |
| 1202 */ | 1219 */ |
| 1203 selectNext: function() { | 1220 selectNext: function() { |
| 1204 this.changeSelection_(this.selectedIndex_ + 1); | 1221 this.changeSelection_(this.selectedIndex_ + 1); |
| 1205 }, | 1222 }, |
| 1206 | 1223 |
| 1207 /** | 1224 /** |
| 1225 * @return {boolean} True if a suggestion is selected by default or because | |
| 1226 * the user arrowed down to it. | |
| 1227 */ | |
| 1228 hasSelectedSuggestion: function() { | |
| 1229 return this.selectedIndex_ != -1; | |
| 1230 }, | |
| 1231 | |
| 1232 /** | |
| 1233 * Clears the selected suggestion. | |
| 1234 */ | |
| 1235 clearSelection: function() { | |
| 1236 this.selectedIndex_ = -1; | |
| 1237 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); | |
| 1238 if (oldSelection) | |
| 1239 oldSelection.classList.remove(CLASSES.SELECTED); | |
| 1240 }, | |
| 1241 | |
| 1242 /** | |
| 1208 * Changes the current selected suggestion index. | 1243 * Changes the current selected suggestion index. |
| 1209 * @param {number} index The new selection to suggest. | 1244 * @param {number} index The new selection to suggest. |
| 1210 * @private | 1245 * @private |
| 1211 */ | 1246 */ |
| 1212 changeSelection_: function(index) { | 1247 changeSelection_: function(index) { |
| 1213 var numSuggestions = this.suggestions_.length; | 1248 var numSuggestions = this.suggestions_.length; |
| 1214 this.selectedIndex_ = Math.min(numSuggestions - 1, Math.max(-1, index)); | 1249 this.selectedIndex_ = Math.min(numSuggestions - 1, Math.max(-1, index)); |
| 1215 | 1250 |
| 1216 this.redrawSelection_(); | 1251 this.redrawSelection_(); |
| 1217 this.redrawHover_(); | 1252 this.redrawHover_(); |
| 1218 }, | 1253 }, |
| 1219 | 1254 |
| 1220 /** | 1255 /** |
| 1221 * Redraws the selected suggestion. | 1256 * Redraws the selected suggestion. |
| 1222 * @private | 1257 * @private |
| 1223 */ | 1258 */ |
| 1224 redrawSelection_: function() { | 1259 redrawSelection_: function() { |
| 1225 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); | 1260 var oldSelection = this.container_.querySelector('.' + CLASSES.SELECTED); |
| 1226 if (oldSelection) | 1261 if (oldSelection) |
| 1227 oldSelection.classList.remove(CLASSES.SELECTED); | 1262 oldSelection.classList.remove(CLASSES.SELECTED); |
| 1228 if (this.selectedIndex_ == -1) { | 1263 if (!this.hasSelectedSuggestion()) { |
| 1229 searchboxApiHandle.setValue(this.inputValue_); | 1264 searchboxApiHandle.setValue(this.inputValue_); |
| 1230 } else { | 1265 } else { |
| 1231 this.suggestions_[this.selectedIndex_].select(true); | 1266 this.suggestions_[this.selectedIndex_].select(true); |
| 1232 searchboxApiHandle.setRestrictedValue( | 1267 searchboxApiHandle.setRestrictedValue( |
| 1233 this.suggestions_[this.selectedIndex_].restrictedId); | 1268 this.suggestions_[this.selectedIndex_].restrictedId); |
| 1234 } | 1269 } |
| 1235 }, | 1270 }, |
| 1236 | 1271 |
| 1237 /** | 1272 /** |
| 1238 * @param {!Window} iframeWindow The window of the iframe that was clicked. | 1273 * @param {!Window} iframeWindow The window of the iframe that was clicked. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1435 if (activeBox) | 1470 if (activeBox) |
| 1436 activeBox.repositionSuggestions(); | 1471 activeBox.repositionSuggestions(); |
| 1437 | 1472 |
| 1438 // This is bound only for initialization. Afterwards this style should only | 1473 // This is bound only for initialization. Afterwards this style should only |
| 1439 // change onmarginchange. | 1474 // change onmarginchange. |
| 1440 window.removeEventListener('resize', setSuggestionStyles); | 1475 window.removeEventListener('resize', setSuggestionStyles); |
| 1441 } | 1476 } |
| 1442 | 1477 |
| 1443 | 1478 |
| 1444 /** | 1479 /** |
| 1445 * Makes keys navigate through suggestions. | 1480 * Handles key press events. |
| 1446 * @param {Object} e The key being pressed. | 1481 * @param {Object} e The key being pressed. |
| 1447 */ | 1482 */ |
| 1448 function handleKeyPress(e) { | 1483 function handleKeyPress(e) { |
| 1449 if (!activeBox) | |
| 1450 return; | |
| 1451 | |
| 1452 switch (e.keyCode) { | 1484 switch (e.keyCode) { |
| 1453 case KEY_UP_ARROW: | 1485 case KEY_UP_ARROW: |
| 1454 activeBox.selectPrevious(); | 1486 if (activeBox) |
| 1487 activeBox.selectPrevious(); | |
| 1455 break; | 1488 break; |
| 1456 case KEY_DOWN_ARROW: | 1489 case KEY_DOWN_ARROW: |
| 1457 activeBox.selectNext(); | 1490 if (activeBox) |
| 1491 activeBox.selectNext(); | |
| 1492 break; | |
| 1493 case KEY_ESC: | |
| 1494 if (activeBox && activeBox.hasSelectedSuggestion()) | |
| 1495 activeBox.clearSelection(); | |
| 1496 else | |
| 1497 showNtp(); | |
| 1458 break; | 1498 break; |
| 1459 } | 1499 } |
| 1460 } | 1500 } |
| 1461 | 1501 |
| 1462 | 1502 |
| 1463 /** | 1503 /** |
| 1464 * Handles postMessage calls from suggestion iframes. | 1504 * Handles postMessage calls from suggestion iframes. |
| 1465 * @param {Object} message A notification that all iframes are done loading or | 1505 * @param {Object} message A notification that all iframes are done loading or |
| 1466 * that an iframe was clicked. | 1506 * that an iframe was clicked. |
| 1467 */ | 1507 */ |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1633 searchboxApiHandle.onchange = updateSuggestions; | 1673 searchboxApiHandle.onchange = updateSuggestions; |
| 1634 searchboxApiHandle.onkeypress = handleKeyPress; | 1674 searchboxApiHandle.onkeypress = handleKeyPress; |
| 1635 // TODO(jered): Re-enable this after http://crbug.com/236492 is fixed. | 1675 // TODO(jered): Re-enable this after http://crbug.com/236492 is fixed. |
| 1636 // searchboxApiHandle.onmarginchange = setSuggestionStyles; | 1676 // searchboxApiHandle.onmarginchange = setSuggestionStyles; |
| 1637 if (window.innerWidth > 0) { | 1677 if (window.innerWidth > 0) { |
| 1638 setSuggestionStyles(); | 1678 setSuggestionStyles(); |
| 1639 } else { | 1679 } else { |
| 1640 // Wait until the overlay is shown to initialize suggestion margins. | 1680 // Wait until the overlay is shown to initialize suggestion margins. |
| 1641 window.addEventListener('resize', setSuggestionStyles); | 1681 window.addEventListener('resize', setSuggestionStyles); |
| 1642 } | 1682 } |
| 1643 searchboxApiHandle.onsubmit = function() { | 1683 searchboxApiHandle.onsubmit = function() { |
|
jeremycho
2013/05/06 20:32:34
Can you add a comment explaining this is needed to
kmadhusu
2013/05/06 21:11:45
Done.
| |
| 1644 var value = searchboxApiHandle.value; | |
| 1645 if (!value) { | |
| 1646 // Interpret onsubmit with an empty query as an ESC key press. | |
| 1647 hideActiveSuggestions(); | |
| 1648 } | |
| 1649 }; | 1684 }; |
| 1650 | 1685 |
| 1651 if (fakebox) { | 1686 if (fakebox) { |
| 1652 // Listener for updating the key capture state. | 1687 // Listener for updating the key capture state. |
| 1653 document.body.onclick = function(event) { | 1688 document.body.onclick = function(event) { |
| 1654 if (isFakeboxClick(event)) | 1689 if (isFakeboxClick(event)) |
| 1655 searchboxApiHandle.startCapturingKeyStrokes(); | 1690 searchboxApiHandle.startCapturingKeyStrokes(); |
| 1656 else if (fakeboxIsFocused()) | 1691 else if (fakeboxIsFocused()) |
| 1657 searchboxApiHandle.stopCapturingKeyStrokes(); | 1692 searchboxApiHandle.stopCapturingKeyStrokes(); |
| 1658 }; | 1693 }; |
| 1659 searchboxApiHandle.onkeycapturechange = function() { | 1694 searchboxApiHandle.onkeycapturechange = function() { |
| 1660 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled); | 1695 setFakeboxFocus(searchboxApiHandle.isKeyCaptureEnabled); |
| 1661 }; | 1696 }; |
| 1662 | 1697 |
| 1663 // Set the cursor alignment based on language directionality. | 1698 // Set the cursor alignment based on language directionality. |
| 1664 $(IDS.CURSOR).style[searchboxApiHandle.rtl ? 'right' : 'left'] = '9px'; | 1699 $(IDS.CURSOR).style[searchboxApiHandle.rtl ? 'right' : 'left'] = '9px'; |
| 1665 } | 1700 } |
| 1666 } | 1701 } |
| 1667 | 1702 |
| 1668 document.addEventListener('DOMContentLoaded', init); | 1703 document.addEventListener('DOMContentLoaded', init); |
| 1669 window.addEventListener('message', handleMessage, false); | 1704 window.addEventListener('message', handleMessage, false); |
| 1670 window.addEventListener('blur', function() { | 1705 window.addEventListener('blur', function() { |
| 1671 if (activeBox) | 1706 if (activeBox) |
| 1672 activeBox.clearHover(); | 1707 activeBox.clearHover(); |
| 1673 }, false); | 1708 }, false); |
| 1674 })(); | 1709 })(); |
| OLD | NEW |