| 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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 DestinationStore.EXTENSION_SEARCH_DURATION_ = 5000; | 332 DestinationStore.EXTENSION_SEARCH_DURATION_ = 5000; |
| 333 | 333 |
| 334 /** | 334 /** |
| 335 * Localizes printer capabilities. | 335 * Localizes printer capabilities. |
| 336 * @param {!Object} capabilities Printer capabilities to localize. | 336 * @param {!Object} capabilities Printer capabilities to localize. |
| 337 * @return {!Object} Localized capabilities. | 337 * @return {!Object} Localized capabilities. |
| 338 * @private | 338 * @private |
| 339 */ | 339 */ |
| 340 DestinationStore.localizeCapabilities_ = function(capabilities) { | 340 DestinationStore.localizeCapabilities_ = function(capabilities) { |
| 341 var mediaSize = capabilities.printer.media_size; | 341 var mediaSize = capabilities.printer.media_size; |
| 342 if (mediaSize) { | 342 if (!mediaSize) |
| 343 var mediaDisplayNames = { | 343 return capabilities; |
| 344 'ISO_A0': 'A0', | 344 |
| 345 'ISO_A1': 'A1', | 345 var mediaDisplayNames = { |
| 346 'ISO_A2': 'A2', | 346 'ISO_A0': 'A0', |
| 347 'ISO_A3': 'A3', | 347 'ISO_A1': 'A1', |
| 348 'ISO_A4': 'A4', | 348 'ISO_A2': 'A2', |
| 349 'ISO_A5': 'A5', | 349 'ISO_A3': 'A3', |
| 350 'NA_LEGAL': 'Legal', | 350 'ISO_A4': 'A4', |
| 351 'NA_LETTER': 'Letter', | 351 'ISO_A5': 'A5', |
| 352 'NA_LEDGER': 'Tabloid' | 352 'ISO_A6': 'A6', |
| 353 }; | 353 'JIS_B5': 'B5 (JIS)', |
| 354 for (var i = 0, media; media = mediaSize.option[i]; i++) { | 354 'NA_EXECUTIVE': 'Executive', |
| 355 // No need to patch capabilities with localized names provided. | 355 'NA_LEGAL': 'Legal', |
| 356 if (!media.custom_display_name_localized) { | 356 'NA_LETTER': 'Letter', |
| 357 media.custom_display_name = | 357 'NA_LEDGER': 'Tabloid', |
| 358 media.custom_display_name || | 358 'OM_FOLIO': 'Folio' |
| 359 mediaDisplayNames[media.name] || | 359 }; |
| 360 media.name; | 360 for (var i = 0, media; media = mediaSize.option[i]; i++) { |
| 361 } | 361 // No need to patch capabilities with localized names provided. |
| 362 if (!media.custom_display_name_localized) { |
| 363 media.custom_display_name = |
| 364 media.custom_display_name || |
| 365 mediaDisplayNames[media.name] || |
| 366 media.name; |
| 362 } | 367 } |
| 363 } | 368 } |
| 364 return capabilities; | 369 return capabilities; |
| 365 }; | 370 }; |
| 366 | 371 |
| 372 /** |
| 373 * Compare two media sizes by their names. |
| 374 * @param {!Object} a Media to compare. |
| 375 * @param {!Object} b Media to compare. |
| 376 * @return {number} 1 if a > b, -1 if a < b, or 0 if a == b. |
| 377 * @private |
| 378 */ |
| 379 DestinationStore.compareMediaNames_ = function(a, b) { |
| 380 var nameA = a.custom_display_name_localized || a.custom_display_name; |
| 381 var nameB = b.custom_display_name_localized || b.custom_display_name; |
| 382 return nameA == nameB ? 0 : (nameA > nameB ? 1 : -1); |
| 383 }; |
| 384 |
| 385 /** |
| 386 * Sort printer media sizes. |
| 387 * @param {!Object} capabilities Printer capabilities to localize. |
| 388 * @return {!Object} Localized capabilities. |
| 389 * @private |
| 390 */ |
| 391 DestinationStore.sortMediaSizes_ = function(capabilities) { |
| 392 var mediaSize = capabilities.printer.media_size; |
| 393 if (!mediaSize) |
| 394 return capabilities; |
| 395 |
| 396 // For the standard sizes, separate into categories, as seen in the Cloud |
| 397 // Print CDD guide: |
| 398 // - North American |
| 399 // - Chinese |
| 400 // - ISO |
| 401 // - Japanese |
| 402 // - Other metric |
| 403 // Otherwise, assume they are custom sizes. |
| 404 var categoryStandardNA = []; |
| 405 var categoryStandardCN = []; |
| 406 var categoryStandardISO = []; |
| 407 var categoryStandardJP = []; |
| 408 var categoryStandardMisc = []; |
| 409 var categoryCustom = []; |
| 410 for (var i = 0, media; media = mediaSize.option[i]; i++) { |
| 411 var name = media.name; |
| 412 var category; |
| 413 if (name.startsWith('NA_')) { |
| 414 category = categoryStandardNA; |
| 415 } else if (name.startsWith('PRC_') || name.startsWith('ROC_') || |
| 416 name == 'OM_DAI_PA_KAI' || name == 'OM_JUURO_KU_KAI' || |
| 417 name == 'OM_PA_KAI') { |
| 418 category = categoryStandardCN; |
| 419 } else if (name.startsWith('ISO_')) { |
| 420 category = categoryStandardISO; |
| 421 } else if (name.startsWith('JIS_') || name.startsWith('JPN_')) { |
| 422 category = categoryStandardJP; |
| 423 } else if (name.startsWith('OM_')) { |
| 424 category = categoryStandardMisc; |
| 425 } else { |
| 426 assert(name == 'CUSTOM', 'Unknown media size. Assuming custom'); |
| 427 category = categoryCustom; |
| 428 } |
| 429 category.push(media); |
| 430 } |
| 431 |
| 432 // For each category, sort by name. |
| 433 categoryStandardNA.sort(DestinationStore.compareMediaNames_); |
| 434 categoryStandardCN.sort(DestinationStore.compareMediaNames_); |
| 435 categoryStandardISO.sort(DestinationStore.compareMediaNames_); |
| 436 categoryStandardJP.sort(DestinationStore.compareMediaNames_); |
| 437 categoryStandardMisc.sort(DestinationStore.compareMediaNames_); |
| 438 categoryCustom.sort(DestinationStore.compareMediaNames_); |
| 439 |
| 440 // Then put it all back together. |
| 441 mediaSize.option = categoryStandardNA; |
| 442 mediaSize.option.push(...categoryStandardCN, ...categoryStandardISO, |
| 443 ...categoryStandardJP, ...categoryStandardMisc, ...categoryCustom); |
| 444 return capabilities; |
| 445 }; |
| 446 |
| 367 DestinationStore.prototype = { | 447 DestinationStore.prototype = { |
| 368 __proto__: cr.EventTarget.prototype, | 448 __proto__: cr.EventTarget.prototype, |
| 369 | 449 |
| 370 /** | 450 /** |
| 371 * @param {string=} opt_account Account to filter destinations by. When | 451 * @param {string=} opt_account Account to filter destinations by. When |
| 372 * omitted, all destinations are returned. | 452 * omitted, all destinations are returned. |
| 373 * @return {!Array<!print_preview.Destination>} List of destinations | 453 * @return {!Array<!print_preview.Destination>} List of destinations |
| 374 * accessible by the {@code account}. | 454 * accessible by the {@code account}. |
| 375 */ | 455 */ |
| 376 destinations: function(opt_account) { | 456 destinations: function(opt_account) { |
| 377 if (opt_account) { | 457 if (opt_account) { |
| 378 return this.destinations_.filter(function(destination) { | 458 return this.destinations_.filter(function(destination) { |
| 379 return !destination.account || destination.account == opt_account; | 459 return !destination.account || destination.account == opt_account; |
| 380 }); | 460 }); |
| 381 } else { | |
| 382 return this.destinations_.slice(0); | |
| 383 } | 461 } |
| 462 return this.destinations_.slice(0); |
| 384 }, | 463 }, |
| 385 | 464 |
| 386 /** | 465 /** |
| 387 * @return {print_preview.Destination} The currently selected destination or | 466 * @return {print_preview.Destination} The currently selected destination or |
| 388 * {@code null} if none is selected. | 467 * {@code null} if none is selected. |
| 389 */ | 468 */ |
| 390 get selectedDestination() { | 469 get selectedDestination() { |
| 391 return this.selectedDestination_; | 470 return this.selectedDestination_; |
| 392 }, | 471 }, |
| 393 | 472 |
| (...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1060 } | 1139 } |
| 1061 }, | 1140 }, |
| 1062 | 1141 |
| 1063 /** | 1142 /** |
| 1064 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to | 1143 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to |
| 1065 * update selected destination to match | 1144 * update selected destination to match |
| 1066 * {@code autoSelectMatchingDestination_}. | 1145 * {@code autoSelectMatchingDestination_}. |
| 1067 * @param {print_preview.Destination=} opt_destination The only destination | 1146 * @param {print_preview.Destination=} opt_destination The only destination |
| 1068 * that was changed or skipped if possibly more than one destination was | 1147 * that was changed or skipped if possibly more than one destination was |
| 1069 * changed. Used as a hint to limit destination search scope against | 1148 * changed. Used as a hint to limit destination search scope against |
| 1070 * {@code autoSelectMatchingDestination_). | 1149 * {@code autoSelectMatchingDestination_}. |
| 1071 */ | 1150 */ |
| 1072 destinationsInserted_: function(opt_destination) { | 1151 destinationsInserted_: function(opt_destination) { |
| 1073 cr.dispatchSimpleEvent( | 1152 cr.dispatchSimpleEvent( |
| 1074 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | 1153 this, DestinationStore.EventType.DESTINATIONS_INSERTED); |
| 1075 if (this.autoSelectMatchingDestination_) { | 1154 if (this.autoSelectMatchingDestination_) { |
| 1076 var destinationsToSearch = | 1155 var destinationsToSearch = |
| 1077 opt_destination && [opt_destination] || this.destinations_; | 1156 opt_destination && [opt_destination] || this.destinations_; |
| 1078 destinationsToSearch.some(function(destination) { | 1157 destinationsToSearch.some(function(destination) { |
| 1079 if (this.autoSelectMatchingDestination_.match(destination)) { | 1158 if (this.autoSelectMatchingDestination_.match(destination)) { |
| 1080 this.selectDestination(destination); | 1159 this.selectDestination(destination); |
| 1081 return true; | 1160 return true; |
| 1082 } | 1161 } |
| 1083 }, this); | 1162 }, this); |
| 1084 } | 1163 } |
| 1085 }, | 1164 }, |
| 1086 | 1165 |
| 1087 /** | 1166 /** |
| 1088 * Updates an existing print destination with capabilities and display name | 1167 * Updates an existing print destination with capabilities and display name |
| 1089 * information. If the destination doesn't already exist, it will be added. | 1168 * information. If the destination doesn't already exist, it will be added. |
| 1090 * @param {!print_preview.Destination} destination Destination to update. | 1169 * @param {!print_preview.Destination} destination Destination to update. |
| 1091 * @return {!print_preview.Destination} The existing destination that was | |
| 1092 * updated or {@code null} if it was the new destination. | |
| 1093 * @private | 1170 * @private |
| 1094 */ | 1171 */ |
| 1095 updateDestination_: function(destination) { | 1172 updateDestination_: function(destination) { |
| 1096 assert(destination.constructor !== Array, 'Single printer expected'); | 1173 assert(destination.constructor !== Array, 'Single printer expected'); |
| 1174 destination.capabilities_ = DestinationStore.localizeCapabilities_( |
| 1175 destination.capabilities_); |
| 1176 destination.capabilities_ = DestinationStore.sortMediaSizes_( |
| 1177 destination.capabilities_); |
| 1097 var existingDestination = this.destinationMap_[this.getKey_(destination)]; | 1178 var existingDestination = this.destinationMap_[this.getKey_(destination)]; |
| 1098 if (existingDestination != null) { | 1179 if (existingDestination != null) { |
| 1099 existingDestination.capabilities = destination.capabilities; | 1180 existingDestination.capabilities = destination.capabilities; |
| 1100 } else { | 1181 } else { |
| 1101 this.insertDestination_(destination); | 1182 this.insertDestination_(destination); |
| 1102 } | 1183 } |
| 1103 | 1184 |
| 1104 if (existingDestination == this.selectedDestination_ || | 1185 if (existingDestination == this.selectedDestination_ || |
| 1105 destination == this.selectedDestination_) { | 1186 destination == this.selectedDestination_) { |
| 1106 this.appState_.persistSelectedDestination(this.selectedDestination_); | 1187 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 1107 cr.dispatchSimpleEvent( | 1188 cr.dispatchSimpleEvent( |
| 1108 this, | 1189 this, |
| 1109 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 1190 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 1110 } | 1191 } |
| 1111 | |
| 1112 return existingDestination; | |
| 1113 }, | 1192 }, |
| 1114 | 1193 |
| 1115 /** | 1194 /** |
| 1116 * Called when the search for Privet printers is done. | 1195 * Called when the search for Privet printers is done. |
| 1117 * @private | 1196 * @private |
| 1118 */ | 1197 */ |
| 1119 endPrivetPrinterSearch_: function() { | 1198 endPrivetPrinterSearch_: function() { |
| 1120 this.nativeLayer_.stopGetPrivetDestinations(); | 1199 this.nativeLayer_.stopGetPrivetDestinations(); |
| 1121 this.isPrivetDestinationSearchInProgress_ = false; | 1200 this.isPrivetDestinationSearchInProgress_ = false; |
| 1122 this.hasLoadedAllPrivetDestinations_ = true; | 1201 this.hasLoadedAllPrivetDestinations_ = true; |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1503 return this.getDestinationKey_( | 1582 return this.getDestinationKey_( |
| 1504 destination.origin, destination.id, destination.account); | 1583 destination.origin, destination.id, destination.account); |
| 1505 } | 1584 } |
| 1506 }; | 1585 }; |
| 1507 | 1586 |
| 1508 // Export | 1587 // Export |
| 1509 return { | 1588 return { |
| 1510 DestinationStore: DestinationStore | 1589 DestinationStore: DestinationStore |
| 1511 }; | 1590 }; |
| 1512 }); | 1591 }); |
| OLD | NEW |