summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/print_preview/print_header.js
blob: 6a74d042d786b3cf83a28b5e0b74fefde080917a (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
// 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.define('print_preview', function() {
  'use strict';

  /**
   * Creates a PrintHeader object. This object encapsulates all the elements
   * and logic related to the top part of the left pane in print_preview.html.
   * @constructor
   */
  function PrintHeader() {
    this.printButton_ = $('print-button');
    this.cancelButton_ = $('cancel-button');
    this.summary_ = $('print-summary');
    this.printButton_.focus();
    this.addEventListeners_();
  }

  cr.addSingletonGetter(PrintHeader);

  PrintHeader.prototype = {
    get printButton() {
      return this.printButton_;
    },

    get cancelButton() {
      return this.cancelButton_;
    },

    get summary() {
      return this.summary_;
    },

    /**
     * Adding event listeners where necessary. Listeners take care of changing
     * their behavior depending on the current state, no need to remove them.
     * @private
     */
    addEventListeners_: function() {
      this.cancelButton_.onclick = function() {
        this.disableCancelButton();
        closePrintPreviewTab();
      }.bind(this);
      this.printButton_.onclick = this.onPrintRequested.bind(this);
      document.addEventListener(customEvents.UPDATE_SUMMARY,
                                this.updateSummary_.bind(this));
      document.addEventListener(customEvents.UPDATE_PRINT_BUTTON,
                                this.updatePrintButton_.bind(this));
      document.addEventListener(customEvents.PDF_GENERATION_ERROR,
                                this.onPDFGenerationError_.bind(this));
      document.addEventListener(customEvents.PRINTER_CAPABILITIES_UPDATED,
                                this.onPrinterCapabilitiesUpdated_.bind(this));
    },

    /**
     * Enables the cancel button and attaches its keydown event listener.
     */
    enableCancelButton: function() {
      window.onkeydown = onKeyDown;
      this.cancelButton_.disabled = false;
    },

    /**
     * Executes when a |customEvents.PDF_GENERATION_ERROR| event occurs.
     * @private
     */
    onPDFGenerationError_: function() {
      this.printButton_.disabled = true;
    },

    /**
     * Executes when a |customEvents.PRINTER_CAPABILITIES_UPDATED| event occurs.
     * @private
     */
    onPrinterCapabilitiesUpdated_: function() {
      getSelectedPrinterName() == PRINT_TO_PDF ?
          this.printButton.textContent = localStrings.getString('saveButton') :
          this.printButton.textContent = localStrings.getString('printButton');
    },

    /**
     * Disables the cancel button and removes its keydown event listener.
     */
    disableCancelButton: function() {
      window.onkeydown = null;
      this.cancelButton_.disabled = true;
    },

    /**
     * Listener executing whenever the print button is clicked or user presses
     * the enter button while focus is in the pages field.
     */
    onPrintRequested: function() {
      var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF;
      if (!printToPDF) {
        this.printButton_.classList.add('loading');
        this.cancelButton_.classList.add('loading');
        this.summary_.innerHTML = localStrings.getString('printing');
      }
      this.disableCancelButton();
      requestToPrintDocument();
    },

    /**
     * Updates the state of |this.printButton_| depending on the user selection.
     * The button is enabled only when the following conditions are true.
     * 1) The selected page ranges are valid.
     * 2) The number of copies is valid (if applicable).
     * @private
     */
    updatePrintButton_: function() {
      if (showingSystemDialog)
        return;
      this.printButton_.disabled = !areSettingsValid();
    },

    /**
     * Updates |this.summary_| based on the currently selected user options.
     * @private
     */
    updateSummary_: function() {
      var printToPDF = getSelectedPrinterName() == PRINT_TO_PDF;
      var copies = printToPDF ? 1 : copiesSettings.numberOfCopies;

      if ((!printToPDF && !copiesSettings.isValid()) ||
          !pageSettings.isPageSelectionValid()) {
        this.summary_.innerHTML = '';
        return;
      }

      if (!marginSettings.areMarginSettingsValid()) {
        this.summary_.innerHTML = '';
        return;
      }

      var pageSet = pageSettings.selectedPagesSet;
      var numOfSheets = pageSet.length;
      if (numOfSheets == 0)
        return;

      var summaryLabel =
          localStrings.getString('printPreviewSheetsLabelSingular');
      var numOfPagesText = '';
      var pagesLabel = localStrings.getString('printPreviewPageLabelPlural');

      if (printToPDF)
        summaryLabel = localStrings.getString('printPreviewPageLabelSingular');

      if (!printToPDF &&
          copiesSettings.duplexMode == print_preview.CopiesSettings.LONG_EDGE) {
        numOfSheets = Math.ceil(numOfSheets / 2);
      }
      numOfSheets *= copies;

      if (numOfSheets > 1) {
        summaryLabel = printToPDF ? pagesLabel :
            localStrings.getString('printPreviewSheetsLabelPlural');
      }

      var html = '';
      if (pageSet.length * copies != numOfSheets) {
        numOfPagesText = pageSet.length * copies;
        html = localStrings.getStringF('printPreviewSummaryFormatLong',
                                       '<b>' + numOfSheets + '</b>',
                                       '<b>' + summaryLabel + '</b>',
                                       numOfPagesText, pagesLabel);
      } else {
        html = localStrings.getStringF('printPreviewSummaryFormatShort',
                                       '<b>' + numOfSheets + '</b>',
                                       '<b>' + summaryLabel + '</b>');
      }

      // Removing extra spaces from within the string.
      html = html.replace(/\s{2,}/g, ' ');
      this.summary_.innerHTML = html;
    }
  };

  return {
    PrintHeader: PrintHeader
  };
});