Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: chrome/browser/resources/pdf/pdf.js

Issue 898673003: Refactor Material Design PDF Viewer toolbar into a single element (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve variable naming and documentation Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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) {
raymes 2015/02/04 00:54:25 nit: should we just attach the event listener to t
Alexandre Carlton 2015/02/04 04:28:47 The viewer-bookmarks-pane also fires 'change-page'
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() {
raymes 2015/02/04 00:54:25 We should be consistent with how we name events (w
Alexandre Carlton 2015/02/04 04:28:47 Done.
160 this.bookmarksPane_.toggle();
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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 }, 366 },
367 367
368 /** 368 /**
369 * @private 369 * @private
370 * Update the loading progress of the document in response to a progress 370 * Update the loading progress of the document in response to a progress
371 * message being received from the plugin. 371 * message being received from the plugin.
372 * @param {number} progress the progress as a percentage. 372 * @param {number} progress the progress as a percentage.
373 */ 373 */
374 updateProgress_: function(progress) { 374 updateProgress_: function(progress) {
375 if (this.isMaterial_) 375 if (this.isMaterial_)
376 this.progressBar_.value = progress; 376 this.materialToolbar_.loadProgress = progress;
377 else 377 else
378 this.progressBar_.progress = progress; 378 this.progressBar_.progress = progress;
379 379
380 if (progress == -1) { 380 if (progress == -1) {
381 // Document load failed. 381 // Document load failed.
382 this.errorScreen_.style.visibility = 'visible'; 382 this.errorScreen_.style.visibility = 'visible';
383 this.sizer_.style.display = 'none'; 383 this.sizer_.style.display = 'none';
384 this.toolbar_.style.visibility = 'hidden'; 384 this.toolbar_.style.visibility = 'hidden';
385 if (this.passwordScreen_.active) { 385 if (this.passwordScreen_.active) {
386 this.passwordScreen_.deny(); 386 this.passwordScreen_.deny();
387 this.passwordScreen_.active = false; 387 this.passwordScreen_.active = false;
388 } 388 }
389 } else if (progress == 100) { 389 } else if (progress == 100) {
390 // Document load complete. 390 // Document load complete.
391 if (this.lastViewportPosition_) 391 if (this.lastViewportPosition_)
392 this.viewport_.position = this.lastViewportPosition_; 392 this.viewport_.position = this.lastViewportPosition_;
393 if (this.isMaterial_) 393 if (!this.isMaterial_)
394 this.pageIndicator_.style.visibility = 'visible'; 394 this.pageIndicator_.style.visibility = 'visible';
395 this.handleURLParams_(); 395 this.handleURLParams_();
396 this.loaded_ = true; 396 this.loaded_ = true;
397 this.sendScriptingMessage_({ 397 this.sendScriptingMessage_({
398 type: 'documentLoaded' 398 type: 'documentLoaded'
399 }); 399 });
400 while (this.delayedScriptingMessages_.length > 0) 400 while (this.delayedScriptingMessages_.length > 0)
401 this.handleScriptingMessage(this.delayedScriptingMessages_.shift()); 401 this.handleScriptingMessage(this.delayedScriptingMessages_.shift());
402 } 402 }
403 }, 403 },
(...skipping 20 matching lines...) Expand all
424 switch (message.data.type.toString()) { 424 switch (message.data.type.toString()) {
425 case 'documentDimensions': 425 case 'documentDimensions':
426 this.documentDimensions_ = message.data; 426 this.documentDimensions_ = message.data;
427 this.viewport_.setDocumentDimensions(this.documentDimensions_); 427 this.viewport_.setDocumentDimensions(this.documentDimensions_);
428 // If we received the document dimensions, the password was good so we 428 // If we received the document dimensions, the password was good so we
429 // can dismiss the password screen. 429 // can dismiss the password screen.
430 if (this.passwordScreen_.active) 430 if (this.passwordScreen_.active)
431 this.passwordScreen_.accept(); 431 this.passwordScreen_.accept();
432 432
433 if (this.isMaterial_) { 433 if (this.isMaterial_) {
434 this.pageIndicator_.docLength = 434 this.materialToolbar_.docLength =
435 this.documentDimensions_.pageDimensions.length; 435 this.documentDimensions_.pageDimensions.length;
436 } else { 436 } else {
437 this.pageIndicator_.initialFadeIn(); 437 this.pageIndicator_.initialFadeIn();
438 } 438 }
439 439
440 this.toolbar_.initialFadeIn(); 440 this.toolbar_.initialFadeIn();
441 break; 441 break;
442 case 'email': 442 case 'email':
443 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc + 443 var href = 'mailto:' + message.data.to + '?cc=' + message.data.cc +
444 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject + 444 '&bcc=' + message.data.bcc + '&subject=' + message.data.subject +
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 case 'setScrollPosition': 478 case 'setScrollPosition':
479 var position = this.viewport_.position; 479 var position = this.viewport_.position;
480 if (message.data.x !== undefined) 480 if (message.data.x !== undefined)
481 position.x = message.data.x; 481 position.x = message.data.x;
482 if (message.data.y !== undefined) 482 if (message.data.y !== undefined)
483 position.y = message.data.y; 483 position.y = message.data.y;
484 this.viewport_.position = position; 484 this.viewport_.position = position;
485 break; 485 break;
486 case 'setTranslatedStrings': 486 case 'setTranslatedStrings':
487 this.passwordScreen_.text = message.data.getPasswordString; 487 this.passwordScreen_.text = message.data.getPasswordString;
488 this.progressBar_.text = message.data.loadingString; 488 if (!this.isMaterial_) {
489 if (!this.isPrintPreview_) 489 this.progressBar_.text = message.data.loadingString;
490 this.progressBar_.style.visibility = 'visible'; 490 if (!this.isPrintPreview_)
491 this.progressBar_.style.visibility = 'visible';
492 }
491 this.errorScreen_.text = message.data.loadFailedString; 493 this.errorScreen_.text = message.data.loadFailedString;
492 break; 494 break;
493 case 'cancelStreamUrl': 495 case 'cancelStreamUrl':
494 chrome.mimeHandlerPrivate.abortStream(); 496 chrome.mimeHandlerPrivate.abortStream();
495 break; 497 break;
496 case 'bookmarks': 498 case 'bookmarks':
497 this.bookmarks_ = message.data.bookmarks; 499 this.bookmarks_ = message.data.bookmarks;
498 if (this.isMaterial_) 500 if (this.isMaterial_)
499 this.bookmarksPane.bookmarks = message.data.bookmarks; 501 this.bookmarksPane_.bookmarks = message.data.bookmarks;
500 break; 502 break;
501 } 503 }
502 }, 504 },
503 505
504 /** 506 /**
505 * @private 507 * @private
506 * A callback that's called before the zoom changes. Notify the plugin to stop 508 * A callback that's called before the zoom changes. Notify the plugin to stop
507 * reacting to scroll events while zoom is taking place to avoid flickering. 509 * reacting to scroll events while zoom is taking place to avoid flickering.
508 */ 510 */
509 beforeZoom_: function() { 511 beforeZoom_: function() {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 this.toolbar_.style.right = toolbarRight + 'px'; 589 this.toolbar_.style.right = toolbarRight + 'px';
588 this.toolbar_.style.bottom = toolbarBottom + 'px'; 590 this.toolbar_.style.bottom = toolbarBottom + 'px';
589 // Hide the toolbar if it doesn't fit in the viewport. 591 // Hide the toolbar if it doesn't fit in the viewport.
590 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0) 592 if (this.toolbar_.offsetLeft < 0 || this.toolbar_.offsetTop < 0)
591 this.toolbar_.style.visibility = 'hidden'; 593 this.toolbar_.style.visibility = 'hidden';
592 else 594 else
593 this.toolbar_.style.visibility = 'visible'; 595 this.toolbar_.style.visibility = 'visible';
594 596
595 // Update the page indicator. 597 // Update the page indicator.
596 var visiblePage = this.viewport_.getMostVisiblePage(); 598 var visiblePage = this.viewport_.getMostVisiblePage();
597 this.pageIndicator_.index = visiblePage; 599 if (this.isMaterial_)
600 this.materialToolbar_.pageIndex = visiblePage;
601 else
602 this.pageIndicator_.index = visiblePage;
603
598 if (!this.isMaterial_) { 604 if (!this.isMaterial_) {
599 if (this.documentDimensions_.pageDimensions.length > 1 && 605 if (this.documentDimensions_.pageDimensions.length > 1 &&
600 hasScrollbars.vertical) { 606 hasScrollbars.vertical) {
601 this.pageIndicator_.style.visibility = 'visible'; 607 this.pageIndicator_.style.visibility = 'visible';
602 } else { 608 } else {
603 this.pageIndicator_.style.visibility = 'hidden'; 609 this.pageIndicator_.style.visibility = 'hidden';
604 } 610 }
605 } 611 }
606 612
607 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage); 613 var visiblePageDimensions = this.viewport_.getPageScreenRect(visiblePage);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 this.lastViewportPosition_ = this.viewport_.position; 685 this.lastViewportPosition_ = this.viewport_.position;
680 686
681 // TODO(raymes): Disable these properly in the plugin. 687 // TODO(raymes): Disable these properly in the plugin.
682 var printButton = $('print-button'); 688 var printButton = $('print-button');
683 if (printButton) 689 if (printButton)
684 printButton.parentNode.removeChild(printButton); 690 printButton.parentNode.removeChild(printButton);
685 var saveButton = $('save-button'); 691 var saveButton = $('save-button');
686 if (saveButton) 692 if (saveButton)
687 saveButton.parentNode.removeChild(saveButton); 693 saveButton.parentNode.removeChild(saveButton);
688 694
689 this.pageIndicator_.pageLabels = message.data.pageNumbers; 695 if (!this.isMaterial_)
696 this.pageIndicator_.pageLabels = message.data.pageNumbers;
690 697
691 this.plugin_.postMessage({ 698 this.plugin_.postMessage({
692 type: 'resetPrintPreviewMode', 699 type: 'resetPrintPreviewMode',
693 url: message.data.url, 700 url: message.data.url,
694 grayscale: message.data.grayscale, 701 grayscale: message.data.grayscale,
695 // If the PDF isn't modifiable we send 0 as the page count so that no 702 // If the PDF isn't modifiable we send 0 as the page count so that no
696 // blank placeholder pages get appended to the PDF. 703 // blank placeholder pages get appended to the PDF.
697 pageCount: (message.data.modifiable ? 704 pageCount: (message.data.modifiable ?
698 message.data.pageNumbers.length : 0) 705 message.data.pageNumbers.length : 0)
699 }); 706 });
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
742 * Each bookmark is an Object containing a: 749 * Each bookmark is an Object containing a:
743 * - title 750 * - title
744 * - page (optional) 751 * - page (optional)
745 * - array of children (themselves bookmarks) 752 * - array of children (themselves bookmarks)
746 * @type {Array} the top-level bookmarks of the PDF. 753 * @type {Array} the top-level bookmarks of the PDF.
747 */ 754 */
748 get bookmarks() { 755 get bookmarks() {
749 return this.bookmarks_; 756 return this.bookmarks_;
750 } 757 }
751 }; 758 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698