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 /** | 7 /** |
8 * @return {number} Width of a scrollbar in pixels | 8 * @return {number} Width of a scrollbar in pixels |
9 */ | 9 */ |
10 function getScrollbarWidth() { | 10 function getScrollbarWidth() { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // to be displayed in the window. It is sized according to the document size | 79 // to be displayed in the window. It is sized according to the document size |
80 // of the pdf and zoom level. | 80 // of the pdf and zoom level. |
81 this.sizer_ = $('sizer'); | 81 this.sizer_ = $('sizer'); |
82 this.toolbar_ = $('toolbar'); | 82 this.toolbar_ = $('toolbar'); |
83 this.pageIndicator_ = $('page-indicator'); | 83 this.pageIndicator_ = $('page-indicator'); |
84 this.progressBar_ = $('progress-bar'); | 84 this.progressBar_ = $('progress-bar'); |
85 this.passwordScreen_ = $('password-screen'); | 85 this.passwordScreen_ = $('password-screen'); |
86 this.passwordScreen_.addEventListener('password-submitted', | 86 this.passwordScreen_.addEventListener('password-submitted', |
87 this.onPasswordSubmitted_.bind(this)); | 87 this.onPasswordSubmitted_.bind(this)); |
88 this.errorScreen_ = $('error-screen'); | 88 this.errorScreen_ = $('error-screen'); |
89 this.bookmarksPane = $('bookmarks-pane'); | 89 this.materialToolbar_ = $('material-toolbar'); |
| 90 this.bookmarksPane_ = $('bookmarks-pane'); |
90 | 91 |
91 // Create the viewport. | 92 // Create the viewport. |
92 this.viewport_ = new Viewport(window, | 93 this.viewport_ = new Viewport(window, |
93 this.sizer_, | 94 this.sizer_, |
94 this.viewportChanged_.bind(this), | 95 this.viewportChanged_.bind(this), |
95 this.beforeZoom_.bind(this), | 96 this.beforeZoom_.bind(this), |
96 this.afterZoom_.bind(this), | 97 this.afterZoom_.bind(this), |
97 getScrollbarWidth()); | 98 getScrollbarWidth()); |
98 // Create the plugin object dynamically so we can set its src. The plugin | 99 // Create the plugin object dynamically so we can set its src. The plugin |
99 // element is sized to fill the entire window and is set to be fixed | 100 // element is sized to fill the entire window and is set to be fixed |
(...skipping 11 matching lines...) Expand all Loading... |
111 this.plugin_.setAttribute('is-material', ''); | 112 this.plugin_.setAttribute('is-material', ''); |
112 | 113 |
113 // Handle scripting messages from outside the extension that wish to interact | 114 // Handle scripting messages from outside the extension that wish to interact |
114 // with it. We also send a message indicating that extension has loaded and | 115 // with it. We also send a message indicating that extension has loaded and |
115 // is ready to receive messages. | 116 // is ready to receive messages. |
116 window.addEventListener('message', this.handleScriptingMessage.bind(this), | 117 window.addEventListener('message', this.handleScriptingMessage.bind(this), |
117 false); | 118 false); |
118 | 119 |
119 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); | 120 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); |
120 if (this.isMaterial_) | 121 if (this.isMaterial_) |
121 $('title').textContent = document.title; | 122 this.materialToolbar_.docTitle = document.title; |
122 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); | 123 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); |
123 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); | 124 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); |
124 var headers = ''; | 125 var headers = ''; |
125 for (var header in this.streamDetails_.responseHeaders) { | 126 for (var header in this.streamDetails_.responseHeaders) { |
126 headers += header + ': ' + | 127 headers += header + ': ' + |
127 this.streamDetails_.responseHeaders[header] + '\n'; | 128 this.streamDetails_.responseHeaders[header] + '\n'; |
128 } | 129 } |
129 this.plugin_.setAttribute('headers', headers); | 130 this.plugin_.setAttribute('headers', headers); |
130 | 131 |
131 if (!this.streamDetails_.embedded) | 132 if (!this.streamDetails_.embedded) |
132 this.plugin_.setAttribute('full-frame', ''); | 133 this.plugin_.setAttribute('full-frame', ''); |
133 document.body.appendChild(this.plugin_); | 134 document.body.appendChild(this.plugin_); |
134 | 135 |
135 this.pageIndicator_.addEventListener('changePage', function(e) { | |
136 this.viewport_.goToPage(e.detail.page); | |
137 }.bind(this)); | |
138 | |
139 if (this.isMaterial_) { | |
140 this.bookmarksPane.addEventListener('changePage', function(e) { | |
141 this.viewport_.goToPage(e.detail.page); | |
142 }.bind(this)); | |
143 } | |
144 | 136 |
145 // Setup the button event listeners. | 137 // Setup the button event listeners. |
146 $('fit-to-width-button').addEventListener('click', | 138 $('fit-to-width-button').addEventListener('click', |
147 this.viewport_.fitToWidth.bind(this.viewport_)); | 139 this.viewport_.fitToWidth.bind(this.viewport_)); |
148 $('fit-to-page-button').addEventListener('click', | 140 $('fit-to-page-button').addEventListener('click', |
149 this.viewport_.fitToPage.bind(this.viewport_)); | 141 this.viewport_.fitToPage.bind(this.viewport_)); |
150 $('zoom-in-button').addEventListener('click', | 142 $('zoom-in-button').addEventListener('click', |
151 this.viewport_.zoomIn.bind(this.viewport_)); | 143 this.viewport_.zoomIn.bind(this.viewport_)); |
152 $('zoom-out-button').addEventListener('click', | 144 $('zoom-out-button').addEventListener('click', |
153 this.viewport_.zoomOut.bind(this.viewport_)); | 145 this.viewport_.zoomOut.bind(this.viewport_)); |
154 $('save-button').addEventListener('click', this.save_.bind(this)); | 146 |
155 $('print-button').addEventListener('click', this.print_.bind(this)); | |
156 if (this.isMaterial_) { | 147 if (this.isMaterial_) { |
157 $('bookmarks-button').addEventListener('click', function() { | 148 this.materialToolbar_.addEventListener('save', this.save_.bind(this)); |
158 this.bookmarksPane.toggle(); | 149 this.materialToolbar_.addEventListener('print', this.print_.bind(this)); |
| 150 this.materialToolbar_.addEventListener('toggle-bookmarks', function() { |
| 151 this.bookmarksPane_.toggle(); |
159 }.bind(this)); | 152 }.bind(this)); |
160 $('rotate-right-button').addEventListener('click', | 153 this.materialToolbar_.addEventListener('rotate-right', |
161 this.rotateClockwise_.bind(this)); | 154 this.rotateClockwise_.bind(this)); |
| 155 document.body.addEventListener('change-page', function(e) { |
| 156 this.viewport_.goToPage(e.detail.page); |
| 157 }.bind(this)); |
| 158 } else { |
| 159 $('save-button').addEventListener('click', this.save_.bind(this)); |
| 160 $('print-button').addEventListener('click', this.print_.bind(this)); |
162 } | 161 } |
163 | 162 |
164 // Setup the keyboard event listener. | 163 // Setup the keyboard event listener. |
165 document.onkeydown = this.handleKeyEvent_.bind(this); | 164 document.onkeydown = this.handleKeyEvent_.bind(this); |
166 | 165 |
167 // Set up the zoom API. | 166 // Set up the zoom API. |
168 if (this.shouldManageZoom_()) { | 167 if (this.shouldManageZoom_()) { |
169 chrome.tabs.setZoomSettings(this.streamDetails_.tabId, | 168 chrome.tabs.setZoomSettings(this.streamDetails_.tabId, |
170 {mode: 'manual', scope: 'per-tab'}, | 169 {mode: 'manual', scope: 'per-tab'}, |
171 this.afterZoom_.bind(this)); | 170 this.afterZoom_.bind(this)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 if (e.ctrlKey || e.metaKey) { | 282 if (e.ctrlKey || e.metaKey) { |
284 this.plugin_.postMessage({ | 283 this.plugin_.postMessage({ |
285 type: 'selectAll' | 284 type: 'selectAll' |
286 }); | 285 }); |
287 // Since we do selection ourselves. | 286 // Since we do selection ourselves. |
288 e.preventDefault(); | 287 e.preventDefault(); |
289 } | 288 } |
290 return; | 289 return; |
291 case 71: // g key. | 290 case 71: // g key. |
292 if (this.isMaterial_ && (e.ctrlKey || e.metaKey)) { | 291 if (this.isMaterial_ && (e.ctrlKey || e.metaKey)) { |
293 this.pageIndicator_.select(); | 292 this.materialToolbar_.selectPageNumber(); |
294 // To prevent the default "find text" behaviour in Chrome. | 293 // To prevent the default "find text" behaviour in Chrome. |
295 e.preventDefault(); | 294 e.preventDefault(); |
296 } | 295 } |
297 return; | 296 return; |
298 case 80: // p key. | 297 case 80: // p key. |
299 if (e.ctrlKey || e.metaKey) { | 298 if (e.ctrlKey || e.metaKey) { |
300 this.print_(); | 299 this.print_(); |
301 // Since we do the printing of the page. | 300 // Since we do the printing of the page. |
302 e.preventDefault(); | 301 e.preventDefault(); |
303 } | 302 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 }, | 384 }, |
386 | 385 |
387 /** | 386 /** |
388 * @private | 387 * @private |
389 * Update the loading progress of the document in response to a progress | 388 * Update the loading progress of the document in response to a progress |
390 * message being received from the plugin. | 389 * message being received from the plugin. |
391 * @param {number} progress the progress as a percentage. | 390 * @param {number} progress the progress as a percentage. |
392 */ | 391 */ |
393 updateProgress_: function(progress) { | 392 updateProgress_: function(progress) { |
394 if (this.isMaterial_) | 393 if (this.isMaterial_) |
395 this.progressBar_.value = progress; | 394 this.materialToolbar_.loadProgress = progress; |
396 else | 395 else |
397 this.progressBar_.progress = progress; | 396 this.progressBar_.progress = progress; |
398 | 397 |
399 if (progress == -1) { | 398 if (progress == -1) { |
400 // Document load failed. | 399 // Document load failed. |
401 this.errorScreen_.style.visibility = 'visible'; | 400 this.errorScreen_.style.visibility = 'visible'; |
402 this.sizer_.style.display = 'none'; | 401 this.sizer_.style.display = 'none'; |
403 this.toolbar_.style.visibility = 'hidden'; | 402 this.toolbar_.style.visibility = 'hidden'; |
404 if (this.passwordScreen_.active) { | 403 if (this.passwordScreen_.active) { |
405 this.passwordScreen_.deny(); | 404 this.passwordScreen_.deny(); |
406 this.passwordScreen_.active = false; | 405 this.passwordScreen_.active = false; |
407 } | 406 } |
408 } else if (progress == 100) { | 407 } else if (progress == 100) { |
409 // Document load complete. | 408 // Document load complete. |
410 if (this.lastViewportPosition_) | 409 if (this.lastViewportPosition_) |
411 this.viewport_.position = this.lastViewportPosition_; | 410 this.viewport_.position = this.lastViewportPosition_; |
412 if (this.isMaterial_) | 411 if (!this.isMaterial_) |
413 this.pageIndicator_.style.visibility = 'visible'; | 412 this.pageIndicator_.style.visibility = 'visible'; |
414 this.handleURLParams_(); | 413 this.handleURLParams_(); |
415 this.loaded_ = true; | 414 this.loaded_ = true; |
416 this.sendScriptingMessage_({ | 415 this.sendScriptingMessage_({ |
417 type: 'documentLoaded' | 416 type: 'documentLoaded' |
418 }); | 417 }); |
419 while (this.delayedScriptingMessages_.length > 0) | 418 while (this.delayedScriptingMessages_.length > 0) |
420 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); | 419 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); |
421 } | 420 } |
422 }, | 421 }, |
(...skipping 20 matching lines...) Expand all Loading... |
443 switch (message.data.type.toString()) { | 442 switch (message.data.type.toString()) { |
444 case 'documentDimensions': | 443 case 'documentDimensions': |
445 this.documentDimensions_ = message.data; | 444 this.documentDimensions_ = message.data; |
446 this.viewport_.setDocumentDimensions(this.documentDimensions_); | 445 this.viewport_.setDocumentDimensions(this.documentDimensions_); |
447 // If we received the document dimensions, the password was good so we | 446 // If we received the document dimensions, the password was good so we |
448 // can dismiss the password screen. | 447 // can dismiss the password screen. |
449 if (this.passwordScreen_.active) | 448 if (this.passwordScreen_.active) |
450 this.passwordScreen_.accept(); | 449 this.passwordScreen_.accept(); |
451 | 450 |
452 if (this.isMaterial_) { | 451 if (this.isMaterial_) { |
453 this.pageIndicator_.docLength = | 452 this.materialToolbar_.docLength = |
454 this.documentDimensions_.pageDimensions.length; | 453 this.documentDimensions_.pageDimensions.length; |
455 } else { | 454 } else { |
456 this.pageIndicator_.initialFadeIn(); | 455 this.pageIndicator_.initialFadeIn(); |
457 } | 456 } |
458 | 457 |
459 this.toolbar_.initialFadeIn(); | 458 this.toolbar_.initialFadeIn(); |
460 break; | 459 break; |
461 case 'email': | 460 case 'email': |
462 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc + | 461 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc + |
463 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject + | 462 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject + |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 case 'setScrollPosition': | 496 case 'setScrollPosition': |
498 var position = this.viewport_.position; | 497 var position = this.viewport_.position; |
499 if (message.data.x !== undefined) | 498 if (message.data.x !== undefined) |
500 position.x = message.data.x; | 499 position.x = message.data.x; |
501 if (message.data.y !== undefined) | 500 if (message.data.y !== undefined) |
502 position.y = message.data.y; | 501 position.y = message.data.y; |
503 this.viewport_.position = position; | 502 this.viewport_.position = position; |
504 break; | 503 break; |
505 case 'setTranslatedStrings': | 504 case 'setTranslatedStrings': |
506 this.passwordScreen_.text = message.data.getPasswordString; | 505 this.passwordScreen_.text = message.data.getPasswordString; |
507 this.progressBar_.text = message.data.loadingString; | 506 if (!this.isMaterial_) { |
508 if (!this.isPrintPreview_) | 507 this.progressBar_.text = message.data.loadingString; |
509 this.progressBar_.style.visibility = 'visible'; | 508 if (!this.isPrintPreview_) |
| 509 this.progressBar_.style.visibility = 'visible'; |
| 510 } |
510 this.errorScreen_.text = message.data.loadFailedString; | 511 this.errorScreen_.text = message.data.loadFailedString; |
511 break; | 512 break; |
512 case 'cancelStreamUrl': | 513 case 'cancelStreamUrl': |
513 chrome.mimeHandlerPrivate.abortStream(); | 514 chrome.mimeHandlerPrivate.abortStream(); |
514 break; | 515 break; |
515 case 'bookmarks': | 516 case 'bookmarks': |
516 this.bookmarks_ = message.data.bookmarks; | 517 this.bookmarks_ = message.data.bookmarks; |
517 if (this.isMaterial_) | 518 if (this.isMaterial_) |
518 this.bookmarksPane.bookmarks = message.data.bookmarks; | 519 this.bookmarksPane_.bookmarks = message.data.bookmarks; |
519 break; | 520 break; |
520 } | 521 } |
521 }, | 522 }, |
522 | 523 |
523 /** | 524 /** |
524 * @private | 525 * @private |
525 * A callback that's called before the zoom changes. Notify the plugin to stop | 526 * A callback that's called before the zoom changes. Notify the plugin to stop |
526 * reacting to scroll events while zoom is taking place to avoid flickering. | 527 * reacting to scroll events while zoom is taking place to avoid flickering. |
527 */ | 528 */ |
528 beforeZoom_: function() { | 529 beforeZoom_: function() { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 this.toolbar_.style.right = toolbarRight + 'px'; | 602 this.toolbar_.style.right = toolbarRight + 'px'; |
602 this.toolbar_.style.bottom = toolbarBottom + 'px'; | 603 this.toolbar_.style.bottom = toolbarBottom + 'px'; |
603 // Hide the toolbar if it doesn't fit in the viewport. | 604 // Hide the toolbar if it doesn't fit in the viewport. |
604 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0) | 605 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0) |
605 this.toolbar_.style.visibility = 'hidden'; | 606 this.toolbar_.style.visibility = 'hidden'; |
606 else | 607 else |
607 this.toolbar_.style.visibility = 'visible'; | 608 this.toolbar_.style.visibility = 'visible'; |
608 | 609 |
609 // Update the page indicator. | 610 // Update the page indicator. |
610 var visiblePage = this.viewport_.getMostVisiblePage(); | 611 var visiblePage = this.viewport_.getMostVisiblePage(); |
611 this.pageIndicator_.index = visiblePage; | 612 if (this.isMaterial_) |
| 613 this.materialToolbar_.pageIndex = visiblePage; |
| 614 else |
| 615 this.pageIndicator_.index = visiblePage; |
| 616 |
612 if (!this.isMaterial_) { | 617 if (!this.isMaterial_) { |
613 if (this.documentDimensions_.pageDimensions.length > 1 && | 618 if (this.documentDimensions_.pageDimensions.length > 1 && |
614 hasScrollbars.vertical) { | 619 hasScrollbars.vertical) { |
615 this.pageIndicator_.style.visibility = 'visible'; | 620 this.pageIndicator_.style.visibility = 'visible'; |
616 } else { | 621 } else { |
617 this.pageIndicator_.style.visibility = 'hidden'; | 622 this.pageIndicator_.style.visibility = 'hidden'; |
618 } | 623 } |
619 } | 624 } |
620 | 625 |
621 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage); | 626 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 this.lastViewportPosition_ = this.viewport_.position; | 698 this.lastViewportPosition_ = this.viewport_.position; |
694 | 699 |
695 // TODO(raymes): Disable these properly in the plugin. | 700 // TODO(raymes): Disable these properly in the plugin. |
696 var printButton = $('print-button'); | 701 var printButton = $('print-button'); |
697 if (printButton) | 702 if (printButton) |
698 printButton.parentNode.removeChild(printButton); | 703 printButton.parentNode.removeChild(printButton); |
699 var saveButton = $('save-button'); | 704 var saveButton = $('save-button'); |
700 if (saveButton) | 705 if (saveButton) |
701 saveButton.parentNode.removeChild(saveButton); | 706 saveButton.parentNode.removeChild(saveButton); |
702 | 707 |
703 this.pageIndicator_.pageLabels = message.data.pageNumbers; | 708 if (!this.isMaterial_) |
| 709 this.pageIndicator_.pageLabels = message.data.pageNumbers; |
704 | 710 |
705 this.plugin_.postMessage({ | 711 this.plugin_.postMessage({ |
706 type: 'resetPrintPreviewMode', | 712 type: 'resetPrintPreviewMode', |
707 url: message.data.url, | 713 url: message.data.url, |
708 grayscale: message.data.grayscale, | 714 grayscale: message.data.grayscale, |
709 // If the PDF isn't modifiable we send 0 as the page count so that no | 715 // If the PDF isn't modifiable we send 0 as the page count so that no |
710 // blank placeholder pages get appended to the PDF. | 716 // blank placeholder pages get appended to the PDF. |
711 pageCount: (message.data.modifiable ? | 717 pageCount: (message.data.modifiable ? |
712 message.data.pageNumbers.length : 0) | 718 message.data.pageNumbers.length : 0) |
713 }); | 719 }); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 * Each bookmark is an Object containing a: | 759 * Each bookmark is an Object containing a: |
754 * - title | 760 * - title |
755 * - page (optional) | 761 * - page (optional) |
756 * - array of children (themselves bookmarks) | 762 * - array of children (themselves bookmarks) |
757 * @type {Array} the top-level bookmarks of the PDF. | 763 * @type {Array} the top-level bookmarks of the PDF. |
758 */ | 764 */ |
759 get bookmarks() { | 765 get bookmarks() { |
760 return this.bookmarks_; | 766 return this.bookmarks_; |
761 } | 767 } |
762 }; | 768 }; |
OLD | NEW |