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

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, 9 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 = tempRange[0];
53 var printTo;
54 if (tempRange.length > 1)
55 printTo = tempRange[1];
56 else
57 printTo = tempRange[0];
58 // Validate the page range information.
59 if (isValidPageRange(printFrom, printTo)) {
60 pageRangesInfo.push({'from': parseInt(printFrom, 10),
61 'to': parseInt(printTo, 10)});
62 }
63 }
64 return pageRangesInfo;
65 }
66
67 function printFile() {
68 var selectedPrinter = $('printer-list').selectedIndex;
69 var printerName = $('printer-list').options[selectedPrinter].textContent;
70 var pageRanges = getPageRanges();
71 var printAll = $('all-pages').checked;
72 var twoSided = $('two-sided').checked;
73 var copies = $('copies').value;
74 var collate = $('collate').checked;
75 var layout = $('layout').options[$('layout').selectedIndex].value;
76 var color = $('color').options[$('color').selectedIndex].value;
77
78 var jobSettings = JSON.stringify({'printerName': printerName,
79 'pageRange': pageRanges,
80 'printAll': printAll,
81 'twoSided': twoSided,
82 'copies': copies,
83 'collate': collate,
84 'layout': layout,
85 'color': color});
86 chrome.send('print', [jobSettings]);
87 }
88
89 /**
24 * Fill the printer list drop down. 90 * Fill the printer list drop down.
25 */ 91 */
26 function setPrinters(printers) { 92 function setPrinters(printers) {
27 if (printers.length > 0) { 93 if (printers.length > 0) {
28 for (var i = 0; i < printers.length; ++i) { 94 for (var i = 0; i < printers.length; ++i) {
29 var option = document.createElement('option'); 95 var option = document.createElement('option');
30 option.textContent = printers[i]; 96 option.textContent = printers[i];
31 $('printer-list').add(option); 97 $('printer-list').add(option);
32 } 98 }
33 } else { 99 } else {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 if (!pdfPlugin.onload) { 143 if (!pdfPlugin.onload) {
78 hasPDFPlugin = false; 144 hasPDFPlugin = false;
79 mainView.removeChild(pdfPlugin); 145 mainView.removeChild(pdfPlugin);
80 $('no-plugin').classList.remove('hidden'); 146 $('no-plugin').classList.remove('hidden');
81 return; 147 return;
82 } 148 }
83 pdfPlugin.onload('onPDFLoad()'); 149 pdfPlugin.onload('onPDFLoad()');
84 } 150 }
85 151
86 window.addEventListener('DOMContentLoaded', load); 152 window.addEventListener('DOMContentLoaded', load);
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview.html ('k') | chrome/browser/ui/webui/print_preview_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698