Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: chrome/browser/resources/print_preview/settings/copies_settings.js

Issue 10108001: Refactor print preview web ui (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address reviewer comments Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 * Component that renders the copies settings UI.
10 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and
11 * write the copies settings.
12 * @constructor
13 * @extends {print_preview.Component}
14 */
15 function CopiesSettings(printTicketStore) {
16 print_preview.Component.call(this);
17
18 /**
19 * Used for writing to the print ticket and validating inputted values.
20 * @type {!print_preview.PrintTicketStore}
21 * @private
22 */
23 this.printTicketStore_ = printTicketStore;
24
25 /**
26 * Timeout used to delay processing of the copies input.
27 * @type {Object}
28 * @private
29 */
30 this.textfieldTimeout_ = null;
31
32 /**
33 * Whether this component is enabled or not.
34 * @type {boolean}
35 * @private
36 */
37 this.isEnabled_ = true;
38
39 /**
40 * Textfield for entering copies values.
41 * @type {HTMLInputElement}
42 * @private
43 */
44 this.textfield_ = null;
45
46 /**
47 * Increment button used to increment the copies value.
48 * @type {HTMLButtonElement}
49 * @private
50 */
51 this.incrementButton_ = null;
52
53 /**
54 * Decrement button used to decrement the copies value.
55 * @type {HTMLButtonElement}
56 * @private
57 */
58 this.decrementButton_ = null;
59
60 /**
61 * Container div for the collate checkbox.
62 * @type {HTMLDivElement}
63 * @private
64 */
65 this.collateDiv_ = null;
66
67 /**
68 * Checkbox used to enable/disable collation.
69 * @type {HTMLInputElement}
70 * @private
71 */
72 this.collateCheckbox_ = null;
73
74 /**
75 * Container div for the duplex checkbox.
76 * @type {HTMLDivElement}
77 * @private
78 */
79 this.duplexDiv_ = null;
80
81 /**
82 * Checkbox used to enable/disable duplexing.
83 * @type {HTMLInputElement}
84 * @private
85 */
86 this.duplexCheckbox_ = null;
87
88 /**
89 * Hint element used to show a hint when the copies value is invalid.
90 * @type {HTMLElement}
91 * @private
92 */
93 this.hintEl_ = null;
94 };
95
96 /**
97 * CSS classes used by the component.
98 * @enum {string}
99 * @private
100 */
101 CopiesSettings.Classes_ = {
102 COPIES: 'copies-settings-copies',
103 INCREMENT: 'copies-settings-increment',
104 DECREMENT: 'copies-settings-decrement',
105 HINT: 'copies-settings-hint',
106 COLLATE: 'copies-settings-collate',
107 COLLATE_CHECKBOX: 'copies-settings-collate-checkbox',
108 DUPLEX: 'copies-settings-duplex',
109 DUPLEX_CHECKBOX: 'copies-settings-duplex-checkbox'
110 };
111
112 /**
113 * Delay in milliseconds before processing the textfield.
114 * @type {number}
115 * @private
116 */
117 CopiesSettings.TEXTFIELD_DELAY_ = 250;
118
119 CopiesSettings.prototype = {
120 __proto__: print_preview.Component.prototype,
121
122 /** @param {boolean} isEnabled Whether the copies settings is enabled. */
123 set isEnabled(isEnabled) {
124 this.textfield_.disabled = !isEnabled;
125 this.collateCheckbox_.disabled = !isEnabled;
126 this.duplexCheckbox_.disabled = !isEnabled;
127 this.isEnabled_ = isEnabled;
128 this.updateState_();
129 },
130
131 /** @override */
132 enterDocument: function() {
133 print_preview.Component.prototype.enterDocument.call(this);
134
135 this.tracker.add(
136 this.textfield_, 'keyup', this.onTextfieldKeyUp_.bind(this));
137 this.tracker.add(
138 this.textfield_, 'blur', this.onTextfieldBlur_.bind(this));
139 this.tracker.add(
140 this.incrementButton_, 'click', this.onButtonClicked_.bind(this, 1));
141 this.tracker.add(
142 this.decrementButton_, 'click', this.onButtonClicked_.bind(this, -1));
143 this.tracker.add(
144 this.duplexCheckbox_,
145 'click',
146 this.onDuplexCheckboxClick_.bind(this));
147 this.tracker.add(
148 this.collateCheckbox_,
149 'click',
150 this.onCollateCheckboxClick_.bind(this));
151 this.tracker.add(
152 this.printTicketStore_,
153 print_preview.PrintTicketStore.EventType.INITIALIZE,
154 this.updateState_.bind(this));
155 this.tracker.add(
156 this.printTicketStore_,
157 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE,
158 this.updateState_.bind(this));
159 this.tracker.add(
160 this.printTicketStore_,
161 print_preview.PrintTicketStore.EventType.TICKET_CHANGE,
162 this.updateState_.bind(this));
163 },
164
165 /** @override */
166 exitDocument: function() {
167 print_preview.Component.prototype.exitDocument.call(this);
168 this.textfield_ = null;
169 this.incrementButton_ = null;
170 this.decrementButton_ = null;
171 this.collateDiv_ = null;
172 this.collateCheckbox_ = null;
173 this.duplexDiv_ = null;
174 this.duplexCheckbox_ = null;
175 this.hintEl_ = null;
176 },
177
178 /** @override */
179 decorateInternal: function() {
180 this.textfield_ = this.getElement().getElementsByClassName(
181 CopiesSettings.Classes_.COPIES)[0];
182 this.incrementButton_ = this.getElement().getElementsByClassName(
183 CopiesSettings.Classes_.INCREMENT)[0];
184 this.decrementButton_ = this.getElement().getElementsByClassName(
185 CopiesSettings.Classes_.DECREMENT)[0];
186 this.collateDiv_ = this.getElement().getElementsByClassName(
187 CopiesSettings.Classes_.COLLATE)[0];
188 this.collateCheckbox_ = this.getElement().getElementsByClassName(
189 CopiesSettings.Classes_.COLLATE_CHECKBOX)[0];
190 this.duplexDiv_ = this.getElement().getElementsByClassName(
191 CopiesSettings.Classes_.DUPLEX)[0];
192 this.duplexCheckbox_ = this.getElement().getElementsByClassName(
193 CopiesSettings.Classes_.DUPLEX_CHECKBOX)[0];
194 this.hintEl_ = this.getElement().getElementsByClassName(
195 CopiesSettings.Classes_.HINT)[0];
196 },
197
198 /**
199 * Updates the state of the copies settings UI controls.
200 * @private
201 */
202 updateState_: function() {
203 if (!this.printTicketStore_.hasCopiesCapability()) {
204 fadeOutOption(this.getElement());
205 return;
206 }
207
208 if (this.textfield_.value != this.printTicketStore_.getCopiesStr()) {
209 this.textfield_.value = this.printTicketStore_.getCopiesStr();
210 }
211
212 var currentValueGreaterThan1 = false;
213 if (this.printTicketStore_.isCopiesValid()) {
214 this.textfield_.classList.remove('invalid');
215 fadeOutElement(this.hintEl_);
216 this.hintEl_.setAttribute('aria-hidden', true);
217 var currentValue = parseInt(this.printTicketStore_.getCopiesStr());
218 var currentValueGreaterThan1 = currentValue > 1;
219 this.incrementButton_.disabled =
220 !this.isEnabled_ ||
221 !this.printTicketStore_.isCopiesValidForValue(currentValue + 1);
222 this.decrementButton_.disabled =
223 !this.isEnabled_ ||
224 !this.printTicketStore_.isCopiesValidForValue(currentValue - 1);
225 } else {
226 this.textfield_.classList.add('invalid');
227 this.hintEl_.setAttribute('aria-hidden', false);
228 fadeInElement(this.hintEl_);
229 this.incrementButton_.disabled = true;
230 this.decrementButton_.disabled = true;
231 }
232
233 if (!(this.collateDiv_.hidden =
234 !this.printTicketStore_.hasCollateCapability() ||
235 !currentValueGreaterThan1)) {
236 this.collateCheckbox_.checked =
237 this.printTicketStore_.isCollateEnabled();
238 }
239
240 // On Windows, some printers don't specify their duplex values in the
241 // printer schema. If the printer duplex value is UNKNOWN_DUPLEX_MODE,
242 // hide the two sided option in preview tab UI.
243 // Ref bug: http://crbug.com/89204
244 if (!(this.duplexDiv_.hidden =
245 !this.printTicketStore_.hasDuplexCapability())) {
246 this.duplexCheckbox_.checked = this.printTicketStore_.isDuplexEnabled();
247 }
248
249 fadeInOption(this.getElement());
250 },
251
252 /**
253 * Called when the duplex checkbox changes state. Updates the print ticket.
254 * @private
255 */
256 onDuplexCheckboxClick_: function() {
257 this.printTicketStore_.updateDuplex(this.duplexCheckbox_.checked);
258 },
259
260 /**
261 * Called whenever the increment/decrement buttons are clicked.
262 * @param {number} delta Must be 1 for an increment button click and -1 for
263 * a decrement button click.
264 * @private
265 */
266 onButtonClicked_: function(delta) {
267 // Assumes text field has a valid number.
268 var newValue = parseInt(this.textfield_.value) + delta;
269 this.printTicketStore_.updateCopies(newValue);
270 },
271
272 /**
273 * Called after a timeout after user input into the textfield.
274 * @private
275 */
276 onTextfieldTimeout_: function() {
277 if (this.textfield_ != '') {
278 this.printTicketStore_.updateCopies(this.textfield_.value);
279 }
280 },
281
282 /**
283 * Called when a keyup event occurs on the textfield. Starts an input
284 * timeout.
285 * @param {Event} event Contains the pressed key.
286 * @private
287 */
288 onTextfieldKeyUp_: function(event) {
289 if (this.textfieldTimeout_) {
290 clearTimeout(this.textfieldTimeout_);
291 }
292 this.textfieldTimeout_ = setTimeout(
293 this.onTextfieldTimeout_.bind(this), CopiesSettings.TEXTFIELD_DELAY_);
294 },
295
296 /**
297 * Called when the focus leaves the textfield. If the textfield is empty,
298 * its value is set to 1.
299 * @private
300 */
301 onTextfieldBlur_: function() {
302 if (this.textfield_.value == '') {
303 this.printTicketStore_.updateCopies('1');
304 }
305 },
306
307 /**
308 * Called when the collate checkbox is clicked. Updates the print ticket.
309 * @private
310 */
311 onCollateCheckboxClick_: function() {
312 this.printTicketStore_.updateCollate(this.collateCheckbox_.checked);
313 }
314 };
315
316 // Export
317 return {
318 CopiesSettings: CopiesSettings
319 };
320 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698