Chromium Code Reviews| 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.toolbarHeight_ = this.isMaterial_ ? $('pdf-toolbar').clientHeight : 0; | 89 this.materialToolbar_ = $('material-toolbar'); |
| 90 this.bookmarksPane = $('bookmarks-pane'); | 90 this.toolbarHeight_ = this.isMaterial_ ? |
| 91 this.materialToolbar_.clientHeight : 0; | |
| 92 this.bookmarksPane_ = $('bookmarks-pane'); | |
| 91 | 93 |
| 92 // Create the viewport. | 94 // Create the viewport. |
| 93 this.viewport_ = new Viewport(window, | 95 this.viewport_ = new Viewport(window, |
| 94 this.sizer_, | 96 this.sizer_, |
| 95 this.viewportChanged_.bind(this), | 97 this.viewportChanged_.bind(this), |
| 96 this.beforeZoom_.bind(this), | 98 this.beforeZoom_.bind(this), |
| 97 this.afterZoom_.bind(this), | 99 this.afterZoom_.bind(this), |
| 98 getScrollbarWidth(), | 100 getScrollbarWidth(), |
| 99 this.toolbarHeight_); | 101 this.toolbarHeight_); |
| 100 // Create the plugin object dynamically so we can set its src. The plugin | 102 // Create the plugin object dynamically so we can set its src. The plugin |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 116 this.plugin_.setAttribute('is-material', ''); | 118 this.plugin_.setAttribute('is-material', ''); |
| 117 | 119 |
| 118 // Handle scripting messages from outside the extension that wish to interact | 120 // Handle scripting messages from outside the extension that wish to interact |
| 119 // with it. We also send a message indicating that extension has loaded and | 121 // with it. We also send a message indicating that extension has loaded and |
| 120 // is ready to receive messages. | 122 // is ready to receive messages. |
| 121 window.addEventListener('message', this.handleScriptingMessage.bind(this), | 123 window.addEventListener('message', this.handleScriptingMessage.bind(this), |
| 122 false); | 124 false); |
| 123 | 125 |
| 124 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); | 126 document.title = getFilenameFromURL(this.streamDetails_.originalUrl); |
| 125 if (this.isMaterial_) | 127 if (this.isMaterial_) |
| 126 $('title').textContent = document.title; | 128 this.materialToolbar_.filename = document.title; |
| 127 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); | 129 this.plugin_.setAttribute('src', this.streamDetails_.originalUrl); |
| 128 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); | 130 this.plugin_.setAttribute('stream-url', this.streamDetails_.streamUrl); |
| 129 var headers = ''; | 131 var headers = ''; |
| 130 for (var header in this.streamDetails_.responseHeaders) { | 132 for (var header in this.streamDetails_.responseHeaders) { |
| 131 headers += header + ': ' + | 133 headers += header + ': ' + |
| 132 this.streamDetails_.responseHeaders[header] + '\n'; | 134 this.streamDetails_.responseHeaders[header] + '\n'; |
| 133 } | 135 } |
| 134 this.plugin_.setAttribute('headers', headers); | 136 this.plugin_.setAttribute('headers', headers); |
| 135 | 137 |
| 136 if (!this.streamDetails_.embedded) | 138 if (!this.streamDetails_.embedded) |
| 137 this.plugin_.setAttribute('full-frame', ''); | 139 this.plugin_.setAttribute('full-frame', ''); |
| 138 document.body.appendChild(this.plugin_); | 140 document.body.appendChild(this.plugin_); |
| 139 | 141 |
| 140 this.pageIndicator_.addEventListener('changePage', function(e) { | 142 document.body.addEventListener('changePage', function(e) { |
| 141 this.viewport_.goToPage(e.detail.page); | 143 this.viewport_.goToPage(e.detail.page); |
| 142 }.bind(this)); | 144 }.bind(this)); |
| 143 | 145 |
| 144 if (this.isMaterial_) { | |
| 145 this.bookmarksPane.addEventListener('changePage', function(e) { | |
| 146 this.viewport_.goToPage(e.detail.page); | |
| 147 }.bind(this)); | |
| 148 } | |
| 149 | |
| 150 // Setup the button event listeners. | 146 // Setup the button event listeners. |
| 151 $('fit-to-width-button').addEventListener('click', | 147 $('fit-to-width-button').addEventListener('click', |
| 152 this.viewport_.fitToWidth.bind(this.viewport_)); | 148 this.viewport_.fitToWidth.bind(this.viewport_)); |
| 153 $('fit-to-page-button').addEventListener('click', | 149 $('fit-to-page-button').addEventListener('click', |
| 154 this.viewport_.fitToPage.bind(this.viewport_)); | 150 this.viewport_.fitToPage.bind(this.viewport_)); |
| 155 $('zoom-in-button').addEventListener('click', | 151 $('zoom-in-button').addEventListener('click', |
| 156 this.viewport_.zoomIn.bind(this.viewport_)); | 152 this.viewport_.zoomIn.bind(this.viewport_)); |
| 157 $('zoom-out-button').addEventListener('click', | 153 $('zoom-out-button').addEventListener('click', |
| 158 this.viewport_.zoomOut.bind(this.viewport_)); | 154 this.viewport_.zoomOut.bind(this.viewport_)); |
| 159 $('save-button').addEventListener('click', this.save_.bind(this)); | 155 |
| 160 $('print-button').addEventListener('click', this.print_.bind(this)); | |
| 161 if (this.isMaterial_) { | 156 if (this.isMaterial_) { |
| 162 $('bookmarks-button').addEventListener('click', function() { | 157 this.materialToolbar_.addEventListener('save', this.save_.bind(this)); |
| 163 this.bookmarksPane.toggle(); | 158 this.materialToolbar_.addEventListener('print', this.print_.bind(this)); |
| 159 this.materialToolbar_.addEventListener('toggle-bookmarks', function() { | |
| 160 this.bookmarksPane_.toggle(); | |
|
Sam McNally
2015/02/03 07:22:45
2 space indent.
Alexandre Carlton
2015/02/03 22:50:42
Done.
| |
| 164 }.bind(this)); | 161 }.bind(this)); |
| 162 } else { | |
| 163 $('save-button').addEventListener('click', this.save_.bind(this)); | |
| 164 $('print-button').addEventListener('click', this.print_.bind(this)); | |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Setup the keyboard event listener. | 167 // Setup the keyboard event listener. |
| 168 document.onkeydown = this.handleKeyEvent_.bind(this); | 168 document.onkeydown = this.handleKeyEvent_.bind(this); |
| 169 | 169 |
| 170 // Set up the zoom API. | 170 // Set up the zoom API. |
| 171 if (this.shouldManageZoom_()) { | 171 if (this.shouldManageZoom_()) { |
| 172 chrome.tabs.setZoomSettings(this.streamDetails_.tabId, | 172 chrome.tabs.setZoomSettings(this.streamDetails_.tabId, |
| 173 {mode: 'manual', scope: 'per-tab'}, | 173 {mode: 'manual', scope: 'per-tab'}, |
| 174 this.afterZoom_.bind(this)); | 174 this.afterZoom_.bind(this)); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 357 }, | 357 }, |
| 358 | 358 |
| 359 /** | 359 /** |
| 360 * @private | 360 * @private |
| 361 * Update the loading progress of the document in response to a progress | 361 * Update the loading progress of the document in response to a progress |
| 362 * message being received from the plugin. | 362 * message being received from the plugin. |
| 363 * @param {number} progress the progress as a percentage. | 363 * @param {number} progress the progress as a percentage. |
| 364 */ | 364 */ |
| 365 updateProgress_: function(progress) { | 365 updateProgress_: function(progress) { |
| 366 if (this.isMaterial_) | 366 if (this.isMaterial_) |
| 367 this.progressBar_.value = progress; | 367 this.materialToolbar_.progress = progress; |
| 368 else | 368 else |
| 369 this.progressBar_.progress = progress; | 369 this.progressBar_.progress = progress; |
| 370 | 370 |
| 371 if (progress == -1) { | 371 if (progress == -1) { |
| 372 // Document load failed. | 372 // Document load failed. |
| 373 this.errorScreen_.style.visibility = 'visible'; | 373 this.errorScreen_.style.visibility = 'visible'; |
| 374 this.sizer_.style.display = 'none'; | 374 this.sizer_.style.display = 'none'; |
| 375 this.toolbar_.style.visibility = 'hidden'; | 375 this.toolbar_.style.visibility = 'hidden'; |
| 376 if (this.passwordScreen_.active) { | 376 if (this.passwordScreen_.active) { |
| 377 this.passwordScreen_.deny(); | 377 this.passwordScreen_.deny(); |
| 378 this.passwordScreen_.active = false; | 378 this.passwordScreen_.active = false; |
| 379 } | 379 } |
| 380 } else if (progress == 100) { | 380 } else if (progress == 100) { |
| 381 // Document load complete. | 381 // Document load complete. |
| 382 if (this.lastViewportPosition_) | 382 if (this.lastViewportPosition_) |
| 383 this.viewport_.position = this.lastViewportPosition_; | 383 this.viewport_.position = this.lastViewportPosition_; |
| 384 if (this.isMaterial_) | 384 if (!this.isMaterial_) |
| 385 this.pageIndicator_.style.visibility = 'visible'; | 385 this.pageIndicator_.style.visibility = 'visible'; |
| 386 this.handleURLParams_(); | 386 this.handleURLParams_(); |
| 387 this.loaded_ = true; | 387 this.loaded_ = true; |
| 388 this.sendScriptingMessage_({ | 388 this.sendScriptingMessage_({ |
| 389 type: 'documentLoaded' | 389 type: 'documentLoaded' |
| 390 }); | 390 }); |
| 391 while (this.delayedScriptingMessages_.length > 0) | 391 while (this.delayedScriptingMessages_.length > 0) |
| 392 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); | 392 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); |
| 393 } | 393 } |
| 394 }, | 394 }, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 415 switch (message.data.type.toString()) { | 415 switch (message.data.type.toString()) { |
| 416 case 'documentDimensions': | 416 case 'documentDimensions': |
| 417 this.documentDimensions_ = message.data; | 417 this.documentDimensions_ = message.data; |
| 418 this.viewport_.setDocumentDimensions(this.documentDimensions_); | 418 this.viewport_.setDocumentDimensions(this.documentDimensions_); |
| 419 // If we received the document dimensions, the password was good so we | 419 // If we received the document dimensions, the password was good so we |
| 420 // can dismiss the password screen. | 420 // can dismiss the password screen. |
| 421 if (this.passwordScreen_.active) | 421 if (this.passwordScreen_.active) |
| 422 this.passwordScreen_.accept(); | 422 this.passwordScreen_.accept(); |
| 423 | 423 |
| 424 if (this.isMaterial_) { | 424 if (this.isMaterial_) { |
| 425 this.pageIndicator_.docLength = | 425 this.materialToolbar_.docLength = |
| 426 this.documentDimensions_.pageDimensions.length; | 426 this.documentDimensions_.pageDimensions.length; |
| 427 } else { | 427 } else { |
| 428 this.pageIndicator_.initialFadeIn(); | 428 this.pageIndicator_.initialFadeIn(); |
| 429 } | 429 } |
| 430 | 430 |
| 431 this.toolbar_.initialFadeIn(); | 431 this.toolbar_.initialFadeIn(); |
| 432 break; | 432 break; |
| 433 case 'email': | 433 case 'email': |
| 434 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc + | 434 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc + |
| 435 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject + | 435 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject + |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 469 case 'setScrollPosition': | 469 case 'setScrollPosition': |
| 470 var position = this.viewport_.position; | 470 var position = this.viewport_.position; |
| 471 if (message.data.x !== undefined) | 471 if (message.data.x !== undefined) |
| 472 position.x = message.data.x; | 472 position.x = message.data.x; |
| 473 if (message.data.y !== undefined) | 473 if (message.data.y !== undefined) |
| 474 position.y = message.data.y; | 474 position.y = message.data.y; |
| 475 this.viewport_.position = position; | 475 this.viewport_.position = position; |
| 476 break; | 476 break; |
| 477 case 'setTranslatedStrings': | 477 case 'setTranslatedStrings': |
| 478 this.passwordScreen_.text = message.data.getPasswordString; | 478 this.passwordScreen_.text = message.data.getPasswordString; |
| 479 this.progressBar_.text = message.data.loadingString; | 479 if (!this.isMaterial_) |
| 480 if (!this.isPrintPreview_) | 480 this.progressBar_.text = message.data.loadingString; |
| 481 if (!this.isPrintPreview_ && !this.isMaterial_) | |
|
Sam McNally
2015/02/03 07:22:45
Move this inside the if above.
Alexandre Carlton
2015/02/03 22:50:43
Done.
| |
| 481 this.progressBar_.style.visibility = 'visible'; | 482 this.progressBar_.style.visibility = 'visible'; |
| 482 this.errorScreen_.text = message.data.loadFailedString; | 483 this.errorScreen_.text = message.data.loadFailedString; |
| 483 break; | 484 break; |
| 484 case 'cancelStreamUrl': | 485 case 'cancelStreamUrl': |
| 485 chrome.mimeHandlerPrivate.abortStream(); | 486 chrome.mimeHandlerPrivate.abortStream(); |
| 486 break; | 487 break; |
| 487 case 'bookmarks': | 488 case 'bookmarks': |
| 488 this.bookmarks_ = message.data.bookmarks; | 489 this.bookmarks_ = message.data.bookmarks; |
| 489 if (this.isMaterial_) | 490 if (this.isMaterial_) |
| 490 this.bookmarksPane.bookmarks = message.data.bookmarks; | 491 this.bookmarksPane_.bookmarks = message.data.bookmarks; |
| 491 break; | 492 break; |
| 492 } | 493 } |
| 493 }, | 494 }, |
| 494 | 495 |
| 495 /** | 496 /** |
| 496 * @private | 497 * @private |
| 497 * A callback that's called before the zoom changes. Notify the plugin to stop | 498 * A callback that's called before the zoom changes. Notify the plugin to stop |
| 498 * reacting to scroll events while zoom is taking place to avoid flickering. | 499 * reacting to scroll events while zoom is taking place to avoid flickering. |
| 499 */ | 500 */ |
| 500 beforeZoom_: function() { | 501 beforeZoom_: function() { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 578 this.toolbar_.style.right = toolbarRight + 'px'; | 579 this.toolbar_.style.right = toolbarRight + 'px'; |
| 579 this.toolbar_.style.bottom = toolbarBottom + 'px'; | 580 this.toolbar_.style.bottom = toolbarBottom + 'px'; |
| 580 // Hide the toolbar if it doesn't fit in the viewport. | 581 // Hide the toolbar if it doesn't fit in the viewport. |
| 581 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0) | 582 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0) |
| 582 this.toolbar_.style.visibility = 'hidden'; | 583 this.toolbar_.style.visibility = 'hidden'; |
| 583 else | 584 else |
| 584 this.toolbar_.style.visibility = 'visible'; | 585 this.toolbar_.style.visibility = 'visible'; |
| 585 | 586 |
| 586 // Update the page indicator. | 587 // Update the page indicator. |
| 587 var visiblePage = this.viewport_.getMostVisiblePage(); | 588 var visiblePage = this.viewport_.getMostVisiblePage(); |
| 588 this.pageIndicator_.index = visiblePage; | 589 if (this.isMaterial_) |
| 590 this.materialToolbar_.index = visiblePage; | |
| 591 else | |
| 592 this.pageIndicator_.index = visiblePage; | |
| 593 | |
| 589 if (!this.isMaterial_) { | 594 if (!this.isMaterial_) { |
| 590 if (this.documentDimensions_.pageDimensions.length > 1 && | 595 if (this.documentDimensions_.pageDimensions.length > 1 && |
| 591 hasScrollbars.vertical) { | 596 hasScrollbars.vertical) { |
| 592 this.pageIndicator_.style.visibility = 'visible'; | 597 this.pageIndicator_.style.visibility = 'visible'; |
| 593 } else { | 598 } else { |
| 594 this.pageIndicator_.style.visibility = 'hidden'; | 599 this.pageIndicator_.style.visibility = 'hidden'; |
| 595 } | 600 } |
| 596 } | 601 } |
| 597 | 602 |
| 598 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage); | 603 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 670 this.lastViewportPosition_ = this.viewport_.position; | 675 this.lastViewportPosition_ = this.viewport_.position; |
| 671 | 676 |
| 672 // TODO(raymes): Disable these properly in the plugin. | 677 // TODO(raymes): Disable these properly in the plugin. |
| 673 var printButton = $('print-button'); | 678 var printButton = $('print-button'); |
| 674 if (printButton) | 679 if (printButton) |
| 675 printButton.parentNode.removeChild(printButton); | 680 printButton.parentNode.removeChild(printButton); |
| 676 var saveButton = $('save-button'); | 681 var saveButton = $('save-button'); |
| 677 if (saveButton) | 682 if (saveButton) |
| 678 saveButton.parentNode.removeChild(saveButton); | 683 saveButton.parentNode.removeChild(saveButton); |
| 679 | 684 |
| 680 this.pageIndicator_.pageLabels = message.data.pageNumbers; | 685 // TODO(alexandrec): Weren't we going to get rid of pageLabels? |
| 686 if (!this.isMaterial_) | |
| 687 this.pageIndicator_.pageLabels = message.data.pageNumbers; | |
|
Alexandre Carlton
2015/02/03 02:22:24
Just drawing attention to this; what should we do
Sam McNally
2015/02/03 07:22:45
We're keeping pageLabels, but we don't need to dea
Alexandre Carlton
2015/02/03 22:50:42
Acknowledged.
| |
| 681 | 688 |
| 682 this.plugin_.postMessage({ | 689 this.plugin_.postMessage({ |
| 683 type: 'resetPrintPreviewMode', | 690 type: 'resetPrintPreviewMode', |
| 684 url: message.data.url, | 691 url: message.data.url, |
| 685 grayscale: message.data.grayscale, | 692 grayscale: message.data.grayscale, |
| 686 // If the PDF isn't modifiable we send 0 as the page count so that no | 693 // If the PDF isn't modifiable we send 0 as the page count so that no |
| 687 // blank placeholder pages get appended to the PDF. | 694 // blank placeholder pages get appended to the PDF. |
| 688 pageCount: (message.data.modifiable ? | 695 pageCount: (message.data.modifiable ? |
| 689 message.data.pageNumbers.length : 0) | 696 message.data.pageNumbers.length : 0) |
| 690 }); | 697 }); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 733 * Each bookmark is an Object containing a: | 740 * Each bookmark is an Object containing a: |
| 734 * - title | 741 * - title |
| 735 * - page (optional) | 742 * - page (optional) |
| 736 * - array of children (themselves bookmarks) | 743 * - array of children (themselves bookmarks) |
| 737 * @type {Array} the top-level bookmarks of the PDF. | 744 * @type {Array} the top-level bookmarks of the PDF. |
| 738 */ | 745 */ |
| 739 get bookmarks() { | 746 get bookmarks() { |
| 740 return this.bookmarks_; | 747 return this.bookmarks_; |
| 741 } | 748 } |
| 742 }; | 749 }; |
| OLD | NEW |