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 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 var mediaDisplayNames = { |
|
Lei Zhang
2016/10/19 07:21:21
We can always add more later. The additions are su
| |
| 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. | |
| 374 * @param {string} b Media to compare. | |
| 375 * @return {boolean} True 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 return nameA > nameB; | |
| 382 }; | |
| 383 | |
| 384 /** | |
| 385 * Sort printer media sizes. | |
| 386 * @param {!Object} capabilities Printer capabilities to localize. | |
| 387 * @return {!Object} Localized capabilities. | |
| 388 * @private | |
| 389 */ | |
| 390 DestinationStore.sortMediaSizes_ = function(capabilities) { | |
| 391 var mediaSize = capabilities.printer.media_size; | |
| 392 if (mediaSize) { | |
| 393 // For the standard sizes, separate into categories, as seen in the Cloud | |
|
Lei Zhang
2016/10/19 07:21:21
I have not seen the code for the Cloud Print websi
| |
| 394 // Print CDD guide: | |
| 395 // - North American | |
| 396 // - Chinese | |
| 397 // - ISO | |
| 398 // - Japanese | |
| 399 // - Other metric | |
| 400 // Otherwise, assume they are custom sizes. | |
| 401 var categoryStandardNA = []; | |
| 402 var categoryStandardCN = []; | |
| 403 var categoryStandardISO = []; | |
| 404 var categoryStandardJP = []; | |
| 405 var categoryStandardMisc = []; | |
| 406 var categoryCustom = []; | |
| 407 for (var i = 0, media; media = mediaSize.option[i]; i++) { | |
| 408 var name = media.name; | |
| 409 var category; | |
| 410 if (name.startsWith('NA_')) { | |
| 411 category = categoryStandardNA; | |
| 412 } else if (name.startsWith('PRC_') || name.startsWith('ROC_') || | |
| 413 name == 'OM_DAI_PA_KAI' || name == 'OM_JUURO_KU_KAI' || | |
| 414 name == 'OM_PA_KAI') { | |
| 415 category = categoryStandardCN; | |
| 416 } else if (name.startsWith('ISO_')) { | |
| 417 category = categoryStandardISO; | |
| 418 } else if (name.startsWith('JIS_') || name.startsWith('JPN_')) { | |
| 419 category = categoryStandardJP; | |
| 420 } else if (name.startsWith('OM_')) { | |
| 421 category = categoryStandardMisc; | |
| 422 } else { | |
| 423 assert(name == 'CUSTOM', 'Unknown media size. Assuming custom'); | |
| 424 category = categoryCustom; | |
| 425 } | |
| 426 category.push(media); | |
| 427 } | |
| 428 | |
| 429 // For each category, sort by name. | |
| 430 categoryStandardNA.sort(DestinationStore.compareMediaNames_); | |
| 431 categoryStandardCN.sort(DestinationStore.compareMediaNames_); | |
| 432 categoryStandardISO.sort(DestinationStore.compareMediaNames_); | |
| 433 categoryStandardJP.sort(DestinationStore.compareMediaNames_); | |
| 434 categoryStandardMisc.sort(DestinationStore.compareMediaNames_); | |
| 435 categoryCustom.sort(DestinationStore.compareMediaNames_); | |
| 436 | |
| 437 // Then put it all back together. | |
| 438 var combined = []; | |
| 439 combined = combined.concat(categoryStandardNA); | |
| 440 combined = combined.concat(categoryStandardCN); | |
| 441 combined = combined.concat(categoryStandardISO); | |
| 442 combined = combined.concat(categoryStandardJP); | |
| 443 combined = combined.concat(categoryStandardMisc); | |
| 444 combined = combined.concat(categoryCustom); | |
| 445 mediaSize.option = combined; | |
| 446 } | |
| 447 return capabilities; | |
| 448 }; | |
| 449 | |
| 367 DestinationStore.prototype = { | 450 DestinationStore.prototype = { |
| 368 __proto__: cr.EventTarget.prototype, | 451 __proto__: cr.EventTarget.prototype, |
| 369 | 452 |
| 370 /** | 453 /** |
| 371 * @param {string=} opt_account Account to filter destinations by. When | 454 * @param {string=} opt_account Account to filter destinations by. When |
| 372 * omitted, all destinations are returned. | 455 * omitted, all destinations are returned. |
| 373 * @return {!Array<!print_preview.Destination>} List of destinations | 456 * @return {!Array<!print_preview.Destination>} List of destinations |
| 374 * accessible by the {@code account}. | 457 * accessible by the {@code account}. |
| 375 */ | 458 */ |
| 376 destinations: function(opt_account) { | 459 destinations: function(opt_account) { |
| (...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1060 } | 1143 } |
| 1061 }, | 1144 }, |
| 1062 | 1145 |
| 1063 /** | 1146 /** |
| 1064 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to | 1147 * Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to |
| 1065 * update selected destination to match | 1148 * update selected destination to match |
| 1066 * {@code autoSelectMatchingDestination_}. | 1149 * {@code autoSelectMatchingDestination_}. |
| 1067 * @param {print_preview.Destination=} opt_destination The only destination | 1150 * @param {print_preview.Destination=} opt_destination The only destination |
| 1068 * that was changed or skipped if possibly more than one destination was | 1151 * that was changed or skipped if possibly more than one destination was |
| 1069 * changed. Used as a hint to limit destination search scope against | 1152 * changed. Used as a hint to limit destination search scope against |
| 1070 * {@code autoSelectMatchingDestination_). | 1153 * {@code autoSelectMatchingDestination_}. |
| 1071 */ | 1154 */ |
| 1072 destinationsInserted_: function(opt_destination) { | 1155 destinationsInserted_: function(opt_destination) { |
| 1073 cr.dispatchSimpleEvent( | 1156 cr.dispatchSimpleEvent( |
| 1074 this, DestinationStore.EventType.DESTINATIONS_INSERTED); | 1157 this, DestinationStore.EventType.DESTINATIONS_INSERTED); |
| 1075 if (this.autoSelectMatchingDestination_) { | 1158 if (this.autoSelectMatchingDestination_) { |
| 1076 var destinationsToSearch = | 1159 var destinationsToSearch = |
| 1077 opt_destination && [opt_destination] || this.destinations_; | 1160 opt_destination && [opt_destination] || this.destinations_; |
| 1078 destinationsToSearch.some(function(destination) { | 1161 destinationsToSearch.some(function(destination) { |
| 1079 if (this.autoSelectMatchingDestination_.match(destination)) { | 1162 if (this.autoSelectMatchingDestination_.match(destination)) { |
| 1080 this.selectDestination(destination); | 1163 this.selectDestination(destination); |
| 1081 return true; | 1164 return true; |
| 1082 } | 1165 } |
| 1083 }, this); | 1166 }, this); |
| 1084 } | 1167 } |
| 1085 }, | 1168 }, |
| 1086 | 1169 |
| 1087 /** | 1170 /** |
| 1088 * Updates an existing print destination with capabilities and display name | 1171 * Updates an existing print destination with capabilities and display name |
| 1089 * information. If the destination doesn't already exist, it will be added. | 1172 * information. If the destination doesn't already exist, it will be added. |
| 1090 * @param {!print_preview.Destination} destination Destination to update. | 1173 * @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 | 1174 * @private |
| 1094 */ | 1175 */ |
| 1095 updateDestination_: function(destination) { | 1176 updateDestination_: function(destination) { |
| 1096 assert(destination.constructor !== Array, 'Single printer expected'); | 1177 assert(destination.constructor !== Array, 'Single printer expected'); |
| 1178 destination.capabilities_ = DestinationStore.localizeCapabilities_( | |
| 1179 destination.capabilities_); | |
| 1180 destination.capabilities_ = DestinationStore.sortMediaSizes_( | |
| 1181 destination.capabilities_); | |
| 1097 var existingDestination = this.destinationMap_[this.getKey_(destination)]; | 1182 var existingDestination = this.destinationMap_[this.getKey_(destination)]; |
| 1098 if (existingDestination != null) { | 1183 if (existingDestination != null) { |
| 1099 existingDestination.capabilities = destination.capabilities; | 1184 existingDestination.capabilities = destination.capabilities; |
| 1100 } else { | 1185 } else { |
| 1101 this.insertDestination_(destination); | 1186 this.insertDestination_(destination); |
| 1102 } | 1187 } |
| 1103 | 1188 |
| 1104 if (existingDestination == this.selectedDestination_ || | 1189 if (existingDestination == this.selectedDestination_ || |
| 1105 destination == this.selectedDestination_) { | 1190 destination == this.selectedDestination_) { |
| 1106 this.appState_.persistSelectedDestination(this.selectedDestination_); | 1191 this.appState_.persistSelectedDestination(this.selectedDestination_); |
| 1107 cr.dispatchSimpleEvent( | 1192 cr.dispatchSimpleEvent( |
| 1108 this, | 1193 this, |
| 1109 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); | 1194 DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY); |
| 1110 } | 1195 } |
| 1111 | |
| 1112 return existingDestination; | |
| 1113 }, | 1196 }, |
| 1114 | 1197 |
| 1115 /** | 1198 /** |
| 1116 * Called when the search for Privet printers is done. | 1199 * Called when the search for Privet printers is done. |
| 1117 * @private | 1200 * @private |
| 1118 */ | 1201 */ |
| 1119 endPrivetPrinterSearch_: function() { | 1202 endPrivetPrinterSearch_: function() { |
| 1120 this.nativeLayer_.stopGetPrivetDestinations(); | 1203 this.nativeLayer_.stopGetPrivetDestinations(); |
| 1121 this.isPrivetDestinationSearchInProgress_ = false; | 1204 this.isPrivetDestinationSearchInProgress_ = false; |
| 1122 this.hasLoadedAllPrivetDestinations_ = true; | 1205 this.hasLoadedAllPrivetDestinations_ = true; |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1503 return this.getDestinationKey_( | 1586 return this.getDestinationKey_( |
| 1504 destination.origin, destination.id, destination.account); | 1587 destination.origin, destination.id, destination.account); |
| 1505 } | 1588 } |
| 1506 }; | 1589 }; |
| 1507 | 1590 |
| 1508 // Export | 1591 // Export |
| 1509 return { | 1592 return { |
| 1510 DestinationStore: DestinationStore | 1593 DestinationStore: DestinationStore |
| 1511 }; | 1594 }; |
| 1512 }); | 1595 }); |
| OLD | NEW |