| OLD | NEW |
| 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 // The previously selected layout mode. It is used in order to prevent the | 21 // The previously selected layout mode. It is used in order to prevent the |
| (...skipping 11 matching lines...) Expand all Loading... |
| 28 // Indicates whether a preview has been requested but not received yet. | 33 // Indicates whether a preview has been requested but not received yet. |
| 29 var isPreviewStillLoading = true; | 34 var isPreviewStillLoading = true; |
| 30 | 35 |
| 31 // Currently selected printer capabilities. | 36 // Currently selected printer capabilities. |
| 32 var printerCapabilities; | 37 var printerCapabilities; |
| 33 | 38 |
| 34 // Used to disable some printing options when the preview is not modifiable. | 39 // Used to disable some printing options when the preview is not modifiable. |
| 35 var previewModifiable = false; | 40 var previewModifiable = false; |
| 36 | 41 |
| 37 // Destination list special value constants. | 42 // Destination list special value constants. |
| 43 const ADD_PRINTER = 'addPrinter'; |
| 44 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters'; |
| 45 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters'; |
| 46 const MORE_PRINTERS = 'morePrinters'; |
| 47 const SIGN_IN = 'signIn'; |
| 38 const PRINT_TO_PDF = 'Print To PDF'; | 48 const PRINT_TO_PDF = 'Print To PDF'; |
| 39 const MANAGE_PRINTERS = 'Manage Printers'; | |
| 40 | 49 |
| 41 /** | 50 /** |
| 42 * Window onload handler, sets up the page and starts print preview by getting | 51 * Window onload handler, sets up the page and starts print preview by getting |
| 43 * the printer list. | 52 * the printer list. |
| 44 */ | 53 */ |
| 45 function onLoad() { | 54 function onLoad() { |
| 46 $('system-dialog-link').addEventListener('click', showSystemDialog); | 55 $('system-dialog-link').addEventListener('click', showSystemDialog); |
| 47 $('cancel-button').addEventListener('click', handleCancelButtonClick); | 56 $('cancel-button').addEventListener('click', handleCancelButtonClick); |
| 48 | 57 |
| 49 if (!checkCompatiblePluginExists()) { | 58 if (!checkCompatiblePluginExists()) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 /** | 119 /** |
| 111 * Gets the selected printer capabilities and updates the controls accordingly. | 120 * Gets the selected printer capabilities and updates the controls accordingly. |
| 112 */ | 121 */ |
| 113 function updateControlsWithSelectedPrinterCapabilities() { | 122 function updateControlsWithSelectedPrinterCapabilities() { |
| 114 var printerList = $('printer-list'); | 123 var printerList = $('printer-list'); |
| 115 var selectedIndex = printerList.selectedIndex; | 124 var selectedIndex = printerList.selectedIndex; |
| 116 if (selectedIndex < 0) | 125 if (selectedIndex < 0) |
| 117 return; | 126 return; |
| 118 | 127 |
| 119 var selectedValue = printerList.options[selectedIndex].value; | 128 var selectedValue = printerList.options[selectedIndex].value; |
| 120 if (selectedValue == PRINT_TO_PDF) { | 129 if (printerList.options[selectedIndex].isCloudPrint) { |
| 130 // TODO(abodenha@chromium.org) Retrieve the correct printer caps. |
| 131 updateWithPrinterCapabilities({}); |
| 132 return; |
| 133 } else if (selectedValue == SIGN_IN || |
| 134 selectedValue == MANAGE_CLOUD_PRINTERS || |
| 135 selectedValue == MANAGE_LOCAL_PRINTERS) { |
| 136 printerList.selectedIndex = lastSelectedPrinterIndex; |
| 137 chrome.send(selectedValue); |
| 138 return; |
| 139 } else if (selectedValue == PRINT_TO_PDF) { |
| 121 updateWithPrinterCapabilities({'disableColorOption': true, | 140 updateWithPrinterCapabilities({'disableColorOption': true, |
| 122 'setColorAsDefault': true}); | 141 'setColorAsDefault': true}); |
| 123 } else if (selectedValue == MANAGE_PRINTERS) { | |
| 124 printerList.selectedIndex = lastSelectedPrinterIndex; | |
| 125 chrome.send('managePrinters'); | |
| 126 return; | |
| 127 } else { | 142 } else { |
| 128 // This message will call back to 'updateWithPrinterCapabilities' | 143 // This message will call back to 'updateWithPrinterCapabilities' |
| 129 // function. | 144 // function. |
| 130 chrome.send('getPrinterCapabilities', [selectedValue]); | 145 chrome.send('getPrinterCapabilities', [selectedValue]); |
| 131 } | 146 } |
| 132 | 147 |
| 133 lastSelectedPrinterIndex = selectedIndex; | 148 lastSelectedPrinterIndex = selectedIndex; |
| 134 | 149 |
| 135 // Regenerate the preview data based on selected printer settings. | 150 // Regenerate the preview data based on selected printer settings. |
| 136 setDefaultValuesAndRegeneratePreview(); | 151 setDefaultValuesAndRegeneratePreview(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 157 $('color-options').classList.remove("hidden"); | 172 $('color-options').classList.remove("hidden"); |
| 158 | 173 |
| 159 if (colorOption.checked != setColorAsDefault) { | 174 if (colorOption.checked != setColorAsDefault) { |
| 160 colorOption.checked = setColorAsDefault; | 175 colorOption.checked = setColorAsDefault; |
| 161 bwOption.checked = !setColorAsDefault; | 176 bwOption.checked = !setColorAsDefault; |
| 162 setColor(colorOption.checked); | 177 setColor(colorOption.checked); |
| 163 } | 178 } |
| 164 } | 179 } |
| 165 | 180 |
| 166 /** | 181 /** |
| 182 * Turn on the integration of Cloud Print. |
| 183 * @param {enable} true if cloud print should be used. |
| 184 */ |
| 185 function setUseCloudPrint(enable, cloudPrintURL) { |
| 186 useCloudPrint = enable; |
| 187 cloudprint.setBaseURL(cloudPrintURL); |
| 188 } |
| 189 |
| 190 /** |
| 191 * Take the PDF data handed to us and submit it to the cloud, closing the print |
| 192 * preview tab once the upload is successful. |
| 193 * @param {pdfData} data to send as the print job. |
| 194 */ |
| 195 function printToCloud(data) { |
| 196 cloudprint.printToCloud(data, finishedCloudPrinting); |
| 197 } |
| 198 |
| 199 /** |
| 200 * Cloud print upload of the PDF file is finished, time to close the dialog. |
| 201 */ |
| 202 function finishedCloudPrinting() { |
| 203 chrome.send('closePrintPreviewTab'); |
| 204 } |
| 205 |
| 206 /** |
| 167 * Disables or enables all controls in the options pane except for the cancel | 207 * Disables or enables all controls in the options pane except for the cancel |
| 168 * button. | 208 * button. |
| 169 */ | 209 */ |
| 170 function setControlsDisabled(disabled) { | 210 function setControlsDisabled(disabled) { |
| 171 var elementList = $('controls').elements; | 211 var elementList = $('controls').elements; |
| 172 for (var i = 0; i < elementList.length; ++i) { | 212 for (var i = 0; i < elementList.length; ++i) { |
| 173 if (elementList[i] == $('cancel-button')) | 213 if (elementList[i] == $('cancel-button')) |
| 174 continue; | 214 continue; |
| 175 elementList[i].disabled = disabled; | 215 elementList[i].disabled = disabled; |
| 176 } | 216 } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 const LONG_EDGE = 1; | 323 const LONG_EDGE = 1; |
| 284 return !isTwoSided() ? SIMPLEX : LONG_EDGE; | 324 return !isTwoSided() ? SIMPLEX : LONG_EDGE; |
| 285 } | 325 } |
| 286 | 326 |
| 287 /** | 327 /** |
| 288 * Creates a JSON string based on the values in the printer settings. | 328 * Creates a JSON string based on the values in the printer settings. |
| 289 * | 329 * |
| 290 * @return {string} JSON string with print job settings. | 330 * @return {string} JSON string with print job settings. |
| 291 */ | 331 */ |
| 292 function getSettingsJSON() { | 332 function getSettingsJSON() { |
| 333 var printerList = $('printer-list'); |
| 334 var selectedPrinter = printerList.selectedIndex; |
| 293 var printAll = $('all-pages').checked; | 335 var printAll = $('all-pages').checked; |
| 294 var deviceName = getSelectedPrinterName(); | 336 var deviceName = getSelectedPrinterName(); |
| 295 var printToPDF = (deviceName == PRINT_TO_PDF); | 337 var printToPDF = (deviceName == PRINT_TO_PDF); |
| 296 | 338 |
| 297 return JSON.stringify({'deviceName': deviceName, | 339 var settings = {'deviceName': deviceName, |
| 298 'pageRange': getSelectedPageRanges(), | 340 'pageRange': getSelectedPageRanges(), |
| 299 'printAll': printAll, | 341 'printAll': printAll, |
| 300 'duplex': getDuplexMode(), | 342 'duplex': getDuplexMode(), |
| 301 'copies': getCopies(), | 343 'copies': getCopies(), |
| 302 'collate': isCollated(), | 344 'collate': isCollated(), |
| 303 'landscape': isLandscape(), | 345 'landscape': isLandscape(), |
| 304 'color': isColor(), | 346 'color': isColor(), |
| 305 'printToPDF': printToPDF}); | 347 'printToPDF': printToPDF}; |
| 348 if (printerList.options[selectedPrinter].isCloudPrint) { |
| 349 settings['cloudPrintID'] = |
| 350 printerList.options[selectedPrinter].value; |
| 351 } |
| 352 |
| 353 return JSON.stringify(settings); |
| 306 } | 354 } |
| 307 | 355 |
| 308 /** | 356 /** |
| 309 * Returns the name of the selected printer or the empty string if no | 357 * Returns the name of the selected printer or the empty string if no |
| 310 * printer is selected. | 358 * printer is selected. |
| 311 */ | 359 */ |
| 312 function getSelectedPrinterName() { | 360 function getSelectedPrinterName() { |
| 313 var printerList = $('printer-list') | 361 var printerList = $('printer-list') |
| 314 var selectedPrinter = printerList.selectedIndex; | 362 var selectedPrinter = printerList.selectedIndex; |
| 315 var deviceName = ''; | 363 var deviceName = ''; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 344 } | 392 } |
| 345 | 393 |
| 346 /** | 394 /** |
| 347 * Fill the printer list drop down. | 395 * Fill the printer list drop down. |
| 348 * Called from PrintPreviewHandler::SendPrinterList(). | 396 * Called from PrintPreviewHandler::SendPrinterList(). |
| 349 * @param {Array} printers Array of printer info objects. | 397 * @param {Array} printers Array of printer info objects. |
| 350 * @param {number} defaultPrinterIndex The index of the default printer. | 398 * @param {number} defaultPrinterIndex The index of the default printer. |
| 351 */ | 399 */ |
| 352 function setPrinters(printers, defaultPrinterIndex) { | 400 function setPrinters(printers, defaultPrinterIndex) { |
| 353 var printerList = $('printer-list'); | 401 var printerList = $('printer-list'); |
| 402 if (useCloudPrint) { |
| 403 displayErrorMessage(localStrings.getString('loadingPrinters')); |
| 404 cloudprint.fetchPrinters(addCloudPrinters); |
| 405 if (printers.length > 0) { |
| 406 option = addDestinationListOption('', '', false, true); |
| 407 option = addDestinationListOption(localStrings.getString('localPrinters'), |
| 408 '', |
| 409 false, |
| 410 true); |
| 411 } |
| 412 } |
| 354 for (var i = 0; i < printers.length; ++i) { | 413 for (var i = 0; i < printers.length; ++i) { |
| 355 addDestinationListOption(printers[i].printerName, printers[i].deviceName, | 414 addDestinationListOption(printers[i].printerName, printers[i].deviceName, |
| 356 i == defaultPrinterIndex, false); | 415 i == defaultPrinterIndex, false); |
| 357 } | 416 } |
| 358 | 417 |
| 359 if (printers.length != 0) | 418 if (printers.length != 0) |
| 360 addDestinationListOption('', '', false, true); | 419 addDestinationListOption('', '', false, true); |
| 361 | 420 |
| 362 // Adding option for saving PDF to disk. | 421 // Adding option for saving PDF to disk. |
| 363 addDestinationListOption(localStrings.getString('printToPDF'), | 422 addDestinationListOption(localStrings.getString('printToPDF'), |
| 364 PRINT_TO_PDF, false, false); | 423 PRINT_TO_PDF, false, false); |
| 365 addDestinationListOption('', '', false, true); | 424 addDestinationListOption('', '', false, true); |
| 366 | 425 |
| 367 // Add an option to manage printers. | 426 // Add options to manage printers. |
| 368 addDestinationListOption(localStrings.getString('managePrinters'), | 427 if (!cr.isChromeOS) { |
| 369 MANAGE_PRINTERS, false, false); | 428 addDestinationListOption(localStrings.getString('manageLocalPrinters'), |
| 429 MANAGE_LOCAL_PRINTERS, false, false); |
| 430 } |
| 431 if (useCloudPrint) { |
| 432 addDestinationListOption(localStrings.getString('manageCloudPrinters'), |
| 433 MANAGE_CLOUD_PRINTERS, false, false); |
| 434 } |
| 370 | 435 |
| 371 printerList.disabled = false; | 436 printerList.disabled = false; |
| 372 updateControlsWithSelectedPrinterCapabilities(); | 437 updateControlsWithSelectedPrinterCapabilities(); |
| 373 } | 438 } |
| 374 | 439 |
| 375 /** | 440 /** |
| 376 * Adds an option to the printer destination list. | 441 * Adds an option to the printer destination list. |
| 377 * @param {String} optionText specifies the option text content. | 442 * @param {String} optionText specifies the option text content. |
| 378 * @param {String} optionValue specifies the option value. | 443 * @param {String} optionValue specifies the option value. |
| 379 * @param {boolean} isDefault is true if the option needs to be selected. | 444 * @param {boolean} isDefault is true if the option needs to be selected. |
| 380 * @param {boolean} isDisabled is true if the option needs to be disabled. | 445 * @param {boolean} isDisabled is true if the option needs to be disabled. |
| 381 */ | 446 */ |
| 382 function addDestinationListOption(optionText, optionValue, isDefault, | 447 function addDestinationListOption(optionText, optionValue, isDefault, |
| 383 isDisabled) { | 448 isDisabled) { |
| 384 var option = document.createElement('option'); | 449 var option = document.createElement('option'); |
| 385 option.textContent = optionText; | 450 option.textContent = optionText; |
| 386 option.value = optionValue; | 451 option.value = optionValue; |
| 387 $('printer-list').add(option); | 452 $('printer-list').add(option); |
| 388 option.selected = isDefault; | 453 option.selected = isDefault; |
| 389 option.disabled = isDisabled; | 454 option.disabled = isDisabled; |
| 455 return option; |
| 390 } | 456 } |
| 391 | 457 |
| 392 /** | 458 /** |
| 459 * Add cloud printers to the list drop down. |
| 460 * Called from the cloudprint object on receipt of printer information from the |
| 461 * cloud print server. |
| 462 */ |
| 463 function addCloudPrinters(printers) { |
| 464 addDestinationListOption(localStrings.getString('cloudPrinters'), |
| 465 '', |
| 466 false, |
| 467 true); |
| 468 if (printers != null) { |
| 469 var l = printers.length; |
| 470 if (l > maxCloudPrinters) { |
| 471 l = maxCloudPrinters; |
| 472 } |
| 473 for (var i = 0; i < l; i++) { |
| 474 var option = addDestinationListOption(printers[i]['name'], |
| 475 printers[i]['id'], |
| 476 i == 0, |
| 477 false); |
| 478 option.isCloudPrint = true; |
| 479 } |
| 480 if (printers.length == 0) { |
| 481 addDestinationListOption(localStrings.getString('addCloudPrinter'), |
| 482 ADD_PRINTER, false, false); |
| 483 } |
| 484 if (l < printers.length) { |
| 485 var option = addDestinationListOption('', '', false, true); |
| 486 addDestinationListOption(localStrings.getString('morePrinters'), |
| 487 MORE_PRINTERS, false, false); |
| 488 } |
| 489 } else { |
| 490 addDestinationListOption(localStrings.getString('signIn'), |
| 491 SIGN_IN, false, false); |
| 492 } |
| 493 } |
| 494 |
| 495 /** |
| 393 * Sets the color mode for the PDF plugin. | 496 * Sets the color mode for the PDF plugin. |
| 394 * Called from PrintPreviewHandler::ProcessColorSetting(). | 497 * Called from PrintPreviewHandler::ProcessColorSetting(). |
| 395 * @param {boolean} color is true if the PDF plugin should display in color. | 498 * @param {boolean} color is true if the PDF plugin should display in color. |
| 396 */ | 499 */ |
| 397 function setColor(color) { | 500 function setColor(color) { |
| 398 var pdfViewer = $('pdf-viewer'); | 501 var pdfViewer = $('pdf-viewer'); |
| 399 if (!pdfViewer) { | 502 if (!pdfViewer) { |
| 400 return; | 503 return; |
| 401 } | 504 } |
| 402 pdfViewer.grayscale(!color); | 505 pdfViewer.grayscale(!color); |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 919 if (!isNumberOfCopiesValid()) | 1022 if (!isNumberOfCopiesValid()) |
| 920 copiesField.value = 1; | 1023 copiesField.value = 1; |
| 921 else { | 1024 else { |
| 922 var newValue = getCopies() + sign * 1; | 1025 var newValue = getCopies() + sign * 1; |
| 923 if (newValue < copiesField.min || newValue > copiesField.max) | 1026 if (newValue < copiesField.min || newValue > copiesField.max) |
| 924 return; | 1027 return; |
| 925 copiesField.value = newValue; | 1028 copiesField.value = newValue; |
| 926 } | 1029 } |
| 927 copiesFieldChanged(); | 1030 copiesFieldChanged(); |
| 928 } | 1031 } |
| 929 | |
| OLD | NEW |