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
3 // found in the LICENSE file.
5 cr
.define('print_preview', function() {
9 * Data model which contains information related to the document to print.
11 * @extends {cr.EventTarget}
13 function DocumentInfo() {
14 cr
.EventTarget
.call(this);
17 * Whether the document is styled by CSS media styles.
21 this.hasCssMediaStyles_
= false;
24 * Whether the document has selected content.
28 this.hasSelection_
= false;
31 * Whether the document to print is modifiable (i.e. can be reflowed).
35 this.isModifiable_
= true;
38 * Whether scaling of the document is prohibited.
42 this.isScalingDisabled_
= false;
45 * Margins of the document in points.
46 * @type {print_preview.Margins}
52 * Number of pages in the document to print.
58 // Create the document info with some initial settings. Actual
59 // page-related information won't be set until preview generation occurs,
60 // so we'll use some defaults until then. This way, the print ticket store
61 // will be valid even if no preview can be generated.
62 var initialPageSize
= new print_preview
.Size(612, 792); // 8.5"x11"
65 * Size of the pages of the document in points.
66 * @type {!print_preview.Size}
69 this.pageSize_
= initialPageSize
;
72 * Printable area of the document in points.
73 * @type {!print_preview.PrintableArea}
76 this.printableArea_
= new print_preview
.PrintableArea(
77 new print_preview
.Coordinate2d(0, 0), initialPageSize
);
87 * Whether this data model has been initialized.
91 this.isInitialized_
= false;
95 * Event types dispatched by this data model.
98 DocumentInfo
.EventType
= {
99 CHANGE
: 'print_preview.DocumentInfo.CHANGE'
102 DocumentInfo
.prototype = {
103 __proto__
: cr
.EventTarget
.prototype,
105 /** @return {boolean} Whether the document is styled by CSS media styles. */
106 get hasCssMediaStyles() {
107 return this.hasCssMediaStyles_
;
110 /** @return {boolean} Whether the document has selected content. */
112 return this.hasSelection_
;
116 * @return {boolean} Whether the document to print is modifiable (i.e. can
120 return this.isModifiable_
;
123 /** @return {boolean} Whether scaling of the document is prohibited. */
124 get isScalingDisabled() {
125 return this.isScalingDisabled_
;
128 /** @return {print_preview.Margins} Margins of the document in points. */
130 return this.margins_
;
133 /** @return {number} Number of pages in the document to print. */
135 return this.pageCount_
;
139 * @return {!print_preview.Size} Size of the pages of the document in
143 return this.pageSize_
;
147 * @return {!print_preview.PrintableArea} Printable area of the document in
150 get printableArea() {
151 return this.printableArea_
;
154 /** @return {string} Title of document. */
160 * Initializes the state of the data model and dispatches a CHANGE event.
161 * @param {boolean} isModifiable Whether the document is modifiable.
162 * @param {string} title Title of the document.
163 * @param {boolean} hasSelection Whether the document has user-selected
166 init: function(isModifiable
, title
, hasSelection
) {
167 this.isModifiable_
= isModifiable
;
169 this.hasSelection_
= hasSelection
;
170 this.isInitialized_
= true;
171 cr
.dispatchSimpleEvent(this, DocumentInfo
.EventType
.CHANGE
);
175 * Updates whether scaling is disabled for the document and dispatches a
177 * @param {boolean} isScalingDisabled Whether scaling of the document is
180 updateIsScalingDisabled: function(isScalingDisabled
) {
181 if (this.isInitialized_
&& this.isScalingDisabled_
!= isScalingDisabled
) {
182 this.isScalingDisabled_
= isScalingDisabled
;
183 cr
.dispatchSimpleEvent(this, DocumentInfo
.EventType
.CHANGE
);
188 * Updates the total number of pages in the document and dispatches a CHANGE
190 * @param {number} pageCount Number of pages in the document.
192 updatePageCount: function(pageCount
) {
193 if (this.isInitialized_
&& this.pageCount_
!= pageCount
) {
194 this.pageCount_
= pageCount
;
195 cr
.dispatchSimpleEvent(this, DocumentInfo
.EventType
.CHANGE
);
200 * Updates information about each page and dispatches a CHANGE event.
201 * @param {!print_preview.PrintableArea} printableArea Printable area of the
202 * document in points.
203 * @param {!print_preview.Size} pageSize Size of the pages of the document
205 * @param {boolean} hasCssMediaStyles Whether the document is styled by CSS
207 * @param {print_preview.Margins} margins Margins of the document in points.
209 updatePageInfo: function(
210 printableArea
, pageSize
, hasCssMediaStyles
, margins
) {
211 if (this.isInitialized_
&&
212 (!this.printableArea_
.equals(printableArea
) ||
213 !this.pageSize_
.equals(pageSize
) ||
214 this.hasCssMediaStyles_
!= hasCssMediaStyles
||
215 this.margins_
== null || !this.margins_
.equals(margins
))) {
216 this.printableArea_
= printableArea
;
217 this.pageSize_
= pageSize
;
218 this.hasCssMediaStyles_
= hasCssMediaStyles
;
219 this.margins_
= margins
;
220 cr
.dispatchSimpleEvent(this, DocumentInfo
.EventType
.CHANGE
);
227 DocumentInfo
: DocumentInfo