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

Side by Side 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: Fixes broken tests Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 cr.define('cloudprint', function() {
6 'use strict';
7
8 /** Namespace which contains a method to parse cloud destinations directly. */
9 function CloudDestinationParser() {};
10
11 /**
12 * Enumeration of cloud destination field names.
13 * @enum {string}
14 * @private
15 */
16 CloudDestinationParser.Field_ = {
17 CAPABILITIES: 'capabilities',
18 DISPLAY_NAME: 'displayName',
19 FORMAT: 'capsFormat',
20 ID: 'id',
21 TAGS: 'tags'
22 };
23
24 /**
25 * Special tag that denotes whether the destination has been recently used.
26 * @type {string}
27 * @private
28 */
29 CloudDestinationParser.RECENT_TAG_ = '^recent';
30
31 /**
32 * Parses a destination from JSON from a Google Cloud Print search or printer
33 * response.
34 * @param {object} json Object that represents a Google Cloud Print search or
35 * printer response.
36 * @return {!print_preview.Destination} Parsed destination.
37 */
38 CloudDestinationParser.parse = function(json) {
39 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) ||
40 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) {
41 throw Error('Cloud destination does not have an ID or a display name');
42 }
43 var isRecent = arrayContains(
44 json[CloudDestinationParser.Field_.TAGS] || [],
45 CloudDestinationParser.RECENT_TAG_);
46 var cloudDest = new print_preview.Destination(
47 json[CloudDestinationParser.Field_.ID],
48 json[CloudDestinationParser.Field_.DISPLAY_NAME],
49 isRecent,
50 false /*isLocal*/,
51 json[CloudDestinationParser.Field_.TAGS] || []);
52 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES) &&
53 json.hasOwnProperty(CloudDestinationParser.Field_.FORMAT)) {
54 cloudDest.capabilities = CloudCapabilitiesParser.parse(
55 json[CloudDestinationParser.Field_.FORMAT],
56 json[CloudDestinationParser.Field_.CAPABILITIES]);
57 }
58 return cloudDest;
59 };
60
61 /**
62 * Namespace which contains a method to parse a cloud destination's print
63 * capabilities.
64 */
65 function CloudCapabilitiesParser() {};
66
67 /**
68 * Enumeration of cloud destination print capabilities field names.
69 * @enum {string}
70 * @private
71 */
72 CloudCapabilitiesParser.Field_ = {
73 CAP_ID: 'name',
74 DEFAULT: 'default',
75 IS_DEFAULT: 'default',
76 OPTIONS: 'options',
77 OPTION_ID: 'name'
78 };
79
80 /**
81 * Parses print capabilities from an object in a given capabilities format.
82 * @param {print_preview.CloudCapabilities.Format} capsFormat Format of the
83 * printer capabilities.
84 * @param {object} json Object representing the cloud capabilities.
85 * @return {!print_preview.CloudCapabilities} Parsed print capabilities.
86 */
87 CloudCapabilitiesParser.parse = function(capsFormat, json) {
88 var colorCapability = null;
89 var duplexCapability = null;
90 var copiesCapability = null;
91 var collateCapability = null;
92 for (var cap, i = 0; cap = json[i]; i++) {
93 var capId = cap[CloudCapabilitiesParser.Field_.CAP_ID];
94 if (capId == print_preview.CollateCapability.Id[capsFormat]) {
95 collateCapability = CloudCapabilitiesParser.parseCollate(capId, cap);
96 } else if (capId == print_preview.ColorCapability.Id[capsFormat]) {
97 colorCapability = CloudCapabilitiesParser.parseColor(capId, cap);
98 } else if (capId == print_preview.CopiesCapability.Id[capsFormat]) {
99 copiesCapability = new print_preview.CopiesCapability(capId);
100 } else if (capId == print_preview.DuplexCapability.Id[capsFormat]) {
101 duplexCapability = CloudCapabilitiesParser.parseDuplex(capId, cap);
102 }
103 }
104 return new print_preview.CloudCapabilities(
105 collateCapability, colorCapability, copiesCapability, duplexCapability);
106 };
107
108 /**
109 * Parses a collate capability from the given object.
110 * @param {string} capId Native ID of the given capability object.
111 * @param {object} Object that represents the collate capability.
112 * @return {print_preview.CollateCapability} Parsed collate capability or
113 * {@code null} if the given capability object was not a valid collate
114 * capability.
115 */
116 CloudCapabilitiesParser.parseCollate = function(capId, cap) {
117 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
118 var collateOption = null;
119 var noCollateOption = null;
120 var isCollateDefault = false;
121 for (var option, i = 0; option = options[i]; i++) {
122 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
123 if (!collateOption &&
124 print_preview.CollateCapability.COLLATE_REGEX.test(optionId)) {
125 collateOption = optionId;
126 isCollateDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
127 } else if (!noCollateOption &&
128 print_preview.CollateCapability.NO_COLLATE_REGEX.test(optionId)) {
129 noCollateOption = optionId;
130 }
131 }
132 if (!collateOption || !noCollateOption) {
133 return null;
134 }
135 return new print_preview.CollateCapability(
136 capId, collateOption, noCollateOption, isCollateDefault);
137 };
138
139 /**
140 * Parses a color capability from the given object.
141 * @param {string} capId Native ID of the given capability object.
142 * @param {object} Object that represents the color capability.
143 * @return {print_preview.ColorCapability} Parsed color capability or
144 * {@code null} if the given capability object was not a valid color
145 * capability.
146 */
147 CloudCapabilitiesParser.parseColor = function(capId, cap) {
148 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
149 var colorOption = null;
150 var bwOption = null;
151 var isColorDefault = false;
152 for (var option, i = 0; option = options[i]; i++) {
153 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
154 if (!colorOption &&
155 print_preview.ColorCapability.COLOR_REGEX.test(optionId)) {
156 colorOption = optionId;
157 isColorDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
158 } else if (!bwOption &&
159 print_preview.ColorCapability.BW_REGEX.test(optionId)) {
160 bwOption = optionId;
161 }
162 }
163 if (!colorOption || !bwOption) {
164 return null;
165 }
166 return new print_preview.ColorCapability(
167 capId, colorOption, bwOption, isColorDefault);
168 };
169
170 /**
171 * Parses a duplex capability from the given object.
172 * @param {string} capId Native ID of the given capability object.
173 * @param {object} Object that represents the duplex capability.
174 * @return {print_preview.DuplexCapability} Parsed duplex capability or
175 * {@code null} if the given capability object was not a valid duplex
176 * capability.
177 */
178 CloudCapabilitiesParser.parseDuplex = function(capId, cap) {
179 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS];
180 var simplexOption = null;
181 var longEdgeOption = null;
182 var isDuplexDefault = false;
183 for (var option, i = 0; option = options[i]; i++) {
184 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID];
185 if (!simplexOption &&
186 print_preview.DuplexCapability.SIMPLEX_REGEX.test(optionId)) {
187 simplexOption = optionId;
188 } else if (!longEdgeOption &&
189 print_preview.DuplexCapability.LONG_EDGE_REGEX.test(optionId)) {
190 longEdgeOption = optionId;
191 isDuplexDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT];
192 }
193 }
194 if (!simplexOption || !longEdgeOption) {
195 return null;
196 }
197 return new print_preview.DuplexCapability(
198 capId, simplexOption, longEdgeOption, isDuplexDefault);
199 };
200
201 // Export
202 return {
203 CloudCapabilitiesParser: CloudCapabilitiesParser,
204 CloudDestinationParser: CloudDestinationParser
205 };
206 });
207
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698