Index: chrome/browser/resources/print_preview/print_preview.js |
=================================================================== |
--- chrome/browser/resources/print_preview/print_preview.js (revision 125906) |
+++ chrome/browser/resources/print_preview/print_preview.js (working copy) |
@@ -103,6 +103,9 @@ |
// True if we need to generate draft preview data. |
var generateDraftData = true; |
+// The last element clicked with the mouse. |
+var lastClickedElement = null; |
+ |
// A dictionary of cloud printers that have been added to the printer |
// dropdown. |
var addedCloudPrinters = {}; |
@@ -150,6 +153,7 @@ |
printHeader = print_preview.PrintHeader.getInstance(); |
document.addEventListener(customEvents.PDF_GENERATION_ERROR, |
cancelPendingPrintRequest); |
+ document.addEventListener('click', setLastClickedElement); |
if (!checkCompatiblePluginExists()) { |
disableInputElementsInSidebar(); |
@@ -226,6 +230,14 @@ |
} |
/** |
+ * Keep track of the last element to receive a click. |
+ * @param {Event} e The click event. |
+ */ |
+function setLastClickedElement(e) { |
+ lastClickedElement = e.target; |
+} |
+ |
+/** |
* Disables the controls in the sidebar, shows the throbber and instructs the |
* backend to open the native print dialog. |
*/ |
@@ -1104,11 +1116,57 @@ |
* Closes this print preview tab. |
*/ |
function closePrintPreviewTab() { |
+ window.removeEventListener('onkeydown', onKeyDown); |
dpapad
2012/03/10 02:28:22
Are you sure this should not be 'keydown'?
Dan Beam
2012/03/10 02:36:57
I'm sure it should be just 'keydown'
|
chrome.send('closePrintPreviewTab'); |
chrome.send('DialogClose'); |
} |
/** |
+ * Pass certain directional keyboard events to the PDF viewer. |
+ * @param {Event} e The keyboard event. |
+ */ |
Dan Beam
2012/03/10 02:36:57
/**
* Pass certain directional keyboard events to
|
+function tryToHandleDirectionKeyDown(e) { |
+ // Make sure the PDF plugin is there. |
+ if (!previewArea.pdfPlugin) |
+ return; |
+ |
+ // We only care about: PageUp, PageDown, Left, Up, Right, Down. |
+ if (!(e.keyCode == 33 || e.keyCode == 34 || |
+ (e.keyCode >= 37 && e.keyCode <= 40))) { |
+ return; |
+ } |
+ |
+ // If the user is holding a modifier key, ignore. |
+ if (e.metaKey || e.altKey || e.shiftKey || e.ctrlKey) |
+ return; |
+ |
+ // Don't handle the key event for these elements. |
+ var tagName = document.activeElement.tagName; |
+ if (tagName == "INPUT" || tagName == "SELECT" || tagName == "EMBED") |
Dan Beam
2012/03/10 02:36:57
s/"/'/ (single quotes in js)
|
+ return; |
+ |
+ // For the most part, if any div of header was the last clicked element, |
+ // then the active element is the body. Starting with the last clicked |
+ // element, and work up the DOM tree to see if any element has a scrollbar. |
+ // If there exists a scrollbar, do not handle the key event here. |
+ var element = document.activeElement; |
+ if (element == document.body) { |
+ if (lastClickedElement) |
+ element = lastClickedElement; |
+ while (element) { |
+ if (element.scrollHeight > element.clientHeight) |
+ return; |
+ element = element.parentElement; |
+ } |
+ } |
+ |
+ // No scroll bar anywhere, or the active element is something else, like a |
+ // button. Note: buttons have a bigger scrollHeight than clientHeight. |
+ previewArea.pdfPlugin.sendKeyEvent(e.keyCode); |
+ e.preventDefault(); |
+} |
+ |
+/** |
* Handle keyboard events. |
* @param {KeyboardEvent} e The keyboard event. |
*/ |
@@ -1117,18 +1175,23 @@ |
if (e.keyCode == 27 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) { |
printHeader.disableCancelButton(); |
closePrintPreviewTab(); |
+ e.preventDefault(); |
} |
+ // Ctrl + Shift + p / Mac equivalent. |
if (e.keyCode == 80) { |
if ((cr.isMac && e.metaKey && e.altKey && !e.shiftKey && !e.ctrlKey) || |
(!cr.isMac && e.shiftKey && e.ctrlKey && !e.altKey && !e.metaKey)) { |
- window.onkeydown = null; |
+ window.detachEventListener('onkeydown', onKeyDown); |
Dan Beam
2012/03/10 02:36:57
'keydown' here as well, sorry to confuse, but remo
|
onSystemDialogLinkClicked(); |
+ e.preventDefault(); |
} |
} |
+ |
+ tryToHandleDirectionKeyDown(e); |
} |
window.addEventListener('DOMContentLoaded', onLoad); |
-window.onkeydown = onKeyDown; |
+window.addEventListener('onkeydown', onKeyDown); |
Dan Beam
2012/03/10 02:36:57
'keydown'
|
/// Pull in all other scripts in a single shot. |
<include src="print_preview_animations.js"/> |