Index: chrome/browser/resources/print_preview/data/app_state.js |
diff --git a/chrome/browser/resources/print_preview/data/app_state.js b/chrome/browser/resources/print_preview/data/app_state.js |
index df06e8b6e7458f15c155d1566d725af21d385daa..4846941cfa6607e674cee6a89cd66e6884236042 100644 |
--- a/chrome/browser/resources/print_preview/data/app_state.js |
+++ b/chrome/browser/resources/print_preview/data/app_state.js |
@@ -2,56 +2,82 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-cr.define('print_preview', function() { |
- 'use strict'; |
+cr.exportPath('print_preview'); |
+ |
+/** |
+ * Enumeration of field names for serialized app state. |
+ * @enum {string} |
+ */ |
+print_preview.AppStateField = { |
+ VERSION: 'version', |
+ RECENT_DESTINATIONS: 'recentDestinations', |
+ IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed', |
+ DPI: 'dpi', |
+ MEDIA_SIZE: 'mediaSize', |
+ MARGINS_TYPE: 'marginsType', |
+ CUSTOM_MARGINS: 'customMargins', |
+ IS_COLOR_ENABLED: 'isColorEnabled', |
+ IS_DUPLEX_ENABLED: 'isDuplexEnabled', |
+ IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', |
+ IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', |
+ IS_COLLATE_ENABLED: 'isCollateEnabled', |
+ IS_FIT_TO_PAGE_ENABLED: 'isFitToPageEnabled', |
+ IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled', |
+ SCALING: 'scaling', |
+ VENDOR_OPTIONS: 'vendorOptions' |
+}; |
+ |
+/** |
+ * Object used to represent a recent destination in the app state. |
+ * @constructor |
+ * @struct |
+ */ |
+function RecentDestination(destination) { |
+ /** |
+ * ID of the RecentDestination. |
+ * @type {string} |
+ */ |
+ this.id = destination.id; |
/** |
- * Object used to represent a recent destination in the app state. |
- * @constructor |
+ * Origin of the RecentDestination. |
+ * @type {string} |
*/ |
- function RecentDestination(destination) { |
- /** |
- * ID of the RecentDestination. |
- * @type {string} |
- */ |
- this.id = destination.id; |
+ this.origin = destination.origin; |
- /** |
- * Origin of the RecentDestination. |
- * @type {string} |
- */ |
- this.origin = destination.origin; |
+ /** |
+ * Account the RecentDestination is registered for. |
+ * @type {string} |
+ */ |
+ this.account = destination.account || ''; |
- /** |
- * Account the RecentDestination is registered for. |
- * @type {string} |
- */ |
- this.account = destination.account || ''; |
+ /** |
+ * CDD of the RecentDestination. |
+ * @type {print_preview.Cdd} |
+ */ |
+ this.capabilities = destination.capabilities; |
- /** |
- * CDD of the RecentDestination. |
- * @type {print_preview.Cdd} |
- */ |
- this.capabilities = destination.capabilities; |
+ /** |
+ * Name of the RecentDestination. |
+ * @type {string} |
+ */ |
+ this.name = destination.name || ''; |
- /** |
- * Name of the RecentDestination. |
- * @type {string} |
- */ |
- this.name = destination.name || ''; |
+ /** |
+ * Extension ID associated with the RecentDestination. |
+ * @type {string} |
+ */ |
+ this.extensionId = destination.extension_id || ''; |
- /** |
- * Extension ID associated with the RecentDestination. |
- * @type {string} |
- */ |
- this.extensionId = destination.extension_id || ''; |
+ /** |
+ * Extension name associated with the RecentDestination. |
+ * @type {string} |
+ */ |
+ this.extensionName = destination.extension_name || ''; |
+} |
- /** |
- * Extension name associated with the RecentDestination. |
- * @type {string} |
- */ |
- this.extensionName = destination.extension_name || ''; |
- }; |
+cr.define('print_preview', function() { |
+ 'use strict'; |
/** |
* Object used to get and persist the print preview application state. |
@@ -64,9 +90,9 @@ cr.define('print_preview', function() { |
* @private |
*/ |
this.state_ = {}; |
- this.state_[AppState.Field.VERSION] = AppState.VERSION_; |
- this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = true; |
- this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
+ this.state_[print_preview.AppStateField.VERSION] = AppState.VERSION_; |
+ this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = true; |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
/** |
* Whether the app state has been initialized. The app state will ignore all |
@@ -75,7 +101,7 @@ cr.define('print_preview', function() { |
* @private |
*/ |
this.isInitialized_ = false; |
- }; |
+ } |
/** |
* Number of recent print destinations to store across browser sessions. |
@@ -83,30 +109,6 @@ cr.define('print_preview', function() { |
*/ |
AppState.NUM_DESTINATIONS_ = 3; |
- |
- /** |
- * Enumeration of field names for serialized app state. |
- * @enum {string} |
- */ |
- AppState.Field = { |
- VERSION: 'version', |
- RECENT_DESTINATIONS: 'recentDestinations', |
- IS_GCP_PROMO_DISMISSED: 'isGcpPromoDismissed', |
- DPI: 'dpi', |
- MEDIA_SIZE: 'mediaSize', |
- MARGINS_TYPE: 'marginsType', |
- CUSTOM_MARGINS: 'customMargins', |
- IS_COLOR_ENABLED: 'isColorEnabled', |
- IS_DUPLEX_ENABLED: 'isDuplexEnabled', |
- IS_HEADER_FOOTER_ENABLED: 'isHeaderFooterEnabled', |
- IS_LANDSCAPE_ENABLED: 'isLandscapeEnabled', |
- IS_COLLATE_ENABLED: 'isCollateEnabled', |
- IS_FIT_TO_PAGE_ENABLED: 'isFitToPageEnabled', |
- IS_CSS_BACKGROUND_ENABLED: 'isCssBackgroundEnabled', |
- SCALING: 'scaling', |
- VENDOR_OPTIONS: 'vendorOptions' |
- }; |
- |
/** |
* Current version of the app state. This value helps to understand how to |
* parse earlier versions of the app state. |
@@ -126,38 +128,40 @@ cr.define('print_preview', function() { |
AppState.prototype = { |
/** |
- * @return {?RecentDestination} The most recent destination, which is |
- * currently the selected destination. |
+ * @return {?RecentDestination} The most recent destination, |
+ * which is currently the selected destination. |
*/ |
get selectedDestination() { |
- return (this.state_[AppState.Field.RECENT_DESTINATIONS].length > 0) ? |
- this.state_[AppState.Field.RECENT_DESTINATIONS][0] : null; |
+ return (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]. |
+ length > 0) ? |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0] : |
+ null; |
}, |
/** |
* @return {boolean} Whether the selected destination is valid. |
*/ |
isSelectedDestinationValid: function() { |
- return this.selectedDestination && |
- this.selectedDestination.id && |
- this.selectedDestination.origin; |
+ return !!this.selectedDestination && |
+ !!this.selectedDestination.id && |
+ !!this.selectedDestination.origin; |
}, |
/** |
- * @return {?Array<!RecentDestination>} The AppState.NUM_DESTINATIONS_ most |
- * recent destinations. |
+ * @return {?Array<!RecentDestination>} The |
+ * AppState.NUM_DESTINATIONS_ most recent destinations. |
*/ |
get recentDestinations() { |
- return this.state_[AppState.Field.RECENT_DESTINATIONS]; |
+ return this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; |
}, |
/** @return {boolean} Whether the GCP promotion has been dismissed. */ |
get isGcpPromoDismissed() { |
- return this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED]; |
+ return this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED]; |
}, |
/** |
- * @param {!print_preview.AppState.Field} field App state field to check if |
+ * @param {!print_preview.AppStateField} field App state field to check if |
* set. |
* @return {boolean} Whether a field has been set in the app state. |
*/ |
@@ -166,11 +170,11 @@ cr.define('print_preview', function() { |
}, |
/** |
- * @param {!print_preview.AppState.Field} field App state field to get. |
+ * @param {!print_preview.AppStateField} field App state field to get. |
* @return {?} Value of the app state field. |
*/ |
getField: function(field) { |
- if (field == AppState.Field.CUSTOM_MARGINS) { |
+ if (field == print_preview.AppStateField.CUSTOM_MARGINS) { |
return this.state_[field] ? |
print_preview.Margins.parse(this.state_[field]) : null; |
} else { |
@@ -188,8 +192,8 @@ cr.define('print_preview', function() { |
if (serializedAppStateStr) { |
try { |
var state = JSON.parse(serializedAppStateStr); |
- if (state[AppState.Field.VERSION] == AppState.VERSION_) { |
- this.state_ = state; |
+ if (state[print_preview.AppStateField.VERSION] == AppState.VERSION_) { |
+ this.state_ = /** @type {Object} */(state); |
} |
} catch(e) { |
console.error('Unable to parse state: ' + e); |
@@ -197,22 +201,23 @@ cr.define('print_preview', function() { |
} |
} else { |
// Set some state defaults. |
- this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = false; |
- this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
+ this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = false; |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
} |
- if (!this.state_[AppState.Field.RECENT_DESTINATIONS]) { |
- this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
- } else if (!(this.state_[AppState.Field.RECENT_DESTINATIONS] instanceof |
- Array)) { |
- var tmp = this.state_[AppState.Field.RECENT_DESTINATIONS]; |
- this.state_[AppState.Field.RECENT_DESTINATIONS] = [tmp]; |
- } else if (!this.state_[AppState.Field.RECENT_DESTINATIONS][0] || |
- !this.state_[AppState.Field.RECENT_DESTINATIONS][0].id) { |
+ if (!this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]) { |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
+ } else if (!(this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] |
+ instanceof Array)) { |
+ var tmp = this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]; |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = [tmp]; |
+ } else if (!this.state_[ |
+ print_preview.AppStateField.RECENT_DESTINATIONS][0] || |
+ !this.state_[print_preview.AppStateField.RECENT_DESTINATIONS][0].id) { |
// read in incorrectly |
- this.state_[AppState.Field.RECENT_DESTINATIONS] = []; |
- } else if (this.state_[AppState.Field.RECENT_DESTINATIONS].length > |
- AppState.NUM_DESTINATIONS_) { |
- this.state_[AppState.Field.RECENT_DESTINATIONS].length = |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS] = []; |
+ } else if (this.state_[print_preview.AppStateField.RECENT_DESTINATIONS]. |
+ length > AppState.NUM_DESTINATIONS_) { |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length = |
AppState.NUM_DESTINATIONS_; |
} |
}, |
@@ -226,13 +231,13 @@ cr.define('print_preview', function() { |
/** |
* Persists the given value for the given field. |
- * @param {!print_preview.AppState.Field} field Field to persist. |
+ * @param {!print_preview.AppStateField} field Field to persist. |
* @param {?} value Value of field to persist. |
*/ |
persistField: function(field, value) { |
if (!this.isInitialized_) |
return; |
- if (field == AppState.Field.CUSTOM_MARGINS) { |
+ if (field == print_preview.AppStateField.CUSTOM_MARGINS) { |
this.state_[field] = value ? value.serialize() : null; |
} else { |
this.state_[field] = value; |
@@ -252,10 +257,11 @@ cr.define('print_preview', function() { |
// and where in the array it is located. |
var newDestination = new RecentDestination(dest); |
var indexFound = this.state_[ |
- AppState.Field.RECENT_DESTINATIONS].findIndex(function(recent) { |
- return (newDestination.id == recent.id && |
- newDestination.origin == recent.origin); |
- }); |
+ print_preview.AppStateField.RECENT_DESTINATIONS].findIndex( |
+ function(recent) { |
+ return (newDestination.id == recent.id && |
+ newDestination.origin == recent.origin); |
+ }); |
// No change |
if (indexFound == 0) { |
@@ -266,15 +272,16 @@ cr.define('print_preview', function() { |
// Shift the array so that the nth most recent destination is located at |
// index n. |
if (indexFound == -1 && |
- this.state_[AppState.Field.RECENT_DESTINATIONS].length == |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].length == |
AppState.NUM_DESTINATIONS_) { |
indexFound = AppState.NUM_DESTINATIONS_ - 1; |
} |
if (indexFound != -1) |
- this.state_[AppState.Field.RECENT_DESTINATIONS].splice(indexFound, 1); |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( |
+ indexFound, 1); |
// Add the most recent destination |
- this.state_[AppState.Field.RECENT_DESTINATIONS].splice( |
+ this.state_[print_preview.AppStateField.RECENT_DESTINATIONS].splice( |
0, 0, newDestination); |
this.persist_(); |
@@ -288,7 +295,8 @@ cr.define('print_preview', function() { |
persistIsGcpPromoDismissed: function(isGcpPromoDismissed) { |
if (!this.isInitialized_) |
return; |
- this.state_[AppState.Field.IS_GCP_PROMO_DISMISSED] = isGcpPromoDismissed; |
+ this.state_[print_preview.AppStateField.IS_GCP_PROMO_DISMISSED] = |
+ isGcpPromoDismissed; |
this.persist_(); |
}, |