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.Component}
17 function CopiesSettings(copiesTicketItem, collateTicketItem) {
18 print_preview.Component.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.Component.prototype,
59 /** @param {boolean} isEnabled Whether the copies settings is enabled. */
60 set isEnabled(isEnabled) {
61 this.getChildElement('input.copies').disabled = !isEnabled;
62 this.getChildElement('input.collate').disabled = !isEnabled;
63 this.isEnabled_ = isEnabled;
67 this.getChildElement('button.increment').disabled = true;
68 this.getChildElement('button.decrement').disabled = true;
73 enterDocument: function() {
74 print_preview.Component.prototype.enterDocument.call(this);
77 this.getChildElement('input.copies'),
79 this.onTextfieldKeyUp_.bind(this));
81 this.getChildElement('input.copies'),
83 this.onTextfieldBlur_.bind(this));
85 this.getChildElement('button.increment'),
87 this.onButtonClicked_.bind(this, 1));
89 this.getChildElement('button.decrement'),
91 this.onButtonClicked_.bind(this, -1));
93 this.getChildElement('input.collate'),
95 this.onCollateCheckboxClick_.bind(this));
97 this.copiesTicketItem_,
98 print_preview.ticket_items.TicketItem.EventType.CHANGE,
99 this.updateState_.bind(this));
101 this.collateTicketItem_,
102 print_preview.ticket_items.TicketItem.EventType.CHANGE,
103 this.updateState_.bind(this));
107 * Updates the state of the copies settings UI controls.
110 updateState_: function() {
111 if (!this.copiesTicketItem_.isCapabilityAvailable()) {
112 fadeOutOption(this.getElement());
116 if (this.getChildElement('input.copies').value !=
117 this.copiesTicketItem_.getValue()) {
118 this.getChildElement('input.copies').value =
119 this.copiesTicketItem_.getValue();
122 var currentValueGreaterThan1 = false;
123 if (this.copiesTicketItem_.isValid()) {
124 this.getChildElement('input.copies').classList.remove('invalid');
125 fadeOutElement(this.getChildElement('.hint'));
126 this.getChildElement('.hint').setAttribute('aria-hidden', true);
127 var currentValue = this.copiesTicketItem_.getValueAsNumber();
128 var currentValueGreaterThan1 = currentValue > 1;
129 this.getChildElement('button.increment').disabled =
131 !this.copiesTicketItem_.wouldValueBeValid(currentValue + 1);
132 this.getChildElement('button.decrement').disabled =
134 !this.copiesTicketItem_.wouldValueBeValid(currentValue - 1);
136 this.getChildElement('input.copies').classList.add('invalid');
137 this.getChildElement('.hint').setAttribute('aria-hidden', false);
138 fadeInElement(this.getChildElement('.hint'));
139 this.getChildElement('button.increment').disabled = true;
140 this.getChildElement('button.decrement').disabled = true;
143 if (!(this.getChildElement('.collate-container').hidden =
144 !this.collateTicketItem_.isCapabilityAvailable() ||
145 !currentValueGreaterThan1)) {
146 this.getChildElement('input.collate').checked =
147 this.collateTicketItem_.getValue();
150 fadeInOption(this.getElement());
154 * Called whenever the increment/decrement buttons are clicked.
155 * @param {number} delta Must be 1 for an increment button click and -1 for
156 * a decrement button click.
159 onButtonClicked_: function(delta) {
160 // Assumes text field has a valid number.
162 parseInt(this.getChildElement('input.copies').value) + delta;
163 this.copiesTicketItem_.updateValue(newValue + '');
167 * Called after a timeout after user input into the textfield.
170 onTextfieldTimeout_: function() {
171 var copiesVal = this.getChildElement('input.copies').value;
172 if (copiesVal != '') {
173 this.copiesTicketItem_.updateValue(copiesVal);
178 * Called when a keyup event occurs on the textfield. Starts an input
180 * @param {Event} event Contains the pressed key.
183 onTextfieldKeyUp_: function(event) {
184 if (this.textfieldTimeout_) {
185 clearTimeout(this.textfieldTimeout_);
187 this.textfieldTimeout_ = setTimeout(
188 this.onTextfieldTimeout_.bind(this), CopiesSettings.TEXTFIELD_DELAY_);
192 * Called when the focus leaves the textfield. If the textfield is empty,
193 * its value is set to 1.
196 onTextfieldBlur_: function() {
197 if (this.getChildElement('input.copies').value == '') {
198 this.copiesTicketItem_.updateValue('1');
203 * Called when the collate checkbox is clicked. Updates the print ticket.
206 onCollateCheckboxClick_: function() {
207 this.collateTicketItem_.updateValue(
208 this.getChildElement('input.collate').checked);
214 CopiesSettings: CopiesSettings