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

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

Issue 7038028: Initial support for cloudprint in print preview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Resolved new conflictswq Created 9 years, 5 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 // If useCloudPrint is true we attempt to connect to cloud print
10 // and populate the list of printers with cloud print printers.
11 var useCloudPrint = false;
12
7 // Store the last selected printer index. 13 // Store the last selected printer index.
8 var lastSelectedPrinterIndex = 0; 14 var lastSelectedPrinterIndex = 0;
9 15
10 // Used to disable some printing options when the preview is not modifiable. 16 // Used to disable some printing options when the preview is not modifiable.
11 var previewModifiable = false; 17 var previewModifiable = false;
12 18
13 // Destination list special value constants. 19 // Destination list special value constants.
20 const ADD_CLOUD_PRINTER = 'addCloudPrinter';
21 const MANAGE_CLOUD_PRINTERS = 'manageCloudPrinters';
22 const MANAGE_LOCAL_PRINTERS = 'manageLocalPrinters';
23 const MORE_PRINTERS = 'morePrinters';
24 const SIGN_IN = 'signIn';
14 const PRINT_TO_PDF = 'Print To PDF'; 25 const PRINT_TO_PDF = 'Print To PDF';
15 const MANAGE_PRINTERS = 'Manage Printers';
16 26
17 // State of the print preview settings. 27 // State of the print preview settings.
18 var printSettings = new PrintSettings(); 28 var printSettings = new PrintSettings();
19 29
20 // The name of the default or last used printer. 30 // The name of the default or last used printer.
21 var defaultOrLastUsedPrinterName = ''; 31 var defaultOrLastUsedPrinterName = '';
22 32
23 // True when a pending print preview request exists. 33 // True when a pending print preview request exists.
24 var hasPendingPreviewRequest = false; 34 var hasPendingPreviewRequest = false;
25 35
26 // True when a pending print file request exists. 36 // True when a pending print file request exists.
27 var hasPendingPrintFileRequest = false; 37 var hasPendingPrintFileRequest = false;
28 38
29 // True when preview tab is hidden. 39 // True when preview tab is hidden.
30 var isTabHidden = false; 40 var isTabHidden = false;
31 41
32 // Object holding all the pages related settings. 42 // Object holding all the pages related settings.
33 var pageSettings; 43 var pageSettings;
34 44
35 // Object holding all the copies related settings. 45 // Object holding all the copies related settings.
36 var copiesSettings; 46 var copiesSettings;
37 47
38 // True if the user has click 'Advanced...' in order to open the system print 48 // True if the user has click 'Advanced...' in order to open the system print
39 // dialog. 49 // dialog.
40 var showingSystemDialog = false; 50 var showingSystemDialog = false;
41 51
52 // The range of options in the printer dropdown controlled by cloud print.
53 var firstCloudPrintOptionPos = 0
54 var lastCloudPrintOptionPos = firstCloudPrintOptionPos;
55
42 /** 56 /**
43 * Window onload handler, sets up the page and starts print preview by getting 57 * Window onload handler, sets up the page and starts print preview by getting
44 * the printer list. 58 * the printer list.
45 */ 59 */
46 function onLoad() { 60 function onLoad() {
47 cr.enablePlatformSpecificCSSRules(); 61 cr.enablePlatformSpecificCSSRules();
48 62
49 $('cancel-button').addEventListener('click', handleCancelButtonClick); 63 $('cancel-button').addEventListener('click', handleCancelButtonClick);
50 64
51 if (!checkCompatiblePluginExists()) { 65 if (!checkCompatiblePluginExists()) {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 } 169 }
156 170
157 /** 171 /**
158 * Gets the selected printer capabilities and updates the controls accordingly. 172 * Gets the selected printer capabilities and updates the controls accordingly.
159 */ 173 */
160 function updateControlsWithSelectedPrinterCapabilities() { 174 function updateControlsWithSelectedPrinterCapabilities() {
161 var printerList = $('printer-list'); 175 var printerList = $('printer-list');
162 var selectedIndex = printerList.selectedIndex; 176 var selectedIndex = printerList.selectedIndex;
163 if (selectedIndex < 0) 177 if (selectedIndex < 0)
164 return; 178 return;
165 179 var skip_refresh = false;
166 var selectedValue = printerList.options[selectedIndex].value; 180 var selectedValue = printerList.options[selectedIndex].value;
167 if (selectedValue == PRINT_TO_PDF) { 181 if (cloudprint.isCloudPrint(printerList.options[selectedIndex])) {
182 updateWithCloudPrinterCapabilities();
183 skip_refresh = true;
184 } else if (selectedValue == SIGN_IN ||
185 selectedValue == MANAGE_CLOUD_PRINTERS ||
186 selectedValue == MANAGE_LOCAL_PRINTERS ||
187 selectedValue == ADD_CLOUD_PRINTER) {
188 printerList.selectedIndex = lastSelectedPrinterIndex;
189 chrome.send(selectedValue);
190 skip_refresh = true;
191 } else if (selectedValue == PRINT_TO_PDF) {
168 updateWithPrinterCapabilities({ 192 updateWithPrinterCapabilities({
169 'disableColorOption': true, 193 'disableColorOption': true,
170 'setColorAsDefault': true, 194 'setColorAsDefault': true,
171 'setDuplexAsDefault': false, 195 'setDuplexAsDefault': false,
172 'disableCopiesOption': true}); 196 'disableCopiesOption': true});
173 } else if (selectedValue == MANAGE_PRINTERS) {
174 printerList.selectedIndex = lastSelectedPrinterIndex;
175 chrome.send('managePrinters');
176 return;
177 } else { 197 } else {
178 // This message will call back to 'updateWithPrinterCapabilities' 198 // This message will call back to 'updateWithPrinterCapabilities'
179 // function. 199 // function.
180 chrome.send('getPrinterCapabilities', [selectedValue]); 200 chrome.send('getPrinterCapabilities', [selectedValue]);
181 } 201 }
202 if (!skip_refresh) {
203 lastSelectedPrinterIndex = selectedIndex;
182 204
205 // Regenerate the preview data based on selected printer settings.
206 setDefaultValuesAndRegeneratePreview();
207 }
208 }
209
210 /**
211 * Updates the printer capabilities for the currently selected
212 * cloud print printer.
213 */
214 function updateWithCloudPrinterCapabilities() {
215 var printerList = $('printer-list');
216 var selectedIndex = printerList.selectedIndex;
217 cloudprint.updatePrinterCaps(printerList.options[selectedIndex],
218 doUpdateCloudPrinterCapabilities);
219 }
220
221 /**
222 * Helper function to do the actual work of updating cloud printer
223 * capabilities.
224 * @param {Object} printer The printer object to set capabilities for.
225 */
226 function doUpdateCloudPrinterCapabilities(printer) {
227 var settings = {'disableColorOption': !cloudprint.supportsColor(printer),
228 'setColorAsDefault': cloudprint.colorIsDefault(printer),
229 'disableCopiesOption': true,
230 'disableLandscapeOption': true};
231 updateWithPrinterCapabilities(settings);
232 var printerList = $('printer-list');
233 var selectedIndex = printerList.selectedIndex;
183 lastSelectedPrinterIndex = selectedIndex; 234 lastSelectedPrinterIndex = selectedIndex;
184 235
185 // Regenerate the preview data based on selected printer settings. 236 // Regenerate the preview data based on selected printer settings.
186 setDefaultValuesAndRegeneratePreview(); 237 setDefaultValuesAndRegeneratePreview();
187 } 238 }
188 239
189 /** 240 /**
190 * Updates the controls with printer capabilities information. 241 * Updates the controls with printer capabilities information.
191 * @param {Object} settingInfo printer setting information. 242 * @param {Object} settingInfo printer setting information.
192 */ 243 */
193 function updateWithPrinterCapabilities(settingInfo) { 244 function updateWithPrinterCapabilities(settingInfo) {
194 var disableColorOption = settingInfo.disableColorOption; 245 var disableColorOption = settingInfo.disableColorOption;
195 var disableCopiesOption = settingInfo.disableCopiesOption; 246 var disableCopiesOption = settingInfo.disableCopiesOption;
196 var setColorAsDefault = settingInfo.setColorAsDefault; 247 var setColorAsDefault = settingInfo.setColorAsDefault;
248 var disableLandscapeOption = settingInfo.disableLandscapeOption;
197 var setDuplexAsDefault = settingInfo.setDuplexAsDefault; 249 var setDuplexAsDefault = settingInfo.setDuplexAsDefault;
198 var color = $('color'); 250 var color = $('color');
199 var bw = $('bw'); 251 var bw = $('bw');
200 var colorOptions = $('color-options'); 252 var colorOptions = $('color-options');
201 253
202 if (disableCopiesOption) { 254 if (disableCopiesOption) {
203 fadeOutElement($('copies-option')); 255 fadeOutElement($('copies-option'));
204 $('hr-before-copies').classList.remove('invisible'); 256 $('hr-before-copies').classList.remove('invisible');
205 } else { 257 } else {
206 fadeInElement($('copies-option')); 258 fadeInElement($('copies-option'));
207 $('hr-before-copies').classList.add('invisible'); 259 $('hr-before-copies').classList.add('invisible');
208 } 260 }
209 261
262 if (disableLandscapeOption) {
263 fadeOutElement($('landscape-option'));
264 } else {
265 fadeInElement($('landscape-option'));
266 }
267
210 disableColorOption ? fadeOutElement(colorOptions) : 268 disableColorOption ? fadeOutElement(colorOptions) :
211 fadeInElement(colorOptions); 269 fadeInElement(colorOptions);
212 colorOptions.setAttribute('aria-hidden', disableColorOption); 270 colorOptions.setAttribute('aria-hidden', disableColorOption);
213 271
214 if (color.checked != setColorAsDefault) { 272 if (color.checked != setColorAsDefault) {
215 color.checked = setColorAsDefault; 273 color.checked = setColorAsDefault;
216 bw.checked = !setColorAsDefault; 274 bw.checked = !setColorAsDefault;
217 } 275 }
218 copiesSettings.twoSidedCheckbox.checked = setDuplexAsDefault; 276 copiesSettings.twoSidedCheckbox.checked = setDuplexAsDefault;
219 } 277 }
220 278
221 /** 279 /**
280 * Turn on the integration of Cloud Print.
281 * @param {boolean} enable True if cloud print should be used.
282 * @param {string} cloudPrintUrl The URL to use for cloud print servers.
283 */
284 function setUseCloudPrint(enable, cloudPrintURL) {
285 useCloudPrint = enable;
286 cloudprint.setBaseURL(cloudPrintURL);
287 }
288
289 /**
290 * Take the PDF data handed to us and submit it to the cloud, closing the print
291 * preview tab once the upload is successful.
292 * @param {string} data Data to send as the print job.
293 */
294 function printToCloud(data) {
295 cloudprint.printToCloud(data, finishedCloudPrinting);
296 }
297
298 /**
299 * Cloud print upload of the PDF file is finished, time to close the dialog.
300 */
301 function finishedCloudPrinting() {
302 window.location = cloudprint.getBaseURL();
303 }
304
305 /**
222 * Checks whether the preview layout setting is set to 'landscape' or not. 306 * Checks whether the preview layout setting is set to 'landscape' or not.
223 * 307 *
224 * @return {boolean} true if layout is 'landscape'. 308 * @return {boolean} true if layout is 'landscape'.
225 */ 309 */
226 function isLandscape() { 310 function isLandscape() {
227 return $('landscape').checked; 311 return $('landscape').checked;
228 } 312 }
229 313
230 /** 314 /**
231 * Checks whether the preview color setting is set to 'color' or not. 315 * Checks whether the preview color setting is set to 'color' or not.
(...skipping 26 matching lines...) Expand all
258 342
259 /** 343 /**
260 * Creates a JSON string based on the values in the printer settings. 344 * Creates a JSON string based on the values in the printer settings.
261 * 345 *
262 * @return {string} JSON string with print job settings. 346 * @return {string} JSON string with print job settings.
263 */ 347 */
264 function getSettingsJSON() { 348 function getSettingsJSON() {
265 var deviceName = getSelectedPrinterName(); 349 var deviceName = getSelectedPrinterName();
266 var printToPDF = (deviceName == PRINT_TO_PDF); 350 var printToPDF = (deviceName == PRINT_TO_PDF);
267 351
268 return JSON.stringify( 352 var settings = {'deviceName': deviceName,
269 {'deviceName': deviceName, 353 'pageRange': pageSettings.selectedPageRanges,
270 'pageRange': pageSettings.selectedPageRanges, 354 'printAll': pageSettings.allPagesRadioButton.checked,
271 'printAll': pageSettings.allPagesRadioButton.checked, 355 'duplex': getDuplexMode(),
272 'duplex': getDuplexMode(), 356 'copies': copiesSettings.numberOfCopies,
273 'copies': copiesSettings.numberOfCopies, 357 'collate': isCollated(),
274 'collate': isCollated(), 358 'landscape': isLandscape(),
275 'landscape': isLandscape(), 359 'color': isColor(),
276 'color': isColor(), 360 'printToPDF': printToPDF};
277 'printToPDF': printToPDF}); 361 var printerList = $('printer-list');
362 var selectedPrinter = printerList.selectedIndex;
363 if (cloudprint.isCloudPrint(printerList.options[selectedPrinter])) {
364 settings['cloudPrintID'] =
365 printerList.options[selectedPrinter].value;
366 }
367
368 return JSON.stringify(settings);
278 } 369 }
279 370
280 /** 371 /**
281 * Returns the name of the selected printer or the empty string if no 372 * Returns the name of the selected printer or the empty string if no
282 * printer is selected. 373 * printer is selected.
374 * @return {string} The name of the currently selected printer.
283 */ 375 */
284 function getSelectedPrinterName() { 376 function getSelectedPrinterName() {
285 var printerList = $('printer-list') 377 var printerList = $('printer-list')
286 var selectedPrinter = printerList.selectedIndex; 378 var selectedPrinter = printerList.selectedIndex;
287 if (selectedPrinter < 0) 379 if (selectedPrinter < 0)
288 return ''; 380 return '';
289 return printerList.options[selectedPrinter].value; 381 return printerList.options[selectedPrinter].value;
290 } 382 }
291 383
292 /** 384 /**
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 * Sends a message to cancel the pending print request. 430 * Sends a message to cancel the pending print request.
339 */ 431 */
340 function cancelPendingPrintRequest() { 432 function cancelPendingPrintRequest() {
341 chrome.send('cancelPendingPrintRequest'); 433 chrome.send('cancelPendingPrintRequest');
342 } 434 }
343 435
344 /** 436 /**
345 * Sends a message to initiate print workflow. 437 * Sends a message to initiate print workflow.
346 */ 438 */
347 function sendPrintFileRequest() { 439 function sendPrintFileRequest() {
348 chrome.send('print', [getSettingsJSON()]); 440 var printerList = $('printer-list');
441 var printer = printerList[printerList.selectedIndex];
442 chrome.send('saveLastPrinter', [printer.textContent,
443 cloudprint.getData(printer)]);
444 chrome.send('print', [getSettingsJSON(),
445 cloudprint.getPrintTicketJSON(printer)]);
349 } 446 }
350 447
351 /** 448 /**
352 * Asks the browser to generate a preview PDF based on current print settings. 449 * Asks the browser to generate a preview PDF based on current print settings.
353 */ 450 */
354 function requestPrintPreview() { 451 function requestPrintPreview() {
355 hasPendingPreviewRequest = true; 452 hasPendingPreviewRequest = true;
356 removeEventListeners(); 453 removeEventListeners();
357 printSettings.save(); 454 printSettings.save();
358 if (!isTabHidden) 455 if (!isTabHidden)
359 showLoadingAnimation(); 456 showLoadingAnimation();
360 457
361 chrome.send('getPreview', [getSettingsJSON()]); 458 chrome.send('getPreview', [getSettingsJSON()]);
362 } 459 }
363 460
364 /** 461 /**
365 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print 462 * Called from PrintPreviewUI::OnFileSelectionCancelled to notify the print
366 * preview tab regarding the file selection cancel event. 463 * preview tab regarding the file selection cancel event.
367 */ 464 */
368 function fileSelectionCancelled() { 465 function fileSelectionCancelled() {
369 // TODO(thestig) re-enable controls here. 466 // TODO(thestig) re-enable controls here.
370 } 467 }
371 468
372 /** 469 /**
373 * Set the default printer. If there is one, generate a print preview. 470 * Set the default printer. If there is one, generate a print preview.
374 * @param {string} printer Name of the default printer. Empty if none. 471 * @param {string} printer Name of the default printer. Empty if none.
472 * @param {string} cloudPrintData Cloud print related data to restore if
473 * the default printer is a cloud printer.
375 */ 474 */
376 function setDefaultPrinter(printer) { 475 function setDefaultPrinter(printer_name, cloudPrintData) {
377 // Add a placeholder value so the printer list looks valid. 476 // Add a placeholder value so the printer list looks valid.
378 addDestinationListOption('', '', true, true); 477 addDestinationListOption('', '', true, true, true);
379 if (printer) { 478 if (printer_name) {
380 defaultOrLastUsedPrinterName = printer; 479 defaultOrLastUsedPrinterName = printer_name;
381 $('printer-list')[0].value = defaultOrLastUsedPrinterName; 480 if (cloudPrintData) {
382 updateControlsWithSelectedPrinterCapabilities(); 481 cloudprint.setDefaultPrinter(printer_name,
482 cloudPrintData,
483 addCloudPrinters,
484 doUpdateCloudPrinterCapabilities);
485 } else {
486 $('printer-list')[0].value = defaultOrLastUsedPrinterName;
487 updateControlsWithSelectedPrinterCapabilities();
488 }
383 } 489 }
384 chrome.send('getPrinters'); 490 chrome.send('getPrinters');
385 } 491 }
386 492
387 /** 493 /**
388 * Fill the printer list drop down. 494 * Fill the printer list drop down.
389 * Called from PrintPreviewHandler::SendPrinterList(). 495 * Called from PrintPreviewHandler::SendPrinterList().
390 * @param {Array} printers Array of printer info objects. 496 * @param {Array} printers Array of printer info objects.
391 */ 497 */
392 function setPrinters(printers) { 498 function setPrinters(printers) {
393 var printerList = $('printer-list'); 499 var printerList = $('printer-list');
394 // If there exists a dummy printer value, then setDefaultPrinter() already 500 // If there exists a dummy printer value, then setDefaultPrinter() already
395 // requested a preview, so no need to do it again. 501 // requested a preview, so no need to do it again.
396 var needPreview = (printerList[0].value == ''); 502 var needPreview = (printerList[0].value == '');
397 for (var i = 0; i < printers.length; ++i) { 503 for (var i = 0; i < printers.length; ++i) {
398 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName); 504 var isDefault = (printers[i].deviceName == defaultOrLastUsedPrinterName);
399 addDestinationListOption(printers[i].printerName, printers[i].deviceName, 505 addDestinationListOption(printers[i].printerName, printers[i].deviceName,
400 isDefault, false); 506 isDefault, false, false);
401 } 507 }
402 508
403 // Remove the dummy printer added in setDefaultPrinter(). 509 if (!cloudprint.isCloudPrint(printerList[0]))
404 printerList.remove(0); 510 // Remove the dummy printer added in setDefaultPrinter().
511 printerList.remove(0);
405 512
406 if (printers.length != 0) 513 if (printers.length != 0)
407 addDestinationListOption('', '', false, true); 514 addDestinationListOption('', '', false, true, true);
408 515
409 // Adding option for saving PDF to disk. 516 // Adding option for saving PDF to disk.
410 addDestinationListOption(localStrings.getString('printToPDF'), 517 addDestinationListOption(localStrings.getString('printToPDF'),
411 PRINT_TO_PDF, 518 PRINT_TO_PDF,
412 defaultOrLastUsedPrinterName == PRINT_TO_PDF, 519 defaultOrLastUsedPrinterName == PRINT_TO_PDF,
520 false,
413 false); 521 false);
414 addDestinationListOption('', '', false, true); 522 addDestinationListOption('', '', false, true, true);
415 523
416 // Add an option to manage printers. 524 // Add options to manage printers.
417 addDestinationListOption(localStrings.getString('managePrinters'), 525 if (!cr.isChromeOS) {
418 MANAGE_PRINTERS, false, false); 526 addDestinationListOption(localStrings.getString('manageLocalPrinters'),
527 MANAGE_LOCAL_PRINTERS, false, false, false);
528 }
529 if (useCloudPrint) {
530 cloudprint.fetchPrinters(addCloudPrinters, false);
531 addDestinationListOption(localStrings.getString('manageCloudPrinters'),
532 MANAGE_CLOUD_PRINTERS, false, false, false);
533 }
419 534
420 printerList.disabled = false; 535 printerList.disabled = false;
421 536
422 if (needPreview) 537 if (needPreview)
423 updateControlsWithSelectedPrinterCapabilities(); 538 updateControlsWithSelectedPrinterCapabilities();
424 } 539 }
425 540
426 /** 541 /**
542 * Creates an option that can be added to the printer destination list.
543 * @param {String} optionText specifies the option text content.
544 * @param {String} optionValue specifies the option value.
545 * @param {boolean} isDefault is true if the option needs to be selected.
546 * @param {boolean} isDisabled is true if the option needs to be disabled.
547 * @param {boolean} isSeparator is true if the option is a visual separator and
548 * needs to be disabled.
549 * @return {Object} The created option.
550 */
551 function createDestinationListOption(optionText, optionValue, isDefault,
552 isDisabled, isSeparator) {
553 var option = document.createElement('option');
554 option.textContent = optionText;
555 option.value = optionValue;
556 option.selected = isDefault;
557 option.disabled = isSeparator || isDisabled;
558 // Adding attribute for improved accessibility.
559 if (isSeparator)
560 option.setAttribute("role", "separator");
561 return option;
562 }
563
564 /**
427 * Adds an option to the printer destination list. 565 * Adds an option to the printer destination list.
428 * @param {String} optionText specifies the option text content. 566 * @param {String} optionText specifies the option text content.
429 * @param {String} optionValue specifies the option value. 567 * @param {String} optionValue specifies the option value.
430 * @param {boolean} isDefault is true if the option needs to be selected. 568 * @param {boolean} isDefault is true if the option needs to be selected.
431 * @param {boolean} isSeparator is true if the option is a visual separator and 569 * @param {boolean} isDisabled is true if the option needs to be disabled.
432 * needs to be disabled. 570 * @return {Object} The created option.
433 */ 571 */
434 function addDestinationListOption(optionText, optionValue, isDefault, 572 function addDestinationListOption(optionText, optionValue, isDefault,
435 isSeparator) { 573 isDisabled, isSeparator) {
436 var option = document.createElement('option'); 574 var option = createDestinationListOption(optionText,
437 option.textContent = optionText; 575 optionValue,
438 option.value = optionValue; 576 isDefault,
577 isDisabled,
578 isSeparator);
439 $('printer-list').add(option); 579 $('printer-list').add(option);
440 option.selected = isDefault; 580 return option;
441 option.disabled = isSeparator;
442 // Adding attribute for improved accessibility.
443 if (isSeparator)
444 option.setAttribute("role", "separator");
445 } 581 }
446 582
447 /** 583 /**
584 * Adds an option to the printer destination list at a specified position.
585 * @param {Integer} position The index in the printer-list wher the option
586 should be created.
587 * @param {String} optionText specifies the option text content.
588 * @param {String} optionValue specifies the option value.
589 * @param {boolean} isDefault is true if the option needs to be selected.
590 * @param {boolean} isDisabled is true if the option needs to be disabled.
591 * @return {Object} The created option.
592 */
593 function addDestinationListOptionAtPosition(position,
594 optionText,
595 optionValue,
596 isDefault,
597 isDisabled,
598 isSeparator) {
599 var option = createDestinationListOption(optionText,
600 optionValue,
601 isDefault,
602 isDisabled,
603 isSeparator);
604 var printerList = $('printer-list');
605 var before = printerList[position];
606 printerList.add(option, before);
607 return option;
608 }
609
610 /**
611 * Removes the list of cloud printers from the printer pull down.
612 */
613 function clearCloudPrinters() {
614 var printerList = $('printer-list');
615 while (firstCloudPrintOptionPos < lastCloudPrintOptionPos) {
616 lastCloudPrintOptionPos--;
617 printerList.remove(lastCloudPrintOptionPos);
618 }
619 }
620
621 /**
622 * Add cloud printers to the list drop down.
623 * Called from the cloudprint object on receipt of printer information from the
624 * cloud print server.
625 * @param {Array} printers Array of printer info objects.
626 * @return {Object} The currently selected printer.
627 */
628 function addCloudPrinters(printers, showMorePrintersOption) {
629 // TODO(abodenha@chromium.org) Avoid removing printers from the list.
630 // Instead search for duplicates and don't add printers that already exist
631 // in the list.
632 clearCloudPrinters();
633 addDestinationListOptionAtPosition(
634 lastCloudPrintOptionPos++,
635 localStrings.getString('cloudPrinters'),
636 '',
637 false,
638 true,
639 false);
640 if (printers != null) {
641 if (printers.length == 0) {
642 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
643 localStrings.getString('addCloudPrinter'),
644 ADD_CLOUD_PRINTER,
645 false,
646 false,
647 false);
648 } else {
649 for (printer in printers) {
650 var option = addDestinationListOptionAtPosition(
651 lastCloudPrintOptionPos++,
652 printers[printer]['name'],
653 printers[printer]['id'],
654 printers[printer]['name'] == defaultOrLastUsedPrinterName,
655 false,
656 false);
657 cloudprint.setCloudPrint(option,
658 printers[printer]['name'],
659 printers[printer]['id']);
660 }
661 if (showMorePrintersOption) {
662 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
663 '',
664 '',
665 false,
666 true,
667 true);
668 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
669 localStrings.getString('morePrinters'),
670 MORE_PRINTERS, false, false, false);
671 }
672 }
673 } else {
674 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
675 localStrings.getString('signIn'),
676 SIGN_IN,
677 false,
678 false,
679 false);
680 }
681 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
682 '',
683 '',
684 false,
685 true,
686 true);
687 addDestinationListOptionAtPosition(lastCloudPrintOptionPos++,
688 localStrings.getString('localPrinters'),
689 '',
690 false,
691 true,
692 false);
693 var printerList = $('printer-list')
694 var selectedPrinter = printerList.selectedIndex;
695 if (selectedPrinter < 0)
696 return null;
697 return printerList.options[selectedPrinter];
698 }
699
700 /**
448 * Sets the color mode for the PDF plugin. 701 * Sets the color mode for the PDF plugin.
449 * Called from PrintPreviewHandler::ProcessColorSetting(). 702 * Called from PrintPreviewHandler::ProcessColorSetting().
450 * @param {boolean} color is true if the PDF plugin should display in color. 703 * @param {boolean} color is true if the PDF plugin should display in color.
451 */ 704 */
452 function setColor(color) { 705 function setColor(color) {
453 var pdfViewer = $('pdf-viewer'); 706 var pdfViewer = $('pdf-viewer');
454 if (!pdfViewer) { 707 if (!pdfViewer) {
455 return; 708 return;
456 } 709 }
457 pdfViewer.grayscale(!color); 710 pdfViewer.grayscale(!color);
711 var printerList = $('printer-list')
712 cloudprint.setColor(printerList[printerList.selectedIndex], color);
458 } 713 }
459 714
460 /** 715 /**
461 * Display an error message in the center of the preview area. 716 * Display an error message in the center of the preview area.
462 * @param {string} errorMessage The error message to be displayed. 717 * @param {string} errorMessage The error message to be displayed.
463 */ 718 */
464 function displayErrorMessage(errorMessage) { 719 function displayErrorMessage(errorMessage) {
465 $('print-button').disabled = true; 720 $('print-button').disabled = true;
466 $('overlay-layer').classList.remove('invisible'); 721 $('overlay-layer').classList.remove('invisible');
467 $('dancing-dots-text').classList.add('hidden'); 722 $('dancing-dots-text').classList.add('hidden');
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
609 pdfViewer.setAttribute('src', 'chrome://print/' + previewUid + '/print.pdf'); 864 pdfViewer.setAttribute('src', 'chrome://print/' + previewUid + '/print.pdf');
610 pdfViewer.setAttribute('aria-live', 'polite'); 865 pdfViewer.setAttribute('aria-live', 'polite');
611 pdfViewer.setAttribute('aria-atomic', 'true'); 866 pdfViewer.setAttribute('aria-atomic', 'true');
612 $('mainview').appendChild(pdfViewer); 867 $('mainview').appendChild(pdfViewer);
613 pdfViewer.onload('onPDFLoad()'); 868 pdfViewer.onload('onPDFLoad()');
614 pdfViewer.removePrintButton(); 869 pdfViewer.removePrintButton();
615 pdfViewer.grayscale(true); 870 pdfViewer.grayscale(true);
616 } 871 }
617 872
618 /** 873 /**
619 * Returns true if a compatible pdf plugin exists, false if it doesn't. 874 * @return {boolean} true if a compatible pdf plugin exists.
620 */ 875 */
621 function checkCompatiblePluginExists() { 876 function checkCompatiblePluginExists() {
622 var dummyPlugin = $('dummy-viewer') 877 var dummyPlugin = $('dummy-viewer')
623 return (dummyPlugin.onload && 878 return (dummyPlugin.onload &&
624 dummyPlugin.goToPage && 879 dummyPlugin.goToPage &&
625 dummyPlugin.removePrintButton); 880 dummyPlugin.removePrintButton);
626 } 881 }
627 882
628 /** 883 /**
629 * Updates the state of print button depending on the user selection. 884 * Updates the state of print button depending on the user selection.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 this.isLandscape = ''; 982 this.isLandscape = '';
728 } 983 }
729 984
730 /** 985 /**
731 * Takes a snapshot of the print settings. 986 * Takes a snapshot of the print settings.
732 */ 987 */
733 PrintSettings.prototype.save = function() { 988 PrintSettings.prototype.save = function() {
734 this.deviceName = getSelectedPrinterName(); 989 this.deviceName = getSelectedPrinterName();
735 this.isLandscape = isLandscape(); 990 this.isLandscape = isLandscape();
736 } 991 }
992
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698