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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/print_preview.js
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js
index b1ceed8d8c2bcab0fe8049ae531ed221728bfc31..25b53af491bfddef008d01fd97d3e94240b4f648 100644
--- a/chrome/browser/resources/print_preview.js
+++ b/chrome/browser/resources/print_preview.js
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -10,9 +10,8 @@ var expectedPageCount = 0;
* Window onload handler, sets up the page.
*/
function load() {
- $('print-button').addEventListener('click', function(e) {
- chrome.send('print');
- });
+ $('print-button').addEventListener('click', printFile);
+
$('cancel-button').addEventListener('click', function(e) {
window.close();
});
@@ -21,6 +20,73 @@ function load() {
};
/**
+ * Page range text validation.
+ * Returns true if |printFromText| and |printToText| are valid page numbers.
+ * TODO (kmadhusu): Get the expected page count and validate the page range
+ * with total number of pages.
+ */
+function isValidPageRange(printFromText, printToText) {
+ var numericExp = /^[0-9]+$/;
+ if (numericExp.test(printFromText) && numericExp.test(printToText)) {
+ var printFrom = Number(printFromText);
+ var printTo = Number(printToText);
+ if (printFrom <= printTo && printFrom != 0 && printTo != 0)
+ return true;
+ }
+ return false;
+}
+
+/**
+ * Parse page range text.
+ * Eg: If page range is specified as '1-3,7-9,8'. Create an array with three
+ * elements. Each array element contains the range information.
+ * [{from:1, to:3}, {from:7, to:9}, {from:8, to:8}]
+ * TODO (kmadhusu): Handle invalid characters.
+ */
+function getPageRanges() {
+ var pageRangesInfo = [];
+ var pageRangeText = $('pages').value;
+ var pageRangeList = pageRangeText.replace(/\s/g, '').split(',');
+ for (var i = 0; i < pageRangeList.length; i++) {
+ var tempRange = pageRangeList[i].split('-');
+ var printFrom = tempRange[0];
+ var printTo;
+ if (tempRange.length > 1)
+ printTo = tempRange[1];
+ else
+ printTo = tempRange[0];
+ // Validate the page range information.
+ if (isValidPageRange(printFrom, printTo)) {
+ pageRangesInfo.push({'from': parseInt(printFrom, 10),
+ 'to': parseInt(printTo, 10)});
+ }
+ }
+ return pageRangesInfo;
+}
+
+function printFile() {
+ var selectedPrinter = $('printer-list').selectedIndex;
+ var printerName = $('printer-list').options[selectedPrinter].textContent;
+ var pageRanges = getPageRanges();
+ var printAll = $('all-pages').checked;
+ var twoSided = $('two-sided').checked;
+ var copies = $('copies').value;
+ var collate = $('collate').checked;
+ var layout = $('layout').options[$('layout').selectedIndex].value;
+ var color = $('color').options[$('color').selectedIndex].value;
+
+ var jobSettings = JSON.stringify({'printerName': printerName,
+ 'pageRange': pageRanges,
+ 'printAll': printAll,
+ 'twoSided': twoSided,
+ 'copies': copies,
+ 'collate': collate,
+ 'layout': layout,
+ 'color': color});
+ chrome.send('print', [jobSettings]);
+}
+
+/**
* Fill the printer list drop down.
*/
function setPrinters(printers) {
« 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