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

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

Issue 2436603002: Cloud Print: Add human readable names for standard media sizes. (Closed)
Patch Set: Address comments Created 4 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 if (nameA > nameB)
383 return 1;
384 if (nameA < nameB)
385 return -1;
386 return 0;
387 };
388
389 /**
390 * Sort printer media sizes.
391 * @param {!Object} capabilities Printer capabilities to localize.
392 * @return {!Object} Localized capabilities.
393 * @private
394 */
395 DestinationStore.sortMediaSizes_ = function(capabilities) {
396 var mediaSize = capabilities.printer.media_size;
397 if (!mediaSize)
398 return capabilities;
399
400 // For the standard sizes, separate into categories, as seen in the Cloud
401 // Print CDD guide:
402 // - North American
403 // - Chinese
404 // - ISO
405 // - Japanese
406 // - Other metric
407 // Otherwise, assume they are custom sizes.
408 var categoryStandardNA = [];
409 var categoryStandardCN = [];
410 var categoryStandardISO = [];
411 var categoryStandardJP = [];
412 var categoryStandardMisc = [];
413 var categoryCustom = [];
414 for (var i = 0, media; media = mediaSize.option[i]; i++) {
415 var name = media.name;
416 var category;
417 if (name.startsWith('NA_')) {
418 category = categoryStandardNA;
419 } else if (name.startsWith('PRC_') || name.startsWith('ROC_') ||
420 name == 'OM_DAI_PA_KAI' || name == 'OM_JUURO_KU_KAI' ||
421 name == 'OM_PA_KAI') {
422 category = categoryStandardCN;
423 } else if (name.startsWith('ISO_')) {
424 category = categoryStandardISO;
425 } else if (name.startsWith('JIS_') || name.startsWith('JPN_')) {
426 category = categoryStandardJP;
427 } else if (name.startsWith('OM_')) {
428 category = categoryStandardMisc;
429 } else {
430 assert(name == 'CUSTOM', 'Unknown media size. Assuming custom');
431 category = categoryCustom;
432 }
433 category.push(media);
434 }
435
436 // For each category, sort by name.
437 categoryStandardNA.sort(DestinationStore.compareMediaNames_);
438 categoryStandardCN.sort(DestinationStore.compareMediaNames_);
439 categoryStandardISO.sort(DestinationStore.compareMediaNames_);
440 categoryStandardJP.sort(DestinationStore.compareMediaNames_);
441 categoryStandardMisc.sort(DestinationStore.compareMediaNames_);
442 categoryCustom.sort(DestinationStore.compareMediaNames_);
443
444 // Then put it all back together.
445 var combined = categoryStandardNA;
446 combined.push(...categoryStandardCN);
dpapad 2016/10/19 19:12:31 Nit (optional): Just realized this. You can also c
dpapad 2016/10/19 19:15:30 Just FYI, you can also do the following, eliminati
Lei Zhang 2016/10/19 19:17:27 Done.
447 combined.push(...categoryStandardISO);
448 combined.push(...categoryStandardJP);
449 combined.push(...categoryStandardMisc);
450 combined.push(...categoryCustom);
451 mediaSize.option = combined;
452 return capabilities;
453 };
454
367 DestinationStore.prototype = { 455 DestinationStore.prototype = {
368 __proto__: cr.EventTarget.prototype, 456 __proto__: cr.EventTarget.prototype,
369 457
370 /** 458 /**
371 * @param {string=} opt_account Account to filter destinations by. When 459 * @param {string=} opt_account Account to filter destinations by. When
372 * omitted, all destinations are returned. 460 * omitted, all destinations are returned.
373 * @return {!Array<!print_preview.Destination>} List of destinations 461 * @return {!Array<!print_preview.Destination>} List of destinations
374 * accessible by the {@code account}. 462 * accessible by the {@code account}.
375 */ 463 */
376 destinations: function(opt_account) { 464 destinations: function(opt_account) {
377 if (opt_account) { 465 if (opt_account) {
378 return this.destinations_.filter(function(destination) { 466 return this.destinations_.filter(function(destination) {
379 return !destination.account || destination.account == opt_account; 467 return !destination.account || destination.account == opt_account;
380 }); 468 });
381 } else {
382 return this.destinations_.slice(0);
383 } 469 }
470 return this.destinations_.slice(0);
384 }, 471 },
385 472
386 /** 473 /**
387 * @return {print_preview.Destination} The currently selected destination or 474 * @return {print_preview.Destination} The currently selected destination or
388 * {@code null} if none is selected. 475 * {@code null} if none is selected.
389 */ 476 */
390 get selectedDestination() { 477 get selectedDestination() {
391 return this.selectedDestination_; 478 return this.selectedDestination_;
392 }, 479 },
393 480
(...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698