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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 streamUrl: url, | 85 streamUrl: url, |
86 originalUrl: url | 86 originalUrl: url |
87 }; | 87 }; |
88 } | 88 } |
89 | 89 |
90 this.plugin_.setAttribute('src', streamDetails.streamUrl); | 90 this.plugin_.setAttribute('src', streamDetails.streamUrl); |
91 if (window.top == window) | 91 if (window.top == window) |
92 this.plugin_.setAttribute('full-frame', ''); | 92 this.plugin_.setAttribute('full-frame', ''); |
93 document.body.appendChild(this.plugin_); | 93 document.body.appendChild(this.plugin_); |
94 | 94 |
95 this.setupEventListeners_(streamDetails); | 95 // Setup the button event listeners. |
| 96 $('fit-to-width-button').addEventListener('click', |
| 97 this.viewport_.fitToWidth.bind(this.viewport_)); |
| 98 $('fit-to-page-button').addEventListener('click', |
| 99 this.viewport_.fitToPage.bind(this.viewport_)); |
| 100 $('zoom-in-button').addEventListener('click', |
| 101 this.viewport_.zoomIn.bind(this.viewport_)); |
| 102 $('zoom-out-button').addEventListener('click', |
| 103 this.viewport_.zoomOut.bind(this.viewport_)); |
| 104 $('save-button-link').href = streamDetails.originalUrl; |
| 105 $('print-button').addEventListener('click', this.print_.bind(this)); |
| 106 |
| 107 // Setup the keyboard event listener. |
| 108 document.onkeydown = this.handleKeyEvent_.bind(this); |
96 } | 109 } |
97 | 110 |
98 PDFViewer.prototype = { | 111 PDFViewer.prototype = { |
99 /** | 112 /** |
100 * @private | 113 * @private |
101 * Sets up event listeners for key shortcuts and also the UI buttons. | 114 * Handle key events. These may come from the user directly or via the |
102 * @param {Object} streamDetails the details of the original HTTP request for | 115 * scripting API. |
103 * the PDF. | 116 * @param {KeyboardEvent} e the event to handle. |
104 */ | 117 */ |
105 setupEventListeners_: function(streamDetails) { | 118 handleKeyEvent_: function(e) { |
106 // Setup the button event listeners. | 119 var position = this.viewport_.position; |
107 $('fit-to-width-button').addEventListener('click', | 120 // Certain scroll events may be sent from outside of the extension. |
108 this.viewport_.fitToWidth.bind(this.viewport_)); | 121 var fromScriptingAPI = e.type == 'scriptingKeypress'; |
109 $('fit-to-page-button').addEventListener('click', | |
110 this.viewport_.fitToPage.bind(this.viewport_)); | |
111 $('zoom-in-button').addEventListener('click', | |
112 this.viewport_.zoomIn.bind(this.viewport_)); | |
113 $('zoom-out-button').addEventListener('click', | |
114 this.viewport_.zoomOut.bind(this.viewport_)); | |
115 $('save-button-link').href = streamDetails.originalUrl; | |
116 $('print-button').addEventListener('click', this.print_.bind(this)); | |
117 | 122 |
118 // Setup keyboard event listeners. | 123 switch (e.keyCode) { |
119 document.onkeydown = function(e) { | 124 case 33: // Page up key. |
120 switch (e.keyCode) { | 125 // Go to the previous page if we are fit-to-page. |
121 case 37: // Left arrow key. | 126 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
122 // Go to the previous page if there are no horizontal scrollbars. | 127 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
123 if (!this.viewport_.documentHasScrollbars().x) { | 128 // Since we do the movement of the page. |
124 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 129 e.preventDefault(); |
125 // Since we do the movement of the page. | 130 } else if (fromScriptingAPI) { |
126 e.preventDefault(); | 131 position.y -= this.viewport.size.height; |
127 } | 132 this.viewport.position = position; |
128 return; | 133 } |
129 case 33: // Page up key. | 134 return; |
130 // Go to the previous page if we are fit-to-page. | 135 case 34: // Page down key. |
131 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 136 // Go to the next page if we are fit-to-page. |
132 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); | 137 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { |
133 // Since we do the movement of the page. | 138 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
134 e.preventDefault(); | 139 // Since we do the movement of the page. |
135 } | 140 e.preventDefault(); |
136 return; | 141 } else if (fromScriptingAPI) { |
137 case 39: // Right arrow key. | 142 position.y += this.viewport.size.height; |
138 // Go to the next page if there are no horizontal scrollbars. | 143 this.viewport.position = position; |
139 if (!this.viewport_.documentHasScrollbars().x) { | 144 } |
140 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 145 return; |
141 // Since we do the movement of the page. | 146 case 37: // Left arrow key. |
142 e.preventDefault(); | 147 // Go to the previous page if there are no horizontal scrollbars. |
143 } | 148 if (!this.viewport_.documentHasScrollbars().x) { |
144 return; | 149 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() - 1); |
145 case 34: // Page down key. | 150 // Since we do the movement of the page. |
146 // Go to the next page if we are fit-to-page. | 151 e.preventDefault(); |
147 if (this.viewport_.fittingType == Viewport.FittingType.FIT_TO_PAGE) { | 152 } else if (fromScriptingAPI) { |
148 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); | 153 position.x -= Viewport.SCROLL_INCREMENT; |
149 // Since we do the movement of the page. | 154 this.viewport.position = position; |
150 e.preventDefault(); | 155 } |
151 } | 156 return; |
152 return; | 157 case 38: // Up arrow key. |
153 case 187: // +/= key. | 158 if (fromScriptingAPI) { |
154 case 107: // Numpad + key. | 159 position.y -= Viewport.SCROLL_INCREMENT; |
155 if (e.ctrlKey || e.metaKey) { | 160 this.viewport.position = position; |
156 this.viewport_.zoomIn(); | 161 } |
157 // Since we do the zooming of the page. | 162 return; |
158 e.preventDefault(); | 163 case 39: // Right arrow key. |
159 } | 164 // Go to the next page if there are no horizontal scrollbars. |
160 return; | 165 if (!this.viewport_.documentHasScrollbars().x) { |
161 case 189: // -/_ key. | 166 this.viewport_.goToPage(this.viewport_.getMostVisiblePage() + 1); |
162 case 109: // Numpad - key. | 167 // Since we do the movement of the page. |
163 if (e.ctrlKey || e.metaKey) { | 168 e.preventDefault(); |
164 this.viewport_.zoomOut(); | 169 } else if (fromScriptingAPI) { |
165 // Since we do the zooming of the page. | 170 position.x += Viewport.SCROLL_INCREMENT; |
166 e.preventDefault(); | 171 this.viewport.position = position; |
167 } | 172 } |
168 return; | 173 return; |
169 case 83: // s key. | 174 case 40: // Down arrow key. |
170 if (e.ctrlKey || e.metaKey) { | 175 if (fromScriptingAPI) { |
171 // Simulate a click on the button so that the <a download ...> | 176 position.y += Viewport.SCROLL_INCREMENT; |
172 // attribute is used. | 177 this.viewport.position = position; |
173 $('save-button-link').click(); | 178 } |
174 // Since we do the saving of the page. | 179 return; |
175 e.preventDefault(); | 180 case 187: // +/= key. |
176 } | 181 case 107: // Numpad + key. |
177 return; | 182 if (e.ctrlKey || e.metaKey) { |
178 case 80: // p key. | 183 this.viewport_.zoomIn(); |
179 if (e.ctrlKey || e.metaKey) { | 184 // Since we do the zooming of the page. |
180 this.print_(); | 185 e.preventDefault(); |
181 // Since we do the printing of the page. | 186 } |
182 e.preventDefault(); | 187 return; |
183 } | 188 case 189: // -/_ key. |
184 return; | 189 case 109: // Numpad - key. |
185 } | 190 if (e.ctrlKey || e.metaKey) { |
186 }.bind(this); | 191 this.viewport_.zoomOut(); |
| 192 // Since we do the zooming of the page. |
| 193 e.preventDefault(); |
| 194 } |
| 195 return; |
| 196 case 83: // s key. |
| 197 if (e.ctrlKey || e.metaKey) { |
| 198 // Simulate a click on the button so that the <a download ...> |
| 199 // attribute is used. |
| 200 $('save-button-link').click(); |
| 201 // Since we do the saving of the page. |
| 202 e.preventDefault(); |
| 203 } |
| 204 return; |
| 205 case 80: // p key. |
| 206 if (e.ctrlKey || e.metaKey) { |
| 207 this.print_(); |
| 208 // Since we do the printing of the page. |
| 209 e.preventDefault(); |
| 210 } |
| 211 return; |
| 212 } |
187 }, | 213 }, |
188 | 214 |
189 /** | 215 /** |
190 * @private | 216 * @private |
191 * Notify the plugin to print. | 217 * Notify the plugin to print. |
192 */ | 218 */ |
193 print_: function() { | 219 print_: function() { |
194 this.plugin_.postMessage({ | 220 this.plugin_.postMessage({ |
195 type: 'print', | 221 type: 'print', |
196 }); | 222 }); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 376 |
351 /** | 377 /** |
352 * @private | 378 * @private |
353 * Handle a scripting message from outside the extension (typically sent by | 379 * Handle a scripting message from outside the extension (typically sent by |
354 * PDFScriptingAPI in a page containing the extension) to interact with the | 380 * PDFScriptingAPI in a page containing the extension) to interact with the |
355 * plugin. | 381 * plugin. |
356 * @param {MessageObject} message the message to handle. | 382 * @param {MessageObject} message the message to handle. |
357 */ | 383 */ |
358 handleScriptingMessage_: function(message) { | 384 handleScriptingMessage_: function(message) { |
359 switch (message.data.type.toString()) { | 385 switch (message.data.type.toString()) { |
| 386 case 'getAccessibilityJSON': |
| 387 case 'loadPreviewPage': |
| 388 this.plugin_.postMessage(message.data); |
| 389 break; |
360 case 'resetPrintPreviewMode': | 390 case 'resetPrintPreviewMode': |
361 if (!this.inPrintPreviewMode_) { | 391 if (!this.inPrintPreviewMode_) { |
362 this.inPrintPreviewMode_ = true; | 392 this.inPrintPreviewMode_ = true; |
363 this.viewport_.fitToPage(); | 393 this.viewport_.fitToPage(); |
364 } | 394 } |
365 | 395 |
366 // Stash the scroll location so that it can be restored when the new | 396 // Stash the scroll location so that it can be restored when the new |
367 // document is loaded. | 397 // document is loaded. |
368 this.lastViewportPosition_ = this.viewport_.position; | 398 this.lastViewportPosition_ = this.viewport_.position; |
369 | 399 |
(...skipping 10 matching lines...) Expand all Loading... |
380 this.plugin_.postMessage({ | 410 this.plugin_.postMessage({ |
381 type: 'resetPrintPreviewMode', | 411 type: 'resetPrintPreviewMode', |
382 url: message.data.url, | 412 url: message.data.url, |
383 grayscale: message.data.grayscale, | 413 grayscale: message.data.grayscale, |
384 // If the PDF isn't modifiable we send 0 as the page count so that no | 414 // If the PDF isn't modifiable we send 0 as the page count so that no |
385 // blank placeholder pages get appended to the PDF. | 415 // blank placeholder pages get appended to the PDF. |
386 pageCount: (message.data.modifiable ? | 416 pageCount: (message.data.modifiable ? |
387 message.data.pageNumbers.length : 0) | 417 message.data.pageNumbers.length : 0) |
388 }); | 418 }); |
389 break; | 419 break; |
390 case 'loadPreviewPage': | 420 case 'sendKeyEvent': |
391 case 'getAccessibilityJSON': | 421 var e = document.createEvent('Event'); |
392 this.plugin_.postMessage(message.data); | 422 e.initEvent('scriptingKeypress'); |
| 423 e.keyCode = message.data.keyCode; |
| 424 this.handleKeyEvent_(e); |
393 break; | 425 break; |
394 } | 426 } |
395 | 427 |
396 }, | 428 }, |
397 | 429 |
398 /** | 430 /** |
399 * @private | 431 * @private |
400 * Send a scripting message outside the extension (typically to | 432 * Send a scripting message outside the extension (typically to |
401 * PDFScriptingAPI in a page containing the extension). | 433 * PDFScriptingAPI in a page containing the extension). |
402 * @param {Object} message the message to send. | 434 * @param {Object} message the message to send. |
403 */ | 435 */ |
404 sendScriptingMessage_: function(message) { | 436 sendScriptingMessage_: function(message) { |
405 window.parent.postMessage(message, '*'); | 437 window.parent.postMessage(message, '*'); |
406 }, | 438 }, |
407 | 439 |
408 /** | 440 /** |
409 * @type {Viewport} the viewport of the PDF viewer. | 441 * @type {Viewport} the viewport of the PDF viewer. |
410 */ | 442 */ |
411 get viewport() { | 443 get viewport() { |
412 return this.viewport_; | 444 return this.viewport_; |
413 } | 445 } |
414 }; | 446 }; |
415 | 447 |
416 var viewer = new PDFViewer(); | 448 var viewer = new PDFViewer(); |
OLD | NEW |