| 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 * Object used to get and persist the print preview application state. |
| 10 * @constructor |
| 11 */ |
| 12 function AppState() { |
| 13 /** |
| 14 * ID of the selected destination. |
| 15 * @type {?string} |
| 16 * @private |
| 17 */ |
| 18 this.selectedDestinationId_ = null; |
| 19 |
| 20 /** |
| 21 * Whether the selected destination is a local destination. |
| 22 * @type {?boolean} |
| 23 * @private |
| 24 */ |
| 25 this.isSelectedDestinationLocal_ = null; |
| 26 |
| 27 /** |
| 28 * Margins type. |
| 29 * @type {print_preview.ticket_items.MarginsType.Value} |
| 30 * @private |
| 31 */ |
| 32 this.marginsType_ = null; |
| 33 |
| 34 /** |
| 35 * Custom margins. |
| 36 * @type {print_preview.Margins} |
| 37 * @private |
| 38 */ |
| 39 this.customMargins_ = null; |
| 40 |
| 41 /** |
| 42 * Whether the color option is enabled. |
| 43 * @type {?boolean} |
| 44 * @private |
| 45 */ |
| 46 this.isColorEnabled_ = null; |
| 47 |
| 48 /** |
| 49 * Whether duplex printing is enabled. |
| 50 * @type {?boolean} |
| 51 * @private |
| 52 */ |
| 53 this.isDuplexEnabled_ = null; |
| 54 |
| 55 /** |
| 56 * Whether the header-footer option is enabled. |
| 57 * @type {?boolean} |
| 58 * @private |
| 59 */ |
| 60 this.isHeaderFooterEnabled_ = null; |
| 61 |
| 62 /** |
| 63 * Whether landscape page orientation is selected. |
| 64 * @type {?boolean} |
| 65 * @private |
| 66 */ |
| 67 this.isLandscapeEnabled_ = null; |
| 68 |
| 69 /** |
| 70 * Whether printing collation is enabled. |
| 71 * @type {?boolean} |
| 72 * @private |
| 73 */ |
| 74 this.isCollateEnabled_ = null; |
| 75 }; |
| 76 |
| 77 /** |
| 78 * Current version of the app state. This value helps to understand how to |
| 79 * parse earlier versions of the app state. |
| 80 * @type {number} |
| 81 * @const |
| 82 * @private |
| 83 */ |
| 84 AppState.VERSION_ = 2; |
| 85 |
| 86 /** |
| 87 * Enumeration of field names for serialized app state. |
| 88 * @enum {string} |
| 89 * @private |
| 90 */ |
| 91 AppState.Field_ = { |
| 92 VERSION: 'version', |
| 93 SELECTED_DESTINATION_ID: 'selectedDestinationId', |
| 94 IS_SELECTED_DESTINATION_LOCAL: 'isSelectedDestinationLocal', |
| 95 MARGINS_TYPE: 'marginsType', |
| 96 CUSTOM_MARGINS: 'customMargins', |
| 97 IS_COLOR_ENABLED: 'isColorEnabled', |
| 98 IS_DUPLEX_ENABLED: 'isDuplexEnabled', |
| 99 IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', |
| 100 IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', |
| 101 IS_COLLATE_ENABLED: 'isCollateEnabled' |
| 102 }; |
| 103 |
| 104 /** |
| 105 * Name of C++ layer function to persist app state. |
| 106 * @type {string} |
| 107 * @const |
| 108 * @private |
| 109 */ |
| 110 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; |
| 111 |
| 112 AppState.prototype = { |
| 113 /** @return {?string} ID of the selected destination. */ |
| 114 get selectedDestinationId() { |
| 115 return this.selectedDestinationId_; |
| 116 }, |
| 117 |
| 118 /** @return {?boolean} Whether the selected destination is local. */ |
| 119 get isSelectedDestinationLocal() { |
| 120 return this.isSelectedDestinationLocal_; |
| 121 }, |
| 122 |
| 123 /** @return {print_preview.ticket_items.MarginsType.Value} Margins type. */ |
| 124 get marginsType() { |
| 125 return this.marginsType_; |
| 126 }, |
| 127 |
| 128 /** @return {print_preview.Margins} Custom margins. */ |
| 129 get customMargins() { |
| 130 return this.customMargins_; |
| 131 }, |
| 132 |
| 133 /** @return {?boolean} Whether the color option is enabled. */ |
| 134 get isColorEnabled() { |
| 135 return this.isColorEnabled_; |
| 136 }, |
| 137 |
| 138 /** @return {?boolean} Whether duplex printing is enabled. */ |
| 139 get isDuplexEnabled() { |
| 140 return this.isDuplexEnabled_; |
| 141 }, |
| 142 |
| 143 /** @return {?boolean} Whether the header-footer option is enabled. */ |
| 144 get isHeaderFooterEnabled() { |
| 145 return this.isHeaderFooterEnabled_; |
| 146 }, |
| 147 |
| 148 /** @return {?boolean} Whether landscape page orientation is selected. */ |
| 149 get isLandscapeEnabled() { |
| 150 return this.isLandscapeEnabled_; |
| 151 }, |
| 152 |
| 153 /** @return {?boolean} Whether printing collation is enabled. */ |
| 154 get isCollateEnabled() { |
| 155 return this.isCollateEnabled_; |
| 156 }, |
| 157 |
| 158 /** |
| 159 * Initializes the app state from a serialized string returned by the native |
| 160 * layer. |
| 161 * @param {?string} serializedAppStateStr Serialized string representation |
| 162 * of the app state. |
| 163 */ |
| 164 init: function(serializedAppStateStr) { |
| 165 if (!serializedAppStateStr) { |
| 166 return; |
| 167 } |
| 168 |
| 169 var state = JSON.parse(serializedAppStateStr); |
| 170 if (state[AppState.Field_.VERSION] == 2) { |
| 171 this.selectedDestinationId_ = |
| 172 state[AppState.Field_.SELECTED_DESTINATION_ID] || null; |
| 173 if (state.hasOwnProperty( |
| 174 AppState.Field_.IS_SELECTED_DESTINATION_LOCAL)) { |
| 175 this.isSelectedDestinationLocal_ = |
| 176 state[AppState.Field_.IS_SELECTED_DESTINATION_LOCAL]; |
| 177 } |
| 178 if (state.hasOwnProperty(AppState.Field_.MARGINS_TYPE)) { |
| 179 this.marginsType_ = state[AppState.Field_.MARGINS_TYPE]; |
| 180 } |
| 181 if (state[AppState.Field_.CUSTOM_MARGINS]) { |
| 182 this.customMargins_ = print_preview.Margins.parse( |
| 183 state[AppState.Field_.CUSTOM_MARGINS]); |
| 184 } |
| 185 if (state.hasOwnProperty(AppState.Field_.IS_COLOR_ENABLED)) { |
| 186 this.isColorEnabled_ = state[AppState.Field_.IS_COLOR_ENABLED]; |
| 187 } |
| 188 if (state.hasOwnProperty(AppState.Field_.IS_DUPLEX_ENABLED)) { |
| 189 this.isDuplexEnabled_ = state[AppState.Field_.IS_DUPLEX_ENABLED]; |
| 190 } |
| 191 if (state.hasOwnProperty(AppState.Field_.IS_HEADER_FOOTER_ENABLED)) { |
| 192 this.isHeaderFooterEnabled_ = |
| 193 state[AppState.Field_.IS_HEADER_FOOTER_ENABLED]; |
| 194 } |
| 195 if (state.hasOwnProperty(AppState.Field_.IS_LANDSCAPE_ENABLED)) { |
| 196 this.isLandscapeEnabled_ = |
| 197 state[AppState.Field_.IS_LANDSCAPE_ENABLED]; |
| 198 } |
| 199 if (state.hasOwnProperty(AppState.Field_.IS_COLLATE_ENABLED)) { |
| 200 this.isCollateEnabled_ = state[AppState.Field_.IS_COLLATE_ENABLED]; |
| 201 } |
| 202 } |
| 203 }, |
| 204 |
| 205 /** |
| 206 * Persists the selected destination. |
| 207 * @param {!print_preview.Destination} dest Destination to persist. |
| 208 */ |
| 209 persistSelectedDestination: function(dest) { |
| 210 this.selectedDestinationId_ = dest.id; |
| 211 this.isSelectedDestinationLocal_ = dest.isLocal; |
| 212 this.persist_(); |
| 213 }, |
| 214 |
| 215 /** |
| 216 * Persists the margins type. |
| 217 * @param {print_preview.ticket_items.MarginsType.Value} marginsType Margins |
| 218 * type. |
| 219 */ |
| 220 persistMarginsType: function(marginsType) { |
| 221 this.marginsType_ = marginsType; |
| 222 this.persist_(); |
| 223 }, |
| 224 |
| 225 /** |
| 226 * Persists custom margins. |
| 227 * @param {print_preview.Margins} customMargins Custom margins. |
| 228 */ |
| 229 persistCustomMargins: function(customMargins) { |
| 230 this.customMargins_ = customMargins; |
| 231 this.persist_(); |
| 232 }, |
| 233 |
| 234 /** |
| 235 * Persists whether color printing is enabled. |
| 236 * @param {?boolean} isColorEnabled Whether color printing is enabled. |
| 237 */ |
| 238 persistIsColorEnabled: function(isColorEnabled) { |
| 239 this.isColorEnabled_ = isColorEnabled; |
| 240 this.persist_(); |
| 241 }, |
| 242 |
| 243 /** |
| 244 * Persists whether duplexing is enabled. |
| 245 * @param {?boolean} isDuplexEnabled Whether duplex printing is enabled. |
| 246 */ |
| 247 persistIsDuplexEnabled: function(isDuplexEnabled) { |
| 248 this.isDuplexEnabled_ = isDuplexEnabled; |
| 249 this.persist_(); |
| 250 }, |
| 251 |
| 252 /** |
| 253 * Persists whether header-footer is enabled. |
| 254 * @param {?boolean} Whether header-footer is enabled. |
| 255 */ |
| 256 persistIsHeaderFooterEnabled: function(isHeaderFooterEnabled) { |
| 257 this.isHeaderFooterEnabled_ = isHeaderFooterEnabled; |
| 258 this.persist_(); |
| 259 }, |
| 260 |
| 261 /** |
| 262 * Persists whether landscape printing is enabled. |
| 263 * @param {?boolean} isLandscapeEnabled landscape printing is enabled. |
| 264 */ |
| 265 persistIsLandscapeEnabled: function(isLandscapeEnabled) { |
| 266 this.isLandscapeEnabled_ = isLandscapeEnabled; |
| 267 this.persist_(); |
| 268 }, |
| 269 |
| 270 /** |
| 271 * Persists whether printing collation is enabled. |
| 272 * @param {?boolean} isCollateEnabled Whether printing collation is enabled. |
| 273 */ |
| 274 persistIsCollateEnabled: function(isCollateEnabled) { |
| 275 this.isCollateEnabled_ = isCollateEnabled; |
| 276 this.persist_(); |
| 277 }, |
| 278 |
| 279 /** |
| 280 * Calls into the native layer to persist the application state. |
| 281 * @private |
| 282 */ |
| 283 persist_: function() { |
| 284 var obj = {}; |
| 285 obj[AppState.Field_.VERSION] = AppState.VERSION_; |
| 286 obj[AppState.Field_.SELECTED_DESTINATION_ID] = |
| 287 this.selectedDestinationId_; |
| 288 obj[AppState.Field_.IS_SELECTED_DESTINATION_LOCAL] = |
| 289 this.isSelectedDestinationLocal_; |
| 290 obj[AppState.Field_.MARGINS_TYPE] = this.marginsType_; |
| 291 if (this.customMargins_) { |
| 292 obj[AppState.Field_.CUSTOM_MARGINS] = this.customMargins_.serialize(); |
| 293 } |
| 294 obj[AppState.Field_.IS_COLOR_ENABLED] = this.isColorEnabled_; |
| 295 obj[AppState.Field_.IS_DUPLEX_ENABLED] = this.isDuplexEnabled_; |
| 296 obj[AppState.Field_.IS_HEADER_FOOTER_ENABLED] = |
| 297 this.isHeaderFooterEnabled_; |
| 298 obj[AppState.Field_.IS_LANDSCAPE_ENABLED] = this.isLandscapeEnabled_; |
| 299 obj[AppState.Field_.IS_COLLATE_ENABLED] = this.isCollateEnabled_; |
| 300 chrome.send(AppState.NATIVE_FUNCTION_NAME_, [JSON.stringify(obj)]); |
| 301 } |
| 302 }; |
| 303 |
| 304 return { |
| 305 AppState: AppState |
| 306 }; |
| 307 }); |
| OLD | NEW |