Chromium Code Reviews| OLD | NEW |
|---|---|
| (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('print_preview', function() { | |
| 6 'use strict'; | |
| 7 | |
| 8 /** | |
| 9 * Print destination data object that holds data for both local and cloud | |
| 10 * destinations. | |
| 11 * @param {string} id ID of the destination. | |
| 12 * @param {string} displayName Display name of the destination. | |
| 13 * @param {boolean} isRecent Whether the destination has been used recently. | |
| 14 * @param {boolean} isLocal Whether the destination is local or cloud-based. | |
| 15 * @param {Array.<string>=} opt_tags Tags associated with the destination. | |
| 16 * @constructor | |
| 17 */ | |
| 18 function Destination(id, displayName, isRecent, isLocal, opt_tags) { | |
| 19 /** | |
| 20 * ID of the destination. | |
| 21 * @type {string} | |
| 22 * @private | |
| 23 */ | |
| 24 this.id_ = id; | |
| 25 | |
| 26 /** | |
| 27 * Display name of the destination. | |
| 28 * @type {string} | |
| 29 * @private | |
| 30 */ | |
| 31 this.displayName_ = displayName; | |
| 32 | |
| 33 /** | |
| 34 * Whether the destination has been used recently. | |
| 35 * @type {boolean} | |
| 36 * @private | |
| 37 */ | |
| 38 this.isRecent_ = isRecent; | |
| 39 | |
| 40 /** | |
| 41 * Whether the destination is local or cloud-based. | |
| 42 * @type {boolean} | |
| 43 * @private | |
| 44 */ | |
| 45 this.isLocal_ = isLocal; | |
| 46 | |
| 47 /** | |
| 48 * Tags associated with the destination. | |
| 49 * @type {!Array.<string>} | |
| 50 * @private | |
| 51 */ | |
| 52 this.tags_ = opt_tags || []; | |
| 53 | |
| 54 /** | |
| 55 * Print capabilities of the destination. | |
| 56 * @type {print_preview.ChromiumCapabilities} | |
| 57 * @private | |
| 58 */ | |
| 59 this.capabilities_ = null; | |
| 60 | |
| 61 /** | |
| 62 * Cache of destination location fetched from tags. | |
| 63 * @type {string} | |
| 64 * @private | |
| 65 */ | |
| 66 this.location_ = null; | |
| 67 }; | |
| 68 | |
| 69 /** | |
| 70 * Prefix of the location destination tag. | |
| 71 * @type {string} | |
| 72 * @const | |
| 73 */ | |
| 74 Destination.LOCATION_TAG_PREFIX = '__cp__printer-location='; | |
| 75 | |
| 76 /** | |
| 77 * Enumeration of Google-promoted destination IDs. | |
| 78 * @enum {string} | |
| 79 */ | |
| 80 Destination.GooglePromotedId = { | |
| 81 DOCS: '__google__docs', | |
| 82 LOCAL_PDF: 'Save as PDF', | |
| 83 PRINT_WITH_CLOUD_PRINT: 'printWithCloudPrint' | |
| 84 }; | |
| 85 | |
| 86 Destination.prototype = { | |
| 87 /** @return {string} ID of the destination. */ | |
| 88 get id() { | |
| 89 return this.id_; | |
| 90 }, | |
| 91 | |
| 92 /** @return {string} Display name of the destination. */ | |
| 93 get displayName() { | |
| 94 return this.displayName_; | |
| 95 }, | |
| 96 | |
| 97 /** @return {boolean} Whether the destination has been used recently. */ | |
| 98 get isRecent() { | |
| 99 return this.isRecent_; | |
| 100 }, | |
| 101 | |
| 102 /** | |
| 103 * @param {boolean} isRecent Whether the destination has been used recently. | |
| 104 */ | |
| 105 set isRecent(isRecent) { | |
| 106 this.isRecent_ = isRecent; | |
| 107 }, | |
| 108 | |
| 109 /** @return {boolean} Whether the destination is local or cloud-based. */ | |
| 110 get isLocal() { | |
| 111 return this.isLocal_; | |
| 112 }, | |
| 113 | |
| 114 /** @return {boolean} Whether the destination is promoted by Google. */ | |
| 115 get isGooglePromoted() { | |
| 116 for (var key in Destination.GooglePromotedId) { | |
| 117 if (Destination.GooglePromotedId[key] == this.id_) { | |
| 118 return true; | |
| 119 } | |
| 120 } | |
| 121 return false; | |
| 122 }, | |
| 123 | |
| 124 /** | |
| 125 * @return {boolean} Whether the destination is the "Print to PDF" | |
| 126 * destination. | |
| 127 */ | |
| 128 get isPrintToPdf() { | |
| 129 return this.id_ == Destination.GooglePromotedId.LOCAL_PDF; | |
| 130 }, | |
| 131 | |
| 132 /** | |
| 133 * @return {boolean} Whether the destination is the "Print with Cloud Print" | |
| 134 * destination. | |
| 135 */ | |
| 136 get isPrintWithCloudPrint() { | |
| 137 return this.id_ == Destination.GooglePromotedId.PRINT_WITH_CLOUD_PRINT; | |
| 138 }, | |
| 139 | |
| 140 /** | |
| 141 * @return {string} The location of the destination, or an empty string if | |
| 142 * the location is unknown. | |
| 143 */ | |
| 144 get location() { | |
| 145 if (this.location_ == null) { | |
| 146 for (var tag, i = 0; tag = this.tags_[i]; i++) { | |
| 147 if (tag.indexOf(Destination.LOCATION_TAG_PREFIX) == 0) { | |
| 148 this.location_ = tag.substring( | |
| 149 Destination.LOCATION_TAG_PREFIX.length); | |
| 150 } | |
| 151 } | |
| 152 if (this.location_ == null) { | |
| 153 this.location_ = ''; | |
| 154 } | |
| 155 } | |
| 156 return this.location_; | |
| 157 }, | |
| 158 | |
| 159 /** @return {!Array.<string>} Tags associated with the destination. */ | |
| 160 get tags() { | |
| 161 return this.tags_.slice(0); | |
| 162 }, | |
| 163 | |
| 164 /** | |
| 165 * @return {print_preview.ChromiumCapabilities} Print capabilities of the | |
| 166 * destination. | |
| 167 */ | |
| 168 get capabilities() { | |
| 169 return this.capabilities_; | |
| 170 }, | |
| 171 | |
| 172 /** | |
| 173 * @param {!print_preview.ChromiumCapabilities} capabilities Print | |
| 174 * capabilities of the destination. | |
| 175 */ | |
| 176 set capabilities(capabilities) { | |
| 177 this.capabilities_ = capabilities; | |
| 178 }, | |
| 179 | |
| 180 /** | |
| 181 * Matches a query against the destination. | |
| 182 * @param {string} query Query to match against the destination. | |
| 183 * @return {boolean} {@code true} if the query matches this destination, | |
| 184 * {@code false} otherwise. | |
| 185 */ | |
| 186 matches: function(query) { | |
| 187 return this.displayName_.toLowerCase().indexOf( | |
| 188 query.toLowerCase().trim()) != -1; | |
| 189 }, | |
| 190 | |
| 191 /** @return {string} Serialized representation of the destination. */ | |
| 192 serialize: function() { | |
| 193 var state = { | |
| 194 'id': this.id_, | |
| 195 'displayName': this.displayName_, | |
| 196 'isRecent': this.isRecent_, | |
| 197 'isLocal': this.isLocal_, | |
| 198 'tags': this.tags_ | |
| 199 }; | |
| 200 if (this.capabilities_) { | |
| 201 state['capabilities'] = this.capabilities_.serialize(); | |
|
dpapad
2012/05/09 15:48:56
Could you get the capabilities object unserialized
Robert Toscano
2012/05/10 00:23:27
Got rid of the serialization code for now.
| |
| 202 } | |
| 203 return JSON.stringify(state); | |
| 204 } | |
| 205 }; | |
| 206 | |
| 207 // Export | |
| 208 return { | |
| 209 Destination: Destination | |
| 210 }; | |
| 211 }); | |
| OLD | NEW |