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

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

Issue 6982030: Print Preview: Detecting plugin existence before generating the preview. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removing unnecessary else if statement Created 9 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/resources/print_preview.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 var localStrings = new LocalStrings(); 5 var localStrings = new LocalStrings();
6 6
7 // Whether or not the PDF plugin supports all the capabilities needed for
8 // print preview.
9 var hasCompatiblePDFPlugin = true;
10
11 // The total page count of the previewed document regardless of which pages the 7 // The total page count of the previewed document regardless of which pages the
12 // user has selected. 8 // user has selected.
13 var totalPageCount = -1; 9 var totalPageCount = -1;
14 10
15 // The previously selected pages by the user. It is used in 11 // The previously selected pages by the user. It is used in
16 // onPageSelectionMayHaveChanged() to make sure that a new preview is not 12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not
17 // requested more often than necessary. 13 // requested more often than necessary.
18 var previouslySelectedPages = []; 14 var previouslySelectedPages = [];
19 15
20 // The previously selected layout mode. It is used in order to prevent the 16 // The previously selected layout mode. It is used in order to prevent the
(...skipping 20 matching lines...) Expand all
41 37
42 // Destination list special value constants. 38 // Destination list special value constants.
43 const PRINT_TO_PDF = 'Print To PDF'; 39 const PRINT_TO_PDF = 'Print To PDF';
44 const MANAGE_PRINTERS = 'Manage Printers'; 40 const MANAGE_PRINTERS = 'Manage Printers';
45 41
46 /** 42 /**
47 * Window onload handler, sets up the page and starts print preview by getting 43 * Window onload handler, sets up the page and starts print preview by getting
48 * the printer list. 44 * the printer list.
49 */ 45 */
50 function onLoad() { 46 function onLoad() {
47 $('system-dialog-link').addEventListener('click', showSystemDialog);
48 $('cancel-button').addEventListener('click', handleCancelButtonClick);
49
50 if (!checkCompatiblePluginExists()) {
51 displayErrorMessage(localStrings.getString('noPlugin'));
52 $('mainview').parentElement.removeChild($('dummy-viewer'));
53 return;
54 }
55 $('mainview').parentElement.removeChild($('dummy-viewer'));
56
51 $('printer-list').disabled = true; 57 $('printer-list').disabled = true;
52 $('print-button').disabled = true; 58 $('print-button').disabled = true;
53 $('print-button').addEventListener('click', printFile); 59 $('print-button').addEventListener('click', printFile);
54 $('cancel-button').addEventListener('click', handleCancelButtonClick);
55 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged); 60 $('all-pages').addEventListener('click', onPageSelectionMayHaveChanged);
56 $('copies').addEventListener('input', copiesFieldChanged); 61 $('copies').addEventListener('input', copiesFieldChanged);
57 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox); 62 $('print-pages').addEventListener('click', handleIndividualPagesCheckbox);
58 $('individual-pages').addEventListener('blur', function() { 63 $('individual-pages').addEventListener('blur', function() {
59 clearTimeout(timerId); 64 clearTimeout(timerId);
60 onPageSelectionMayHaveChanged(); 65 onPageSelectionMayHaveChanged();
61 }); 66 });
62 $('individual-pages').addEventListener('focus', addTimerToPageRangeField); 67 $('individual-pages').addEventListener('focus', addTimerToPageRangeField);
63 $('individual-pages').addEventListener('input', pageRangesFieldChanged); 68 $('individual-pages').addEventListener('input', pageRangesFieldChanged);
64 $('two-sided').addEventListener('click', handleTwoSidedClick) 69 $('two-sided').addEventListener('click', handleTwoSidedClick)
65 $('landscape').addEventListener('click', onLayoutModeToggle); 70 $('landscape').addEventListener('click', onLayoutModeToggle);
66 $('portrait').addEventListener('click', onLayoutModeToggle); 71 $('portrait').addEventListener('click', onLayoutModeToggle);
67 $('color').addEventListener('click', function() { setColor(true); }); 72 $('color').addEventListener('click', function() { setColor(true); });
68 $('bw').addEventListener('click', function() { setColor(false); }); 73 $('bw').addEventListener('click', function() { setColor(false); });
69 $('printer-list').addEventListener( 74 $('printer-list').addEventListener(
70 'change', updateControlsWithSelectedPrinterCapabilities); 75 'change', updateControlsWithSelectedPrinterCapabilities);
71 $('system-dialog-link').addEventListener('click', showSystemDialog);
72 $('increment').addEventListener('click', 76 $('increment').addEventListener('click',
73 function() { onCopiesButtonsClicked(1); }); 77 function() { onCopiesButtonsClicked(1); });
74 $('decrement').addEventListener('click', 78 $('decrement').addEventListener('click',
75 function() { onCopiesButtonsClicked(-1); }); 79 function() { onCopiesButtonsClicked(-1); });
76 $('controls').onsubmit = function() { return false; }; 80 $('controls').onsubmit = function() { return false; };
77 chrome.send('getPrinters'); 81 chrome.send('getPrinters');
78 } 82 }
79 83
80 /** 84 /**
81 * Asks the browser to close the preview tab. 85 * Asks the browser to close the preview tab.
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 function printFile() { 320 function printFile() {
317 chrome.send('print', [getSettingsJSON()]); 321 chrome.send('print', [getSettingsJSON()]);
318 } 322 }
319 323
320 /** 324 /**
321 * Asks the browser to generate a preview PDF based on current print settings. 325 * Asks the browser to generate a preview PDF based on current print settings.
322 */ 326 */
323 function requestPrintPreview() { 327 function requestPrintPreview() {
324 isPreviewStillLoading = true; 328 isPreviewStillLoading = true;
325 setControlsDisabled(true); 329 setControlsDisabled(true);
326 $('dancing-dots').classList.remove('hidden');
327 $('dancing-dots').classList.remove('invisible'); 330 $('dancing-dots').classList.remove('invisible');
328 chrome.send('getPreview', [getSettingsJSON()]); 331 chrome.send('getPreview', [getSettingsJSON()]);
329 } 332 }
330 333
331 /** 334 /**
332 * Fill the printer list drop down. 335 * Fill the printer list drop down.
333 * Called from PrintPreviewHandler::SendPrinterList(). 336 * Called from PrintPreviewHandler::SendPrinterList().
334 * @param {Array} printers Array of printer info objects. 337 * @param {Array} printers Array of printer info objects.
335 * @param {number} defaultPrinterIndex The index of the default printer. 338 * @param {number} defaultPrinterIndex The index of the default printer.
336 */ 339 */
(...skipping 30 matching lines...) Expand all
367 if (is_default) 370 if (is_default)
368 option.selected = true; 371 option.selected = true;
369 } 372 }
370 373
371 /** 374 /**
372 * Sets the color mode for the PDF plugin. 375 * Sets the color mode for the PDF plugin.
373 * Called from PrintPreviewHandler::ProcessColorSetting(). 376 * Called from PrintPreviewHandler::ProcessColorSetting().
374 * @param {boolean} color is true if the PDF plugin should display in color. 377 * @param {boolean} color is true if the PDF plugin should display in color.
375 */ 378 */
376 function setColor(color) { 379 function setColor(color) {
377 if (!hasCompatiblePDFPlugin) {
378 return;
379 }
380 var pdfViewer = $('pdf-viewer'); 380 var pdfViewer = $('pdf-viewer');
381 if (!pdfViewer) { 381 if (!pdfViewer) {
382 return; 382 return;
383 } 383 }
384 pdfViewer.grayscale(!color); 384 pdfViewer.grayscale(!color);
385 } 385 }
386 386
387 /** 387 /**
388 * Display an error message in the center of the preview area. 388 * Display an error message in the center of the preview area.
389 * @param (string) errorMessage The error message to be displayed. 389 * @param (string) errorMessage The error message to be displayed.
390 */ 390 */
391 function displayErrorMessage(errorMessage) { 391 function displayErrorMessage(errorMessage) {
392 isPreviewStillLoading = false; 392 isPreviewStillLoading = false;
393 $('dancing-dots').classList.remove('invisible');
393 $('dancing-dots-text').classList.add('hidden'); 394 $('dancing-dots-text').classList.add('hidden');
394 $('error-text').innerHTML = errorMessage; 395 $('error-text').innerHTML = errorMessage;
395 $('error-text').classList.remove('hidden'); 396 $('error-text').classList.remove('hidden');
396 setControlsDisabled(true); 397 setControlsDisabled(true);
397 398
398 var pdfViewer = $('pdf-viewer'); 399 var pdfViewer = $('pdf-viewer');
399 if (pdfViewer) 400 if (pdfViewer)
400 $('mainview').removeChild(pdfViewer); 401 $('mainview').removeChild(pdfViewer);
401 } 402 }
402 403
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 456
456 createPDFPlugin(); 457 createPDFPlugin();
457 isPreviewStillLoading = false; 458 isPreviewStillLoading = false;
458 updatePrintSummary(); 459 updatePrintSummary();
459 } 460 }
460 461
461 /** 462 /**
462 * Create the PDF plugin or reload the existing one. 463 * Create the PDF plugin or reload the existing one.
463 */ 464 */
464 function createPDFPlugin() { 465 function createPDFPlugin() {
465 if (!hasCompatiblePDFPlugin) {
466 return;
467 }
468
469 // Enable the print button. 466 // Enable the print button.
470 if (!$('printer-list').disabled) { 467 if (!$('printer-list').disabled) {
471 $('print-button').disabled = false; 468 $('print-button').disabled = false;
472 } 469 }
473 470
474 var pdfViewer = $('pdf-viewer'); 471 var pdfViewer = $('pdf-viewer');
475 if (pdfViewer) { 472 if (pdfViewer) {
476 // Older version of the PDF plugin may not have this method. 473 // Older version of the PDF plugin may not have this method.
477 // TODO(thestig) Eventually remove this check. 474 // TODO(thestig) Eventually remove this check.
478 if (pdfViewer.goToPage) { 475 if (pdfViewer.goToPage) {
479 // Need to call this before the reload(), where the plugin resets its 476 // Need to call this before the reload(), where the plugin resets its
480 // internal page count. 477 // internal page count.
481 pdfViewer.goToPage('0'); 478 pdfViewer.goToPage('0');
482 } 479 }
483 pdfViewer.reload(); 480 pdfViewer.reload();
484 pdfViewer.grayscale(!isColor()); 481 pdfViewer.grayscale(!isColor());
485 return; 482 return;
486 } 483 }
487 484
488 var pdfPlugin = document.createElement('embed'); 485 var pdfPlugin = document.createElement('embed');
489 pdfPlugin.setAttribute('id', 'pdf-viewer'); 486 pdfPlugin.setAttribute('id', 'pdf-viewer');
490 pdfPlugin.setAttribute('type', 'application/pdf'); 487 pdfPlugin.setAttribute('type', 'application/pdf');
491 pdfPlugin.setAttribute('src', 'chrome://print/print.pdf'); 488 pdfPlugin.setAttribute('src', 'chrome://print/print.pdf');
492 var mainView = $('mainview'); 489 var mainView = $('mainview');
493 mainView.appendChild(pdfPlugin); 490 mainView.appendChild(pdfPlugin);
494
495 // Check to see if the PDF plugin is our PDF plugin. (or compatible)
496 if (!pdfPlugin.onload) {
497 hasCompatiblePDFPlugin = false;
498 displayErrorMessage(localStrings.getString('noPlugin'));
499 return;
500 }
501 pdfPlugin.onload('onPDFLoad()'); 491 pdfPlugin.onload('onPDFLoad()');
502 492
503 // Older version of the PDF plugin may not have this method. 493 // Older version of the PDF plugin may not have this method.
504 // TODO(thestig) Eventually remove this check. 494 // TODO(thestig) Eventually remove this check.
505 if (pdfPlugin.removePrintButton) { 495 if (pdfPlugin.removePrintButton) {
506 pdfPlugin.removePrintButton(); 496 pdfPlugin.removePrintButton();
507 } 497 }
508 498
509 pdfPlugin.grayscale(true); 499 pdfPlugin.grayscale(true);
510 } 500 }
511 501
512 /** 502 /**
503 * Returns true if a compatible pdf plugin exists, false if it doesn't.
504 */
505 function checkCompatiblePluginExists() {
506 var dummyPlugin = $('dummy-viewer')
507 return !!dummyPlugin.onload;
508 }
509
510 /**
513 * Updates the state of print button depending on the user selection. 511 * Updates the state of print button depending on the user selection.
514 * The button is enabled only when the following conditions are true. 512 * The button is enabled only when the following conditions are true.
515 * 1) The selected page ranges are valid. 513 * 1) The selected page ranges are valid.
516 * 2) The number of copies is valid. 514 * 2) The number of copies is valid.
517 */ 515 */
518 function updatePrintButtonState() { 516 function updatePrintButtonState() {
519 $('print-button').disabled = (!isNumberOfCopiesValid() || 517 $('print-button').disabled = (!isNumberOfCopiesValid() ||
520 getSelectedPagesValidityLevel() != 1); 518 getSelectedPagesValidityLevel() != 1);
521 } 519 }
522 520
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 /** 886 /**
889 * Executed when the 'increment' or 'decrement' button is clicked. 887 * Executed when the 'increment' or 'decrement' button is clicked.
890 */ 888 */
891 function onCopiesButtonsClicked(sign) { 889 function onCopiesButtonsClicked(sign) {
892 if($('copies').value == 1 && (sign == -1)) 890 if($('copies').value == 1 && (sign == -1))
893 return; 891 return;
894 $('copies').value = getCopies() + sign * 1; 892 $('copies').value = getCopies() + sign * 1;
895 copiesFieldChanged(); 893 copiesFieldChanged();
896 } 894 }
897 895
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698