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

Unified Diff: chrome/browser/resources/print_preview/data/cloud_parsers.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove new widget Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/data/cloud_parsers.js
diff --git a/chrome/browser/resources/print_preview/data/cloud_parsers.js b/chrome/browser/resources/print_preview/data/cloud_parsers.js
new file mode 100644
index 0000000000000000000000000000000000000000..fb1a0b1cd8986d7b842d877d3fa4dec5329b362b
--- /dev/null
+++ b/chrome/browser/resources/print_preview/data/cloud_parsers.js
@@ -0,0 +1,162 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+cr.define('cloudprint', function() {
+ 'use strict';
+
+ function CloudDestinationParser() {};
+
+ /**
+ * Enumeration of cloud destination field names.
+ * @enum {string}
+ * @private
+ */
+ CloudDestinationParser.Field_ = {
+ CAPABILITIES: 'capabilities',
+ DISPLAY_NAME: 'displayName',
+ FORMAT: 'capsFormat',
+ ID: 'id',
+ TAGS: 'tags'
+ };
+
+ /**
+ * Special tag that denotes whether the destination has been recently used.
+ * @type {string}
+ * @private
+ */
+ CloudDestinationParser.RECENT_TAG_ = '^recent';
+
+ /**
+ * Parses a destination from JSON from a Google Cloud Print search or printer
+ * response.
+ * @return {!print_preview.Destination} Parsed destination.
+ */
+ CloudDestinationParser.parse = function(json) {
+ // TODO Error handling.
+ var isRecent = arrayContains(
+ json[CloudDestinationParser.Field_.TAGS],
+ CloudDestinationParser.RECENT_TAG_);
+ var cloudDest = new print_preview.Destination(
+ json[CloudDestinationParser.Field_.ID],
+ json[CloudDestinationParser.Field_.DISPLAY_NAME],
+ isRecent,
+ false /*isLocal*/,
+ json[CloudDestinationParser.Field_.TAGS]);
+ if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES) &&
+ json.hasOwnProperty(CloudDestinationParser.Field_.FORMAT)) {
+ cloudDest.capabilities = CloudCapabilitiesParser.parse(
+ json[CloudDestinationParser.Field_.FORMAT],
+ json[CloudDestinationParser.Field_.CAPABILITIES]);
+ }
+ return cloudDest;
+ };
+
+ function CloudCapabilitiesParser() {};
+
+ CloudCapabilitiesParser.Field_ = {
+ CAP_ID: 'name',
+ DEFAULT: 'default',
+ IS_DEFAULT: 'default',
+ OPTIONS: 'options',
+ OPTION_ID: 'name'
+ };
+
+ CloudCapabilitiesParser.parse = function(capsFormat, json) {
+ log('CloudCapabilitiesParser.parse');
+ var colorCapability = null;
+ var duplexCapability = null;
+ var copiesCapability = null;
+ var collateCapability = null;
+ for (var cap, i = 0; cap = json[i]; i++) {
+ var capId = cap[CloudCapabilitiesParser.Field_.CAP_ID];
+ if (capId == print_preview.CollateCapability.Id[capsFormat]) {
+ collateCapability = CloudCapabilitiesParser.parseCollate(capId, cap);
+ } else if (capId == print_preview.ColorCapability.Id[capsFormat]) {
+ colorCapability = CloudCapabilitiesParser.parseColor(capId, cap);
+ } else if (capId == print_preview.CopiesCapability.Id[capsFormat]) {
+ copiesCapability = new print_preview.CopiesCapability(capId);
+ } else if (capId == print_preview.DuplexCapability.Id[capsFormat]) {
+ duplexCapability = CloudCapabilitiesParser.parseDuplex(capId, cap);
+ }
+ }
+ return new print_preview.CloudCapabilities(
+ collateCapability, colorCapability, copiesCapability, duplexCapability);
+ };
+
+ CloudCapabilitiesParser.parseCollate = function(capId, cap) {
+ var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
+ var collateOption = null;
+ var noCollateOption = null;
+ var isCollateDefault = false;
+ for (var option, i = 0; option = options[i]; i++) {
+ var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
+ if (!collateOption &&
+ print_preview.CollateCapability.COLLATE_REGEX.test(optionId)) {
+ collateOption = optionId;
+ isCollateDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
+ } else if (!noCollateOption &&
+ print_preview.CollateCapability.NO_COLLATE_REGEX.test(optionId)) {
+ noCollateOption = optionId;
+ }
+ }
+ if (!collateOption || !noCollateOption) {
+ return null;
+ }
+ return new print_preview.CollateCapability(
+ capId, collateOption, noCollateOption, isCollateDefault);
+ };
+
+ CloudCapabilitiesParser.parseColor = function(capId, cap) {
+ var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
+ var colorOption = null;
+ var bwOption = null;
+ var isColorDefault = false;
+ for (var option, i = 0; option = options[i]; i++) {
+ var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
+ if (!colorOption &&
+ print_preview.ColorCapability.COLOR_REGEX.test(optionId)) {
+ colorOption = optionId;
+ isColorDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
+ } else if (!bwOption &&
+ print_preview.ColorCapability.BW_REGEX.test(optionId)) {
+ bwOption = optionId;
+ }
+ }
+ if (!colorOption || !bwOption) {
+ return null;
+ }
+ return new print_preview.ColorCapability(
+ capId, colorOption, bwOption, isColorDefault);
+ };
+
+ CloudCapabilitiesParser.parseDuplex = function(capId, cap) {
+ var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
+ var simplexOption = null;
+ var longEdgeOption = null;
+ var isDuplexDefault = false;
+ for (var option, i = 0; option = options[i]; i++) {
+ var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
+ if (!simplexOption &&
+ print_preview.DuplexCapability.SIMPLEX_REGEX.test(optionId)) {
+ simplexOption = optionId;
+ } else if (!longEdgeOption &&
+ print_preview.DuplexCapability.LONG_EDGE_REGEX.test(optionId)) {
+ longEdgeOption = optionId;
+ isDuplexDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
+ }
+ }
+ if (!simplexOption || !longEdgeOption) {
+ return null;
+ }
+ return new print_preview.DuplexCapability(
+ capId, simplexOption, longEdgeOption, isDuplexDefault);
+ };
+
+ // Export
+ return {
+ CloudCapabilitiesParser: CloudCapabilitiesParser,
+ CloudDestinationParser: CloudDestinationParser
+ };
+});
+

Powered by Google App Engine
This is Rietveld 408576698