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 * |
| 11 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and |
| 12 * write the copies settings. |
| 13 * @constructor |
| 14 * @extends {print_preview.Component} |
| 15 */ |
| 16 function CopiesSettings(printTicketStore) { |
| 17 print_preview.Component.call(this); |
| 18 |
| 19 /** |
| 20 * Used for writing to the print ticket and validating inputted values. |
| 21 * @type {!print_preview.PrintTicketStore} |
| 22 * @private |
| 23 */ |
| 24 this.printTicketStore_ = printTicketStore; |
| 25 |
| 26 /** |
| 27 * Timeout used to delay processing of the copies input. |
| 28 * @type {Object} |
| 29 * @private |
| 30 */ |
| 31 this.textfieldTimeout_ = null; |
| 32 |
| 33 /** |
| 34 * Whether this component is enabled or not. |
| 35 * @type {boolean} |
| 36 * @private |
| 37 */ |
| 38 this.isEnabled_ = true; |
| 39 }; |
| 40 |
| 41 CopiesSettings.Classes_ = { |
| 42 COPIES: 'copies-settings-copies', |
| 43 INCREMENT: 'copies-settings-increment', |
| 44 DECREMENT: 'copies-settings-decrement', |
| 45 HINT: 'copies-settings-hint', |
| 46 COLLATE: 'copies-settings-collate', |
| 47 COLLATE_CHECKBOX: 'copies-settings-collate-checkbox', |
| 48 DUPLEX: 'copies-settings-duplex', |
| 49 DUPLEX_CHECKBOX: 'copies-settings-duplex-checkbox' |
| 50 }; |
| 51 |
| 52 /** |
| 53 * Delay in milliseconds before processing the textfield. |
| 54 * @type {number} |
| 55 * @private |
| 56 */ |
| 57 CopiesSettings.TEXTFIELD_DELAY_ = 250; |
| 58 |
| 59 CopiesSettings.prototype = { |
| 60 __proto__: print_preview.Component.prototype, |
| 61 |
| 62 set isEnabled(isEnabled) { |
| 63 this.textfield_.disabled = !isEnabled; |
| 64 this.collateCheckbox_.disabled = !isEnabled; |
| 65 this.duplexCheckbox_.disabled = !isEnabled; |
| 66 this.isEnabled_ = isEnabled; |
| 67 this.updateState_(); |
| 68 }, |
| 69 |
| 70 /** @override */ |
| 71 enterDocument: function() { |
| 72 print_preview.Component.prototype.enterDocument.call(this); |
| 73 this.tracker.add( |
| 74 this.textfield_, 'keyup', this.onTextfieldKeyUp_.bind(this)); |
| 75 this.tracker.add( |
| 76 this.textfield_, 'blur', this.onTextfieldBlur_.bind(this)); |
| 77 this.tracker.add( |
| 78 this.incrementButton_, 'click', this.onButtonClicked_.bind(this, 1)); |
| 79 this.tracker.add( |
| 80 this.decrementButton_, 'click', this.onButtonClicked_.bind(this, -1)); |
| 81 this.tracker.add( |
| 82 this.duplexCheckbox_, |
| 83 'click', |
| 84 this.onDuplexCheckboxClick_.bind(this)); |
| 85 this.tracker.add( |
| 86 this.collateCheckbox_, |
| 87 'click', |
| 88 this.onCollateCheckboxClick_.bind(this)); |
| 89 this.tracker.add( |
| 90 this.printTicketStore_, |
| 91 print_preview.PrintTicketStore.Event.CAPABILITIES_CHANGE, |
| 92 this.onCapabilitiesChange_.bind(this)); |
| 93 this.tracker.add( |
| 94 this.printTicketStore_, |
| 95 print_preview.PrintTicketStore.Event.TICKET_CHANGE, |
| 96 this.onTicketChange_.bind(this)); |
| 97 }, |
| 98 |
| 99 get textfield_() { |
| 100 return this.getElement().getElementsByClassName( |
| 101 CopiesSettings.Classes_.COPIES)[0]; |
| 102 }, |
| 103 |
| 104 get incrementButton_() { |
| 105 return this.getElement().getElementsByClassName( |
| 106 CopiesSettings.Classes_.INCREMENT)[0]; |
| 107 }, |
| 108 |
| 109 get decrementButton_() { |
| 110 return this.getElement().getElementsByClassName( |
| 111 CopiesSettings.Classes_.DECREMENT)[0]; |
| 112 }, |
| 113 |
| 114 get collateCheckbox_() { |
| 115 return this.getElement().getElementsByClassName( |
| 116 CopiesSettings.Classes_.COLLATE_CHECKBOX)[0]; |
| 117 }, |
| 118 |
| 119 get duplexCheckbox_() { |
| 120 return this.getElement().getElementsByClassName( |
| 121 CopiesSettings.Classes_.DUPLEX_CHECKBOX)[0]; |
| 122 }, |
| 123 |
| 124 updateState_: function() { |
| 125 var hintEl = this.getElement().getElementsByClassName( |
| 126 CopiesSettings.Classes_.HINT)[0]; |
| 127 if (this.printTicketStore_.isCopiesValid()) { |
| 128 var currentValue = parseInt(this.printTicketStore_.getCopiesStr()); |
| 129 if (this.textfield_.value != (currentValue + '')) { |
| 130 this.textfield_.value = currentValue; |
| 131 } |
| 132 this.textfield_.classList.remove('invalid'); |
| 133 fadeOutElement(hintEl); |
| 134 hintEl.setAttribute('aria-hidden', true); |
| 135 this.incrementButton_.disabled = |
| 136 !this.isEnabled_ || |
| 137 !this.printTicketStore_.isCopiesValidForValue(currentValue + 1); |
| 138 this.decrementButton_.disabled = |
| 139 !this.isEnabled_ || |
| 140 !this.printTicketStore_.isCopiesValidForValue(currentValue - 1); |
| 141 var collate = this.getElement().getElementsByClassName( |
| 142 CopiesSettings.Classes_.COLLATE)[0]; |
| 143 collate.hidden = |
| 144 !this.printTicketStore_.hasCollateCapability() || |
| 145 currentValue <= 1; |
| 146 } else { |
| 147 this.textfield_.classList.add('invalid'); |
| 148 hintEl.setAttribute('aria-hidden', false); |
| 149 fadeInElement(hintEl); |
| 150 this.incrementButton_.disabled = true; |
| 151 this.decrementButton_.disabled = true; |
| 152 } |
| 153 |
| 154 var isCollate = this.printTicketStore_.isCollateEnabled(); |
| 155 if (this.collateCheckbox_.checked != isCollate) { |
| 156 this.collateCheckbox_.checked = isCollate; |
| 157 } |
| 158 |
| 159 var isDuplex = this.printTicketStore_.isDuplexEnabled(); |
| 160 if (this.duplexCheckbox_.checked != isDuplex) { |
| 161 this.duplexCheckbox_.checked = isDuplex; |
| 162 } |
| 163 }, |
| 164 |
| 165 onDuplexCheckboxClick_: function() { |
| 166 this.printTicketStore_.updateDuplex(this.duplexCheckbox_.checked); |
| 167 }, |
| 168 |
| 169 /** |
| 170 * Called whenever the increment/decrement buttons are clicked. |
| 171 * @param {number} delta Must be 1 for an increment button click and -1 for |
| 172 * a decrement button click. |
| 173 * @private |
| 174 */ |
| 175 onButtonClicked_: function(delta) { |
| 176 // Assumes text field has a valid number. |
| 177 var newValue = parseInt(this.textfield_.value) + delta; |
| 178 this.printTicketStore_.updateCopies(newValue); |
| 179 }, |
| 180 |
| 181 /** |
| 182 * Called after a timeout after user input into the textfield. |
| 183 * @private |
| 184 */ |
| 185 onTextfieldTimeout_: function() { |
| 186 if (this.textfield_ != '') { |
| 187 this.printTicketStore_.updateCopies(this.textfield_.value); |
| 188 } |
| 189 }, |
| 190 |
| 191 /** |
| 192 * Called when a keyup event occurs on the textfield. Starts an input |
| 193 * timeout. |
| 194 * @param {Event} evt Contains the pressed key. |
| 195 * @private |
| 196 */ |
| 197 onTextfieldKeyUp_: function(evt) { |
| 198 if (this.textfieldTimeout_) { |
| 199 clearTimeout(this.textfieldTimeout_); |
| 200 } |
| 201 this.textfieldTimeout_ = setTimeout( |
| 202 this.onTextfieldTimeout_.bind(this), |
| 203 CopiesSettings.TEXTFIELD_DELAY_); |
| 204 }, |
| 205 |
| 206 /** |
| 207 * Called when the focus leaves the textfield. If the textfield is empty, |
| 208 * its value is set to 1. |
| 209 * @private |
| 210 */ |
| 211 onTextfieldBlur_: function() { |
| 212 if (this.textfield_.value == '') { |
| 213 this.textfield_.value = '1'; |
| 214 this.printTicketStore_.updateCopies('1'); |
| 215 } |
| 216 }, |
| 217 |
| 218 /** |
| 219 * Called when the collate checkbox is clicked. Updates the print ticket. |
| 220 */ |
| 221 onCollateCheckboxClick_: function() { |
| 222 this.printTicketStore_.updateCollate(this.collateCheckbox_.checked); |
| 223 }, |
| 224 |
| 225 /** |
| 226 * Called when the ticket store's capabilities have changed. |
| 227 * @param {cr.Event} evt Contains the new capabilities. |
| 228 * @private |
| 229 */ |
| 230 onCapabilitiesChange_: function(evt) { |
| 231 // On Windows, some printers don't specify their duplex values in the |
| 232 // printer schema. If the printer duplex value is UNKNOWN_DUPLEX_MODE, |
| 233 // hide the two sided option in preview tab UI. |
| 234 // Ref bug: http://crbug.com/89204 |
| 235 var duplex = this.getElement().getElementsByClassName( |
| 236 CopiesSettings.Classes_.DUPLEX)[0]; |
| 237 if (this.printTicketStore_.hasDuplexCapability()) { |
| 238 duplex.checked = this.printTicketStore_.isDuplexEnabled(); |
| 239 duplex.hidden = false; |
| 240 } else { |
| 241 duplex.hidden = true; |
| 242 } |
| 243 |
| 244 if (this.printTicketStore_.hasCopiesCapability()) { |
| 245 this.textfield_.value = this.printTicketStore_.getCopiesStr(); |
| 246 fadeInOption(this.getElement()); |
| 247 } else { |
| 248 fadeOutOption(this.getElement()); |
| 249 } |
| 250 |
| 251 if (this.printTicketStore_.hasCollateCapability()) { |
| 252 this.collateCheckbox_.checked = |
| 253 this.printTicketStore_.isCollateEnabled(); |
| 254 } |
| 255 |
| 256 this.updateState_(); |
| 257 }, |
| 258 |
| 259 /** |
| 260 * Called when the print ticket changes. |
| 261 * @private |
| 262 */ |
| 263 onTicketChange_: function() { |
| 264 this.updateState_(); |
| 265 } |
| 266 }; |
| 267 |
| 268 // Export |
| 269 return { |
| 270 CopiesSettings: CopiesSettings |
| 271 }; |
| 272 }); |
OLD | NEW |