| 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.exportPath('print_preview'); | 5 cr.exportPath('print_preview'); |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Enumeration of field names for serialized app state. | 8 * Enumeration of field names for serialized app state. |
| 9 * @enum {string} | 9 * @enum {string} |
| 10 */ | 10 */ |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 * @private | 125 * @private |
| 126 */ | 126 */ |
| 127 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; | 127 AppState.NATIVE_FUNCTION_NAME_ = 'saveAppState'; |
| 128 | 128 |
| 129 AppState.prototype = { | 129 AppState.prototype = { |
| 130 /** | 130 /** |
| 131 * @return {?RecentDestination} The most recent destination, | 131 * @return {?RecentDestination} The most recent destination, |
| 132 * which is currently the selected destination. | 132 * which is currently the selected destination. |
| 133 */ | 133 */ |
| 134 get selectedDestination() { | 134 get selectedDestination() { |
| 135 return (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]. | 135 return (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] |
| 136 length > 0) ? | 136 .length > 0) ? |
| 137 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0] : | 137 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0] : |
| 138 null; | 138 null; |
| 139 }, | 139 }, |
| 140 | 140 |
| 141 /** | 141 /** |
| 142 * @return {boolean} Whether the selected destination is valid. | 142 * @return {boolean} Whether the selected destination is valid. |
| 143 */ | 143 */ |
| 144 isSelectedDestinationValid: function() { | 144 isSelectedDestinationValid: function() { |
| 145 return !!this.selectedDestination && | 145 return !!this.selectedDestination && !!this.selectedDestination.id && |
| 146 !!this.selectedDestination.id && | 146 !!this.selectedDestination.origin; |
| 147 !!this.selectedDestination.origin; | |
| 148 }, | 147 }, |
| 149 | 148 |
| 150 /** | 149 /** |
| 151 * @return {?Array<!RecentDestination>} The | 150 * @return {?Array<!RecentDestination>} The |
| 152 * AppState.NUM_DESTINATIONS_ most recent destinations. | 151 * AppState.NUM_DESTINATIONS_ most recent destinations. |
| 153 */ | 152 */ |
| 154 get recentDestinations() { | 153 get recentDestinations() { |
| 155 return this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; | 154 return this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; |
| 156 }, | 155 }, |
| 157 | 156 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 169 return this.state_.hasOwnProperty(field); | 168 return this.state_.hasOwnProperty(field); |
| 170 }, | 169 }, |
| 171 | 170 |
| 172 /** | 171 /** |
| 173 * @param {!print_preview.AppStateField} field App state field to get. | 172 * @param {!print_preview.AppStateField} field App state field to get. |
| 174 * @return {?} Value of the app state field. | 173 * @return {?} Value of the app state field. |
| 175 */ | 174 */ |
| 176 getField: function(field) { | 175 getField: function(field) { |
| 177 if (field == print_preview.AppStateField.CUSTOM_MARGINS) { | 176 if (field == print_preview.AppStateField.CUSTOM_MARGINS) { |
| 178 return this.state_[field] ? | 177 return this.state_[field] ? |
| 179 print_preview.Margins.parse(this.state_[field]) : null; | 178 print_preview.Margins.parse(this.state_[field]) : |
| 179 null; |
| 180 } else { | 180 } else { |
| 181 return this.state_[field]; | 181 return this.state_[field]; |
| 182 } | 182 } |
| 183 }, | 183 }, |
| 184 | 184 |
| 185 /** | 185 /** |
| 186 * Initializes the app state from a serialized string returned by the native | 186 * Initializes the app state from a serialized string returned by the native |
| 187 * layer. | 187 * layer. |
| 188 * @param {?string} serializedAppStateStr Serialized string representation | 188 * @param {?string} serializedAppStateStr Serialized string representation |
| 189 * of the app state. | 189 * of the app state. |
| 190 */ | 190 */ |
| 191 init: function(serializedAppStateStr) { | 191 init: function(serializedAppStateStr) { |
| 192 if (serializedAppStateStr) { | 192 if (serializedAppStateStr) { |
| 193 try { | 193 try { |
| 194 var state = JSON.parse(serializedAppStateStr); | 194 var state = JSON.parse(serializedAppStateStr); |
| 195 if (state[print_preview.AppStateField.VERSION] == AppState.VERSION_) { | 195 if (state[print_preview.AppStateField.VERSION] == AppState.VERSION_) { |
| 196 this.state_ = /** @type {Object} */(state); | 196 this.state_ = /** @type {Object} */ (state); |
| 197 } | 197 } |
| 198 } catch(e) { | 198 } catch (e) { |
| 199 console.error('Unable to parse state: ' + e); | 199 console.error('Unable to parse state: ' + e); |
| 200 // Proceed with default state. | 200 // Proceed with default state. |
| 201 } | 201 } |
| 202 } else { | 202 } else { |
| 203 // Set some state defaults. | 203 // Set some state defaults. |
| 204 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = false; | 204 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = false; |
| 205 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; | 205 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
| 206 } | 206 } |
| 207 if (!this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]) { | 207 if (!this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]) { |
| 208 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; | 208 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
| 209 } else if (!(this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] | 209 } else if (!(this.state_[print_preview.AppStateField |
| 210 instanceof Array)) { | 210 .RECENT_DESTINATIONS] instanceof |
| 211 Array)) { |
| 211 var tmp = this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; | 212 var tmp = this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; |
| 212 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [tmp]; | 213 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [tmp]; |
| 213 } else if (!this.state_[ | 214 } else if ( |
| 214 print_preview.AppStateField.RECENT_DESTINATIONS][0] || | 215 !this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0] || |
| 215 !this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0].id) { | 216 !this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0].id) { |
| 216 // read in incorrectly | 217 // read in incorrectly |
| 217 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; | 218 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
| 218 } else if (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]. | 219 } else if ( |
| 219 length > AppState.NUM_DESTINATIONS_) { | 220 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length > |
| 221 AppState.NUM_DESTINATIONS_) { |
| 220 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length = | 222 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length = |
| 221 AppState.NUM_DESTINATIONS_; | 223 AppState.NUM_DESTINATIONS_; |
| 222 } | 224 } |
| 223 }, | 225 }, |
| 224 | 226 |
| 225 /** | 227 /** |
| 226 * Sets to initialized state. Now object will accept persist requests. | 228 * Sets to initialized state. Now object will accept persist requests. |
| 227 */ | 229 */ |
| 228 setInitialized: function() { | 230 setInitialized: function() { |
| 229 this.isInitialized_ = true; | 231 this.isInitialized_ = true; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 249 * Persists the selected destination. | 251 * Persists the selected destination. |
| 250 * @param {!print_preview.Destination} dest Destination to persist. | 252 * @param {!print_preview.Destination} dest Destination to persist. |
| 251 */ | 253 */ |
| 252 persistSelectedDestination: function(dest) { | 254 persistSelectedDestination: function(dest) { |
| 253 if (!this.isInitialized_) | 255 if (!this.isInitialized_) |
| 254 return; | 256 return; |
| 255 | 257 |
| 256 // Determine if this destination is already in the recent destinations, | 258 // Determine if this destination is already in the recent destinations, |
| 257 // and where in the array it is located. | 259 // and where in the array it is located. |
| 258 var newDestination = new RecentDestination(dest); | 260 var newDestination = new RecentDestination(dest); |
| 259 var indexFound = this.state_[ | 261 var indexFound = |
| 260 print_preview.AppStateField.RECENT_DESTINATIONS].findIndex( | 262 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] |
| 261 function(recent) { | 263 .findIndex(function(recent) { |
| 262 return (newDestination.id == recent.id && | 264 return ( |
| 263 newDestination.origin == recent.origin); | 265 newDestination.id == recent.id && |
| 266 newDestination.origin == recent.origin); |
| 264 }); | 267 }); |
| 265 | 268 |
| 266 // No change | 269 // No change |
| 267 if (indexFound == 0) { | 270 if (indexFound == 0) { |
| 268 this.persist_(); | 271 this.persist_(); |
| 269 return; | 272 return; |
| 270 } | 273 } |
| 271 | 274 |
| 272 // Shift the array so that the nth most recent destination is located at | 275 // Shift the array so that the nth most recent destination is located at |
| 273 // index n. | 276 // index n. |
| 274 if (indexFound == -1 && | 277 if (indexFound == -1 && |
| 275 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length == | 278 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length == |
| 276 AppState.NUM_DESTINATIONS_) { | 279 AppState.NUM_DESTINATIONS_) { |
| 277 indexFound = AppState.NUM_DESTINATIONS_ - 1; | 280 indexFound = AppState.NUM_DESTINATIONS_ - 1; |
| 278 } | 281 } |
| 279 if (indexFound != -1) | 282 if (indexFound != -1) |
| 280 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( | 283 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( |
| 281 indexFound, 1); | 284 indexFound, 1); |
| 282 | 285 |
| 283 // Add the most recent destination | 286 // Add the most recent destination |
| 284 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( | 287 this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( |
| 285 0, 0, newDestination); | 288 0, 0, newDestination); |
| 286 | 289 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 298 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = | 301 this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = |
| 299 isGcpPromoDismissed; | 302 isGcpPromoDismissed; |
| 300 this.persist_(); | 303 this.persist_(); |
| 301 }, | 304 }, |
| 302 | 305 |
| 303 /** | 306 /** |
| 304 * Calls into the native layer to persist the application state. | 307 * Calls into the native layer to persist the application state. |
| 305 * @private | 308 * @private |
| 306 */ | 309 */ |
| 307 persist_: function() { | 310 persist_: function() { |
| 308 chrome.send(AppState.NATIVE_FUNCTION_NAME_, | 311 chrome.send( |
| 309 [JSON.stringify(this.state_)]); | 312 AppState.NATIVE_FUNCTION_NAME_, [JSON.stringify(this.state_)]); |
| 310 } | 313 } |
| 311 }; | 314 }; |
| 312 | 315 |
| 313 return { | 316 return {AppState: AppState}; |
| 314 AppState: AppState | |
| 315 }; | |
| 316 }); | 317 }); |
| OLD | NEW |