diff options
author | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-17 22:39:14 +0000 |
---|---|---|
committer | dpapad@chromium.org <dpapad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-17 22:39:14 +0000 |
commit | 3a2630d61cbff2fffff994b9f9bcdb7f9fcc1893 (patch) | |
tree | 32df61f6bad01c2692fc4c54fcfe08765a7b1d9b /chrome/browser/resources/print_preview | |
parent | 1979453de9770526bf4b6f8b7adab57f5e4b2de6 (diff) | |
download | chromium_src-3a2630d61cbff2fffff994b9f9bcdb7f9fcc1893.zip chromium_src-3a2630d61cbff2fffff994b9f9bcdb7f9fcc1893.tar.gz chromium_src-3a2630d61cbff2fffff994b9f9bcdb7f9fcc1893.tar.bz2 |
Print Preview: Cleaning up pdf plugin initialization code
BUG=NONE
TEST=NONE
Review URL: http://codereview.chromium.org/9133010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117985 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/resources/print_preview')
-rw-r--r-- | chrome/browser/resources/print_preview/preview_area.js | 50 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview/print_preview.js | 52 |
2 files changed, 58 insertions, 44 deletions
diff --git a/chrome/browser/resources/print_preview/preview_area.js b/chrome/browser/resources/print_preview/preview_area.js index 08bd567..26a4444 100644 --- a/chrome/browser/resources/print_preview/preview_area.js +++ b/chrome/browser/resources/print_preview/preview_area.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -104,6 +104,54 @@ cr.define('print_preview', function() { }, /** + * Initializes the PDF plugin and places it on the page. + * @param {!string} srcURL The URL of the document to be loaded in the + * plugin. + * @private + */ + createPDFPlugin_: function(srcURL) { + this.pdfPlugin_ = document.createElement('embed'); + this.pdfPlugin_.setAttribute('id', 'pdf-viewer'); + this.pdfPlugin_.setAttribute( + 'type', 'application/x-google-chrome-print-preview-pdf'); + this.pdfPlugin_.setAttribute('src', srcURL); + this.pdfPlugin_.setAttribute('aria-live', 'polite'); + this.pdfPlugin_.setAttribute('aria-atomic', 'true'); + $('mainview').appendChild(this.pdfPlugin_); + + this.pdfPlugin_.onload('onPDFLoad()'); + this.pdfPlugin_.onScroll('onPreviewPositionChanged()'); + this.pdfPlugin_.onPluginSizeChanged('onPreviewPositionChanged()'); + this.pdfPlugin_.removePrintButton(); + this.pdfPlugin_.grayscale(true); + }, + + /** + * Reloads the plugin with a new url. + * @param {string} srcURL The URL to load in the plugin. + * @private + */ + reloadPDFPlugin_: function(srcURL) { + // Need to call this before the reload(), where the plugin resets its + // internal page count. + this.pdfPlugin_.goToPage('0'); + this.pdfPlugin_.resetPrintPreviewUrl(srcURL); + this.pdfPlugin_.reload(); + this.pdfPlugin_.grayscale( + colorSettings.colorMode == print_preview.ColorSettings.GRAY); + }, + + /** + * Creates the PDF plugin or reloads the existing one. + * @param {number} srcDataIndex Preview data source index. + */ + createOrReloadPDFPlugin: function(srcDataIndex) { + var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); + this.pdfPlugin_ ? this.reloadPDFPlugin_(srcURL) : + this.createPDFPlugin_(srcURL); + }, + + /** * Queries the plugin for the location of the most visible page and updates * |this.pageLocationNormalized|. */ diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js index e45aa60..baa6658 100644 --- a/chrome/browser/resources/print_preview/print_preview.js +++ b/chrome/browser/resources/print_preview/print_preview.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -774,11 +774,10 @@ function addDestinationListOptionAtPosition(position, * @param {boolean} color is true if the PDF plugin should display in color. */ function setColor(color) { - var pdfViewer = $('pdf-viewer'); - if (!pdfViewer) { + if (!previewArea.pdfPlugin) return; - } - pdfViewer.grayscale(!color); + + previewArea.pdfPlugin.grayscale(!color); var printerList = $('printer-list'); cloudprint.setColor(printerList[printerList.selectedIndex], color); } @@ -829,7 +828,7 @@ function onPDFLoad() { } function setPluginPreviewPageCount() { - $('pdf-viewer').printPreviewPageCount( + previewArea.pdfPlugin.printPreviewPageCount( pageSettings.previouslySelectedPages.length); } @@ -897,7 +896,7 @@ function reloadPreviewPages(previewUid, previewResponseId) { checkAndHideOverlayLayerIfValid(); var pageSet = pageSettings.previouslySelectedPages; for (var i = 0; i < pageSet.length; i++) { - $('pdf-viewer').loadPreviewPage( + previewArea.pdfPlugin.loadPreviewPage( getPageSrcURL(previewUid, pageSet[i] - 1), i); } @@ -933,9 +932,9 @@ function onDidPreviewPage(pageNumber, previewUid, previewResponseId) { currentPreviewUid = previewUid; if (pageIndex == 0) - createPDFPlugin(pageNumber); + previewArea.createOrReloadPDFPlugin(pageNumber); - $('pdf-viewer').loadPreviewPage( + previewArea.pdfPlugin.loadPreviewPage( getPageSrcURL(previewUid, pageNumber), pageIndex); if (pageIndex + 1 == pageSettings.previouslySelectedPages.length) { @@ -962,7 +961,7 @@ function updatePrintPreview(previewUid, previewResponseId) { // If the preview is not modifiable the plugin has not been created yet. currentPreviewUid = previewUid; hasPendingPreviewRequest = false; - createPDFPlugin(PRINT_READY_DATA_INDEX); + previewArea.createOrReloadPDFPlugin(PRINT_READY_DATA_INDEX); } cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); @@ -1016,39 +1015,6 @@ function hasOnlyPageSettingsChanged() { } /** - * Create the PDF plugin or reload the existing one. - * @param {number} srcDataIndex Preview data source index. - */ -function createPDFPlugin(srcDataIndex) { - var pdfViewer = $('pdf-viewer'); - var srcURL = getPageSrcURL(currentPreviewUid, srcDataIndex); - if (pdfViewer) { - // Need to call this before the reload(), where the plugin resets its - // internal page count. - pdfViewer.goToPage('0'); - pdfViewer.resetPrintPreviewUrl(srcURL); - pdfViewer.reload(); - pdfViewer.grayscale( - colorSettings.colorMode == print_preview.ColorSettings.GRAY); - return; - } - - pdfViewer = document.createElement('embed'); - pdfViewer.setAttribute('id', 'pdf-viewer'); - pdfViewer.setAttribute('type', - 'application/x-google-chrome-print-preview-pdf'); - pdfViewer.setAttribute('src', srcURL); - pdfViewer.setAttribute('aria-live', 'polite'); - pdfViewer.setAttribute('aria-atomic', 'true'); - $('mainview').appendChild(pdfViewer); - pdfViewer.onload('onPDFLoad()'); - pdfViewer.onScroll('onPreviewPositionChanged()'); - pdfViewer.onPluginSizeChanged('onPreviewPositionChanged()'); - pdfViewer.removePrintButton(); - pdfViewer.grayscale(true); -} - -/** * Called either when there is a scroll event or when the plugin size changes. */ function onPreviewPositionChanged() { |