Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
|
dpapad
2016/10/20 23:15:54
I don't think the year needs to be updated.
rbpotter
2016/10/20 23:38:31
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Component that renders the copies settings UI. | 9 * Component that renders the copies settings UI. |
| 10 * @param {!print_preview.ticket_items.Copies} copiesTicketItem Used to read | 10 * @param {!print_preview.ticket_items.Copies} copiesTicketItem Used to read |
| 11 * and write the copies value. | 11 * and write the copies value. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 25 this.copiesTicketItem_ = copiesTicketItem; | 25 this.copiesTicketItem_ = copiesTicketItem; |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Used to read and write the collate value. | 28 * Used to read and write the collate value. |
| 29 * @type {!print_preview.ticket_items.Collate} | 29 * @type {!print_preview.ticket_items.Collate} |
| 30 * @private | 30 * @private |
| 31 */ | 31 */ |
| 32 this.collateTicketItem_ = collateTicketItem; | 32 this.collateTicketItem_ = collateTicketItem; |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Timeout used to delay processing of the copies input. | 35 * Timeout used to delay processing of the copies input in ms |
| 36 * @type {?number} | 36 * @type {?number} |
| 37 * @private | 37 * @private |
| 38 */ | 38 */ |
| 39 this.textfieldTimeout_ = null; | 39 this.textfieldTimeout_ = null; |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Whether this component is enabled or not. | 42 * Whether this component is enabled or not. |
| 43 * @type {boolean} | 43 * @type {boolean} |
| 44 * @private | 44 * @private |
| 45 */ | 45 */ |
| 46 this.isEnabled_ = true; | 46 this.isEnabled_ = true; |
| 47 | |
| 48 /** | |
| 49 * The element for the user input value. | |
| 50 * @type {HTMLElement} | |
| 51 * @private | |
| 52 */ | |
| 53 this.inputField_ = null; | |
| 47 }; | 54 }; |
| 48 | 55 |
| 49 /** | 56 /** |
| 50 * Delay in milliseconds before processing the textfield. | 57 * Delay in milliseconds before processing the textfield. |
| 51 * @type {number} | 58 * @type {number} |
| 52 * @private | 59 * @private |
| 53 */ | 60 */ |
| 54 CopiesSettings.TEXTFIELD_DELAY_ = 250; | 61 CopiesSettings.TEXTFIELD_DELAY_MS_ = 250; |
| 55 | 62 |
| 56 CopiesSettings.prototype = { | 63 CopiesSettings.prototype = { |
| 57 __proto__: print_preview.SettingsSection.prototype, | 64 __proto__: print_preview.SettingsSection.prototype, |
| 58 | 65 |
| 59 /** @override */ | 66 /** @override */ |
| 60 isAvailable: function() { | 67 isAvailable: function() { |
| 61 return this.copiesTicketItem_.isCapabilityAvailable(); | 68 return this.copiesTicketItem_.isCapabilityAvailable(); |
| 62 }, | 69 }, |
| 63 | 70 |
| 64 /** @override */ | 71 /** @override */ |
| 65 hasCollapsibleContent: function() { | 72 hasCollapsibleContent: function() { |
| 66 return false; | 73 return false; |
| 67 }, | 74 }, |
| 68 | 75 |
| 69 /** @override */ | 76 /** @override */ |
| 70 set isEnabled(isEnabled) { | 77 set isEnabled(isEnabled) { |
| 71 this.getChildElement('input.copies').disabled = !isEnabled; | 78 this.inputField_.disabled = !isEnabled; |
| 72 this.getChildElement('input.collate').disabled = !isEnabled; | 79 this.getChildElement('input.collate').disabled = !isEnabled; |
| 73 this.isEnabled_ = isEnabled; | 80 this.isEnabled_ = isEnabled; |
| 74 if (isEnabled) { | 81 if (isEnabled) { |
| 75 this.updateState_(); | 82 this.updateState_(); |
| 76 } else { | |
| 77 this.getChildElement('button.increment').disabled = true; | |
| 78 this.getChildElement('button.decrement').disabled = true; | |
| 79 } | 83 } |
| 80 }, | 84 }, |
| 81 | 85 |
| 82 /** @override */ | 86 /** @override */ |
| 83 enterDocument: function() { | 87 enterDocument: function() { |
| 88 this.inputField_ = this.getChildElement('input.copies'); | |
| 84 print_preview.SettingsSection.prototype.enterDocument.call(this); | 89 print_preview.SettingsSection.prototype.enterDocument.call(this); |
| 85 this.tracker.add( | 90 this.tracker.add( |
| 86 this.getChildElement('input.copies'), | 91 this.inputField_, |
| 87 'keydown', | 92 'keydown', |
| 88 this.onTextfieldKeyDown_.bind(this)); | 93 this.onTextfieldKeyDown_.bind(this)); |
| 89 this.tracker.add( | 94 this.tracker.add( |
| 90 this.getChildElement('input.copies'), | 95 this.inputField_, |
| 91 'input', | 96 'input', |
| 92 this.onTextfieldInput_.bind(this)); | 97 this.onTextfieldInput_.bind(this)); |
| 93 this.tracker.add( | 98 this.tracker.add( |
| 94 this.getChildElement('input.copies'), | 99 this.inputField_, |
| 95 'blur', | 100 'blur', |
| 96 this.onTextfieldBlur_.bind(this)); | 101 this.onTextfieldBlur_.bind(this)); |
| 97 this.tracker.add( | 102 this.tracker.add( |
| 98 this.getChildElement('button.increment'), | |
| 99 'click', | |
| 100 this.onButtonClicked_.bind(this, 1)); | |
| 101 this.tracker.add( | |
| 102 this.getChildElement('button.decrement'), | |
| 103 'click', | |
| 104 this.onButtonClicked_.bind(this, -1)); | |
| 105 this.tracker.add( | |
| 106 this.getChildElement('input.collate'), | 103 this.getChildElement('input.collate'), |
| 107 'click', | 104 'click', |
| 108 this.onCollateCheckboxClick_.bind(this)); | 105 this.onCollateCheckboxClick_.bind(this)); |
| 109 this.tracker.add( | 106 this.tracker.add( |
| 110 this.copiesTicketItem_, | 107 this.copiesTicketItem_, |
| 111 print_preview.ticket_items.TicketItem.EventType.CHANGE, | 108 print_preview.ticket_items.TicketItem.EventType.CHANGE, |
| 112 this.updateState_.bind(this)); | 109 this.updateState_.bind(this)); |
| 113 this.tracker.add( | 110 this.tracker.add( |
| 114 this.collateTicketItem_, | 111 this.collateTicketItem_, |
| 115 print_preview.ticket_items.TicketItem.EventType.CHANGE, | 112 print_preview.ticket_items.TicketItem.EventType.CHANGE, |
| 116 this.updateState_.bind(this)); | 113 this.updateState_.bind(this)); |
| 117 }, | 114 }, |
| 118 | 115 |
| 119 /** | 116 /** |
| 120 * Updates the state of the copies settings UI controls. | 117 * Updates the state of the copies settings UI controls. |
| 121 * @private | 118 * @private |
| 122 */ | 119 */ |
| 123 updateState_: function() { | 120 updateState_: function() { |
| 124 if (this.isAvailable()) { | 121 if (this.isAvailable()) { |
| 125 if (this.getChildElement('input.copies').value != | 122 if (this.inputField_.value != this.copiesTicketItem_.getValue()) |
| 126 this.copiesTicketItem_.getValue()) { | 123 this.inputField_.value = this.copiesTicketItem_.getValue(); |
| 127 this.getChildElement('input.copies').value = | |
| 128 this.copiesTicketItem_.getValue(); | |
| 129 } | |
| 130 | 124 |
| 131 var currentValueGreaterThan1 = false; | 125 var currentValueGreaterThan1 = false; |
| 132 if (this.copiesTicketItem_.isValid()) { | 126 if (this.copiesTicketItem_.isValid()) { |
| 133 this.getChildElement('input.copies').classList.remove('invalid'); | 127 this.inputField_.classList.remove('invalid'); |
| 134 fadeOutElement(this.getChildElement('.hint')); | 128 fadeOutElement(this.getChildElement('.hint')); |
| 135 var currentValue = this.copiesTicketItem_.getValueAsNumber(); | 129 var currentValue = this.copiesTicketItem_.getValueAsNumber(); |
| 136 var currentValueGreaterThan1 = currentValue > 1; | 130 currentValueGreaterThan1 = currentValue > 1; |
| 137 this.getChildElement('button.increment').disabled = | |
| 138 !this.isEnabled_ || | |
| 139 !this.copiesTicketItem_.wouldValueBeValid(currentValue + 1); | |
| 140 this.getChildElement('button.decrement').disabled = | |
| 141 !this.isEnabled_ || | |
| 142 !this.copiesTicketItem_.wouldValueBeValid(currentValue - 1); | |
| 143 } else { | 131 } else { |
| 144 this.getChildElement('input.copies').classList.add('invalid'); | 132 this.inputField_.classList.add('invalid'); |
| 145 fadeInElement(this.getChildElement('.hint')); | 133 fadeInElement(this.getChildElement('.hint')); |
| 146 this.getChildElement('button.increment').disabled = true; | |
| 147 this.getChildElement('button.decrement').disabled = true; | |
| 148 } | 134 } |
| 149 | 135 |
| 150 if (!(this.getChildElement('.collate-container').hidden = | 136 if (!(this.getChildElement('.collate-container').hidden = |
| 151 !this.collateTicketItem_.isCapabilityAvailable() || | 137 !this.collateTicketItem_.isCapabilityAvailable() || |
| 152 !currentValueGreaterThan1)) { | 138 !currentValueGreaterThan1)) { |
| 153 this.getChildElement('input.collate').checked = | 139 this.getChildElement('input.collate').checked = |
| 154 this.collateTicketItem_.getValue(); | 140 this.collateTicketItem_.getValue(); |
| 155 } | 141 } |
| 156 } | 142 } |
| 157 this.updateUiStateInternal(); | 143 this.updateUiStateInternal(); |
| 158 }, | 144 }, |
| 159 | 145 |
| 160 /** | 146 /** |
| 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. | |
| 164 * @private | |
| 165 */ | |
| 166 onButtonClicked_: function(delta) { | |
| 167 // Assumes text field has a valid number. | |
| 168 var newValue = | |
| 169 parseInt(this.getChildElement('input.copies').value, 10) + delta; | |
| 170 this.copiesTicketItem_.updateValue(newValue + ''); | |
| 171 }, | |
| 172 | |
| 173 /** | |
| 174 * Called after a timeout after user input into the textfield. | 147 * Called after a timeout after user input into the textfield. |
| 175 * @private | 148 * @private |
| 176 */ | 149 */ |
| 177 onTextfieldTimeout_: function() { | 150 onTextfieldTimeout_: function() { |
| 178 this.textfieldTimeout_ = null; | 151 this.textfieldTimeout_ = null; |
| 179 var copiesVal = this.getChildElement('input.copies').value; | 152 var copiesVal = this.inputField_.value; |
| 180 if (copiesVal != '') { | 153 if (copiesVal != '') { |
| 181 this.copiesTicketItem_.updateValue(copiesVal); | 154 this.copiesTicketItem_.updateValue(copiesVal); |
| 182 } | 155 } |
| 183 }, | 156 }, |
| 184 | 157 |
| 185 /** | 158 /** |
| 186 * Called when a key is pressed on the custom input. | 159 * Called when a key is pressed on the custom input. |
| 187 * @param {Event} event Contains the key that was pressed. | 160 * @param {Event} event Contains the key that was pressed. |
| 188 * @private | 161 * @private |
| 189 */ | 162 */ |
| 190 onTextfieldKeyDown_: function(event) { | 163 onTextfieldKeyDown_: function(event) { |
| 191 if (event.keyCode == 13 /*enter*/) { | 164 if (event.keyCode != 'Enter') |
| 192 if (this.textfieldTimeout_) { | 165 return; |
| 193 clearTimeout(this.textfieldTimeout_); | 166 |
| 194 } | 167 if (this.textfieldTimeout_) |
| 195 this.onTextfieldTimeout_(); | 168 clearTimeout(this.textfieldTimeout_); |
| 196 } | 169 this.onTextfieldTimeout_(); |
| 197 }, | 170 }, |
| 198 | 171 |
| 199 /** | 172 /** |
| 200 * Called when a input event occurs on the textfield. Starts an input | 173 * Called when a input event occurs on the textfield. Starts an input |
| 201 * timeout. | 174 * timeout. |
| 202 * @private | 175 * @private |
| 203 */ | 176 */ |
| 204 onTextfieldInput_: function() { | 177 onTextfieldInput_: function() { |
| 205 if (this.textfieldTimeout_) { | 178 if (this.textfieldTimeout_) { |
| 206 clearTimeout(this.textfieldTimeout_); | 179 clearTimeout(this.textfieldTimeout_); |
| 207 } | 180 } |
| 208 this.textfieldTimeout_ = setTimeout( | 181 this.textfieldTimeout_ = setTimeout( |
| 209 this.onTextfieldTimeout_.bind(this), CopiesSettings.TEXTFIELD_DELAY_); | 182 this.onTextfieldTimeout_.bind(this), |
| 183 CopiesSettings.TEXTFIELD_DELAY_MS_); | |
| 210 }, | 184 }, |
| 211 | 185 |
| 212 /** | 186 /** |
| 213 * Called when the focus leaves the textfield. If the textfield is empty, | 187 * Called when the focus leaves the textfield. If the textfield is empty, |
| 214 * its value is set to 1. | 188 * its value is set to 1. |
| 215 * @private | 189 * @private |
| 216 */ | 190 */ |
| 217 onTextfieldBlur_: function() { | 191 onTextfieldBlur_: function() { |
| 218 if (this.getChildElement('input.copies').value == '') { | 192 if (this.inputField_.value == '') { |
| 219 // Do it asynchronously to avoid moving focus to Print button in | 193 // Do it asynchronously to avoid moving focus to Print button in |
| 220 // PrintHeader.onTicketChange_. | 194 // PrintHeader.onTicketChange_. |
| 221 setTimeout((function() { | 195 setTimeout((function() { |
| 222 this.copiesTicketItem_.updateValue('1'); | 196 this.copiesTicketItem_.updateValue('1'); |
| 223 }).bind(this), 0); | 197 }).bind(this), 0); |
| 224 } | 198 } |
| 225 }, | 199 }, |
| 226 | 200 |
| 227 /** | 201 /** |
| 228 * Called when the collate checkbox is clicked. Updates the print ticket. | 202 * Called when the collate checkbox is clicked. Updates the print ticket. |
| 229 * @private | 203 * @private |
| 230 */ | 204 */ |
| 231 onCollateCheckboxClick_: function() { | 205 onCollateCheckboxClick_: function() { |
| 232 this.collateTicketItem_.updateValue( | 206 this.collateTicketItem_.updateValue( |
| 233 this.getChildElement('input.collate').checked); | 207 this.getChildElement('input.collate').checked); |
| 234 } | 208 } |
| 235 }; | 209 }; |
| 236 | 210 |
| 237 // Export | 211 // Export |
| 238 return { | 212 return { |
| 239 CopiesSettings: CopiesSettings | 213 CopiesSettings: CopiesSettings |
| 240 }; | 214 }; |
| 241 }); | 215 }); |
| OLD | NEW |