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

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

Issue 7038028: Initial support for cloudprint in print preview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Lint! Created 9 years, 6 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) 2011 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 // require: cr/ui/print_preview_cloud.js
6
5 var localStrings = new LocalStrings(); 7 var localStrings = new LocalStrings();
6 8
9 var useCloudPrint = false;
10 var maxCloudPrinters = 10;
11
7 // The total page count of the previewed document regardless of which pages the 12 // The total page count of the previewed document regardless of which pages the
8 // user has selected. 13 // user has selected.
9 var totalPageCount = -1; 14 var totalPageCount = -1;
10 15
11 // The previously selected pages by the user. It is used in 16 // The previously selected pages by the user. It is used in
12 // onPageSelectionMayHaveChanged() to make sure that a new preview is not 17 // onPageSelectionMayHaveChanged() to make sure that a new preview is not
13 // requested more often than necessary. 18 // requested more often than necessary.
14 var previouslySelectedPages = []; 19 var previouslySelectedPages = [];
15 20
16 // Timer id of the page range textfield. It is used to reset the timer whenever 21 // Timer id of the page range textfield. It is used to reset the timer whenever
17 // needed. 22 // needed.
18 var timerId; 23 var timerId;
19 24
20 // Store the last selected printer index. 25 // Store the last selected printer index.
21 var lastSelectedPrinterIndex = 0; 26 var lastSelectedPrinterIndex = 0;
22 27
23 // Used to disable some printing options when the preview is not modifiable. 28 // Used to disable some printing options when the preview is not modifiable.
24 var previewModifiable = false; 29 var previewModifiable = false;
25 30
26 // Destination list special value constants. 31 // Destination list special value constants.
32 const ADD_PRINTER = 'addPrinter';
33 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters';
34 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters';
35 const MORE_PRINTERS = 'morePrinters';
36 const SIGN_IN = 'signIn';
27 const PRINT_TO_PDF = 'Print To PDF'; 37 const PRINT_TO_PDF = 'Print To PDF';
28 const MANAGE_PRINTERS = 'Manage Printers';
29 38
30 // State of the print preview settings. 39 // State of the print preview settings.
31 var printSettings = new PrintSettings(); 40 var printSettings = new PrintSettings();
32 41
33 /** 42 /**
34 * Window onload handler, sets up the page and starts print preview by getting 43 * Window onload handler, sets up the page and starts print preview by getting
35 * the printer list. 44 * the printer list.
36 */ 45 */
37 function onLoad() { 46 function onLoad() {
38 $('system-dialog-link').addEventListener('click', showSystemDialog); 47 $('system-dialog-link').addEventListener('click', showSystemDialog);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 /** 156 /**
148 * Gets the selected printer capabilities and updates the controls accordingly. 157 * Gets the selected printer capabilities and updates the controls accordingly.
149 */ 158 */
150 function updateControlsWithSelectedPrinterCapabilities() { 159 function updateControlsWithSelectedPrinterCapabilities() {
151 var printerList = $('printer-list'); 160 var printerList = $('printer-list');
152 var selectedIndex = printerList.selectedIndex; 161 var selectedIndex = printerList.selectedIndex;
153 if (selectedIndex < 0) 162 if (selectedIndex < 0)
154 return; 163 return;
155 164
156 var selectedValue = printerList.options[selectedIndex].value; 165 var selectedValue = printerList.options[selectedIndex].value;
157 if (selectedValue == PRINT_TO_PDF) { 166 if (printerList.options[selectedIndex].isCloudPrint) {
167 updateWithCloudPrinterCapabilities();
168 return;
169 } else if (selectedValue == SIGN_IN ||
170 selectedValue == MANAGE_CLOUD_PRINTERS ||
171 selectedValue == MANAGE_LOCAL_PRINTERS) {
172 printerList.selectedIndex = lastSelectedPrinterIndex;
173 chrome.send(selectedValue);
174 return;
175 } else if (selectedValue == PRINT_TO_PDF) {
158 updateWithPrinterCapabilities({'disableColorOption': true, 176 updateWithPrinterCapabilities({'disableColorOption': true,
159 'setColorAsDefault': true, 177 'setColorAsDefault': true,
160 'disableCopiesOption': true}); 178 'disableCopiesOption': true,
161 } else if (selectedValue == MANAGE_PRINTERS) { 179 'disableLandscapeOption': false});
162 printerList.selectedIndex = lastSelectedPrinterIndex;
163 chrome.send('managePrinters');
164 return;
165 } else { 180 } else {
166 // This message will call back to 'updateWithPrinterCapabilities' 181 // This message will call back to 'updateWithPrinterCapabilities'
167 // function. 182 // function.
168 chrome.send('getPrinterCapabilities', [selectedValue]); 183 chrome.send('getPrinterCapabilities', [selectedValue]);
169 } 184 }
170 185
171 lastSelectedPrinterIndex = selectedIndex; 186 lastSelectedPrinterIndex = selectedIndex;
172 187
173 // Regenerate the preview data based on selected printer settings. 188 // Regenerate the preview data based on selected printer settings.
174 setDefaultValuesAndRegeneratePreview(); 189 setDefaultValuesAndRegeneratePreview();
175 } 190 }
176 191
192 function updateWithCloudPrinterCapabilities() {
193 var printerList = $('printer-list');
194 var selectedIndex = printerList.selectedIndex;
195 if (printerList.options[selectedIndex].capabilities) {
196 doUpdateCloudPrinterCapabilities(
197 printerList.options[selectedIndex],
198 printerList.options[selectedIndex].capabilities);
199 } else {
200 cloudprint.updatePrinterCaps(printerList.options[selectedIndex],
201 doUpdateCloudPrinterCapabilities);
202 }
203 }
204
205 function doUpdateCloudPrinterCapabilities(printer) {
206 var settings = {'disableColorOption': !cloudprint.supportsColor(printer),
207 'setColorAsDefault': cloudprint.colorIsDefault(printer),
208 'disableCopiesOption': true,
209 'disableLandscapeOption': true};
210 updateWithPrinterCapabilities(settings);
211 var printerList = $('printer-list');
212 var selectedIndex = printerList.selectedIndex;
213 lastSelectedPrinterIndex = selectedIndex;
214
215 // Regenerate the preview data based on selected printer settings.
216 setDefaultValuesAndRegeneratePreview();
217 }
218
177 /** 219 /**
178 * Updates the controls with printer capabilities information. 220 * Updates the controls with printer capabilities information.
179 * @param {Object} settingInfo printer setting information. 221 * @param {Object} settingInfo printer setting information.
180 */ 222 */
181 function updateWithPrinterCapabilities(settingInfo) { 223 function updateWithPrinterCapabilities(settingInfo) {
182 var disableColorOption = settingInfo.disableColorOption; 224 var disableColorOption = settingInfo.disableColorOption;
183 var disableCopiesOption = settingInfo.disableCopiesOption; 225 var disableCopiesOption = settingInfo.disableCopiesOption;
184 var setColorAsDefault = settingInfo.setColorAsDefault; 226 var setColorAsDefault = settingInfo.setColorAsDefault;
227 var disableLandscapeOption = settingInfo.disableLandscapeOption;
185 var colorOption = $('color'); 228 var colorOption = $('color');
186 var bwOption = $('bw'); 229 var bwOption = $('bw');
187 230
188 if (disableCopiesOption) { 231 if (disableCopiesOption) {
189 fadeOutElement($('copies-option')); 232 fadeOutElement($('copies-option'));
190 $('hr-before-copies').classList.remove('invisible'); 233 $('hr-before-copies').classList.remove('invisible');
191 } else { 234 } else {
192 fadeInElement($('copies-option')); 235 fadeInElement($('copies-option'));
193 $('hr-before-copies').classList.add('invisible'); 236 $('hr-before-copies').classList.add('invisible');
194 } 237 }
195 238
239 if (disableLandscapeOption) {
240 fadeOutElement($('landscape-option'));
241 } else {
242 fadeInElement($('landscape-option'));
243 }
244
196 disableColorOption ? fadeOutElement($('color-options')) : 245 disableColorOption ? fadeOutElement($('color-options')) :
197 fadeInElement($('color-options')); 246 fadeInElement($('color-options'));
198 247
199 if (colorOption.checked != setColorAsDefault) { 248 if (colorOption.checked != setColorAsDefault) {
200 colorOption.checked = setColorAsDefault; 249 colorOption.checked = setColorAsDefault;
201 bwOption.checked = !setColorAsDefault; 250 bwOption.checked = !setColorAsDefault;
202 setColor(colorOption.checked); 251 setColor(colorOption.checked);
203 } 252 }
204 } 253 }
205 254
206 /** 255 /**
256 * Turn on the integration of Cloud Print.
257 * @param {enable} true if cloud print should be used.
258 */
259 function setUseCloudPrint(enable, cloudPrintURL) {
260 useCloudPrint = enable;
261 cloudprint.setBaseURL(cloudPrintURL);
262 }
263
264 /**
265 * Take the PDF data handed to us and submit it to the cloud, closing the print
266 * preview tab once the upload is successful.
267 * @param {pdfData} data to send as the print job.
268 */
269 function printToCloud(data) {
270 cloudprint.printToCloud(data, finishedCloudPrinting);
271 }
272
273 /**
274 * Cloud print upload of the PDF file is finished, time to close the dialog.
275 */
276 function finishedCloudPrinting() {
277 window.location = cloudprint.getBaseURL();
278 }
279
280 /**
281 * Disables or enables all controls in the options pane except for the cancel
282 * button.
283 */
284 function setControlsDisabled(disabled) {
285 var elementList = $('controls').elements;
286 for (var i = 0; i < elementList.length; ++i) {
287 if (elementList[i] == $('cancel-button'))
288 continue;
289 elementList[i].disabled = disabled;
290 }
291 }
292
293 /**
207 * Validates the copies text field value. 294 * Validates the copies text field value.
208 * NOTE: An empty copies field text is considered valid because the blur event 295 * NOTE: An empty copies field text is considered valid because the blur event
209 * listener of this field will set it back to a default value. 296 * listener of this field will set it back to a default value.
210 * @return {boolean} true if the number of copies is valid else returns false. 297 * @return {boolean} true if the number of copies is valid else returns false.
211 */ 298 */
212 function isNumberOfCopiesValid() { 299 function isNumberOfCopiesValid() {
213 var copiesFieldText = $('copies').value.replace(/\s/g, ''); 300 var copiesFieldText = $('copies').value.replace(/\s/g, '');
214 if (copiesFieldText == '') 301 if (copiesFieldText == '')
215 return true; 302 return true;
216 303
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 const LONG_EDGE = 1; 374 const LONG_EDGE = 1;
288 return !isTwoSided() ? SIMPLEX : LONG_EDGE; 375 return !isTwoSided() ? SIMPLEX : LONG_EDGE;
289 } 376 }
290 377
291 /** 378 /**
292 * Creates a JSON string based on the values in the printer settings. 379 * Creates a JSON string based on the values in the printer settings.
293 * 380 *
294 * @return {string} JSON string with print job settings. 381 * @return {string} JSON string with print job settings.
295 */ 382 */
296 function getSettingsJSON() { 383 function getSettingsJSON() {
384 var printerList = $('printer-list');
385 var selectedPrinter = printerList.selectedIndex;
297 var printAll = $('all-pages').checked; 386 var printAll = $('all-pages').checked;
298 var deviceName = getSelectedPrinterName(); 387 var deviceName = getSelectedPrinterName();
299 var printToPDF = (deviceName == PRINT_TO_PDF); 388 var printToPDF = (deviceName == PRINT_TO_PDF);
300 389
301 return JSON.stringify({'deviceName': deviceName, 390 var settings = {'deviceName': deviceName,
302 'pageRange': getSelectedPageRanges(), 391 'pageRange': getSelectedPageRanges(),
303 'printAll': printAll, 392 'printAll': printAll,
304 'duplex': getDuplexMode(), 393 'duplex': getDuplexMode(),
305 'copies': getCopies(), 394 'copies': getCopies(),
306 'collate': isCollated(), 395 'collate': isCollated(),
307 'landscape': isLandscape(), 396 'landscape': isLandscape(),
308 'color': isColor(), 397 'color': isColor(),
309 'printToPDF': printToPDF}); 398 'printToPDF': printToPDF};
399 if (printerList.options[selectedPrinter].isCloudPrint) {
400 settings['cloudPrintID'] =
401 printerList.options[selectedPrinter].value;
402 }
403
404 return JSON.stringify(settings);
310 } 405 }
311 406
312 /** 407 /**
313 * Returns the name of the selected printer or the empty string if no 408 * Returns the name of the selected printer or the empty string if no
314 * printer is selected. 409 * printer is selected.
315 */ 410 */
316 function getSelectedPrinterName() { 411 function getSelectedPrinterName() {
317 var printerList = $('printer-list') 412 var printerList = $('printer-list')
318 var selectedPrinter = printerList.selectedIndex; 413 var selectedPrinter = printerList.selectedIndex;
319 var deviceName = ''; 414 var deviceName = '';
320 if (selectedPrinter >= 0) 415 if (selectedPrinter >= 0)
321 deviceName = printerList.options[selectedPrinter].value; 416 deviceName = printerList.options[selectedPrinter].value;
322 return deviceName; 417 return deviceName;
323 } 418 }
324 419
325 /** 420 /**
326 * Asks the browser to print the preview PDF based on current print settings. 421 * Asks the browser to print the preview PDF based on current print settings.
327 */ 422 */
328 function printFile() { 423 function printFile() {
329 if (getSelectedPrinterName() != PRINT_TO_PDF) { 424 if (getSelectedPrinterName() != PRINT_TO_PDF) {
330 $('print-button').classList.add('loading'); 425 $('print-button').classList.add('loading');
331 $('cancel-button').classList.add('loading'); 426 $('cancel-button').classList.add('loading');
332 $('print-summary').innerHTML = localStrings.getString('printing'); 427 $('print-summary').innerHTML = localStrings.getString('printing');
333 removeEventListeners(); 428 removeEventListeners();
334 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, 429 var printerList = $('printer-list')
335 1000); 430 var printer = printerList[printerList.selectedIndex];
431 window.setTimeout(function() { chrome.send('print',
432 [getSettingsJSON(), cloudprint.getPrintTicketJSON(printer)]); }, 1000);
336 } else 433 } else
337 chrome.send('print', [getSettingsJSON()]); 434 chrome.send('print', [getSettingsJSON(), null]);
338 } 435 }
339 436
340 /** 437 /**
341 * Asks the browser to generate a preview PDF based on current print settings. 438 * Asks the browser to generate a preview PDF based on current print settings.
342 */ 439 */
343 function requestPrintPreview() { 440 function requestPrintPreview() {
344 removeEventListeners(); 441 removeEventListeners();
345 printSettings.save(); 442 printSettings.save();
346 showLoadingAnimation(); 443 showLoadingAnimation();
347 chrome.send('getPreview', [getSettingsJSON()]); 444 chrome.send('getPreview', [getSettingsJSON()]);
(...skipping 14 matching lines...) Expand all
362 } 459 }
363 460
364 /** 461 /**
365 * Fill the printer list drop down. 462 * Fill the printer list drop down.
366 * Called from PrintPreviewHandler::SendPrinterList(). 463 * Called from PrintPreviewHandler::SendPrinterList().
367 * @param {Array} printers Array of printer info objects. 464 * @param {Array} printers Array of printer info objects.
368 * @param {number} defaultPrinterIndex The index of the default printer. 465 * @param {number} defaultPrinterIndex The index of the default printer.
369 */ 466 */
370 function setPrinters(printers, defaultPrinterIndex) { 467 function setPrinters(printers, defaultPrinterIndex) {
371 var printerList = $('printer-list'); 468 var printerList = $('printer-list');
469 if (useCloudPrint) {
470 cloudprint.fetchPrinters(addCloudPrinters);
471 if (printers.length > 0) {
472 option = addDestinationListOption('', '', false, true);
473 option = addDestinationListOption(localStrings.getString('localPrinters'),
474 '',
475 false,
476 true);
477 }
478 }
372 // If there exists a dummy printer value, then setDefaultPrinter() already 479 // If there exists a dummy printer value, then setDefaultPrinter() already
373 // requested a preview, so no need to do it again. 480 // requested a preview, so no need to do it again.
374 var needPreview = (printerList[0].value == ''); 481 var needPreview = (printerList[0].value == '');
375 for (var i = 0; i < printers.length; ++i) { 482 for (var i = 0; i < printers.length; ++i) {
376 // Check if we are looking at the default printer. 483 // Check if we are looking at the default printer.
377 if (i == defaultPrinterIndex) { 484 if (i == defaultPrinterIndex) {
378 // If the default printer from setDefaultPrinter() does not match the 485 // If the default printer from setDefaultPrinter() does not match the
379 // enumerated value, (re)generate the print preview. 486 // enumerated value, (re)generate the print preview.
380 if (printers[i].deviceName != printerList[0].value) 487 if (printers[i].deviceName != printerList[0].value)
381 needPreview = true; 488 needPreview = true;
382 } 489 }
383 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 490 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
384 i == defaultPrinterIndex, false); 491 i == defaultPrinterIndex, false);
385 } 492 }
386 493
387 // Remove the dummy printer added in setDefaultPrinter(). 494 // Remove the dummy printer added in setDefaultPrinter().
388 printerList.remove(0); 495 printerList.remove(0);
389 496
390 if (printers.length != 0) 497 if (printers.length != 0)
391 addDestinationListOption('', '', false, true); 498 addDestinationListOption('', '', false, true);
392 499
393 // Adding option for saving PDF to disk. 500 // Adding option for saving PDF to disk.
394 addDestinationListOption(localStrings.getString('printToPDF'), 501 addDestinationListOption(localStrings.getString('printToPDF'),
395 PRINT_TO_PDF, false, false); 502 PRINT_TO_PDF, false, false);
396 addDestinationListOption('', '', false, true); 503 addDestinationListOption('', '', false, true);
397 504
398 // Add an option to manage printers. 505 // Add options to manage printers.
399 addDestinationListOption(localStrings.getString('managePrinters'), 506 if (!cr.isChromeOS) {
400 MANAGE_PRINTERS, false, false); 507 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
508 MANAGE_LOCAL_PRINTERS, false, false);
509 }
510 if (useCloudPrint) {
511 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
512 MANAGE_CLOUD_PRINTERS, false, false);
513 }
401 514
402 printerList.disabled = false; 515 printerList.disabled = false;
403 516
404 if (needPreview) 517 if (needPreview)
405 updateControlsWithSelectedPrinterCapabilities(); 518 updateControlsWithSelectedPrinterCapabilities();
406 } 519 }
407 520
408 /** 521 /**
409 * Adds an option to the printer destination list. 522 * Adds an option to the printer destination list.
410 * @param {String} optionText specifies the option text content. 523 * @param {String} optionText specifies the option text content.
411 * @param {String} optionValue specifies the option value. 524 * @param {String} optionValue specifies the option value.
412 * @param {boolean} isDefault is true if the option needs to be selected. 525 * @param {boolean} isDefault is true if the option needs to be selected.
413 * @param {boolean} isDisabled is true if the option needs to be disabled. 526 * @param {boolean} isDisabled is true if the option needs to be disabled.
414 */ 527 */
415 function addDestinationListOption(optionText, optionValue, isDefault, 528 function addDestinationListOption(optionText, optionValue, isDefault,
416 isDisabled) { 529 isDisabled) {
417 var option = document.createElement('option'); 530 var option = document.createElement('option');
418 option.textContent = optionText; 531 option.textContent = optionText;
419 option.value = optionValue; 532 option.value = optionValue;
420 $('printer-list').add(option); 533 $('printer-list').add(option);
421 option.selected = isDefault; 534 option.selected = isDefault;
422 option.disabled = isDisabled; 535 option.disabled = isDisabled;
536 return option;
423 } 537 }
424 538
425 /** 539 /**
540 * Add cloud printers to the list drop down.
541 * Called from the cloudprint object on receipt of printer information from the
542 * cloud print server.
543 */
544 function addCloudPrinters(printers) {
545 addDestinationListOption(localStrings.getString('cloudPrinters'),
546 '',
547 false,
548 true);
549 if (printers != null) {
550 var l = printers.length;
551 if (l > maxCloudPrinters) {
552 l = maxCloudPrinters;
553 }
554 for (var i = 0; i < l; i++) {
555 var option = addDestinationListOption(printers[i]['name'],
556 printers[i]['id'],
557 i == 0,
558 false);
559 option.isCloudPrint = true;
560 }
561 if (printers.length == 0) {
562 addDestinationListOption(localStrings.getString('addCloudPrinter'),
563 ADD_PRINTER, false, false);
564 }
565 if (l < printers.length) {
566 var option = addDestinationListOption('', '', false, true);
567 addDestinationListOption(localStrings.getString('morePrinters'),
568 MORE_PRINTERS, false, false);
569 }
570 } else {
571 addDestinationListOption(localStrings.getString('signIn'),
572 SIGN_IN, false, false);
573 }
574 }
575
576 /**
426 * Sets the color mode for the PDF plugin. 577 * Sets the color mode for the PDF plugin.
427 * Called from PrintPreviewHandler::ProcessColorSetting(). 578 * Called from PrintPreviewHandler::ProcessColorSetting().
428 * @param {boolean} color is true if the PDF plugin should display in color. 579 * @param {boolean} color is true if the PDF plugin should display in color.
429 */ 580 */
430 function setColor(color) { 581 function setColor(color) {
431 var pdfViewer = $('pdf-viewer'); 582 var pdfViewer = $('pdf-viewer');
432 if (!pdfViewer) { 583 if (!pdfViewer) {
433 return; 584 return;
434 } 585 }
435 pdfViewer.grayscale(!color); 586 pdfViewer.grayscale(!color);
587 var printerList = $('printer-list')
588 cloudprint.setColor(printerList[printerList.selectedIndex], color);
436 } 589 }
437 590
438 /** 591 /**
439 * Display an error message in the center of the preview area. 592 * Display an error message in the center of the preview area.
440 * @param {string} errorMessage The error message to be displayed. 593 * @param {string} errorMessage The error message to be displayed.
441 * @param {boolean} showButton Indivates whether the "Reopen the page" button 594 * @param {boolean} showButton Indivates whether the "Reopen the page" button
442 * should be displayed. 595 * should be displayed.
443 */ 596 */
444 function displayErrorMessage(errorMessage, showButton) { 597 function displayErrorMessage(errorMessage, showButton) {
445 $('overlay-layer').classList.remove('invisible'); 598 $('overlay-layer').classList.remove('invisible');
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 this.isLandscape = ''; 1118 this.isLandscape = '';
966 } 1119 }
967 1120
968 /** 1121 /**
969 * Takes a snapshot of the print settings. 1122 * Takes a snapshot of the print settings.
970 */ 1123 */
971 PrintSettings.prototype.save = function() { 1124 PrintSettings.prototype.save = function() {
972 this.deviceName = getSelectedPrinterName(); 1125 this.deviceName = getSelectedPrinterName();
973 this.isLandscape = isLandscape(); 1126 this.isLandscape = isLandscape();
974 } 1127 }
1128
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698