| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Object used to represent a recent destination in the app state. | 9 * Object used to represent a recent destination in the app state. |
| 10 * @constructor | 10 * @constructor |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 */ | 124 */ |
| 125 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; | 125 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; |
| 126 | 126 |
| 127 AppState.prototype = { | 127 AppState.prototype = { |
| 128 /** | 128 /** |
| 129 * @return {?RecentDestination} The most recent destination, which is | 129 * @return {?RecentDestination} The most recent destination, which is |
| 130 * currently the selected destination. | 130 * currently the selected destination. |
| 131 */ | 131 */ |
| 132 get selectedDestination() { | 132 get selectedDestination() { |
| 133 return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ? | 133 return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ? |
| 134 this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null; | 134 this.state_[AppState.Field.RECENT_DESTINATIONS][0] : |
| 135 null; |
| 135 }, | 136 }, |
| 136 | 137 |
| 137 /** | 138 /** |
| 138 * @return {boolean} Whether the selected destination is valid. | 139 * @return {boolean} Whether the selected destination is valid. |
| 139 */ | 140 */ |
| 140 isSelectedDestinationValid: function() { | 141 isSelectedDestinationValid: function() { |
| 141 return this.selectedDestination && | 142 return this.selectedDestination && this.selectedDestination.id && |
| 142 this.selectedDestination.id && | 143 this.selectedDestination.origin; |
| 143 this.selectedDestination.origin; | |
| 144 }, | 144 }, |
| 145 | 145 |
| 146 /** | 146 /** |
| 147 * @return {?Array<!RecentDestination>} The AppState.NUM_DESTINATIONS_ most | 147 * @return {?Array<!RecentDestination>} The AppState.NUM_DESTINATIONS_ most |
| 148 * recent destinations. | 148 * recent destinations. |
| 149 */ | 149 */ |
| 150 get recentDestinations() { | 150 get recentDestinations() { |
| 151 return this.state_[AppState.Field.RECENT_DESTINATIONS]; | 151 return this.state_[AppState.Field.RECENT_DESTINATIONS]; |
| 152 }, | 152 }, |
| 153 | 153 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 165 return this.state_.hasOwnProperty(field); | 165 return this.state_.hasOwnProperty(field); |
| 166 }, | 166 }, |
| 167 | 167 |
| 168 /** | 168 /** |
| 169 * @param {!print_preview.AppState.Field} field App state field to get. | 169 * @param {!print_preview.AppState.Field} field App state field to get. |
| 170 * @return {?} Value of the app state field. | 170 * @return {?} Value of the app state field. |
| 171 */ | 171 */ |
| 172 getField: function(field) { | 172 getField: function(field) { |
| 173 if (field == AppState.Field.CUSTOM_MARGINS) { | 173 if (field == AppState.Field.CUSTOM_MARGINS) { |
| 174 return this.state_[field] ? | 174 return this.state_[field] ? |
| 175 print_preview.Margins.parse(this.state_[field]) : null; | 175 print_preview.Margins.parse(this.state_[field]) : |
| 176 null; |
| 176 } else { | 177 } else { |
| 177 return this.state_[field]; | 178 return this.state_[field]; |
| 178 } | 179 } |
| 179 }, | 180 }, |
| 180 | 181 |
| 181 /** | 182 /** |
| 182 * Initializes the app state from a serialized string returned by the native | 183 * Initializes the app state from a serialized string returned by the native |
| 183 * layer. | 184 * layer. |
| 184 * @param {?string} serializedAppStateStr Serialized string representation | 185 * @param {?string} serializedAppStateStr Serialized string representation |
| 185 * of the app state. | 186 * of the app state. |
| 186 */ | 187 */ |
| 187 init: function(serializedAppStateStr) { | 188 init: function(serializedAppStateStr) { |
| 188 if (serializedAppStateStr) { | 189 if (serializedAppStateStr) { |
| 189 try { | 190 try { |
| 190 var state = JSON.parse(serializedAppStateStr); | 191 var state = JSON.parse(serializedAppStateStr); |
| 191 if (state[AppState.Field.VERSION] == AppState.VERSION_) { | 192 if (state[AppState.Field.VERSION] == AppState.VERSION_) { |
| 192 this.state_ = state; | 193 this.state_ = state; |
| 193 } | 194 } |
| 194 } catch(e) { | 195 } catch (e) { |
| 195 console.error('Unable to parse state: ' + e); | 196 console.error('Unable to parse state: ' + e); |
| 196 // Proceed with default state. | 197 // Proceed with default state. |
| 197 } | 198 } |
| 198 } else { | 199 } else { |
| 199 // Set some state defaults. | 200 // Set some state defaults. |
| 200 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; | 201 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; |
| 201 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; | 202 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
| 202 } | 203 } |
| 203 if (!this.state_[AppState.Field.RECENT_DESTINATIONS]) { | 204 if (!this.state_[AppState.Field.RECENT_DESTINATIONS]) { |
| 204 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; | 205 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
| 205 } else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof | 206 } else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof |
| 206 Array)) { | 207 Array)) { |
| 207 var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS]; | 208 var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS]; |
| 208 this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp]; | 209 this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp]; |
| 209 } else if (!this.state_[AppState.Field.RECENT_DESTINATIONS][0] || | 210 } else if ( |
| 211 !this.state_[AppState.Field.RECENT_DESTINATIONS][0] || |
| 210 !this.state_[AppState.Field.RECENT_DESTINATIONS][0].id) { | 212 !this.state_[AppState.Field.RECENT_DESTINATIONS][0].id) { |
| 211 // read in incorrectly | 213 // read in incorrectly |
| 212 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; | 214 this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
| 213 } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length > | 215 } else if ( |
| 216 this.state_[AppState.Field.RECENT_DESTINATIONS].length > |
| 214 AppState.NUM_DESTINATIONS_) { | 217 AppState.NUM_DESTINATIONS_) { |
| 215 this.state_[AppState.Field.RECENT_DESTINATIONS].length = | 218 this.state_[AppState.Field.RECENT_DESTINATIONS].length = |
| 216 AppState.NUM_DESTINATIONS_; | 219 AppState.NUM_DESTINATIONS_; |
| 217 } | 220 } |
| 218 if (!loadTimeData.getBoolean('scalingEnabled')) | 221 if (!loadTimeData.getBoolean('scalingEnabled')) |
| 219 this.state_[AppState.Field.SCALING] = 100; | 222 this.state_[AppState.Field.SCALING] = 100; |
| 220 | 223 |
| 221 }, | 224 }, |
| 222 | 225 |
| 223 /** | 226 /** |
| (...skipping 23 matching lines...) Expand all Loading... |
| 247 * Persists the selected destination. | 250 * Persists the selected destination. |
| 248 * @param {!print_preview.Destination} dest Destination to persist. | 251 * @param {!print_preview.Destination} dest Destination to persist. |
| 249 */ | 252 */ |
| 250 persistSelectedDestination: function(dest) { | 253 persistSelectedDestination: function(dest) { |
| 251 if (!this.isInitialized_) | 254 if (!this.isInitialized_) |
| 252 return; | 255 return; |
| 253 | 256 |
| 254 // Determine if this destination is already in the recent destinations, | 257 // Determine if this destination is already in the recent destinations, |
| 255 // and where in the array it is located. | 258 // and where in the array it is located. |
| 256 var newDestination = new RecentDestination(dest); | 259 var newDestination = new RecentDestination(dest); |
| 257 var indexFound = this.state_[ | 260 var indexFound = |
| 258 AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) { | 261 this.state_[AppState.Field.RECENT_DESTINATIONS].findIndex(function( |
| 259 return (newDestination.id == recent.id && | 262 recent) { |
| 260 newDestination.origin == recent.origin); | 263 return ( |
| 264 newDestination.id == recent.id && |
| 265 newDestination.origin == recent.origin); |
| 261 }); | 266 }); |
| 262 | 267 |
| 263 // No change | 268 // No change |
| 264 if (indexFound == 0) { | 269 if (indexFound == 0) { |
| 265 this.persist_(); | 270 this.persist_(); |
| 266 return; | 271 return; |
| 267 } | 272 } |
| 268 | 273 |
| 269 // Shift the array so that the nth most recent destination is located at | 274 // Shift the array so that the nth most recent destination is located at |
| 270 // index n. | 275 // index n. |
| 271 if (indexFound == -1 && | 276 if (indexFound == -1 && |
| 272 this.state_[AppState.Field.RECENT_DESTINATIONS].length == | 277 this.state_[AppState.Field.RECENT_DESTINATIONS].length == |
| 273 AppState.NUM_DESTINATIONS_) { | 278 AppState.NUM_DESTINATIONS_) { |
| 274 indexFound = AppState.NUM_DESTINATIONS_ - 1; | 279 indexFound = AppState.NUM_DESTINATIONS_ - 1; |
| 275 } | 280 } |
| 276 if (indexFound != -1) | 281 if (indexFound != -1) |
| 277 this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1); | 282 this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1); |
| 278 | 283 |
| 279 // Add the most recent destination | 284 // Add the most recent destination |
| 280 this.state_[AppState.Field.RECENT_DESTINATIONS].splice( | 285 this.state_[AppState.Field.RECENT_DESTINATIONS].splice( |
| 281 0, 0, newDestination); | 286 0, 0, newDestination); |
| 282 | 287 |
| 283 this.persist_(); | 288 this.persist_(); |
| 284 }, | 289 }, |
| 285 | 290 |
| 286 /** | 291 /** |
| 287 * Persists whether the GCP promotion has been dismissed. | 292 * Persists whether the GCP promotion has been dismissed. |
| 288 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been | 293 * @param {boolean} isGcpPromoDismissed Whether the GCP promotion has been |
| 289 * dismissed. | 294 * dismissed. |
| 290 */ | 295 */ |
| 291 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { | 296 persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { |
| 292 if (!this.isInitialized_) | 297 if (!this.isInitialized_) |
| 293 return; | 298 return; |
| 294 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; | 299 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; |
| 295 this.persist_(); | 300 this.persist_(); |
| 296 }, | 301 }, |
| 297 | 302 |
| 298 /** | 303 /** |
| 299 * Calls into the native layer to persist the application state. | 304 * Calls into the native layer to persist the application state. |
| 300 * @private | 305 * @private |
| 301 */ | 306 */ |
| 302 persist_: function() { | 307 persist_: function() { |
| 303 chrome.send(AppState.NATIVE_FUNCTION_NAME_, | 308 chrome.send( |
| 304 [JSON.stringify(this.state_)]); | 309 AppState.NATIVE_FUNCTION_NAME_, [JSON.stringify(this.state_)]); |
| 305 } | 310 } |
| 306 }; | 311 }; |
| 307 | 312 |
| 308 return { | 313 return {AppState: AppState}; |
| 309 AppState: AppState | |
| 310 }; | |
| 311 }); | 314 }); |
| OLD | NEW |