| 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 <include src="../../../../ui/webui/resources/js/util.js"> | 7 <include src="../../../../ui/webui/resources/js/util.js"> |
| 8 <include src="pdf_scripting_api.js"> | 8 <include src="pdf_scripting_api.js"> |
| 9 <include src="viewport.js"> | 9 <include src="viewport.js"> |
| 10 | 10 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 for (var header in this.streamDetails.responseHeaders) { | 100 for (var header in this.streamDetails.responseHeaders) { |
| 101 headers += header + ': ' + | 101 headers += header + ': ' + |
| 102 this.streamDetails.responseHeaders[header] + '\n'; | 102 this.streamDetails.responseHeaders[header] + '\n'; |
| 103 } | 103 } |
| 104 this.plugin_.setAttribute('headers', headers); | 104 this.plugin_.setAttribute('headers', headers); |
| 105 | 105 |
| 106 if (window.top == window) | 106 if (window.top == window) |
| 107 this.plugin_.setAttribute('full-frame', ''); | 107 this.plugin_.setAttribute('full-frame', ''); |
| 108 document.body.appendChild(this.plugin_); | 108 document.body.appendChild(this.plugin_); |
| 109 | 109 |
| 110 this.setupEventListeners_(); | 110 // Setup the button event listeners. |
| 111 $('fit-to-width-button').addEventListener('click', |
| 112 this.viewport_.fitToWidth.bind(this.viewport_)); |
| 113 $('fit-to-page-button').addEventListener('click', |
| 114 this.viewport_.fitToPage.bind(this.viewport_)); |
| 115 $('zoom-in-button').addEventListener('click', |
| 116 this.viewport_.zoomIn.bind(this.viewport_)); |
| 117 $('zoom-out-button').addEventListener('click', |
| 118 this.viewport_.zoomOut.bind(this.viewport_)); |
| 119 $('save-button-link').href = this.streamDetails.originalUrl; |
| 120 $('print-button').addEventListener('click', this.print_.bind(this)); |
| 121 |
| 122 // Setup the keyboard event listener. |
| 123 document.onkeydown = this.handleKeyEvent_.bind(this); |
| 111 } | 124 } |
| 112 | 125 |
| 113 PDFViewer.prototype = { | 126 PDFViewer.prototype = { |
| 114 /** | 127 /** |
| 115 * @private | 128 * @private |
| 116 * Sets up event listeners for key shortcuts and also the UI buttons. | 129 * Handle key events. These may come from the user directly or via the |
| 130 * scripting API. |
| 131 * @param {KeyboardEvent} e the event to handle. |
| 117 */ | 132 */ |
| 118 setupEventListeners_: function() { | 133 handleKeyEvent_: function(e) { |
| 119 // Setup the button event listeners. | 134 var position = this.viewport_.position; |
| 120 $('fit-to-width-button').addEventListener('click', | 135 // Certain scroll events may be sent from outside of the extension. |
| 121 this.viewport_.fitToWidth.bind(this.viewport_)); | 136 var fromScriptingAPI = e.type == 'scriptingKeypress'; |
| 122 $('fit-to-page-button').addEventListener('click', | |
| 123 this.viewport_.fitToPage.bind(this.viewport_)); | |
| 124 $('zoom-in-button').addEventListener('click', | |
| 125 this.viewport_.zoomIn.bind(this.viewport_)); | |
| 126 $('zoom-out-button').addEventListener('click', | |
| 127 this.viewport_.zoomOut.bind(this.viewport_)); | |
| 128 $('save-button-link').href = this.streamDetails.originalUrl; | |
| 129 $('print-button').addEventListener('click', this.print_.bind(this)); | |
| 130 | 137 |
| 131 // Setup keyboard event listeners. | 138 switch (e.keyCode) { |
| 132 document.onkeydown = function(e) { | 139 case 33: // Page up key. |
| 133 switch (e.keyCode) { | 140 // Go to the previous page if we are fit-to-page. |
| 134 case 37: // Left arrow key. | 141 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 135 // Go to the previous page if there are no horizontal scrollbars. | 142 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
| 136 if (!this.viewport_.documentHasScrollbars().x) { | 143 // Since we do the movement of the page. |
| 137 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 144 e.preventDefault(); |
| 138 // Since we do the movement of the page. | 145 } else if (fromScriptingAPI) { |
| 139 e.preventDefault(); | 146 position.y -= this.viewport.size.height; |
| 140 } | 147 this.viewport.position = position; |
| 141 return; | 148 } |
| 142 case 33: // Page up key. | 149 return; |
| 143 // Go to the previous page if we are fit-to-page. | 150 case 34: // Page down key. |
| 144 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 151 // Go to the next page if we are fit-to-page. |
| 145 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 152 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
| 146 // Since we do the movement of the page. | 153 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
| 147 e.preventDefault(); | 154 // Since we do the movement of the page. |
| 148 } | 155 e.preventDefault(); |
| 149 return; | 156 } else if (fromScriptingAPI) { |
| 150 case 39: // Right arrow key. | 157 position.y += this.viewport.size.height; |
| 151 // Go to the next page if there are no horizontal scrollbars. | 158 this.viewport.position = position; |
| 152 if (!this.viewport_.documentHasScrollbars().x) { | 159 } |
| 153 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 160 return; |
| 154 // Since we do the movement of the page. | 161 case 37: // Left arrow key. |
| 155 e.preventDefault(); | 162 // Go to the previous page if there are no horizontal scrollbars. |
| 156 } | 163 if (!this.viewport_.documentHasScrollbars().x) { |
| 157 return; | 164 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
| 158 case 34: // Page down key. | 165 // Since we do the movement of the page. |
| 159 // Go to the next page if we are fit-to-page. | 166 e.preventDefault(); |
| 160 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 167 } else if (fromScriptingAPI) { |
| 161 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 168 position.x -= Viewport.SCROLL_INCREMENT; |
| 162 // Since we do the movement of the page. | 169 this.viewport.position = position; |
| 163 e.preventDefault(); | 170 } |
| 164 } | 171 return; |
| 165 return; | 172 case 38: // Up arrow key. |
| 166 case 187: // +/= key. | 173 if (fromScriptingAPI) { |
| 167 case 107: // Numpad + key. | 174 position.y -= Viewport.SCROLL_INCREMENT; |
| 168 if (e.ctrlKey || e.metaKey) { | 175 this.viewport.position = position; |
| 169 this.viewport_.zoomIn(); | 176 } |
| 170 // Since we do the zooming of the page. | 177 return; |
| 171 e.preventDefault(); | 178 case 39: // Right arrow key. |
| 172 } | 179 // Go to the next page if there are no horizontal scrollbars. |
| 173 return; | 180 if (!this.viewport_.documentHasScrollbars().x) { |
| 174 case 189: // -/_ key. | 181 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
| 175 case 109: // Numpad - key. | 182 // Since we do the movement of the page. |
| 176 if (e.ctrlKey || e.metaKey) { | 183 e.preventDefault(); |
| 177 this.viewport_.zoomOut(); | 184 } else if (fromScriptingAPI) { |
| 178 // Since we do the zooming of the page. | 185 position.x += Viewport.SCROLL_INCREMENT; |
| 179 e.preventDefault(); | 186 this.viewport.position = position; |
| 180 } | 187 } |
| 181 return; | 188 return; |
| 182 case 83: // s key. | 189 case 40: // Down arrow key. |
| 183 if (e.ctrlKey || e.metaKey) { | 190 if (fromScriptingAPI) { |
| 184 // Simulate a click on the button so that the <a download ...> | 191 position.y += Viewport.SCROLL_INCREMENT; |
| 185 // attribute is used. | 192 this.viewport.position = position; |
| 186 $('save-button-link').click(); | 193 } |
| 187 // Since we do the saving of the page. | 194 return; |
| 188 e.preventDefault(); | 195 case 187: // +/= key. |
| 189 } | 196 case 107: // Numpad + key. |
| 190 return; | 197 if (e.ctrlKey || e.metaKey) { |
| 191 case 80: // p key. | 198 this.viewport_.zoomIn(); |
| 192 if (e.ctrlKey || e.metaKey) { | 199 // Since we do the zooming of the page. |
| 193 this.print_(); | 200 e.preventDefault(); |
| 194 // Since we do the printing of the page. | 201 } |
| 195 e.preventDefault(); | 202 return; |
| 196 } | 203 case 189: // -/_ key. |
| 197 return; | 204 case 109: // Numpad - key. |
| 198 } | 205 if (e.ctrlKey || e.metaKey) { |
| 199 }.bind(this); | 206 this.viewport_.zoomOut(); |
| 207 // Since we do the zooming of the page. |
| 208 e.preventDefault(); |
| 209 } |
| 210 return; |
| 211 case 83: // s key. |
| 212 if (e.ctrlKey || e.metaKey) { |
| 213 // Simulate a click on the button so that the <a download ...> |
| 214 // attribute is used. |
| 215 $('save-button-link').click(); |
| 216 // Since we do the saving of the page. |
| 217 e.preventDefault(); |
| 218 } |
| 219 return; |
| 220 case 80: // p key. |
| 221 if (e.ctrlKey || e.metaKey) { |
| 222 this.print_(); |
| 223 // Since we do the printing of the page. |
| 224 e.preventDefault(); |
| 225 } |
| 226 return; |
| 227 } |
| 200 }, | 228 }, |
| 201 | 229 |
| 202 /** | 230 /** |
| 203 * @private | 231 * @private |
| 204 * Notify the plugin to print. | 232 * Notify the plugin to print. |
| 205 */ | 233 */ |
| 206 print_: function() { | 234 print_: function() { |
| 207 this.plugin_.postMessage({ | 235 this.plugin_.postMessage({ |
| 208 type: 'print', | 236 type: 'print', |
| 209 }); | 237 }); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 | 412 |
| 385 /** | 413 /** |
| 386 * @private | 414 * @private |
| 387 * Handle a scripting message from outside the extension (typically sent by | 415 * Handle a scripting message from outside the extension (typically sent by |
| 388 * PDFScriptingAPI in a page containing the extension) to interact with the | 416 * PDFScriptingAPI in a page containing the extension) to interact with the |
| 389 * plugin. | 417 * plugin. |
| 390 * @param {MessageObject} message the message to handle. | 418 * @param {MessageObject} message the message to handle. |
| 391 */ | 419 */ |
| 392 handleScriptingMessage_: function(message) { | 420 handleScriptingMessage_: function(message) { |
| 393 switch (message.data.type.toString()) { | 421 switch (message.data.type.toString()) { |
| 422 case 'getAccessibilityJSON': |
| 423 case 'loadPreviewPage': |
| 424 this.plugin_.postMessage(message.data); |
| 425 break; |
| 394 case 'resetPrintPreviewMode': | 426 case 'resetPrintPreviewMode': |
| 395 if (!this.inPrintPreviewMode_) { | 427 if (!this.inPrintPreviewMode_) { |
| 396 this.inPrintPreviewMode_ = true; | 428 this.inPrintPreviewMode_ = true; |
| 397 this.viewport_.fitToPage(); | 429 this.viewport_.fitToPage(); |
| 398 } | 430 } |
| 399 | 431 |
| 400 // Stash the scroll location so that it can be restored when the new | 432 // Stash the scroll location so that it can be restored when the new |
| 401 // document is loaded. | 433 // document is loaded. |
| 402 this.lastViewportPosition_ = this.viewport_.position; | 434 this.lastViewportPosition_ = this.viewport_.position; |
| 403 | 435 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 414 this.plugin_.postMessage({ | 446 this.plugin_.postMessage({ |
| 415 type: 'resetPrintPreviewMode', | 447 type: 'resetPrintPreviewMode', |
| 416 url: message.data.url, | 448 url: message.data.url, |
| 417 grayscale: message.data.grayscale, | 449 grayscale: message.data.grayscale, |
| 418 // If the PDF isn't modifiable we send 0 as the page count so that no | 450 // If the PDF isn't modifiable we send 0 as the page count so that no |
| 419 // blank placeholder pages get appended to the PDF. | 451 // blank placeholder pages get appended to the PDF. |
| 420 pageCount: (message.data.modifiable ? | 452 pageCount: (message.data.modifiable ? |
| 421 message.data.pageNumbers.length : 0) | 453 message.data.pageNumbers.length : 0) |
| 422 }); | 454 }); |
| 423 break; | 455 break; |
| 424 case 'loadPreviewPage': | 456 case 'sendKeyEvent': |
| 425 case 'getAccessibilityJSON': | 457 var e = document.createEvent('Event'); |
| 426 this.plugin_.postMessage(message.data); | 458 e.initEvent('scriptingKeypress'); |
| 459 e.keyCode = message.data.keyCode; |
| 460 this.handleKeyEvent_(e); |
| 427 break; | 461 break; |
| 428 } | 462 } |
| 429 | 463 |
| 430 }, | 464 }, |
| 431 | 465 |
| 432 /** | 466 /** |
| 433 * @private | 467 * @private |
| 434 * Send a scripting message outside the extension (typically to | 468 * Send a scripting message outside the extension (typically to |
| 435 * PDFScriptingAPI in a page containing the extension). | 469 * PDFScriptingAPI in a page containing the extension). |
| 436 * @param {Object} message the message to send. | 470 * @param {Object} message the message to send. |
| 437 */ | 471 */ |
| 438 sendScriptingMessage_: function(message) { | 472 sendScriptingMessage_: function(message) { |
| 439 window.parent.postMessage(message, '*'); | 473 window.parent.postMessage(message, '*'); |
| 440 }, | 474 }, |
| 441 | 475 |
| 442 /** | 476 /** |
| 443 * @type {Viewport} the viewport of the PDF viewer. | 477 * @type {Viewport} the viewport of the PDF viewer. |
| 444 */ | 478 */ |
| 445 get viewport() { | 479 get viewport() { |
| 446 return this.viewport_; | 480 return this.viewport_; |
| 447 } | 481 } |
| 448 }; | 482 }; |
| 449 | 483 |
| 450 var viewer = new PDFViewer(); | 484 var viewer = new PDFViewer(); |
| OLD | NEW |