- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / print_header.js
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.
4
5 cr.define('print_preview', function() {
6   'use strict';
7
8   /**
9    * Creates a PrintHeader object. This object encapsulates all the elements
10    * and logic related to the top part of the left pane in print_preview.html.
11    * @param {!print_preview.PrintTicketStore} printTicketStore Used to read
12    *     information about the document.
13    * @param {!print_preview.DestinationStore} destinationStore Used to get the
14    *     selected destination.
15    * @constructor
16    * @extends {print_preview.Component}
17    */
18   function PrintHeader(printTicketStore, destinationStore) {
19     print_preview.Component.call(this);
20
21     /**
22      * Used to read information about the document.
23      * @type {!print_preview.PrintTicketStore}
24      * @private
25      */
26     this.printTicketStore_ = printTicketStore;
27
28     /**
29      * Used to get the selected destination.
30      * @type {!print_preview.DestinationStore}
31      * @private
32      */
33     this.destinationStore_ = destinationStore;
34
35     /**
36      * Whether the component is enabled.
37      * @type {boolean}
38      * @private
39      */
40     this.isEnabled_ = true;
41
42     /**
43      * Whether the print button is enabled.
44      * @type {boolean}
45      * @private
46      */
47     this.isPrintButtonEnabled_ = true;
48   };
49
50   /**
51    * Event types dispatched by the print header.
52    * @enum {string}
53    */
54   PrintHeader.EventType = {
55     PRINT_BUTTON_CLICK: 'print_preview.PrintHeader.PRINT_BUTTON_CLICK',
56     CANCEL_BUTTON_CLICK: 'print_preview.PrintHeader.CANCEL_BUTTON_CLICK'
57   };
58
59   PrintHeader.prototype = {
60     __proto__: print_preview.Component.prototype,
61
62     set isEnabled(isEnabled) {
63       this.isEnabled_ = isEnabled;
64       this.updatePrintButtonEnabledState_();
65       this.getChildElement('button.cancel').disabled = !isEnabled;
66     },
67
68     set isPrintButtonEnabled(isEnabled) {
69       this.isPrintButtonEnabled_ = isEnabled;
70       this.updatePrintButtonEnabledState_();
71     },
72
73     /** @param {string} message Error message to display in the print header. */
74     setErrorMessage: function(message) {
75       var summaryEl = this.getChildElement('.summary');
76       summaryEl.innerHTML = '';
77       summaryEl.textContent = message;
78     },
79
80     /** @override */
81     enterDocument: function() {
82       print_preview.Component.prototype.enterDocument.call(this);
83
84       // User events
85       this.tracker.add(
86           this.getChildElement('button.cancel'),
87           'click',
88           this.onCancelButtonClick_.bind(this));
89       this.tracker.add(
90           this.getChildElement('button.print'),
91           'click',
92           this.onPrintButtonClick_.bind(this));
93
94       // Data events.
95       this.tracker.add(
96           this.printTicketStore_,
97           print_preview.PrintTicketStore.EventType.INITIALIZE,
98           this.onTicketChange_.bind(this));
99       this.tracker.add(
100           this.printTicketStore_,
101           print_preview.PrintTicketStore.EventType.DOCUMENT_CHANGE,
102           this.onTicketChange_.bind(this));
103       this.tracker.add(
104           this.printTicketStore_,
105           print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
106           this.onTicketChange_.bind(this));
107       this.tracker.add(
108           this.destinationStore_,
109           print_preview.DestinationStore.EventType.DESTINATION_SELECT,
110           this.onDestinationSelect_.bind(this));
111       this.tracker.add(
112           this.printTicketStore_.copies,
113           print_preview.ticket_items.TicketItem.EventType.CHANGE,
114           this.onTicketChange_.bind(this));
115       this.tracker.add(
116           this.printTicketStore_.duplex,
117           print_preview.ticket_items.TicketItem.EventType.CHANGE,
118           this.onTicketChange_.bind(this));
119       this.tracker.add(
120           this.printTicketStore_.pageRange,
121           print_preview.ticket_items.TicketItem.EventType.CHANGE,
122           this.onTicketChange_.bind(this));
123     },
124
125     /**
126      * Updates Print Button state.
127      * @private
128      */
129     updatePrintButtonEnabledState_: function() {
130       this.getChildElement('button.print').disabled =
131           !this.isEnabled_ ||
132           !this.isPrintButtonEnabled_ ||
133           !this.printTicketStore_.isTicketValid();
134     },
135
136     /**
137      * Updates the summary element based on the currently selected user options.
138      * @private
139      */
140     updateSummary_: function() {
141       if (!this.printTicketStore_.isTicketValid()) {
142         this.getChildElement('.summary').innerHTML = '';
143         return;
144       }
145
146       var summaryLabel =
147           localStrings.getString('printPreviewSheetsLabelSingular');
148       var pagesLabel = localStrings.getString('printPreviewPageLabelPlural');
149
150       var saveToPdf = this.destinationStore_.selectedDestination &&
151           this.destinationStore_.selectedDestination.id ==
152               print_preview.Destination.GooglePromotedId.SAVE_AS_PDF;
153       if (saveToPdf) {
154         summaryLabel = localStrings.getString('printPreviewPageLabelSingular');
155       }
156
157       var numPages = this.printTicketStore_.pageRange.getPageNumberSet().size;
158       var numSheets = numPages;
159       if (!saveToPdf && this.printTicketStore_.duplex.getValue()) {
160         numSheets = Math.ceil(numPages / 2);
161       }
162
163       var copies = this.printTicketStore_.copies.getValueAsNumber();
164       numSheets *= copies;
165       numPages *= copies;
166
167       if (numSheets > 1) {
168         summaryLabel = saveToPdf ? pagesLabel :
169             localStrings.getString('printPreviewSheetsLabelPlural');
170       }
171
172       var html;
173       if (numPages != numSheets) {
174         html = localStrings.getStringF('printPreviewSummaryFormatLong',
175                                        '<b>' + numSheets + '</b>',
176                                        '<b>' + summaryLabel + '</b>',
177                                        numPages,
178                                        pagesLabel);
179       } else {
180         html = localStrings.getStringF('printPreviewSummaryFormatShort',
181                                        '<b>' + numSheets + '</b>',
182                                        '<b>' + summaryLabel + '</b>');
183       }
184
185       // Removing extra spaces from within the string.
186       html = html.replace(/\s{2,}/g, ' ');
187       this.getChildElement('.summary').innerHTML = html;
188     },
189
190     /**
191      * Called when the print button is clicked. Dispatches a PRINT_DOCUMENT
192      * common event.
193      * @private
194      */
195     onPrintButtonClick_: function() {
196       if (this.destinationStore_.selectedDestination.id !=
197           print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
198         this.getChildElement('button.print').classList.add('loading');
199         this.getChildElement('button.cancel').classList.add('loading');
200         this.getChildElement('.summary').innerHTML =
201             localStrings.getString('printing');
202       }
203       cr.dispatchSimpleEvent(this, PrintHeader.EventType.PRINT_BUTTON_CLICK);
204     },
205
206     /**
207      * Called when the cancel button is clicked. Dispatches a
208      * CLOSE_PRINT_PREVIEW event.
209      * @private
210      */
211     onCancelButtonClick_: function() {
212       cr.dispatchSimpleEvent(this, PrintHeader.EventType.CANCEL_BUTTON_CLICK);
213     },
214
215     /**
216      * Called when a new destination is selected. Updates the text on the print
217      * button.
218      * @private
219      */
220     onDestinationSelect_: function() {
221       var isSaveLabel = this.destinationStore_.selectedDestination.id ==
222           print_preview.Destination.GooglePromotedId.SAVE_AS_PDF ||
223           this.destinationStore_.selectedDestination.id ==
224               print_preview.Destination.GooglePromotedId.DOCS;
225       this.getChildElement('button.print').textContent = isSaveLabel ?
226           localStrings.getString('saveButton') :
227           localStrings.getString('printButton');
228       this.getChildElement('button.print').focus();
229     },
230
231     /**
232      * Called when the print ticket has changed. Disables the print button if
233      * any of the settings are invalid.
234      * @private
235      */
236     onTicketChange_: function() {
237       this.updatePrintButtonEnabledState_();
238       this.updateSummary_();
239       if (document.activeElement == null ||
240           document.activeElement == document.body) {
241         this.getChildElement('button.print').focus();
242       }
243     }
244   };
245
246   // Export
247   return {
248     PrintHeader: PrintHeader
249   };
250 });