| 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 get and persist the print preview application state. | 9 * Object used to get and persist the print preview application state. |
| 10 * @constructor | 10 * @constructor |
| 11 */ | 11 */ |
| 12 function AppState() { | 12 function AppState() { |
| 13 /** | 13 /** |
| 14 * Internal representation of application state. | 14 * Internal representation of application state. |
| 15 * @type {Object.<string, print_preview.ValueType>} | 15 * @type {Object.<string, ?>} |
| 16 * @private | 16 * @private |
| 17 */ | 17 */ |
| 18 this.state_ = {}; | 18 this.state_ = {}; |
| 19 this.state_[AppState.Field.VERSION] = AppState.VERSION_; | 19 this.state_[AppState.Field.VERSION] = AppState.VERSION_; |
| 20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; | 20 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Whether the app state has been initialized. The app state will ignore all | 23 * Whether the app state has been initialized. The app state will ignore all |
| 24 * writes until it has been initialized. | 24 * writes until it has been initialized. |
| 25 * @type {boolean} | 25 * @type {boolean} |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 * @param {!print_preview.AppState.Field} field App state field to check if | 107 * @param {!print_preview.AppState.Field} field App state field to check if |
| 108 * set. | 108 * set. |
| 109 * @return {boolean} Whether a field has been set in the app state. | 109 * @return {boolean} Whether a field has been set in the app state. |
| 110 */ | 110 */ |
| 111 hasField: function(field) { | 111 hasField: function(field) { |
| 112 return this.state_.hasOwnProperty(field); | 112 return this.state_.hasOwnProperty(field); |
| 113 }, | 113 }, |
| 114 | 114 |
| 115 /** | 115 /** |
| 116 * @param {!print_preview.AppState.Field} field App state field to get. | 116 * @param {!print_preview.AppState.Field} field App state field to get. |
| 117 * @return {print_preview.ValueType} Value of the app state field. | 117 * @return {?} Value of the app state field. |
| 118 */ | 118 */ |
| 119 getField: function(field) { | 119 getField: function(field) { |
| 120 if (field == AppState.Field.CUSTOM_MARGINS) { | 120 if (field == AppState.Field.CUSTOM_MARGINS) { |
| 121 return this.state_[field] ? | 121 return this.state_[field] ? |
| 122 print_preview.Margins.parse(this.state_[field]) : null; | 122 print_preview.Margins.parse(this.state_[field]) : null; |
| 123 } else { | 123 } else { |
| 124 return this.state_[field]; | 124 return this.state_[field]; |
| 125 } | 125 } |
| 126 }, | 126 }, |
| 127 | 127 |
| 128 /** | 128 /** |
| 129 * Initializes the app state from a serialized string returned by the native | 129 * Initializes the app state from a serialized string returned by the native |
| 130 * layer. | 130 * layer. |
| 131 * @param {?string} serializedAppStateStr Serialized string representation | 131 * @param {?string} serializedAppStateStr Serialized string representation |
| 132 * of the app state. | 132 * of the app state. |
| 133 * @param {?string} systemDefaultDestinationId ID of the system default | 133 * @param {?string} systemDefaultDestinationId ID of the system default |
| 134 * destination. | 134 * destination. |
| 135 */ | 135 */ |
| 136 init: function(serializedAppStateStr, systemDefaultDestinationId) { | 136 init: function(serializedAppStateStr, systemDefaultDestinationId) { |
| 137 if (serializedAppStateStr) { | 137 if (serializedAppStateStr) { |
| 138 var state = JSON.parse(serializedAppStateStr); | 138 var state = /** @type {Object.<string, ?>} */( |
| 139 JSON.parse(serializedAppStateStr)); |
| 139 if (state[AppState.Field.VERSION] == AppState.VERSION_) { | 140 if (state[AppState.Field.VERSION] == AppState.VERSION_) { |
| 140 this.state_ = state; | 141 this.state_ = state; |
| 141 } | 142 } |
| 142 } else { | 143 } else { |
| 143 // Set some state defaults. | 144 // Set some state defaults. |
| 144 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; | 145 this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; |
| 145 } | 146 } |
| 146 // Default to system destination, if no destination was selected. | 147 // Default to system destination, if no destination was selected. |
| 147 if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] || | 148 if (!this.state_[AppState.Field.SELECTED_DESTINATION_ID] || |
| 148 !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) { | 149 !this.state_[AppState.Field.SELECTED_DESTINATION_ORIGIN]) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 159 /** | 160 /** |
| 160 * Sets to initialized state. Now object will accept persist requests. | 161 * Sets to initialized state. Now object will accept persist requests. |
| 161 */ | 162 */ |
| 162 setInitialized: function() { | 163 setInitialized: function() { |
| 163 this.isInitialized_ = true; | 164 this.isInitialized_ = true; |
| 164 }, | 165 }, |
| 165 | 166 |
| 166 /** | 167 /** |
| 167 * Persists the given value for the given field. | 168 * Persists the given value for the given field. |
| 168 * @param {!print_preview.AppState.Field} field Field to persist. | 169 * @param {!print_preview.AppState.Field} field Field to persist. |
| 169 * @param {print_preview.ValueType} value Value of field to persist. | 170 * @param {?} value Value of field to persist. |
| 170 */ | 171 */ |
| 171 persistField: function(field, value) { | 172 persistField: function(field, value) { |
| 172 if (!this.isInitialized_) | 173 if (!this.isInitialized_) |
| 173 return; | 174 return; |
| 174 if (field == AppState.Field.CUSTOM_MARGINS) { | 175 if (field == AppState.Field.CUSTOM_MARGINS) { |
| 175 this.state_[field] = value ? value.serialize() : null; | 176 this.state_[field] = value ? value.serialize() : null; |
| 176 } else { | 177 } else { |
| 177 this.state_[field] = value; | 178 this.state_[field] = value; |
| 178 } | 179 } |
| 179 this.persist_(); | 180 this.persist_(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 persist_: function() { | 215 persist_: function() { |
| 215 chrome.send(AppState.NATIVE_FUNCTION_NAME_, | 216 chrome.send(AppState.NATIVE_FUNCTION_NAME_, |
| 216 [JSON.stringify(this.state_)]); | 217 [JSON.stringify(this.state_)]); |
| 217 } | 218 } |
| 218 }; | 219 }; |
| 219 | 220 |
| 220 return { | 221 return { |
| 221 AppState: AppState | 222 AppState: AppState |
| 222 }; | 223 }; |
| 223 }); | 224 }); |
| OLD | NEW |