Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Unified Diff: chrome/browser/resources/print_preview/data/print_ticket_store.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/print_preview/data/print_ticket_store.js
diff --git a/chrome/browser/resources/print_preview/data/print_ticket_store.js b/chrome/browser/resources/print_preview/data/print_ticket_store.js
new file mode 100644
index 0000000000000000000000000000000000000000..c15722478b6fc1109103f2c57c787229e96537e0
--- /dev/null
+++ b/chrome/browser/resources/print_preview/data/print_ticket_store.js
@@ -0,0 +1,517 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// TODO Optimize this to not dispatch CHANGE events when nothing really changed.
+
+cr.define('print_preview', function() {
+ 'use strict';
+
+ /**
+ * Storage of the print ticket and document statistics.
+ *
+ * Dispatches events when the contents of the print ticket or document
+ * statistics change. Also handles validation of the print ticket against
+ * destination capabilities and against the document.
+ *
+ * @param {print_preview.DestinationStore!} destinationStore Destination store
+ * used to get the selected destination.
+ * @constructor
+ * @extends {cr.EventTarget}
+ */
+ function PrintTicketStore(destinationStore) {
+ cr.EventTarget.call(this);
+
+ /**
+ * Destination store used to get the selected destination.
+ * @type {print_preview.DestinationStore!}
+ * @private
+ */
+ this.destinationStore_ = destinationStore;
+
+ /**
+ * Information about the document to print.
+ * @type {DocumentInfo}
+ * @private
+ */
+ this.documentInfo_ = new DocumentInfo();
+
+ /**
+ * Cached page number set. Used to not have to parse page range string
+ * everytime its requested.
+ * @type {print_preview.PageNumberSet}
+ * @private
+ */
+ this.pageNumberSet_ = null;
+
+ /**
+ * Printing capabilities of Chromium and the currently selected destination.
+ * @type {print_preview.ChromiumCapabilities}
+ * @private
+ */
+ this.capabilities_ = null;
+
+ /**
+ * Print ticket information. Used to print and generate previews.
+ * @type {print_preview.ChromiumPrintTicket}
+ * @private
+ */
+ this.ticket_ = null;
+
+ /**
+ * Current measurement system. Used to work with margin measurements.
+ * @type {print_preview.MeasurementSystem}
+ * @private
+ */
+ this.measurementSystem_ = null;
+ };
+
+ /**
+ * Events dispatched by the print ticket store.
+ * @enum {string}
+ */
+ PrintTicketStore.Event = {
+ CAPABILITIES_CHANGE: 'print_preview.PrintTicketStore.CAPABILITIES_CHANGE',
+ DOCUMENT_CHANGE: 'print_preview.PrintTicketStore.DOCUMENT_CHANGE',
+ INITIALIZE: 'print_preview.PrintTicketStore.INITIALIZE',
+ TICKET_CHANGE: 'print_preview.PrintTicketStore.TICKET_CHANGE',
+ };
+
+ PrintTicketStore.prototype = {
+ __proto__: cr.EventTarget.prototype,
+
+ get isDocumentModifiable() {
+ return this.documentInfo_.isModifiable;
+ },
+
+ get pageCount() {
+ return this.documentInfo_.pageCount;
+ },
+
+ /** @override */
+ dispatchEvent: function(evt) {
+ // TODO REMOVE ME
+ log(evt.type);
+ cr.EventTarget.prototype.dispatchEvent.call(this, evt);
+ },
+
+ /**
+ * Initializes the print ticket store.
+ * @param {boolean} isDocumentModifiable Whether the document to print is
+ * modifiable (i.e. can be re-flowed by Chromium).
+ * @param {boolean} isDuplexEnabled Previous duplex setting.
+ * @param {boolean} isHeaderFooterEnabled Previous header-footer setting.
+ * @param {print_preview.MarginType} marginType Previous margin type.
+ */
+ initialize: function(
+ isDocumentModifiable,
+ isDuplexEnabled,
+ isHeaderFooterEnabled,
+ marginType,
+ customMarginsInfo,
+ numberFormat,
+ measurementSystem) {
+
+ this.documentInfo_.isModifiable = isDocumentModifiable;
+
+ // Create capabilities that can be handled by Chromium.
+ this.capabilities_ = new print_preview.ChromiumCapabilities();
+ this.capabilities_.hasPageRangeCapability = true;
+ this.capabilities_.hasColorCapability = true;
+ this.capabilities_.hasCopiesCapability = true;
+ this.capabilities_.hasCollateCapability = true;
+ this.capabilities_.hasDuplexCapability = true;
+ if (isDocumentModifiable) {
+ this.capabilities_.hasOrientationCapability = true;
+ this.capabilities_.hasMarginsCapability = true;
+ this.capabilities_.hasHeaderFooterCapability = true;
+ }
+
+ var numberFormatSymbols =
+ print_preview.MeasurementSystem.parseNumberFormat(numberFormat);
+ this.measurementSystem_ = new print_preview.MeasurementSystem(
+ numberFormatSymbols[0], numberFormatSymbols[1], measurementSystem);
+
+ marginType = marginType != null ?
+ marginType : print_preview.Margins.Type.DEFAULT;
+ var customMargins = null;
+ if (marginType == print_preview.Margins.Type.CUSTOM) {
+ if (customMarginsInfo != null) {
+ customMargins = new print_preview.Margins(
+ customMarginsInfo[0], customMarginsInfo[1],
+ customMarginsInfo[2], customMarginsInfo[3],
+ this.measurementSystem_);
+ } else {
+ marginType = print_preview.Margins.Type.DEFAULT;
+ }
+ }
+
+ this.ticket_ = new print_preview.ChromiumPrintTicket(this.capabilities_);
+ this.ticket_.isDuplexEnabled = isDuplexEnabled;
+ this.ticket_.marginType = marginType;
+ this.ticket_.pageRangeStr = '';
+ if (marginType == print_preview.Margins.Type.CUSTOM) {
+ // TODO this.ticket_.customMargins = customMargins;
+ }
+ this.ticket_.isHeaderFooterEnabled = isHeaderFooterEnabled;
+
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.INITIALIZE);
+ },
+
+ createTicketForPreviewGenerator: function() {
+ if (!this.isTicketValid()) {
+ throw Error(
+ 'Requesting ticket for preview generator when ticket is not valid');
+ }
+
+ var settings = {
+ 'pageRange': [], // TODO this.ticket_.pageRangeSequence,
+ 'landscape': this.ticket_.isLandscapeEnabled,
+ 'color': this.ticket_.colorMode,
+ 'headerFooterEnabled': this.ticket_.isHeaderFooterEnabled,
+ 'marginsType': this.ticket_.marginType,
+
+ // This field seems to issue a reloadPreviewPages call when true.
+ 'generateDraftData': true, // TODO What should this value be?
+
+ // NOTE: Even though the following fields don't directly relate to the
+ // preview, they still need to be included.
+ 'duplex': this.ticket_.duplexMode,
+ 'copies': this.getCopies(),
+ 'collate': this.ticket_.isCollateEnabled,
+ 'previewModifiable': this.documentInfo_.isModifiable,
+ 'printToPDF': false,
+ 'printWithCloudPrint': false,
+ 'deviceName': 'foo',
+ 'cloudPrintID': 'foo'
+ };
+
+ if (this.ticket_.marginType == print_preview.Margins.Type.CUSTOM) {
+ // TODO settings['marginsCustom'] =
+ }
+
+ return settings;
+ },
+
+ updateDestinationCapabilities: function(caps) {
+ if (this.capabilities_.hasPageRangeCapability =
+ caps.hasPageRangeCapability) {
+ this.capabilities_.defaultPageRangeStr = caps.defaultPageRangeStr;
+ }
+ if (this.capabilities_.hasColorCapability = caps.hasColorCapability) {
+ this.capabilities_.defaultIsColorEnabled = caps.defaultIsColorEnabled;
+ }
+ if (this.capabilities_.hasCopiesCapability = caps.hasCopiesCapability) {
+ this.capabilities_.defaultCopiesStr = caps.defaultCopiesStr;
+ }
+ if (this.capabilities_.hasCollateCapability = caps.hasCollateCapability) {
+ this.capabilities_.defaultIsCollateEnabled =
+ caps.defaultIsCollateEnabled;
+ }
+ if (this.capabilities_.hasDuplexCapability = caps.hasDuplexCapability) {
+ this.capabilities_.defaultIsDuplexEnabled = caps.defaultIsDuplexEnabled;
+ }
+ if (this.documentInfo_.isModifiable) {
+ if (this.capabilities_.hasOrientationCapability =
+ caps.hasOrientationCapability) {
+ this.capabilities_.defaultIsLandscapeEnabled =
+ caps.defaultIsLandscapeEnabled;
+ }
+ if (this.capabilities_.hasMarginsCapability =
+ caps.hasMarginsCapability) {
+ this.capabilities_.defaultMarginType = caps.defaultMarginType;
+ }
+ if (this.capabilities_.hasHeaderFooterCapability =
+ caps.hasHeaderFooterCapability) {
+ this.capabilities_.defaultIsHeaderFooterEnabled =
+ caps.defaultIsHeaderFooterEnabled;
+ }
+ }
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.CAPABILITIES_CHANGE);
+ },
+
+ updatePageCount: function(pageCount) {
+ if (this.documentInfo_.pageCount != pageCount) {
+ log('print_preview.PrintTicketStore.updatePageCount -- updating count to ' + pageCount);
+ this.documentInfo_.pageCount = pageCount;
+ this.pageNumberSet_ = null;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.DOCUMENT_CHANGE);
+ }
+ },
+
+ hasCopiesCapability: function() {
+ return this.capabilities_.hasCopiesCapability;
+ },
+
+ isCopiesValid: function() {
+ return this.isCopiesValidForValue(this.getCopiesStr());
+ },
+
+ isCopiesValidForValue: function(value) {
+ if (/[^\d]+/.test(value)) {
+ return false;
+ }
+ var copies = parseInt(value);
+ if (copies > 999 || copies < 1) {
+ return false;
+ }
+ return true;
+ },
+
+ getCopies: function() {
+ return parseInt(this.getCopiesStr());
+ },
+
+ getCopiesStr: function() {
+ return this.ticket_.copiesStr;
+ },
+
+ updateCopies: function(copies) {
+ if (!this.capabilities_.hasCopiesCapability) {
+ throw Error(
+ 'Updating copies capability but destination does not have a ' +
+ 'copies capability');
+ }
+ if (this.ticket_.copiesStr != copies) {
+ this.ticket_.copiesStr = copies;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ hasCollateCapability: function() {
+ return this.capabilities_.hasCollateCapability;
+ },
+
+ isCollateEnabled: function() {
+ return this.ticket_.isCollateEnabled;
+ },
+
+ updateCollate: function(isCollate) {
+ if (!this.capabilities_.hasCollateCapability) {
+ throw Error(
+ 'Updating collate capability but destination does not have a ' +
+ 'collate capability');
+ }
+ if (this.ticket_.isCollateEnabled != isCollate) {
+ this.ticket_.isCollateEnabled = isCollate;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ hasColorCapability: function() {
+ return this.capabilities_.hasColorCapability;
+ },
+
+ isColorEnabled: function() {
+ return this.ticket_.isColorEnabled;
+ },
+
+ /**
+ * @param {boolean} isColor Whether the color option is enabled.
+ */
+ updateColor: function(isColor) {
+ if (!this.capabilities_.hasColorCapability) {
+ throw Error(
+ 'Updating color capability but destination does not have a color ' +
+ 'capability');
+ }
+ if (this.ticket_.isColorEnabled != isColor) {
+ this.ticket_.isColorEnabled = isColor;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ /** @return {boolean} Whether the header-footer capability is available. */
+ hasHeaderFooterCapability: function() {
+ if (!this.capabilities_.hasHeaderFooterCapability) {
+ return false;
+ }
+ // Checks the printable area and updates the visibility of header footer
+ // option based on the selected margins.
+ return true;
+ // TODO
+// var headerFooterApplies = true;
+// if (marginsType ==
+// print_preview.MarginSettings.MARGINS_VALUE_NO_MARGINS ||
+// !previewModifiable) {
+// headerFooterApplies = false;
+// } else if (marginsType !=
+// print_preview.MarginSettings.MARGINS_VALUE_MINIMUM) {
+// if (cr.isLinux || cr.isChromeOS) {
+// headerFooterApplies = pageLayout.marginTop > 0 ||
+// pageLayout.marginBottom > 0;
+// } else {
+// var pageHeight = pageLayout.marginTop + pageLayout.marginBottom +
+// pageLayout.contentHeight;
+// headerFooterApplies =
+// (pageLayout.marginTop > pageLayout.printableAreaY) ||
+// (pageLayout.marginBottom >
+// (pageHeight - pageLayout.printableAreaY -
+// pageLayout.printableAreaHeight));
+// }
+// }
+// this.setVisible_(headerFooterApplies);
+ },
+
+ /** @return {boolean} Whether the header-footer setting is enabled. */
+ isHeaderFooterEnabled: function() {
+ return this.ticket_.isHeaderFooterEnabled;
+ },
+
+ updateHeaderFooter: function(isHeaderFooterEnabled) {
+ if (!this.capabilities_.hasHeaderFooterCapability) {
+ throw Error(
+ 'Updating header-footer capability but destination does not have ' +
+ 'a header-footer capability');
+ }
+ if (this.ticket_.isHeaderFooterEnabled != isHeaderFooterEnabled) {
+ this.ticket_.isHeaderFooterEnabled = isHeaderFooterEnabled;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ hasOrientationCapability: function() {
+ // TODO Is this true? Maybe we can still rotate if the document is not
+ // modifiable. This is the old behavior.
+ return this.capabilities_.hasOrientationCapability;
+ },
+
+ isLandscapeEnabled: function() {
+ return this.ticket_.isLandscapeEnabled;
+ },
+
+ updateOrientation: function(isLandscape) {
+ if (!this.capabilities_.hasOrientationCapability) {
+ throw Error(
+ 'Updating orientation capability but destination does not have ' +
+ 'an orientation capability');
+ }
+ if (this.ticket_.isLandscapeEnabled != isLandscape) {
+ this.ticket_.isLandscapeEnabled = isLandscape;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ hasDuplexCapability: function() {
+ return this.capabilities_.hasDuplexCapability;
+ },
+
+ isDuplexEnabled: function() {
+ return this.ticket_.isDuplexEnabled;
+ },
+
+ updateDuplex: function(isDuplex) {
+ if (!this.capabilities_.hasDuplexCapability) {
+ throw Error(
+ 'Updating duplex capability but destination does not have a ' +
+ 'duplex capability');
+ }
+ if (this.ticket_.isDuplexEnabled != isDuplex) {
+ this.ticket_.isDuplexEnabled = isDuplex;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ }
+ },
+
+ hasMarginCapability: function() {
+ return this.capabilities_.hasMarginCapability;
+ },
+
+ getMarginType: function() {
+ return this.ticket_.marginType;
+ },
+
+ updateMarginType: function() {
+ if (!this.capabilities_.hasMarginsCapability) {
+ throw Error(
+ 'Updating margins capability but destination does not have a ' +
+ 'margin capability');
+ }
+ // TODO
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ },
+
+ hasPageRangeCapability: function() {
+ return this.capabilities_.hasPageRangeCapability;
+ },
+
+ isPageRangeValid: function() {
+ var pageRangeStr = this.ticket_.pageRangeStr;
+ return pageRangeStr == '' ||
+ isPageRangeTextValid(pageRangeStr, this.documentInfo_.pageCount);
+ },
+
+ getPageRangeStr: function() {
+ return this.ticket_.pageRangeStr;
+ },
+
+ getPageNumberSet: function() {
+ if (this.pageNumberSet_ == null) {
+ this.pageNumberSet_ = print_preview.PageNumberSet.parse(
+ this.ticket_.pageRangeStr, this.documentInfo_.pageCount);
+ }
+ return this.pageNumberSet_;
+ },
+
+ updatePageRange: function(pageRangeStr) {
+ if (!this.capabilities_.hasPageRangeCapability) {
+ throw Error(
+ 'Updating page-range capability but destination does not have a ' +
+ 'page-range capability');
+ }
+ log('print_preview.PrintTicketStore.updatePageRange -- trying update page range str to ' + pageRangeStr);
+ if (this.ticket_.pageRangeStr != pageRangeStr) {
+ this.ticket_.pageRangeStr = pageRangeStr;
+ this.pageNumberSet_ = null;
+ cr.dispatchSimpleEvent(this, PrintTicketStore.Event.TICKET_CHANGE);
+ } else {
+ log('print_preview.PrintTicketStore.updatePageRange -- ignoring because no change');
+ }
+ },
+
+ /**
+ * Checking if the stored print ticket is valid.
+ * @return {boolean} {@code true} if the stored print ticket is valid,
+ * {@code false} otherwise.
+ */
+ isTicketValid: function() {
+ // TODO Validate margins
+ return this.isCopiesValid() && this.isPageRangeValid();
+ },
+
+ serializeTicket: function() {
+ return 'persisted ticket';
+ },
+
+ /**
+ * Generates a print ticket suitable for sending to Google Cloud Print.
+ * @return {string} Serialized form of a Google Cloud Print print ticket.
+ */
+ createTicketForCloudPrint: function() {
+ // TODO
+ },
+
+ /**
+ * Generates a print ticket suitable for sending to Chromium.
+ * @return {Object} Object to send to Chromium.
+ */
+ createTicketForChromium: function() {
+ // TODO
+ }
+ };
+
+ /**
+ * Object which contains information related to the document to print.
+ * @constructor
+ */
+ function DocumentInfo() {
+ this.isModifiable = true;
+ this.pageCount = 1;
+ this.hasPageSizeStyle = false;
+ this.pageLayout = null;
+ };
+
+ // Export
+ return {
+ PrintTicketStore: PrintTicketStore
+ };
+});

Powered by Google App Engine
This is Rietveld 408576698