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

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

Issue 6533006: Print Preview: Hook up the print button to initiate printing without displaying a print dialog. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Addressed review comments. Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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 var hasPDFPlugin = true; 6 var hasPDFPlugin = true;
7 var expectedPageCount = 0; 7 var expectedPageCount = 0;
8 8
9 /** 9 /**
10 * Window onload handler, sets up the page. 10 * Window onload handler, sets up the page.
11 */ 11 */
12 function load() { 12 function load() {
13 $('print-button').addEventListener('click', function(e) { 13 $('print-button').addEventListener('click', printFile);
14 chrome.send('print'); 14
15 });
16 $('cancel-button').addEventListener('click', function(e) { 15 $('cancel-button').addEventListener('click', function(e) {
17 window.close(); 16 window.close();
18 }); 17 });
19 18
20 chrome.send('getPrinters'); 19 chrome.send('getPrinters');
21 }; 20 };
22 21
23 /** 22 /**
23 * Page range text validation.
24 * Returns true if |printFromText| and |printToText| are valid page numbers.
25 * TODO (kmadhusu): Get the expected page count and validate the page range
26 * with total number of pages.
27 */
28 function isValidPageRange(printFromText, printToText) {
29 var numericExp = /^[0-9]+$/;
30 if (numericExp.test(printFromText) && numericExp.test(printToText)) {
31 var printFrom = Number(printFromText);
32 var printTo = Number(printToText);
33 if (printFrom <= printTo && printFrom != 0 && printTo != 0)
34 return true;
35 }
36 return false;
37 }
38
39 /**
40 * Parse page range text.
41 * Eg: If page range is specified as '1-3,7-9,8'. Create an array with three
42 * elements. Each array element contains the range information.
43 * [{from:1, to:3}, {from:7, to:9}, {from:8, to:8}]
44 * TODO (kmadhusu): Handle invalid characters.
45 */
46 function getPageRanges() {
47 var pageRangesInfo = [];
48 var pageRangeText = $('pages').value;
49 var pageRangeList = pageRangeText.replace(/\s/g, '').split(',');
50 for (var i = 0; i < pageRangeList.length; i++) {
51 var tempRange = pageRangeList[i].split('-');
52 var printFrom;
53 var printTo;
54 if (tempRange.length > 1) {
55 printFrom = tempRange[0];
arv (Not doing code reviews) 2011/02/24 23:21:03 This one could be moved out of the if since it is
kmadhusu 2011/03/01 01:55:50 Done.
56 printTo = tempRange[1];
57 } else {
58 printFrom = tempRange[0];
59 printTo = tempRange[0];
60 }
61 // Validate the page range information.
62 if (isValidPageRange(printFrom, printTo)) {
63 pageRangesInfo.push({'from': parseInt(printFrom, 10),
64 'to': parseInt(printTo, 10)});
65 }
66 }
67 return pageRangesInfo;
68 }
69
70 function printFile() {
71 var selectedPrinter = $('printer-list').selectedIndex;
72 var printerName = $('printer-list').options[selectedPrinter].textContent;
73 var pageRanges = getPageRanges();
74 var printAll = $('all-pages').checked;
75 var twoSided = $('two-sided').checked;
76 var copies = $('copies').value;
77 var collate = $('collate').checked;
78 var layout = $('layout').options[$('layout').selectedIndex].value;
79 var color = $('color').options[$('color').selectedIndex].value;
80
81 var jobSettings = JSON.stringify({'printerName': printerName,
82 'pageRange': pageRanges,
83 'printAll': printAll,
84 'twoSided': twoSided,
85 'copies': copies,
86 'collate': collate,
87 'layout': layout,
88 'color': color});
89 chrome.send('print', [jobSettings]);
90 }
91
92 /**
24 * Fill the printer list drop down. 93 * Fill the printer list drop down.
25 */ 94 */
26 function setPrinters(printers) { 95 function setPrinters(printers) {
27 if (printers.length > 0) { 96 if (printers.length > 0) {
28 for (var i = 0; i < printers.length; ++i) { 97 for (var i = 0; i < printers.length; ++i) {
29 var option = document.createElement('option'); 98 var option = document.createElement('option');
30 option.textContent = printers[i]; 99 option.textContent = printers[i];
31 $('printer-list').add(option); 100 $('printer-list').add(option);
32 } 101 }
33 } else { 102 } else {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 if (!pdfPlugin.onload) { 146 if (!pdfPlugin.onload) {
78 hasPDFPlugin = false; 147 hasPDFPlugin = false;
79 mainView.removeChild(pdfPlugin); 148 mainView.removeChild(pdfPlugin);
80 $('no-plugin').classList.remove('hidden'); 149 $('no-plugin').classList.remove('hidden');
81 return; 150 return;
82 } 151 }
83 pdfPlugin.onload('onPDFLoad()'); 152 pdfPlugin.onload('onPDFLoad()');
84 } 153 }
85 154
86 window.addEventListener('DOMContentLoaded', load); 155 window.addEventListener('DOMContentLoaded', load);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698