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

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: Latest merge. Removed whitelist code.wq 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;
dpapad 2011/06/14 01:14:46 Document these variables.
Albert Bodenhamer 2011/06/14 19:43:54 Done.
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 // The name of the default or last used printer. 42 // The name of the default or last used printer.
34 var defaultOrLastUsedPrinterName = ''; 43 var defaultOrLastUsedPrinterName = '';
35 44
36 // True when a pending print preview request exists. 45 // True when a pending print preview request exists.
37 var hasPendingPreviewRequest = false; 46 var hasPendingPreviewRequest = false;
38 47
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 } 205 }
197 206
198 /** 207 /**
199 * Gets the selected printer capabilities and updates the controls accordingly. 208 * Gets the selected printer capabilities and updates the controls accordingly.
200 */ 209 */
201 function updateControlsWithSelectedPrinterCapabilities() { 210 function updateControlsWithSelectedPrinterCapabilities() {
202 var printerList = $('printer-list'); 211 var printerList = $('printer-list');
203 var selectedIndex = printerList.selectedIndex; 212 var selectedIndex = printerList.selectedIndex;
204 if (selectedIndex < 0) 213 if (selectedIndex < 0)
205 return; 214 return;
206 215 var skip_refresh = false;
207 var selectedValue = printerList.options[selectedIndex].value; 216 var selectedValue = printerList.options[selectedIndex].value;
208 if (selectedValue == PRINT_TO_PDF) { 217 if (printerList.options[selectedIndex].isCloudPrint) {
218 updateWithCloudPrinterCapabilities();
219 skip_refresh = true;
220 } else if (selectedValue == SIGN_IN ||
221 selectedValue == MANAGE_CLOUD_PRINTERS ||
222 selectedValue == MANAGE_LOCAL_PRINTERS) {
223 printerList.selectedIndex = lastSelectedPrinterIndex;
224 chrome.send(selectedValue);
225 skip_refresh = true;
226 } else if (selectedValue == PRINT_TO_PDF) {
209 updateWithPrinterCapabilities({'disableColorOption': true, 227 updateWithPrinterCapabilities({'disableColorOption': true,
210 'setColorAsDefault': true, 228 'setColorAsDefault': true,
211 'disableCopiesOption': true}); 229 'disableCopiesOption': true,
212 } else if (selectedValue == MANAGE_PRINTERS) { 230 'disableLandscapeOption': false});
213 printerList.selectedIndex = lastSelectedPrinterIndex;
214 chrome.send('managePrinters');
215 return;
216 } else { 231 } else {
217 // This message will call back to 'updateWithPrinterCapabilities' 232 // This message will call back to 'updateWithPrinterCapabilities'
218 // function. 233 // function.
219 chrome.send('getPrinterCapabilities', [selectedValue]); 234 chrome.send('getPrinterCapabilities', [selectedValue]);
220 } 235 }
236 if (!skip_refresh) {
237 lastSelectedPrinterIndex = selectedIndex;
221 238
239 // Regenerate the preview data based on selected printer settings.
240 setDefaultValuesAndRegeneratePreview();
241 }
242 }
243
244 function updateWithCloudPrinterCapabilities() {
245 var printerList = $('printer-list');
246 var selectedIndex = printerList.selectedIndex;
247 if (printerList.options[selectedIndex].capabilities) {
248 doUpdateCloudPrinterCapabilities(
249 printerList.options[selectedIndex],
250 printerList.options[selectedIndex].capabilities);
251 } else {
252 cloudprint.updatePrinterCaps(printerList.options[selectedIndex],
253 doUpdateCloudPrinterCapabilities);
254 }
255 }
256
257 function doUpdateCloudPrinterCapabilities(printer) {
258 var settings = {'disableColorOption': !cloudprint.supportsColor(printer),
259 'setColorAsDefault': cloudprint.colorIsDefault(printer),
260 'disableCopiesOption': true,
261 'disableLandscapeOption': true};
262 updateWithPrinterCapabilities(settings);
263 var printerList = $('printer-list');
264 var selectedIndex = printerList.selectedIndex;
222 lastSelectedPrinterIndex = selectedIndex; 265 lastSelectedPrinterIndex = selectedIndex;
223 266
224 // Regenerate the preview data based on selected printer settings. 267 // Regenerate the preview data based on selected printer settings.
225 setDefaultValuesAndRegeneratePreview(); 268 setDefaultValuesAndRegeneratePreview();
226 } 269 }
227 270
228 /** 271 /**
229 * Updates the controls with printer capabilities information. 272 * Updates the controls with printer capabilities information.
230 * @param {Object} settingInfo printer setting information. 273 * @param {Object} settingInfo printer setting information.
231 */ 274 */
232 function updateWithPrinterCapabilities(settingInfo) { 275 function updateWithPrinterCapabilities(settingInfo) {
233 var disableColorOption = settingInfo.disableColorOption; 276 var disableColorOption = settingInfo.disableColorOption;
234 var disableCopiesOption = settingInfo.disableCopiesOption; 277 var disableCopiesOption = settingInfo.disableCopiesOption;
235 var setColorAsDefault = settingInfo.setColorAsDefault; 278 var setColorAsDefault = settingInfo.setColorAsDefault;
279 var disableLandscapeOption = settingInfo.disableLandscapeOption;
236 var colorOption = $('color'); 280 var colorOption = $('color');
237 var bwOption = $('bw'); 281 var bwOption = $('bw');
238 282
239 if (disableCopiesOption) { 283 if (disableCopiesOption) {
240 fadeOutElement($('copies-option')); 284 fadeOutElement($('copies-option'));
241 $('hr-before-copies').classList.remove('invisible'); 285 $('hr-before-copies').classList.remove('invisible');
242 } else { 286 } else {
243 fadeInElement($('copies-option')); 287 fadeInElement($('copies-option'));
244 $('hr-before-copies').classList.add('invisible'); 288 $('hr-before-copies').classList.add('invisible');
245 } 289 }
246 290
291 if (disableLandscapeOption) {
292 fadeOutElement($('landscape-option'));
293 } else {
294 fadeInElement($('landscape-option'));
295 }
296
247 disableColorOption ? fadeOutElement($('color-options')) : 297 disableColorOption ? fadeOutElement($('color-options')) :
248 fadeInElement($('color-options')); 298 fadeInElement($('color-options'));
249 299
250 if (colorOption.checked != setColorAsDefault) { 300 if (colorOption.checked != setColorAsDefault) {
251 colorOption.checked = setColorAsDefault; 301 colorOption.checked = setColorAsDefault;
252 bwOption.checked = !setColorAsDefault; 302 bwOption.checked = !setColorAsDefault;
253 } 303 }
254 } 304 }
255 305
256 /** 306 /**
307 * Turn on the integration of Cloud Print.
308 * @param {enable} true if cloud print should be used.
dpapad 2011/06/14 01:14:46 @param {boolean} enable True if could print should
309 */
310 function setUseCloudPrint(enable, cloudPrintURL) {
311 useCloudPrint = enable;
312 cloudprint.setBaseURL(cloudPrintURL);
313 }
314
315 /**
316 * Take the PDF data handed to us and submit it to the cloud, closing the print
317 * preview tab once the upload is successful.
318 * @param {pdfData} data to send as the print job.
dpapad 2011/06/14 01:14:46 Here and elsewhere @param {string} pdfData ....
319 */
320 function printToCloud(data) {
321 cloudprint.printToCloud(data, finishedCloudPrinting);
322 }
323
324 /**
325 * Cloud print upload of the PDF file is finished, time to close the dialog.
326 */
327 function finishedCloudPrinting() {
328 window.location = cloudprint.getBaseURL();
329 }
330
331 /**
257 * Validates the copies text field value. 332 * Validates the copies text field value.
258 * NOTE: An empty copies field text is considered valid because the blur event 333 * NOTE: An empty copies field text is considered valid because the blur event
259 * listener of this field will set it back to a default value. 334 * listener of this field will set it back to a default value.
260 * @return {boolean} true if the number of copies is valid else returns false. 335 * @return {boolean} true if the number of copies is valid else returns false.
261 */ 336 */
262 function isNumberOfCopiesValid() { 337 function isNumberOfCopiesValid() {
263 var copiesFieldText = $('copies').value.replace(/\s/g, ''); 338 var copiesFieldText = $('copies').value.replace(/\s/g, '');
264 if (copiesFieldText == '') 339 if (copiesFieldText == '')
265 return true; 340 return true;
266 341
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 const LONG_EDGE = 1; 412 const LONG_EDGE = 1;
338 return !isTwoSided() ? SIMPLEX : LONG_EDGE; 413 return !isTwoSided() ? SIMPLEX : LONG_EDGE;
339 } 414 }
340 415
341 /** 416 /**
342 * Creates a JSON string based on the values in the printer settings. 417 * Creates a JSON string based on the values in the printer settings.
343 * 418 *
344 * @return {string} JSON string with print job settings. 419 * @return {string} JSON string with print job settings.
345 */ 420 */
346 function getSettingsJSON() { 421 function getSettingsJSON() {
422 var printerList = $('printer-list');
423 var selectedPrinter = printerList.selectedIndex;
347 var printAll = $('all-pages').checked; 424 var printAll = $('all-pages').checked;
348 var deviceName = getSelectedPrinterName(); 425 var deviceName = getSelectedPrinterName();
349 var printToPDF = (deviceName == PRINT_TO_PDF); 426 var printToPDF = (deviceName == PRINT_TO_PDF);
350 427
351 return JSON.stringify({'deviceName': deviceName, 428 var settings = {'deviceName': deviceName,
352 'pageRange': getSelectedPageRanges(), 429 'pageRange': getSelectedPageRanges(),
353 'printAll': printAll, 430 'printAll': printAll,
354 'duplex': getDuplexMode(), 431 'duplex': getDuplexMode(),
355 'copies': getCopies(), 432 'copies': getCopies(),
356 'collate': isCollated(), 433 'collate': isCollated(),
357 'landscape': isLandscape(), 434 'landscape': isLandscape(),
358 'color': isColor(), 435 'color': isColor(),
359 'printToPDF': printToPDF}); 436 'printToPDF': printToPDF};
437 if (printerList.options[selectedPrinter].isCloudPrint) {
438 settings['cloudPrintID'] =
439 printerList.options[selectedPrinter].value;
440 }
441
442 return JSON.stringify(settings);
360 } 443 }
361 444
362 /** 445 /**
363 * Returns the name of the selected printer or the empty string if no 446 * Returns the name of the selected printer or the empty string if no
364 * printer is selected. 447 * printer is selected.
365 */ 448 */
366 function getSelectedPrinterName() { 449 function getSelectedPrinterName() {
367 var printerList = $('printer-list') 450 var printerList = $('printer-list')
368 var selectedPrinter = printerList.selectedIndex; 451 var selectedPrinter = printerList.selectedIndex;
369 var deviceName = ''; 452 var deviceName = '';
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 * Sends a message to cancel the pending print request. 489 * Sends a message to cancel the pending print request.
407 */ 490 */
408 function cancelPendingPrintRequest() { 491 function cancelPendingPrintRequest() {
409 chrome.send('cancelPendingPrintRequest'); 492 chrome.send('cancelPendingPrintRequest');
410 } 493 }
411 494
412 /** 495 /**
413 * Sends a message to initiate print workflow. 496 * Sends a message to initiate print workflow.
414 */ 497 */
415 function sendPrintFileRequest() { 498 function sendPrintFileRequest() {
416 chrome.send('print', [getSettingsJSON()]); 499 var printerList = $('printer-list')
dpapad 2011/06/14 01:14:46 Add ; at the end.
Albert Bodenhamer 2011/06/14 19:43:54 Done.
500 var printer = printerList[printerList.selectedIndex];
501 chrome.send('print', [getSettingsJSON(),
502 cloudprint.getPrintTicketJSON(printer)]);
417 } 503 }
418 504
419 /** 505 /**
420 * Asks the browser to generate a preview PDF based on current print settings. 506 * Asks the browser to generate a preview PDF based on current print settings.
421 */ 507 */
422 function requestPrintPreview() { 508 function requestPrintPreview() {
423 hasPendingPreviewRequest = true; 509 hasPendingPreviewRequest = true;
424 removeEventListeners(); 510 removeEventListeners();
425 printSettings.save(); 511 printSettings.save();
426 showLoadingAnimation(); 512 showLoadingAnimation();
(...skipping 15 matching lines...) Expand all
442 chrome.send('getPrinters'); 528 chrome.send('getPrinters');
443 } 529 }
444 530
445 /** 531 /**
446 * Fill the printer list drop down. 532 * Fill the printer list drop down.
447 * Called from PrintPreviewHandler::SendPrinterList(). 533 * Called from PrintPreviewHandler::SendPrinterList().
448 * @param {Array} printers Array of printer info objects. 534 * @param {Array} printers Array of printer info objects.
449 */ 535 */
450 function setPrinters(printers) { 536 function setPrinters(printers) {
451 var printerList = $('printer-list'); 537 var printerList = $('printer-list');
538 if (useCloudPrint) {
539 cloudprint.fetchPrinters(addCloudPrinters);
540 if (printers.length > 0) {
541 option = addDestinationListOption('', '', false, true);
542 option = addDestinationListOption(localStrings.getString('localPrinters'),
543 '',
544 false,
545 true);
546 }
547 }
452 // If there exists a dummy printer value, then setDefaultPrinter() already 548 // If there exists a dummy printer value, then setDefaultPrinter() already
453 // requested a preview, so no need to do it again. 549 // requested a preview, so no need to do it again.
454 var needPreview = (printerList[0].value == ''); 550 var needPreview = (printerList[0].value == '');
455 for (var i = 0; i < printers.length; ++i) { 551 for (var i = 0; i < printers.length; ++i) {
456 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName); 552 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName);
457 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 553 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
458 isDefault, false); 554 isDefault, false);
459 } 555 }
460 556
461 // Remove the dummy printer added in setDefaultPrinter(). 557 // Remove the dummy printer added in setDefaultPrinter().
462 printerList.remove(0); 558 printerList.remove(0);
463 559
464 if (printers.length != 0) 560 if (printers.length != 0)
465 addDestinationListOption('', '', false, true); 561 addDestinationListOption('', '', false, true);
466 562
467 // Adding option for saving PDF to disk. 563 // Adding option for saving PDF to disk.
468 addDestinationListOption(localStrings.getString('printToPDF'), 564 addDestinationListOption(localStrings.getString('printToPDF'),
469 PRINT_TO_PDF, 565 PRINT_TO_PDF,
470 defaultOrLastUsedPrinterName == PRINT_TO_PDF, 566 defaultOrLastUsedPrinterName == PRINT_TO_PDF,
471 false); 567 false);
472 addDestinationListOption('', '', false, true); 568 addDestinationListOption('', '', false, true);
473 569
474 // Add an option to manage printers. 570 // Add options to manage printers.
475 addDestinationListOption(localStrings.getString('managePrinters'), 571 if (!cr.isChromeOS) {
476 MANAGE_PRINTERS, false, false); 572 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
573 MANAGE_LOCAL_PRINTERS, false, false);
574 }
575 if (useCloudPrint) {
576 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
577 MANAGE_CLOUD_PRINTERS, false, false);
578 }
477 579
478 printerList.disabled = false; 580 printerList.disabled = false;
479 581
480 if (needPreview) 582 if (needPreview)
481 updateControlsWithSelectedPrinterCapabilities(); 583 updateControlsWithSelectedPrinterCapabilities();
482 } 584 }
483 585
484 /** 586 /**
485 * Adds an option to the printer destination list. 587 * Adds an option to the printer destination list.
486 * @param {String} optionText specifies the option text content. 588 * @param {String} optionText specifies the option text content.
487 * @param {String} optionValue specifies the option value. 589 * @param {String} optionValue specifies the option value.
488 * @param {boolean} isDefault is true if the option needs to be selected. 590 * @param {boolean} isDefault is true if the option needs to be selected.
489 * @param {boolean} isDisabled is true if the option needs to be disabled. 591 * @param {boolean} isDisabled is true if the option needs to be disabled.
592 * returns the created option.
dpapad 2011/06/14 01:14:46 For consistency could you use the following format
Albert Bodenhamer 2011/06/14 19:43:54 Done.
490 */ 593 */
491 function addDestinationListOption(optionText, optionValue, isDefault, 594 function addDestinationListOption(optionText, optionValue, isDefault,
492 isDisabled) { 595 isDisabled) {
493 var option = document.createElement('option'); 596 var option = document.createElement('option');
494 option.textContent = optionText; 597 option.textContent = optionText;
495 option.value = optionValue; 598 option.value = optionValue;
496 $('printer-list').add(option); 599 $('printer-list').add(option);
497 option.selected = isDefault; 600 option.selected = isDefault;
498 option.disabled = isDisabled; 601 option.disabled = isDisabled;
602 return option;
499 } 603 }
500 604
501 /** 605 /**
606 * Add cloud printers to the list drop down.
607 * Called from the cloudprint object on receipt of printer information from the
608 * cloud print server.
609 */
610 function addCloudPrinters(printers) {
611 addDestinationListOption(localStrings.getString('cloudPrinters'),
612 '',
613 false,
614 true);
615 if (printers != null) {
616 var l = printers.length;
dpapad 2011/06/14 01:14:46 Nit: This makes it shorter to write but harder to
617 if (l > maxCloudPrinters) {
618 l = maxCloudPrinters;
619 }
620 for (var i = 0; i < l; i++) {
621 var option = addDestinationListOption(printers[i]['name'],
622 printers[i]['id'],
623 i == 0,
624 printers[i]['name'] == defaultOrLastUsedPrinterName);
625 option.isCloudPrint = true;
626 }
627 if (printers.length == 0) {
628 addDestinationListOption(localStrings.getString('addCloudPrinter'),
629 ADD_PRINTER, false, false);
630 }
631 if (l < printers.length) {
632 var option = addDestinationListOption('', '', false, true);
633 addDestinationListOption(localStrings.getString('morePrinters'),
634 MORE_PRINTERS, false, false);
635 }
636 } else {
637 addDestinationListOption(localStrings.getString('signIn'),
638 SIGN_IN, false, false);
639 }
640 }
641
642 /**
502 * Sets the color mode for the PDF plugin. 643 * Sets the color mode for the PDF plugin.
503 * Called from PrintPreviewHandler::ProcessColorSetting(). 644 * Called from PrintPreviewHandler::ProcessColorSetting().
504 * @param {boolean} color is true if the PDF plugin should display in color. 645 * @param {boolean} color is true if the PDF plugin should display in color.
505 */ 646 */
506 function setColor(color) { 647 function setColor(color) {
507 var pdfViewer = $('pdf-viewer'); 648 var pdfViewer = $('pdf-viewer');
508 if (!pdfViewer) { 649 if (!pdfViewer) {
509 return; 650 return;
510 } 651 }
511 pdfViewer.grayscale(!color); 652 pdfViewer.grayscale(!color);
653 var printerList = $('printer-list')
654 cloudprint.setColor(printerList[printerList.selectedIndex], color);
512 } 655 }
513 656
514 /** 657 /**
515 * Display an error message in the center of the preview area. 658 * Display an error message in the center of the preview area.
516 * @param {string} errorMessage The error message to be displayed. 659 * @param {string} errorMessage The error message to be displayed.
517 */ 660 */
518 function displayErrorMessage(errorMessage) { 661 function displayErrorMessage(errorMessage) {
519 hasError = true; 662 hasError = true;
520 $('print-button').disabled = true; 663 $('print-button').disabled = true;
521 $('overlay-layer').classList.remove('invisible'); 664 $('overlay-layer').classList.remove('invisible');
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 this.isLandscape = ''; 1200 this.isLandscape = '';
1058 } 1201 }
1059 1202
1060 /** 1203 /**
1061 * Takes a snapshot of the print settings. 1204 * Takes a snapshot of the print settings.
1062 */ 1205 */
1063 PrintSettings.prototype.save = function() { 1206 PrintSettings.prototype.save = function() {
1064 this.deviceName = getSelectedPrinterName(); 1207 this.deviceName = getSelectedPrinterName();
1065 this.isLandscape = isLandscape(); 1208 this.isLandscape = isLandscape();
1066 } 1209 }
1210
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698