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 * Component that renders the copies settings UI.
10 * @param {!print_preview.ticket_items.Copies} copiesTicketItem Used to read
11 * and write the copies value.
12 * @param {!print_preview.ticket_items.Collate} collateTicketItem Used to read
13 * and write the collate value.
15 * @extends {print_preview.SettingsSection}
17 function CopiesSettings(copiesTicketItem, collateTicketItem) {
18 print_preview.SettingsSection.call(this);
21 * Used to read and write the copies value.
22 * @type {!print_preview.ticket_items.Copies}
25 this.copiesTicketItem_ = copiesTicketItem;
28 * Used to read and write the collate value.
29 * @type {!print_preview.ticket_items.Collate}
32 this.collateTicketItem_ = collateTicketItem;
35 * Timeout used to delay processing of the copies input.
39 this.textfieldTimeout_ = null;
42 * Whether this component is enabled or not.
46 this.isEnabled_ = true;
50 * Delay in milliseconds before processing the textfield.
54 CopiesSettings.TEXTFIELD_DELAY_ = 250;
56 CopiesSettings.prototype = {
57 __proto__: print_preview.SettingsSection.prototype,
60 isAvailable: function() {
61 return this.copiesTicketItem_.isCapabilityAvailable();
65 hasCollapsibleContent: function() {
70 set isEnabled(isEnabled) {
71 this.getChildElement('input.copies').disabled = !isEnabled;
72 this.getChildElement('input.collate').disabled = !isEnabled;
73 this.isEnabled_ = isEnabled;
77 this.getChildElement('button.increment').disabled = true;
78 this.getChildElement('button.decrement').disabled = true;
83 enterDocument: function() {
84 print_preview.SettingsSection.prototype.enterDocument.call(this);
86 this.getChildElement('input.copies'),
88 this.onTextfieldKeyDown_.bind(this));
90 this.getChildElement('input.copies'),
92 this.onTextfieldInput_.bind(this));
94 this.getChildElement('input.copies'),
96 this.onTextfieldBlur_.bind(this));
98 this.getChildElement('button.increment'),
100 this.onButtonClicked_.bind(this, 1));
102 this.getChildElement('button.decrement'),
104 this.onButtonClicked_.bind(this, -1));
106 this.getChildElement('input.collate'),
108 this.onCollateCheckboxClick_.bind(this));
110 this.copiesTicketItem_,
111 print_preview.ticket_items.TicketItem.EventType.CHANGE,
112 this.updateState_.bind(this));
114 this.collateTicketItem_,
115 print_preview.ticket_items.TicketItem.EventType.CHANGE,
116 this.updateState_.bind(this));
120 * Updates the state of the copies settings UI controls.
123 updateState_: function() {
124 if (this.isAvailable()) {
125 if (this.getChildElement('input.copies').value !=
126 this.copiesTicketItem_.getValue()) {
127 this.getChildElement('input.copies').value =
128 this.copiesTicketItem_.getValue();
131 var currentValueGreaterThan1 = false;
132 if (this.copiesTicketItem_.isValid()) {
133 this.getChildElement('input.copies').classList.remove('invalid');
134 fadeOutElement(this.getChildElement('.hint'));
135 var currentValue = this.copiesTicketItem_.getValueAsNumber();
136 var currentValueGreaterThan1 = currentValue > 1;
137 this.getChildElement('button.increment').disabled =
139 !this.copiesTicketItem_.wouldValueBeValid(currentValue + 1);
140 this.getChildElement('button.decrement').disabled =
142 !this.copiesTicketItem_.wouldValueBeValid(currentValue - 1);
144 this.getChildElement('input.copies').classList.add('invalid');
145 fadeInElement(this.getChildElement('.hint'));
146 this.getChildElement('button.increment').disabled = true;
147 this.getChildElement('button.decrement').disabled = true;
150 if (!(this.getChildElement('.collate-container').hidden =
151 !this.collateTicketItem_.isCapabilityAvailable() ||
152 !currentValueGreaterThan1)) {
153 this.getChildElement('input.collate').checked =
154 this.collateTicketItem_.getValue();
157 this.updateUiStateInternal();
161 * Called whenever the increment/decrement buttons are clicked.
162 * @param {number} delta Must be 1 for an increment button click and -1 for
163 * a decrement button click.
166 onButtonClicked_: function(delta) {
167 // Assumes text field has a valid number.
169 parseInt(this.getChildElement('input.copies').value, 10) + delta;
170 this.copiesTicketItem_.updateValue(newValue + '');
174 * Called after a timeout after user input into the textfield.
177 onTextfieldTimeout_: function() {
178 this.textfieldTimeout_ = null;
179 var copiesVal = this.getChildElement('input.copies').value;
180 if (copiesVal != '') {
181 this.copiesTicketItem_.updateValue(copiesVal);
186 * Called when a key is pressed on the custom input.
187 * @param {Event} event Contains the key that was pressed.
190 onTextfieldKeyDown_: function(event) {
191 if (event.keyCode == 13 /*enter*/) {
192 if (this.textfieldTimeout_) {
193 clearTimeout(this.textfieldTimeout_);
195 this.onTextfieldTimeout_();
200 * Called when a input event occurs on the textfield. Starts an input
204 onTextfieldInput_: function() {
205 if (this.textfieldTimeout_) {
206 clearTimeout(this.textfieldTimeout_);
208 this.textfieldTimeout_ = setTimeout(
209 this.onTextfieldTimeout_.bind(this), CopiesSettings.TEXTFIELD_DELAY_);
213 * Called when the focus leaves the textfield. If the textfield is empty,
214 * its value is set to 1.
217 onTextfieldBlur_: function() {
218 if (this.getChildElement('input.copies').value == '') {
219 // Do it asynchronously to avoid moving focus to Print button in
220 // PrintHeader.onTicketChange_.
221 setTimeout((function() {
222 this.copiesTicketItem_.updateValue('1');
228 * Called when the collate checkbox is clicked. Updates the print ticket.
231 onCollateCheckboxClick_: function() {
232 this.collateTicketItem_.updateValue(
233 this.getChildElement('input.collate').checked);
239 CopiesSettings: CopiesSettings