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