OLD | NEW |
(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 if (isEnabled) { |
| 129 this.updateState_(); |
| 130 } else { |
| 131 this.textfield_.disabled = true; |
| 132 this.incrementButton_.disabled = true; |
| 133 this.decrementButton_.disabled = true; |
| 134 this.duplexCheckbox_.disabled = true; |
| 135 } |
| 136 }, |
| 137 |
| 138 /** @override */ |
| 139 enterDocument: function() { |
| 140 print_preview.Component.prototype.enterDocument.call(this); |
| 141 |
| 142 this.tracker.add( |
| 143 this.textfield_, 'keyup', this.onTextfieldKeyUp_.bind(this)); |
| 144 this.tracker.add( |
| 145 this.textfield_, 'blur', this.onTextfieldBlur_.bind(this)); |
| 146 this.tracker.add( |
| 147 this.incrementButton_, 'click', this.onButtonClicked_.bind(this, 1)); |
| 148 this.tracker.add( |
| 149 this.decrementButton_, 'click', this.onButtonClicked_.bind(this, -1)); |
| 150 this.tracker.add( |
| 151 this.duplexCheckbox_, |
| 152 'click', |
| 153 this.onDuplexCheckboxClick_.bind(this)); |
| 154 this.tracker.add( |
| 155 this.collateCheckbox_, |
| 156 'click', |
| 157 this.onCollateCheckboxClick_.bind(this)); |
| 158 this.tracker.add( |
| 159 this.printTicketStore_, |
| 160 print_preview.PrintTicketStore.EventType.INITIALIZE, |
| 161 this.updateState_.bind(this)); |
| 162 this.tracker.add( |
| 163 this.printTicketStore_, |
| 164 print_preview.PrintTicketStore.EventType.CAPABILITIES_CHANGE, |
| 165 this.updateState_.bind(this)); |
| 166 this.tracker.add( |
| 167 this.printTicketStore_, |
| 168 print_preview.PrintTicketStore.EventType.TICKET_CHANGE, |
| 169 this.updateState_.bind(this)); |
| 170 }, |
| 171 |
| 172 /** @override */ |
| 173 exitDocument: function() { |
| 174 print_preview.Component.prototype.exitDocument.call(this); |
| 175 this.textfield_ = null; |
| 176 this.incrementButton_ = null; |
| 177 this.decrementButton_ = null; |
| 178 this.collateDiv_ = null; |
| 179 this.collateCheckbox_ = null; |
| 180 this.duplexDiv_ = null; |
| 181 this.duplexCheckbox_ = null; |
| 182 this.hintEl_ = null; |
| 183 }, |
| 184 |
| 185 /** @override */ |
| 186 decorateInternal: function() { |
| 187 this.textfield_ = this.getElement().getElementsByClassName( |
| 188 CopiesSettings.Classes_.COPIES)[0]; |
| 189 this.incrementButton_ = this.getElement().getElementsByClassName( |
| 190 CopiesSettings.Classes_.INCREMENT)[0]; |
| 191 this.decrementButton_ = this.getElement().getElementsByClassName( |
| 192 CopiesSettings.Classes_.DECREMENT)[0]; |
| 193 this.collateDiv_ = this.getElement().getElementsByClassName( |
| 194 CopiesSettings.Classes_.COLLATE)[0]; |
| 195 this.collateCheckbox_ = this.getElement().getElementsByClassName( |
| 196 CopiesSettings.Classes_.COLLATE_CHECKBOX)[0]; |
| 197 this.duplexDiv_ = this.getElement().getElementsByClassName( |
| 198 CopiesSettings.Classes_.DUPLEX)[0]; |
| 199 this.duplexCheckbox_ = this.getElement().getElementsByClassName( |
| 200 CopiesSettings.Classes_.DUPLEX_CHECKBOX)[0]; |
| 201 this.hintEl_ = this.getElement().getElementsByClassName( |
| 202 CopiesSettings.Classes_.HINT)[0]; |
| 203 }, |
| 204 |
| 205 /** |
| 206 * Updates the state of the copies settings UI controls. |
| 207 * @private |
| 208 */ |
| 209 updateState_: function() { |
| 210 if (!this.printTicketStore_.hasCopiesCapability()) { |
| 211 fadeOutOption(this.getElement()); |
| 212 return; |
| 213 } |
| 214 |
| 215 if (this.textfield_.value != this.printTicketStore_.getCopiesStr()) { |
| 216 this.textfield_.value = this.printTicketStore_.getCopiesStr(); |
| 217 } |
| 218 |
| 219 var currentValueGreaterThan1 = false; |
| 220 if (this.printTicketStore_.isCopiesValid()) { |
| 221 this.textfield_.classList.remove('invalid'); |
| 222 fadeOutElement(this.hintEl_); |
| 223 this.hintEl_.setAttribute('aria-hidden', true); |
| 224 var currentValue = parseInt(this.printTicketStore_.getCopiesStr()); |
| 225 var currentValueGreaterThan1 = currentValue > 1; |
| 226 this.incrementButton_.disabled = |
| 227 !this.isEnabled_ || |
| 228 !this.printTicketStore_.isCopiesValidForValue(currentValue + 1); |
| 229 this.decrementButton_.disabled = |
| 230 !this.isEnabled_ || |
| 231 !this.printTicketStore_.isCopiesValidForValue(currentValue - 1); |
| 232 } else { |
| 233 this.textfield_.classList.add('invalid'); |
| 234 this.hintEl_.setAttribute('aria-hidden', false); |
| 235 fadeInElement(this.hintEl_); |
| 236 this.incrementButton_.disabled = true; |
| 237 this.decrementButton_.disabled = true; |
| 238 } |
| 239 |
| 240 if (!(this.collateDiv_.hidden = |
| 241 !this.printTicketStore_.hasCollateCapability() || |
| 242 !currentValueGreaterThan1)) { |
| 243 this.collateCheckbox_.checked = |
| 244 this.printTicketStore_.isCollateEnabled(); |
| 245 } |
| 246 |
| 247 // On Windows, some printers don't specify their duplex values in the |
| 248 // printer schema. If the printer duplex value is UNKNOWN_DUPLEX_MODE, |
| 249 // hide the two sided option in preview tab UI. |
| 250 // Ref bug: http://crbug.com/89204 |
| 251 if (!(this.duplexDiv_.hidden = |
| 252 !this.printTicketStore_.hasDuplexCapability())) { |
| 253 this.duplexCheckbox_.checked = this.printTicketStore_.isDuplexEnabled(); |
| 254 } |
| 255 |
| 256 fadeInOption(this.getElement()); |
| 257 }, |
| 258 |
| 259 /** |
| 260 * Called when the duplex checkbox changes state. Updates the print ticket. |
| 261 * @private |
| 262 */ |
| 263 onDuplexCheckboxClick_: function() { |
| 264 this.printTicketStore_.updateDuplex(this.duplexCheckbox_.checked); |
| 265 }, |
| 266 |
| 267 /** |
| 268 * Called whenever the increment/decrement buttons are clicked. |
| 269 * @param {number} delta Must be 1 for an increment button click and -1 for |
| 270 * a decrement button click. |
| 271 * @private |
| 272 */ |
| 273 onButtonClicked_: function(delta) { |
| 274 // Assumes text field has a valid number. |
| 275 var newValue = parseInt(this.textfield_.value) + delta; |
| 276 this.printTicketStore_.updateCopies(newValue); |
| 277 }, |
| 278 |
| 279 /** |
| 280 * Called after a timeout after user input into the textfield. |
| 281 * @private |
| 282 */ |
| 283 onTextfieldTimeout_: function() { |
| 284 if (this.textfield_ != '') { |
| 285 this.printTicketStore_.updateCopies(this.textfield_.value); |
| 286 } |
| 287 }, |
| 288 |
| 289 /** |
| 290 * Called when a keyup event occurs on the textfield. Starts an input |
| 291 * timeout. |
| 292 * @param {Event} event Contains the pressed key. |
| 293 * @private |
| 294 */ |
| 295 onTextfieldKeyUp_: function(event) { |
| 296 if (this.textfieldTimeout_) { |
| 297 clearTimeout(this.textfieldTimeout_); |
| 298 } |
| 299 this.textfieldTimeout_ = setTimeout( |
| 300 this.onTextfieldTimeout_.bind(this), CopiesSettings.TEXTFIELD_DELAY_); |
| 301 }, |
| 302 |
| 303 /** |
| 304 * Called when the focus leaves the textfield. If the textfield is empty, |
| 305 * its value is set to 1. |
| 306 * @private |
| 307 */ |
| 308 onTextfieldBlur_: function() { |
| 309 if (this.textfield_.value == '') { |
| 310 this.printTicketStore_.updateCopies('1'); |
| 311 } |
| 312 }, |
| 313 |
| 314 /** |
| 315 * Called when the collate checkbox is clicked. Updates the print ticket. |
| 316 * @private |
| 317 */ |
| 318 onCollateCheckboxClick_: function() { |
| 319 this.printTicketStore_.updateCollate(this.collateCheckbox_.checked); |
| 320 } |
| 321 }; |
| 322 |
| 323 // Export |
| 324 return { |
| 325 CopiesSettings: CopiesSettings |
| 326 }; |
| 327 }); |
OLD | NEW |