OLD | NEW |
---|---|
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 | 7 |
8 /** | 8 /** |
9 * Window onload handler, sets up the page. | 9 * Window onload handler, sets up the page. |
10 */ | 10 */ |
11 function load() { | 11 function load() { |
12 $('print-button').addEventListener('click', function(e) { | 12 $('print-button').addEventListener('click', printFile); |
13 chrome.send('print'); | 13 |
14 }); | |
15 $('cancel-button').addEventListener('click', function(e) { | 14 $('cancel-button').addEventListener('click', function(e) { |
16 window.close(); | 15 window.close(); |
17 }); | 16 }); |
18 | 17 |
19 chrome.send('getPrinters'); | 18 chrome.send('getPrinters'); |
20 }; | 19 }; |
21 | 20 |
22 /** | 21 /** |
22 * Page range text validation. | |
23 * Returns true if |printFromText| and |printToText| are valid page numbers. | |
24 * TODO (kmadhusu): Get the expected page count and validate the page range | |
25 * with total number of pages. | |
26 */ | |
27 function isValidPageRange(printFromText, printToText) { | |
28 var numericExp = /^[0-9]+$/; | |
29 if (printFromText.match(numericExp) && printToText.match(numericExp)) { | |
30 var printFrom = parseInt(printFromText, 10); | |
31 var printTo = parseInt(printToText, 10); | |
32 if ((printFrom > 0) && (printTo > 0) && (printFrom <= printTo)) | |
33 return true; | |
34 } | |
35 return false; | |
36 } | |
37 | |
38 /** | |
39 * Parse page range text. | |
40 * Eg: If page range is specified as "1-3,7-9,8". Create an array with three | |
41 * elements. Each array element contains the range information. | |
42 * [{from:1, to:3}, {from:7, to:9}, {from:8, to:8}] | |
43 * TODO (kmadhusu): Handle invalid characters. | |
44 */ | |
45 function getPageRanges() { | |
46 var pageRangesInfo = new Array(); | |
47 var pageRangeText = $('pages').value; | |
48 var pageRangeList = pageRangeText.split(','); | |
49 for (var i = 0; i < pageRangeList.length; i++) { | |
50 var tempRange = pageRangeList[i].split('-'); | |
51 var printFrom; | |
52 var printTo; | |
53 if (tempRange.length > 1) { | |
54 printFrom = tempRange[0]; | |
55 printTo = tempRange[1]; | |
56 } else { | |
57 printFrom = tempRange[0]; | |
58 printTo = tempRange[0]; | |
59 } | |
60 // Validate the page range information. | |
61 if (isValidPageRange(printFrom, printTo)) { | |
62 var pageRangeParams = new Object(); | |
63 pageRangeParams.from = parseInt(printFrom, 10); | |
64 pageRangeParams.to = parseInt(printTo, 10); | |
65 pageRangesInfo.push(pageRangeParams); | |
66 } | |
67 } | |
68 return pageRangesInfo; | |
69 } | |
70 | |
71 function printFile() { | |
72 var selectedPrinter = $('printer-list').selectedIndex; | |
73 var printerName = $('printer-list').options[selectedPrinter].textContent; | |
74 var pageRanges = getPageRanges(); | |
75 var printAll = $('all-pages').checked; | |
76 var twoSided = $('two-sided').checked; | |
77 var copies = $('copies').value; | |
78 var collate = $('collate').checked; | |
79 var layout = $('layout').options[$('layout').selectedIndex].value; | |
80 var color = $('color').options[$('color').selectedIndex].value; | |
81 | |
82 var jobSettings = JSON.stringify({"printerName": printerName, | |
83 "pageRange": pageRanges, | |
Lei Zhang
2011/02/18 05:21:42
nit: indentation.
kmadhusu
2011/02/21 01:30:26
Done.
| |
84 "printAll": printAll, | |
85 "twoSided": twoSided, | |
86 "copies": copies, | |
87 "collate": collate, | |
88 "layout": layout, | |
89 "color": color}); | |
90 chrome.send('print', [jobSettings]); | |
91 } | |
92 | |
93 /** | |
23 * Fill the printer list drop down. | 94 * Fill the printer list drop down. |
24 */ | 95 */ |
25 function setPrinters(printers) { | 96 function setPrinters(printers) { |
26 if (printers.length > 0) { | 97 if (printers.length > 0) { |
27 for (var i = 0; i < printers.length; ++i) { | 98 for (var i = 0; i < printers.length; ++i) { |
28 var option = document.createElement('option'); | 99 var option = document.createElement('option'); |
29 option.textContent = printers[i]; | 100 option.textContent = printers[i]; |
30 $('printer-list').add(option); | 101 $('printer-list').add(option); |
31 } | 102 } |
32 } else { | 103 } else { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 if (!pdfPlugin.onload) { | 138 if (!pdfPlugin.onload) { |
68 hasPDFPlugin = false; | 139 hasPDFPlugin = false; |
69 mainView.removeChild(pdfPlugin); | 140 mainView.removeChild(pdfPlugin); |
70 $('no-plugin').classList.remove('hidden'); | 141 $('no-plugin').classList.remove('hidden'); |
71 return; | 142 return; |
72 } | 143 } |
73 pdfPlugin.onload('onPDFLoad()'); | 144 pdfPlugin.onload('onPDFLoad()'); |
74 } | 145 } |
75 | 146 |
76 window.addEventListener('DOMContentLoaded', load); | 147 window.addEventListener('DOMContentLoaded', load); |
OLD | NEW |