summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/print_preview/data/document_info.js
blob: d85124d3a130f5e7fff42c776b93f7c1f657d881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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.

cr.exportPath('print_preview');

/**
 * @typedef {{
 *   hasCssMediaStyles: boolean,
 *   hasSelection: boolean,
 *   isModifiable: boolean,
 *   isPdf: boolean,
 *   isScalingDisabled: boolean,
 *   fitToPageScaling: number,
 *   pageCount: number,
 *   title: string,
 * }}
 */
print_preview.DocumentSettings;

/**
 * @typedef {{
 *   marginTop: number,
 *   marginLeft: number,
 *   marginBottom: number,
 *   marginRight: number,
 *   contentWidth: number,
 *   contentHeight: number,
 *   printableAreaX: number,
 *   printableAreaY: number,
 *   printableAreaWidth: number,
 *   printableAreaHeight: number,
 * }}
 */
print_preview.PageLayoutInfo;

Polymer({
  is: 'print-preview-document-info',

  behaviors: [WebUIListenerBehavior],

  properties: {
    /** @type {!print_preview.DocumentSettings} */
    documentSettings: {
      type: Object,
      notify: true,
      value: function() {
        return {
          hasCssMediaStyles: false,
          hasSelection: false,
          isModifiable: true,
          isPdf: false,
          isScalingDisabled: false,
          fitToPageScaling: 100,
          pageCount: 0,
          title: '',
        };
      },
    },

    inFlightRequestId: {
      type: Number,
      value: -1,
    },

    /** @type {print_preview.Margins} */
    margins: {
      type: Object,
      notify: true,
    },

    /**
     * Size of the pages of the document in points. Actual page-related
     * information won't be set until preview generation occurs, so use
     * a default value until then.
     * @type {!print_preview.Size}
     */
    pageSize: {
      type: Object,
      notify: true,
      value: function() {
        return new print_preview.Size(612, 792);
      },
    },

    /**
     * Printable area of the document in points.
     * @type {!print_preview.PrintableArea}
     */
    printableArea: {
      type: Object,
      notify: true,
      value: function() {
        return new print_preview.PrintableArea(
            new print_preview.Coordinate2d(0, 0),
            new print_preview.Size(612, 792));
      },
    },
  },

  /**
   * Whether this data model has been initialized.
   * @private {boolean}
   */
  isInitialized_: false,

  /** @override */
  attached: function() {
    this.addWebUIListener(
        'page-count-ready', this.onPageCountReady_.bind(this));
    this.addWebUIListener(
        'page-layout-ready', this.onPageLayoutReady_.bind(this));
  },

  /**
   * Initializes the state of the data model.
   * @param {boolean} isModifiable Whether the document is modifiable.
   * @param {boolean} isPdf Whether the document is PDF.
   * @param {string} title Title of the document.
   * @param {boolean} hasSelection Whether the document has user-selected
   *     content.
   */
  init: function(isModifiable, isPdf, title, hasSelection) {
    this.isInitialized_ = true;
    this.set('documentSettings.isModifiable', isModifiable);
    // TODO(crbug.com/702995): Remove once Flash is deprecated.
    this.set('documentSettings.isPdf', isPdf);
    this.set('documentSettings.title', title);
    this.set('documentSettings.hasSelection', hasSelection);
  },

  /**
   * Updates whether scaling is disabled for the document.
   * @param {boolean} isScalingDisabled Whether scaling of the document is
   *     prohibited.
   */
  updateIsScalingDisabled: function(isScalingDisabled) {
    if (this.isInitialized_) {
      this.set('documentSettings.isScalingDisabled', isScalingDisabled);
    }
  },

  /**
   * Called when the page layout of the document is ready. Always occurs
   * as a result of a preview request.
   * @param {!print_preview.PageLayoutInfo} pageLayout Layout information
   *     about the document.
   * @param {boolean} hasCustomPageSizeStyle Whether this document has a
   *     custom page size or style to use.
   * @private
   */
  onPageLayoutReady_: function(pageLayout, hasCustomPageSizeStyle) {
    const origin = new print_preview.Coordinate2d(
        pageLayout.printableAreaX, pageLayout.printableAreaY);
    const size = new print_preview.Size(
        pageLayout.printableAreaWidth, pageLayout.printableAreaHeight);

    const margins = new print_preview.Margins(
        Math.round(pageLayout.marginTop), Math.round(pageLayout.marginRight),
        Math.round(pageLayout.marginBottom), Math.round(pageLayout.marginLeft));

    const o = print_preview.CustomMarginsOrientation;
    const pageSize = new print_preview.Size(
        pageLayout.contentWidth + margins.get(o.LEFT) + margins.get(o.RIGHT),
        pageLayout.contentHeight + margins.get(o.TOP) + margins.get(o.BOTTOM));

    if (this.isInitialized_) {
      this.printableArea = new print_preview.PrintableArea(origin, size);
      this.pageSize = pageSize;
      this.set('documentSettings.hasCssMediaStyles', hasCustomPageSizeStyle);
      this.margins = margins;
    }
  },

  /**
   * Called when the document page count is received from the native layer.
   * Always occurs as a result of a preview request.
   * @param {number} pageCount The document's page count.
   * @param {number} previewResponseId The request ID for this page count event.
   * @param {number} fitToPageScaling The scaling required to fit the document
   *     to page.
   * @private
   */
  onPageCountReady_: function(pageCount, previewResponseId, fitToPageScaling) {
    if (this.inFlightRequestId != previewResponseId || !this.isInitialized_) {
      return;
    }
    this.set('documentSettings.pageCount', pageCount);
    this.set('documentSettings.fitToPageScaling', fitToPageScaling);
  },
});