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

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: Review feedback 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 // 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 202 }
194 203
195 /** 204 /**
196 * Gets the selected printer capabilities and updates the controls accordingly. 205 * Gets the selected printer capabilities and updates the controls accordingly.
197 */ 206 */
198 function updateControlsWithSelectedPrinterCapabilities() { 207 function updateControlsWithSelectedPrinterCapabilities() {
199 var printerList = $('printer-list'); 208 var printerList = $('printer-list');
200 var selectedIndex = printerList.selectedIndex; 209 var selectedIndex = printerList.selectedIndex;
201 if (selectedIndex < 0) 210 if (selectedIndex < 0)
202 return; 211 return;
203 212 var skip_refresh = false;
204 var selectedValue = printerList.options[selectedIndex].value; 213 var selectedValue = printerList.options[selectedIndex].value;
205 if (selectedValue == PRINT_TO_PDF) { 214 if (printerList.options[selectedIndex].isCloudPrint) {
215 updateWithCloudPrinterCapabilities();
216 skip_refresh = true;
217 } else if (selectedValue == SIGN_IN ||
218 selectedValue == MANAGE_CLOUD_PRINTERS ||
219 selectedValue == MANAGE_LOCAL_PRINTERS) {
220 printerList.selectedIndex = lastSelectedPrinterIndex;
221 chrome.send(selectedValue);
222 return;
sanjeevr 2011/06/15 20:24:36 You forgot setting skip_refresh to true here inste
223 } else if (selectedValue == PRINT_TO_PDF) {
206 updateWithPrinterCapabilities({'disableColorOption': true, 224 updateWithPrinterCapabilities({'disableColorOption': true,
207 'setColorAsDefault': true, 225 'setColorAsDefault': true,
208 'disableCopiesOption': true}); 226 'disableCopiesOption': true,
209 } else if (selectedValue == MANAGE_PRINTERS) { 227 'disableLandscapeOption': false});
210 printerList.selectedIndex = lastSelectedPrinterIndex;
211 chrome.send('managePrinters');
212 return;
213 } else { 228 } else {
214 // This message will call back to 'updateWithPrinterCapabilities' 229 // This message will call back to 'updateWithPrinterCapabilities'
215 // function. 230 // function.
216 chrome.send('getPrinterCapabilities', [selectedValue]); 231 chrome.send('getPrinterCapabilities', [selectedValue]);
217 } 232 }
233 if (!skip_refresh) {
234 lastSelectedPrinterIndex = selectedIndex;
218 235
236 // Regenerate the preview data based on selected printer settings.
237 setDefaultValuesAndRegeneratePreview();
238 }
239 }
240
241 function updateWithCloudPrinterCapabilities() {
242 var printerList = $('printer-list');
243 var selectedIndex = printerList.selectedIndex;
244 if (printerList.options[selectedIndex].capabilities) {
245 doUpdateCloudPrinterCapabilities(
246 printerList.options[selectedIndex],
247 printerList.options[selectedIndex].capabilities);
248 } else {
249 cloudprint.updatePrinterCaps(printerList.options[selectedIndex],
250 doUpdateCloudPrinterCapabilities);
251 }
252 }
253
254 function doUpdateCloudPrinterCapabilities(printer) {
255 var settings = {'disableColorOption': !cloudprint.supportsColor(printer),
256 'setColorAsDefault': cloudprint.colorIsDefault(printer),
257 'disableCopiesOption': true,
258 'disableLandscapeOption': true};
259 updateWithPrinterCapabilities(settings);
260 var printerList = $('printer-list');
261 var selectedIndex = printerList.selectedIndex;
219 lastSelectedPrinterIndex = selectedIndex; 262 lastSelectedPrinterIndex = selectedIndex;
220 263
221 // Regenerate the preview data based on selected printer settings. 264 // Regenerate the preview data based on selected printer settings.
222 setDefaultValuesAndRegeneratePreview(); 265 setDefaultValuesAndRegeneratePreview();
223 } 266 }
224 267
225 /** 268 /**
226 * Updates the controls with printer capabilities information. 269 * Updates the controls with printer capabilities information.
227 * @param {Object} settingInfo printer setting information. 270 * @param {Object} settingInfo printer setting information.
228 */ 271 */
229 function updateWithPrinterCapabilities(settingInfo) { 272 function updateWithPrinterCapabilities(settingInfo) {
230 var disableColorOption = settingInfo.disableColorOption; 273 var disableColorOption = settingInfo.disableColorOption;
231 var disableCopiesOption = settingInfo.disableCopiesOption; 274 var disableCopiesOption = settingInfo.disableCopiesOption;
232 var setColorAsDefault = settingInfo.setColorAsDefault; 275 var setColorAsDefault = settingInfo.setColorAsDefault;
276 var disableLandscapeOption = settingInfo.disableLandscapeOption;
233 var colorOption = $('color'); 277 var colorOption = $('color');
234 var bwOption = $('bw'); 278 var bwOption = $('bw');
235 279
236 if (disableCopiesOption) { 280 if (disableCopiesOption) {
237 fadeOutElement($('copies-option')); 281 fadeOutElement($('copies-option'));
238 $('hr-before-copies').classList.remove('invisible'); 282 $('hr-before-copies').classList.remove('invisible');
239 } else { 283 } else {
240 fadeInElement($('copies-option')); 284 fadeInElement($('copies-option'));
241 $('hr-before-copies').classList.add('invisible'); 285 $('hr-before-copies').classList.add('invisible');
242 } 286 }
243 287
288 if (disableLandscapeOption) {
289 fadeOutElement($('landscape-option'));
290 } else {
291 fadeInElement($('landscape-option'));
292 }
293
244 disableColorOption ? fadeOutElement($('color-options')) : 294 disableColorOption ? fadeOutElement($('color-options')) :
245 fadeInElement($('color-options')); 295 fadeInElement($('color-options'));
246 296
247 if (colorOption.checked != setColorAsDefault) { 297 if (colorOption.checked != setColorAsDefault) {
248 colorOption.checked = setColorAsDefault; 298 colorOption.checked = setColorAsDefault;
249 bwOption.checked = !setColorAsDefault; 299 bwOption.checked = !setColorAsDefault;
250 } 300 }
251 } 301 }
252 302
253 /** 303 /**
304 * Turn on the integration of Cloud Print.
305 * @param {enable} true if cloud print should be used.
306 */
307 function setUseCloudPrint(enable, cloudPrintURL) {
308 useCloudPrint = enable;
309 cloudprint.setBaseURL(cloudPrintURL);
310 }
311
312 /**
313 * Take the PDF data handed to us and submit it to the cloud, closing the print
314 * preview tab once the upload is successful.
315 * @param {pdfData} data to send as the print job.
316 */
317 function printToCloud(data) {
318 cloudprint.printToCloud(data, finishedCloudPrinting);
319 }
320
321 /**
322 * Cloud print upload of the PDF file is finished, time to close the dialog.
323 */
324 function finishedCloudPrinting() {
325 window.location = cloudprint.getBaseURL();
326 }
327
328 /**
254 * Validates the copies text field value. 329 * Validates the copies text field value.
255 * NOTE: An empty copies field text is considered valid because the blur event 330 * NOTE: An empty copies field text is considered valid because the blur event
256 * listener of this field will set it back to a default value. 331 * listener of this field will set it back to a default value.
257 * @return {boolean} true if the number of copies is valid else returns false. 332 * @return {boolean} true if the number of copies is valid else returns false.
258 */ 333 */
259 function isNumberOfCopiesValid() { 334 function isNumberOfCopiesValid() {
260 var copiesFieldText = $('copies').value.replace(/\s/g, ''); 335 var copiesFieldText = $('copies').value.replace(/\s/g, '');
261 if (copiesFieldText == '') 336 if (copiesFieldText == '')
262 return true; 337 return true;
263 338
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 const LONG_EDGE = 1; 409 const LONG_EDGE = 1;
335 return !isTwoSided() ? SIMPLEX : LONG_EDGE; 410 return !isTwoSided() ? SIMPLEX : LONG_EDGE;
336 } 411 }
337 412
338 /** 413 /**
339 * Creates a JSON string based on the values in the printer settings. 414 * Creates a JSON string based on the values in the printer settings.
340 * 415 *
341 * @return {string} JSON string with print job settings. 416 * @return {string} JSON string with print job settings.
342 */ 417 */
343 function getSettingsJSON() { 418 function getSettingsJSON() {
419 var printerList = $('printer-list');
420 var selectedPrinter = printerList.selectedIndex;
344 var printAll = $('all-pages').checked; 421 var printAll = $('all-pages').checked;
345 var deviceName = getSelectedPrinterName(); 422 var deviceName = getSelectedPrinterName();
346 var printToPDF = (deviceName == PRINT_TO_PDF); 423 var printToPDF = (deviceName == PRINT_TO_PDF);
347 424
348 return JSON.stringify({'deviceName': deviceName, 425 var settings = {'deviceName': deviceName,
349 'pageRange': getSelectedPageRanges(), 426 'pageRange': getSelectedPageRanges(),
350 'printAll': printAll, 427 'printAll': printAll,
351 'duplex': getDuplexMode(), 428 'duplex': getDuplexMode(),
352 'copies': getCopies(), 429 'copies': getCopies(),
353 'collate': isCollated(), 430 'collate': isCollated(),
354 'landscape': isLandscape(), 431 'landscape': isLandscape(),
355 'color': isColor(), 432 'color': isColor(),
356 'printToPDF': printToPDF}); 433 'printToPDF': printToPDF};
434 if (printerList.options[selectedPrinter].isCloudPrint) {
435 settings['cloudPrintID'] =
436 printerList.options[selectedPrinter].value;
437 }
438
439 return JSON.stringify(settings);
357 } 440 }
358 441
359 /** 442 /**
360 * Returns the name of the selected printer or the empty string if no 443 * Returns the name of the selected printer or the empty string if no
361 * printer is selected. 444 * printer is selected.
362 */ 445 */
363 function getSelectedPrinterName() { 446 function getSelectedPrinterName() {
364 var printerList = $('printer-list') 447 var printerList = $('printer-list')
365 var selectedPrinter = printerList.selectedIndex; 448 var selectedPrinter = printerList.selectedIndex;
366 var deviceName = ''; 449 var deviceName = '';
(...skipping 12 matching lines...) Expand all
379 if (getSelectedPrinterName() != PRINT_TO_PDF) 462 if (getSelectedPrinterName() != PRINT_TO_PDF)
380 chrome.send('hidePreview'); 463 chrome.send('hidePreview');
381 return; 464 return;
382 } 465 }
383 466
384 if (getSelectedPrinterName() != PRINT_TO_PDF) { 467 if (getSelectedPrinterName() != PRINT_TO_PDF) {
385 $('print-button').classList.add('loading'); 468 $('print-button').classList.add('loading');
386 $('cancel-button').classList.add('loading'); 469 $('cancel-button').classList.add('loading');
387 $('print-summary').innerHTML = localStrings.getString('printing'); 470 $('print-summary').innerHTML = localStrings.getString('printing');
388 removeEventListeners(); 471 removeEventListeners();
389 window.setTimeout(function() { chrome.send('print', [getSettingsJSON()]); }, 472 var printerList = $('printer-list')
390 1000); 473 var printer = printerList[printerList.selectedIndex];
474 window.setTimeout(function() { chrome.send('print',
475 [getSettingsJSON(), cloudprint.getPrintTicketJSON(printer)]); }, 1000);
391 } else { 476 } else {
392 chrome.send('print', [getSettingsJSON()]); 477 chrome.send('print', [getSettingsJSON(), null]);
393 } 478 }
394 } 479 }
395 480
396 /** 481 /**
397 * Asks the browser to generate a preview PDF based on current print settings. 482 * Asks the browser to generate a preview PDF based on current print settings.
398 */ 483 */
399 function requestPrintPreview() { 484 function requestPrintPreview() {
400 hasPendingPreviewRequest = true; 485 hasPendingPreviewRequest = true;
401 removeEventListeners(); 486 removeEventListeners();
402 printSettings.save(); 487 printSettings.save();
(...skipping 16 matching lines...) Expand all
419 chrome.send('getPrinters'); 504 chrome.send('getPrinters');
420 } 505 }
421 506
422 /** 507 /**
423 * Fill the printer list drop down. 508 * Fill the printer list drop down.
424 * Called from PrintPreviewHandler::SendPrinterList(). 509 * Called from PrintPreviewHandler::SendPrinterList().
425 * @param {Array} printers Array of printer info objects. 510 * @param {Array} printers Array of printer info objects.
426 */ 511 */
427 function setPrinters(printers) { 512 function setPrinters(printers) {
428 var printerList = $('printer-list'); 513 var printerList = $('printer-list');
514 if (useCloudPrint) {
515 cloudprint.fetchPrinters(addCloudPrinters);
516 if (printers.length > 0) {
517 option = addDestinationListOption('', '', false, true);
518 option = addDestinationListOption(localStrings.getString('localPrinters'),
519 '',
520 false,
521 true);
522 }
523 }
429 // If there exists a dummy printer value, then setDefaultPrinter() already 524 // If there exists a dummy printer value, then setDefaultPrinter() already
430 // requested a preview, so no need to do it again. 525 // requested a preview, so no need to do it again.
431 var needPreview = (printerList[0].value == ''); 526 var needPreview = (printerList[0].value == '');
432 for (var i = 0; i < printers.length; ++i) { 527 for (var i = 0; i < printers.length; ++i) {
433 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName); 528 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName);
434 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 529 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
435 isDefault, false); 530 isDefault, false);
436 } 531 }
437 532
438 // Remove the dummy printer added in setDefaultPrinter(). 533 // Remove the dummy printer added in setDefaultPrinter().
439 printerList.remove(0); 534 printerList.remove(0);
440 535
441 if (printers.length != 0) 536 if (printers.length != 0)
442 addDestinationListOption('', '', false, true); 537 addDestinationListOption('', '', false, true);
443 538
444 // Adding option for saving PDF to disk. 539 // Adding option for saving PDF to disk.
445 addDestinationListOption(localStrings.getString('printToPDF'), 540 addDestinationListOption(localStrings.getString('printToPDF'),
446 PRINT_TO_PDF, 541 PRINT_TO_PDF,
447 defaultOrLastUsedPrinterName == PRINT_TO_PDF, 542 defaultOrLastUsedPrinterName == PRINT_TO_PDF,
448 false); 543 false);
449 addDestinationListOption('', '', false, true); 544 addDestinationListOption('', '', false, true);
450 545
451 // Add an option to manage printers. 546 // Add options to manage printers.
452 addDestinationListOption(localStrings.getString('managePrinters'), 547 if (!cr.isChromeOS) {
453 MANAGE_PRINTERS, false, false); 548 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
549 MANAGE_LOCAL_PRINTERS, false, false);
550 }
551 if (useCloudPrint) {
552 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
553 MANAGE_CLOUD_PRINTERS, false, false);
554 }
454 555
455 printerList.disabled = false; 556 printerList.disabled = false;
456 557
457 if (needPreview) 558 if (needPreview)
458 updateControlsWithSelectedPrinterCapabilities(); 559 updateControlsWithSelectedPrinterCapabilities();
459 } 560 }
460 561
461 /** 562 /**
462 * Adds an option to the printer destination list. 563 * Adds an option to the printer destination list.
463 * @param {String} optionText specifies the option text content. 564 * @param {String} optionText specifies the option text content.
464 * @param {String} optionValue specifies the option value. 565 * @param {String} optionValue specifies the option value.
465 * @param {boolean} isDefault is true if the option needs to be selected. 566 * @param {boolean} isDefault is true if the option needs to be selected.
466 * @param {boolean} isDisabled is true if the option needs to be disabled. 567 * @param {boolean} isDisabled is true if the option needs to be disabled.
568 * returns the created option.
467 */ 569 */
468 function addDestinationListOption(optionText, optionValue, isDefault, 570 function addDestinationListOption(optionText, optionValue, isDefault,
469 isDisabled) { 571 isDisabled) {
470 var option = document.createElement('option'); 572 var option = document.createElement('option');
471 option.textContent = optionText; 573 option.textContent = optionText;
472 option.value = optionValue; 574 option.value = optionValue;
473 $('printer-list').add(option); 575 $('printer-list').add(option);
474 option.selected = isDefault; 576 option.selected = isDefault;
475 option.disabled = isDisabled; 577 option.disabled = isDisabled;
578 return option;
476 } 579 }
477 580
478 /** 581 /**
582 * Add cloud printers to the list drop down.
583 * Called from the cloudprint object on receipt of printer information from the
584 * cloud print server.
585 */
586 function addCloudPrinters(printers) {
587 addDestinationListOption(localStrings.getString('cloudPrinters'),
588 '',
589 false,
590 true);
591 if (printers != null) {
592 var l = printers.length;
593 if (l > maxCloudPrinters) {
594 l = maxCloudPrinters;
595 }
596 for (var i = 0; i < l; i++) {
597 var option = addDestinationListOption(printers[i]['name'],
598 printers[i]['id'],
599 i == 0,
600 printers[i]['name'] == defaultOrLastUsedPrinterName);
601 option.isCloudPrint = true;
602 }
603 if (printers.length == 0) {
604 addDestinationListOption(localStrings.getString('addCloudPrinter'),
605 ADD_PRINTER, false, false);
606 }
607 if (l < printers.length) {
608 var option = addDestinationListOption('', '', false, true);
609 addDestinationListOption(localStrings.getString('morePrinters'),
610 MORE_PRINTERS, false, false);
611 }
612 } else {
613 addDestinationListOption(localStrings.getString('signIn'),
614 SIGN_IN, false, false);
615 }
616 }
617
618 /**
479 * Sets the color mode for the PDF plugin. 619 * Sets the color mode for the PDF plugin.
480 * Called from PrintPreviewHandler::ProcessColorSetting(). 620 * Called from PrintPreviewHandler::ProcessColorSetting().
481 * @param {boolean} color is true if the PDF plugin should display in color. 621 * @param {boolean} color is true if the PDF plugin should display in color.
482 */ 622 */
483 function setColor(color) { 623 function setColor(color) {
484 var pdfViewer = $('pdf-viewer'); 624 var pdfViewer = $('pdf-viewer');
485 if (!pdfViewer) { 625 if (!pdfViewer) {
486 return; 626 return;
487 } 627 }
488 pdfViewer.grayscale(!color); 628 pdfViewer.grayscale(!color);
629 var printerList = $('printer-list')
630 cloudprint.setColor(printerList[printerList.selectedIndex], color);
489 } 631 }
490 632
491 /** 633 /**
492 * Display an error message in the center of the preview area. 634 * Display an error message in the center of the preview area.
493 * @param {string} errorMessage The error message to be displayed. 635 * @param {string} errorMessage The error message to be displayed.
494 */ 636 */
495 function displayErrorMessage(errorMessage) { 637 function displayErrorMessage(errorMessage) {
496 hasError = true; 638 hasError = true;
497 $('print-button').disabled = true; 639 $('print-button').disabled = true;
498 $('overlay-layer').classList.remove('invisible'); 640 $('overlay-layer').classList.remove('invisible');
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 this.isLandscape = ''; 1173 this.isLandscape = '';
1032 } 1174 }
1033 1175
1034 /** 1176 /**
1035 * Takes a snapshot of the print settings. 1177 * Takes a snapshot of the print settings.
1036 */ 1178 */
1037 PrintSettings.prototype.save = function() { 1179 PrintSettings.prototype.save = function() {
1038 this.deviceName = getSelectedPrinterName(); 1180 this.deviceName = getSelectedPrinterName();
1039 this.isLandscape = isLandscape(); 1181 this.isLandscape = isLandscape();
1040 } 1182 }
1183
OLDNEW
« no previous file with comments | « chrome/browser/resources/print_preview.html ('k') | chrome/browser/resources/print_preview_cloud.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698