OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * A data store that stores destinations and dispatches events when the data | 9 * A data store that stores destinations and dispatches events when the data |
10 * store changes. | 10 * store changes. |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 this.isInAutoSelectMode_ = false; | 77 this.isInAutoSelectMode_ = false; |
78 | 78 |
79 /** | 79 /** |
80 * Event tracker used to track event listeners of the destination store. | 80 * Event tracker used to track event listeners of the destination store. |
81 * @type {!EventTracker} | 81 * @type {!EventTracker} |
82 * @private | 82 * @private |
83 */ | 83 */ |
84 this.tracker_ = new EventTracker(); | 84 this.tracker_ = new EventTracker(); |
85 | 85 |
86 /** | 86 /** |
| 87 * Whether PDF printer is enabled. It's disabled, for example, in App Kiosk |
| 88 * mode. |
| 89 * @type {boolean} |
| 90 * @private |
| 91 */ |
| 92 this.pdfPrinterEnabled_ = false; |
| 93 |
| 94 /** |
87 * Used to fetch cloud-based print destinations. | 95 * Used to fetch cloud-based print destinations. |
88 * @type {print_preview.CloudPrintInterface} | 96 * @type {print_preview.CloudPrintInterface} |
89 * @private | 97 * @private |
90 */ | 98 */ |
91 this.cloudPrintInterface_ = null; | 99 this.cloudPrintInterface_ = null; |
92 | 100 |
93 /** | 101 /** |
94 * Maps user account to the list of origins for which destinations are | 102 * Maps user account to the list of origins for which destinations are |
95 * already loaded. | 103 * already loaded. |
96 * @type {!Object.<string, Array.<print_preview.Destination.Origin>>} | 104 * @type {!Object.<string, Array.<print_preview.Destination.Origin>>} |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 }, | 247 }, |
240 | 248 |
241 /** | 249 /** |
242 * @return {print_preview.Destination} The currently selected destination or | 250 * @return {print_preview.Destination} The currently selected destination or |
243 * {@code null} if none is selected. | 251 * {@code null} if none is selected. |
244 */ | 252 */ |
245 get selectedDestination() { | 253 get selectedDestination() { |
246 return this.selectedDestination_; | 254 return this.selectedDestination_; |
247 }, | 255 }, |
248 | 256 |
| 257 /** @return {boolean} Whether destination selection is pending or not. */ |
| 258 get isAutoSelectDestinationInProgress() { |
| 259 return this.selectedDestination_ == null && |
| 260 this.autoSelectTimeout_ != null; |
| 261 }, |
| 262 |
249 /** | 263 /** |
250 * @return {boolean} Whether a search for local destinations is in progress. | 264 * @return {boolean} Whether a search for local destinations is in progress. |
251 */ | 265 */ |
252 get isLocalDestinationSearchInProgress() { | 266 get isLocalDestinationSearchInProgress() { |
253 return this.isLocalDestinationSearchInProgress_ || | 267 return this.isLocalDestinationSearchInProgress_ || |
254 this.isPrivetDestinationSearchInProgress_; | 268 this.isPrivetDestinationSearchInProgress_; |
255 }, | 269 }, |
256 | 270 |
257 /** | 271 /** |
258 * @return {boolean} Whether a search for cloud destinations is in progress. | 272 * @return {boolean} Whether a search for cloud destinations is in progress. |
259 */ | 273 */ |
260 get isCloudDestinationSearchInProgress() { | 274 get isCloudDestinationSearchInProgress() { |
261 return this.cloudPrintInterface_ && | 275 return this.cloudPrintInterface_ && |
262 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; | 276 this.cloudPrintInterface_.isCloudDestinationSearchInProgress; |
263 }, | 277 }, |
264 | 278 |
265 /** | 279 /** |
266 * Initializes the destination store. Sets the initially selected | 280 * Initializes the destination store. Sets the initially selected |
267 * destination. If any inserted destinations match this ID, that destination | 281 * destination. If any inserted destinations match this ID, that destination |
268 * will be automatically selected. This method must be called after the | 282 * will be automatically selected. This method must be called after the |
269 * print_preview.AppState has been initialized. | 283 * print_preview.AppState has been initialized. |
270 * @private | 284 * @param {boolean} isInAppKioskMode Whether the print preview is in App |
| 285 * Kiosk mode. |
271 */ | 286 */ |
272 init: function() { | 287 init: function(isInAppKioskMode) { |
| 288 this.pdfPrinterEnabled_ = !isInAppKioskMode; |
273 this.isInAutoSelectMode_ = true; | 289 this.isInAutoSelectMode_ = true; |
| 290 this.createLocalPdfPrintDestination_(); |
274 if (!this.appState_.selectedDestinationId || | 291 if (!this.appState_.selectedDestinationId || |
275 !this.appState_.selectedDestinationOrigin) { | 292 !this.appState_.selectedDestinationOrigin) { |
276 this.onAutoSelectFailed_(); | 293 this.selectDefaultDestination_(); |
277 } else { | 294 } else { |
278 var key = this.getDestinationKey_( | 295 var key = this.getDestinationKey_( |
279 this.appState_.selectedDestinationOrigin, | 296 this.appState_.selectedDestinationOrigin, |
280 this.appState_.selectedDestinationId, | 297 this.appState_.selectedDestinationId, |
281 this.appState_.selectedDestinationAccount); | 298 this.appState_.selectedDestinationAccount); |
282 var candidate = this.destinationMap_[key]; | 299 var candidate = this.destinationMap_[key]; |
283 if (candidate != null) { | 300 if (candidate != null) { |
284 this.selectDestination(candidate); | 301 this.selectDestination(candidate); |
285 } else if (this.appState_.selectedDestinationOrigin == | 302 } else if (this.appState_.selectedDestinationOrigin == |
286 print_preview.Destination.Origin.LOCAL) { | 303 print_preview.Destination.Origin.LOCAL) { |
(...skipping 25 matching lines...) Expand all Loading... |
312 print_preview.Destination.Origin.PRIVET, | 329 print_preview.Destination.Origin.PRIVET, |
313 destinationName, | 330 destinationName, |
314 false /*isRecent*/, | 331 false /*isRecent*/, |
315 print_preview.Destination.ConnectionStatus.ONLINE); | 332 print_preview.Destination.ConnectionStatus.ONLINE); |
316 this.selectedDestination_.capabilities = | 333 this.selectedDestination_.capabilities = |
317 this.appState_.selectedDestinationCapabilities; | 334 this.appState_.selectedDestinationCapabilities; |
318 | 335 |
319 cr.dispatchSimpleEvent( | 336 cr.dispatchSimpleEvent( |
320 this, | 337 this, |
321 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY); | 338 DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY); |
322 | |
323 } else { | 339 } else { |
324 this.onAutoSelectFailed_(); | 340 this.selectDefaultDestination_(); |
325 } | 341 } |
326 } | 342 } |
327 }, | 343 }, |
328 | 344 |
329 /** | 345 /** |
330 * Sets the destination store's Google Cloud Print interface. | 346 * Sets the destination store's Google Cloud Print interface. |
331 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface | 347 * @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface |
332 * to set. | 348 * to set. |
333 */ | 349 */ |
334 setCloudPrintInterface: function(cloudPrintInterface) { | 350 setCloudPrintInterface: function(cloudPrintInterface) { |
(...skipping 25 matching lines...) Expand all Loading... |
360 return this.destinations_.every(function(dest) { | 376 return this.destinations_.every(function(dest) { |
361 return dest.isLocal || | 377 return dest.isLocal || |
362 dest.id == print_preview.Destination.GooglePromotedId.DOCS || | 378 dest.id == print_preview.Destination.GooglePromotedId.DOCS || |
363 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; | 379 dest.id == print_preview.Destination.GooglePromotedId.FEDEX; |
364 }); | 380 }); |
365 }, | 381 }, |
366 | 382 |
367 /** @param {!print_preview.Destination} Destination to select. */ | 383 /** @param {!print_preview.Destination} Destination to select. */ |
368 selectDestination: function(destination) { | 384 selectDestination: function(destination) { |
369 this.isInAutoSelectMode_ = false; | 385 this.isInAutoSelectMode_ = false; |
370 this.cancelAutoSelectTimeout_(); | 386 // When auto select expires, DESTINATION_SELECT event has to be dispatched |
371 if (destination == this.selectedDestination_) { | 387 // anyway (see isAutoSelectDestinationInProgress() logic). |
| 388 if (this.autoSelectTimeout_) { |
| 389 clearTimeout(this.autoSelectTimeout_); |
| 390 this.autoSelectTimeout_ = null; |
| 391 } else if (destination == this.selectedDestination_) { |
372 return; | 392 return; |
373 } | 393 } |
374 if (destination == null) { | 394 if (destination == null) { |
375 this.selectedDestination_ = null; | 395 this.selectedDestination_ = null; |
376 cr.dispatchSimpleEvent( | 396 cr.dispatchSimpleEvent( |
377 this, DestinationStore.EventType.DESTINATION_SELECT); | 397 this, DestinationStore.EventType.DESTINATION_SELECT); |
378 return; | 398 return; |
379 } | 399 } |
380 // Update and persist selected destination. | 400 // Update and persist selected destination. |
381 this.selectedDestination_ = destination; | 401 this.selectedDestination_ = destination; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 this, | 447 this, |
428 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 448 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
429 } | 449 } |
430 }, | 450 }, |
431 | 451 |
432 /** | 452 /** |
433 * Selects 'Save to PDF' destination (since it always exists). | 453 * Selects 'Save to PDF' destination (since it always exists). |
434 * @private | 454 * @private |
435 */ | 455 */ |
436 selectDefaultDestination_: function() { | 456 selectDefaultDestination_: function() { |
437 var destination = this.destinationMap_[this.getDestinationKey_( | 457 var saveToPdfKey = this.getDestinationKey_( |
438 print_preview.Destination.Origin.LOCAL, | 458 print_preview.Destination.Origin.LOCAL, |
439 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, | 459 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
440 '')] || null; | 460 ''); |
441 assert(destination != null, 'Save to PDF printer not found'); | 461 this.selectDestination( |
442 this.selectDestination(destination); | 462 this.destinationMap_[saveToPdfKey] || this.destinations_[0] || null); |
443 }, | 463 }, |
444 | 464 |
445 /** Initiates loading of local print destinations. */ | 465 /** Initiates loading of local print destinations. */ |
446 startLoadLocalDestinations: function() { | 466 startLoadLocalDestinations: function() { |
447 if (!this.hasLoadedAllLocalDestinations_) { | 467 if (!this.hasLoadedAllLocalDestinations_) { |
448 this.hasLoadedAllLocalDestinations_ = true; | 468 this.hasLoadedAllLocalDestinations_ = true; |
449 this.nativeLayer_.startGetLocalDestinations(); | 469 this.nativeLayer_.startGetLocalDestinations(); |
450 this.isLocalDestinationSearchInProgress_ = true; | 470 this.isLocalDestinationSearchInProgress_ = true; |
451 cr.dispatchSimpleEvent( | 471 cr.dispatchSimpleEvent( |
452 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); | 472 this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
651 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, | 671 print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET, |
652 this.onPrivetCapabilitiesSet_.bind(this)); | 672 this.onPrivetCapabilitiesSet_.bind(this)); |
653 }, | 673 }, |
654 | 674 |
655 /** | 675 /** |
656 * Creates a local PDF print destination. | 676 * Creates a local PDF print destination. |
657 * @return {!print_preview.Destination} Created print destination. | 677 * @return {!print_preview.Destination} Created print destination. |
658 * @private | 678 * @private |
659 */ | 679 */ |
660 createLocalPdfPrintDestination_: function() { | 680 createLocalPdfPrintDestination_: function() { |
661 return new print_preview.Destination( | 681 // TODO(alekseys): Create PDF printer in the native code and send its |
662 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, | 682 // capabilities back with other local printers. |
663 print_preview.Destination.Type.LOCAL, | 683 if (this.pdfPrinterEnabled_) { |
664 print_preview.Destination.Origin.LOCAL, | 684 this.insertDestination_(new print_preview.Destination( |
665 localStrings.getString('printToPDF'), | 685 print_preview.Destination.GooglePromotedId.SAVE_AS_PDF, |
666 false /*isRecent*/, | 686 print_preview.Destination.Type.LOCAL, |
667 print_preview.Destination.ConnectionStatus.ONLINE); | 687 print_preview.Destination.Origin.LOCAL, |
| 688 localStrings.getString('printToPDF'), |
| 689 false /*isRecent*/, |
| 690 print_preview.Destination.ConnectionStatus.ONLINE)); |
| 691 } |
668 }, | 692 }, |
669 | 693 |
670 /** | 694 /** |
671 * Resets the state of the destination store to its initial state. | 695 * Resets the state of the destination store to its initial state. |
672 * @private | 696 * @private |
673 */ | 697 */ |
674 reset_: function() { | 698 reset_: function() { |
675 this.destinations_ = []; | 699 this.destinations_ = []; |
676 this.destinationMap_ = {}; | 700 this.destinationMap_ = {}; |
677 this.selectDestination(null); | 701 this.selectDestination(null); |
678 this.loadedCloudOrigins_ = {}; | 702 this.loadedCloudOrigins_ = {}; |
679 this.hasLoadedAllLocalDestinations_ = false; | 703 this.hasLoadedAllLocalDestinations_ = false; |
680 // TODO(alekseys): Create PDF printer in the native code and send its | 704 |
681 // capabilities back with other local printers. | 705 clearTimeout(this.autoSelectTimeout_); |
682 this.insertDestination_(this.createLocalPdfPrintDestination_()); | 706 this.autoSelectTimeout_ = setTimeout( |
683 this.resetAutoSelectTimeout_(); | 707 this.selectDefaultDestination_.bind(this), |
| 708 DestinationStore.AUTO_SELECT_TIMEOUT_); |
684 }, | 709 }, |
685 | 710 |
686 /** | 711 /** |
687 * Resets destination auto selection timeout. | |
688 * @private | |
689 */ | |
690 resetAutoSelectTimeout_: function() { | |
691 this.cancelAutoSelectTimeout_(); | |
692 this.autoSelectTimeout_ = | |
693 setTimeout(this.onAutoSelectFailed_.bind(this), | |
694 DestinationStore.AUTO_SELECT_TIMEOUT_); | |
695 }, | |
696 | |
697 /** | |
698 * Cancels destination auto selection timeout. | |
699 * @private | |
700 */ | |
701 cancelAutoSelectTimeout_: function() { | |
702 if (this.autoSelectTimeout_ != null) { | |
703 clearTimeout(this.autoSelectTimeout_); | |
704 this.autoSelectTimeout_ = null; | |
705 } | |
706 }, | |
707 | |
708 /** | |
709 * Called when the local destinations have been got from the native layer. | 712 * Called when the local destinations have been got from the native layer. |
710 * @param {Event} Contains the local destinations. | 713 * @param {Event} Contains the local destinations. |
711 * @private | 714 * @private |
712 */ | 715 */ |
713 onLocalDestinationsSet_: function(event) { | 716 onLocalDestinationsSet_: function(event) { |
714 var localDestinations = event.destinationInfos.map(function(destInfo) { | 717 var localDestinations = event.destinationInfos.map(function(destInfo) { |
715 return print_preview.LocalDestinationParser.parse(destInfo); | 718 return print_preview.LocalDestinationParser.parse(destInfo); |
716 }); | 719 }); |
717 this.insertDestinations_(localDestinations); | 720 this.insertDestinations_(localDestinations); |
718 this.isLocalDestinationSearchInProgress_ = false; | 721 this.isLocalDestinationSearchInProgress_ = false; |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
869 }, | 872 }, |
870 | 873 |
871 /** | 874 /** |
872 * Called from native layer after the user was requested to sign in, and did | 875 * Called from native layer after the user was requested to sign in, and did |
873 * so successfully. | 876 * so successfully. |
874 * @private | 877 * @private |
875 */ | 878 */ |
876 onDestinationsReload_: function() { | 879 onDestinationsReload_: function() { |
877 this.reset_(); | 880 this.reset_(); |
878 this.isInAutoSelectMode_ = true; | 881 this.isInAutoSelectMode_ = true; |
| 882 this.createLocalPdfPrintDestination_(); |
879 this.startLoadLocalDestinations(); | 883 this.startLoadLocalDestinations(); |
880 this.startLoadCloudDestinations(); | 884 this.startLoadCloudDestinations(); |
881 this.startLoadPrivetDestinations(); | 885 this.startLoadPrivetDestinations(); |
882 }, | 886 }, |
883 | 887 |
884 /** | |
885 * Called when auto-selection fails. Selects the first destination in store. | |
886 * @private | |
887 */ | |
888 onAutoSelectFailed_: function() { | |
889 this.cancelAutoSelectTimeout_(); | |
890 this.selectDefaultDestination_(); | |
891 }, | |
892 | |
893 // TODO(vitalybuka): Remove three next functions replacing Destination.id | 888 // TODO(vitalybuka): Remove three next functions replacing Destination.id |
894 // and Destination.origin by complex ID. | 889 // and Destination.origin by complex ID. |
895 /** | 890 /** |
896 * Returns key to be used with {@code destinationMap_}. | 891 * Returns key to be used with {@code destinationMap_}. |
897 * @param {!print_preview.Destination.Origin} origin Destination origin. | 892 * @param {!print_preview.Destination.Origin} origin Destination origin. |
898 * @return {string} id Destination id. | 893 * @return {string} id Destination id. |
899 * @return {string} account User account destination is registered for. | 894 * @return {string} account User account destination is registered for. |
900 * @private | 895 * @private |
901 */ | 896 */ |
902 getDestinationKey_: function(origin, id, account) { | 897 getDestinationKey_: function(origin, id, account) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
936 return id == this.appState_.selectedDestinationId && | 931 return id == this.appState_.selectedDestinationId && |
937 origin == this.appState_.selectedDestinationOrigin; | 932 origin == this.appState_.selectedDestinationOrigin; |
938 } | 933 } |
939 }; | 934 }; |
940 | 935 |
941 // Export | 936 // Export |
942 return { | 937 return { |
943 DestinationStore: DestinationStore | 938 DestinationStore: DestinationStore |
944 }; | 939 }; |
945 }); | 940 }); |
OLD | NEW |