Roll src/third_party/WebKit aa8346d:dbb8a38 (svn 202629:202630)
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / document_info.js
blob589b0685a9ab3aff24c2aac7f3f54f52973a3bde
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() {
6 'use strict';
8 /**
9 * Data model which contains information related to the document to print.
10 * @constructor
11 * @extends {cr.EventTarget}
13 function DocumentInfo() {
14 cr.EventTarget.call(this);
16 /**
17 * Whether the document is styled by CSS media styles.
18 * @type {boolean}
19 * @private
21 this.hasCssMediaStyles_ = false;
23 /**
24 * Whether the document has selected content.
25 * @type {boolean}
26 * @private
28 this.hasSelection_ = false;
30 /**
31 * Whether the document to print is modifiable (i.e. can be reflowed).
32 * @type {boolean}
33 * @private
35 this.isModifiable_ = true;
37 /**
38 * Whether scaling of the document is prohibited.
39 * @type {boolean}
40 * @private
42 this.isScalingDisabled_ = false;
44 /**
45 * Margins of the document in points.
46 * @type {print_preview.Margins}
47 * @private
49 this.margins_ = null;
51 /**
52 * Number of pages in the document to print.
53 * @type {number}
54 * @private
56 this.pageCount_ = 0;
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"
64 /**
65 * Size of the pages of the document in points.
66 * @type {!print_preview.Size}
67 * @private
69 this.pageSize_ = initialPageSize;
71 /**
72 * Printable area of the document in points.
73 * @type {!print_preview.PrintableArea}
74 * @private
76 this.printableArea_ = new print_preview.PrintableArea(
77 new print_preview.Coordinate2d(0, 0), initialPageSize);
79 /**
80 * Title of document.
81 * @type {string}
82 * @private
84 this.title_ = '';
86 /**
87 * Whether this data model has been initialized.
88 * @type {boolean}
89 * @private
91 this.isInitialized_ = false;
94 /**
95 * Event types dispatched by this data model.
96 * @enum {string}
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. */
111 get hasSelection() {
112 return this.hasSelection_;
116 * @return {boolean} Whether the document to print is modifiable (i.e. can
117 * be reflowed).
119 get isModifiable() {
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. */
129 get margins() {
130 return this.margins_;
133 /** @return {number} Number of pages in the document to print. */
134 get pageCount() {
135 return this.pageCount_;
139 * @return {!print_preview.Size} Size of the pages of the document in
140 * points.
142 get pageSize() {
143 return this.pageSize_;
147 * @return {!print_preview.PrintableArea} Printable area of the document in
148 * points.
150 get printableArea() {
151 return this.printableArea_;
154 /** @return {string} Title of document. */
155 get title() {
156 return this.title_;
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
164 * content.
166 init: function(isModifiable, title, hasSelection) {
167 this.isModifiable_ = isModifiable;
168 this.title_ = title;
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
176 * CHANGE event.
177 * @param {boolean} isScalingDisabled Whether scaling of the document is
178 * prohibited.
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
189 * event.
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
204 * in points.
205 * @param {boolean} hasCssMediaStyles Whether the document is styled by CSS
206 * media styles.
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);
225 // Export
226 return {
227 DocumentInfo: DocumentInfo