summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-12 20:55:57 +0000
committerthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-03-12 20:55:57 +0000
commitd7122d530b75667192a4af903f16e4b2aa7508fd (patch)
tree5d8bcbcb884398a42258a02663df8d1bebfdd822
parent70d1f9d29bac91664a99d0b2ce11836accf1a8d1 (diff)
downloadchromium_src-d7122d530b75667192a4af903f16e4b2aa7508fd.zip
chromium_src-d7122d530b75667192a4af903f16e4b2aa7508fd.tar.gz
chromium_src-d7122d530b75667192a4af903f16e4b2aa7508fd.tar.bz2
Print Preview: Pass direction keys to the PDF viewer when possible.
BUG=116275 TEST=see bug. Review URL: http://codereview.chromium.org/9663037 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@126217 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/resources/print_preview/print_preview.js67
1 files changed, 65 insertions, 2 deletions
diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js
index 350985d..d39d666 100644
--- a/chrome/browser/resources/print_preview/print_preview.js
+++ b/chrome/browser/resources/print_preview/print_preview.js
@@ -103,6 +103,9 @@ var currentPreviewUid = '';
// 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 @@ function onLoad() {
printHeader = print_preview.PrintHeader.getInstance();
document.addEventListener(customEvents.PDF_GENERATION_ERROR,
cancelPendingPrintRequest);
+ document.addEventListener('click', setLastClickedElement);
if (!checkCompatiblePluginExists()) {
disableInputElementsInSidebar();
@@ -226,6 +230,14 @@ function enableInputElementsInSidebar() {
}
/**
+ * 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 @@ function setInitiatorTabTitle(initiatorTabTitle) {
* Closes this print preview tab.
*/
function closePrintPreviewTab() {
+ window.removeEventListener('keydown', onKeyDown);
chrome.send('closePrintPreviewTab');
chrome.send('DialogClose');
}
/**
+ * Pass certain directional keyboard events to the PDF viewer.
+ * @param {Event} e The keydown event.
+ */
+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')
+ 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 @@ function onKeyDown(e) {
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.removeEventListener('keydown', onKeyDown);
onSystemDialogLinkClicked();
+ e.preventDefault();
}
}
+
+ tryToHandleDirectionKeyDown(e);
}
window.addEventListener('DOMContentLoaded', onLoad);
-window.onkeydown = onKeyDown;
+window.addEventListener('keydown', onKeyDown);
/// Pull in all other scripts in a single shot.
<include src="print_preview_animations.js"/>