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