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 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.split(','); | |
arv (Not doing code reviews)
2011/02/23 19:38:52
Should we not support white spaces in the text as
kmadhusu
2011/02/24 02:11:34
Done.
| |
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]; | |
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 if (!pdfPlugin.onload) { | 143 if (!pdfPlugin.onload) { |
75 hasPDFPlugin = false; | 144 hasPDFPlugin = false; |
76 mainView.removeChild(pdfPlugin); | 145 mainView.removeChild(pdfPlugin); |
77 $('no-plugin').classList.remove('hidden'); | 146 $('no-plugin').classList.remove('hidden'); |
78 return; | 147 return; |
79 } | 148 } |
80 pdfPlugin.onload('onPDFLoad()'); | 149 pdfPlugin.onload('onPDFLoad()'); |
81 } | 150 } |
82 | 151 |
83 window.addEventListener('DOMContentLoaded', load); | 152 window.addEventListener('DOMContentLoaded', load); |
OLD | NEW |