Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 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 scaling settings UI. | 9 * Component that renders the scaling settings UI. |
| 10 * @param {!print_preview.ticket_items.scalingTicketItem} scalingTicketItem | 10 * @param {!print_preview.ticket_items.scalingTicketItem} scalingTicketItem |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 isFitToPageSelected: function() { | 116 isFitToPageSelected: function() { |
| 117 return this.fitToPageTicketItem_.isCapabilityAvailable() && | 117 return this.fitToPageTicketItem_.isCapabilityAvailable() && |
| 118 this.fitToPageTicketItem_.getValue(); | 118 this.fitToPageTicketItem_.getValue(); |
| 119 }, | 119 }, |
| 120 | 120 |
| 121 /** | 121 /** |
| 122 * @return {number} The current input field value as an integer. | 122 * @return {number} The current input field value as an integer. |
| 123 * @private | 123 * @private |
| 124 */ | 124 */ |
| 125 getInputAsNumber: function() { | 125 getInputAsNumber: function() { |
| 126 return (/[^\d]+/.test(this.inputField_.value)) ? | 126 return Number(this.inputField_.value).valueOf(); |
| 127 0 : // Return an invalid scaling so that the hint will display. | |
| 128 parseInt(this.inputField_.value, 10); | |
| 129 }, | 127 }, |
| 130 | 128 |
| 131 /** | 129 /** |
| 132 * Display the fit to page scaling in the scaling field if there is a valid | 130 * Display the fit to page scaling in the scaling field if there is a valid |
| 133 * fit to page scaling value. If not, make the field blank. | 131 * fit to page scaling value. If not, make the field blank. |
| 134 * @private | 132 * @private |
| 135 */ | 133 */ |
| 136 displayFitToPageScaling: function() { | 134 displayFitToPageScaling: function() { |
| 137 this.inputField_.value = this.fitToPageScaling_ || ''; | 135 this.inputField_.value = this.fitToPageScaling_ || ''; |
| 138 }, | 136 }, |
| 139 | 137 |
| 140 /** | 138 /** |
| 139 * Whether the displayed scaling value matches the fit to page scaling. | |
| 140 * @private | |
| 141 */ | |
| 142 displayMatchesFitToPage: function() { | |
| 143 return (this.getInputAsNumber() == this.fitToPageScaling_ || | |
| 144 (this.inputField_.value == '' && !this.fitToPageScaling_)); | |
| 145 }, | |
| 146 | |
| 147 /** | |
| 141 * Updates the fit to page scaling value of the scalings settings UI. | 148 * Updates the fit to page scaling value of the scalings settings UI. |
| 142 * @param {number} fitToPageScaling The updated percentage scaling required | 149 * @param {number} fitToPageScaling The updated percentage scaling required |
| 143 * to fit the document to page. | 150 * to fit the document to page. |
| 144 */ | 151 */ |
| 145 updateFitToPageScaling: function(fitToPageScaling) { | 152 updateFitToPageScaling: function(fitToPageScaling) { |
| 146 this.fitToPageScaling_ = fitToPageScaling; | 153 this.fitToPageScaling_ = fitToPageScaling; |
| 147 // Display the new value if fit to page is checked. | 154 // Display the new value if fit to page is checked. |
| 148 if (this.isFitToPageSelected()) | 155 if (this.isFitToPageSelected()) |
| 149 this.displayFitToPageScaling(); | 156 this.displayFitToPageScaling(); |
| 150 }, | 157 }, |
| 151 | 158 |
| 152 /** | 159 /** |
| 153 * Updates the state of the scaling settings UI controls. | 160 * Updates the state of the scaling settings UI controls. |
| 154 * @private | 161 * @private |
| 155 */ | 162 */ |
| 156 updateState_: function() { | 163 updateState_: function() { |
| 157 if (this.isAvailable()) { | 164 if (this.isAvailable()) { |
| 158 var displayedValue = this.getInputAsNumber(); | 165 var displayedValue = this.getInputAsNumber(); |
| 159 var inputString = this.inputField_.value; | 166 // If fit to page is selected and the display matches, mark valid |
| 160 // Display should be updated to match the ticket item unless displayed | 167 // and return. |
| 161 // value is invalid and non-blank or fit to page is checked. In these | 168 if (this.isFitToPageSelected() && this.displayMatchesFitToPage()) { |
| 162 // cases, the ticket item is not updated and retains its last valid, | 169 this.inputField_.classList.remove('invalid'); |
| 163 // user specified value. | 170 fadeOutElement(this.getChildElement('.hint')); |
| 164 if ((inputString == '' || | 171 this.updateUiStateInternal(); |
| 165 (displayedValue !== this.scalingTicketItem_.getValueAsNumber() && | 172 return; |
| 166 this.scalingTicketItem_.wouldValueBeValid(inputString))) && | |
| 167 !this.isFitToPageSelected()) { | |
| 168 this.inputField_.value = this.scalingTicketItem_.getValue(); | |
| 169 displayedValue = this.getInputAsNumber(); | |
| 170 inputString = this.inputField_.value; | |
| 171 } | 173 } |
| 172 | 174 |
| 173 // If fit to page is selected and the value matches the fit to page | 175 if (!this.inputField_.validity.valid) { |
| 174 // scaling, or if the value is valid, no hint is displayed | |
| 175 if (this.scalingTicketItem_.wouldValueBeValid(inputString) || | |
| 176 (this.isFitToPageSelected() && | |
| 177 (displayedValue == this.fitToPageScaling_ || | |
| 178 (!this.fitToPageScaling_ && inputString == '')))) { | |
| 179 this.inputField_.classList.remove('invalid'); | |
| 180 fadeOutElement(this.getChildElement('.hint')); | |
| 181 } else { | |
| 182 // Invalid value. Display the hint. | |
| 183 this.inputField_.classList.add('invalid'); | 176 this.inputField_.classList.add('invalid'); |
| 184 fadeInElement(this.getChildElement('.hint')); | 177 fadeInElement(this.getChildElement('.hint')); |
| 178 this.updateUiStateInternal(); | |
| 179 return; | |
| 185 } | 180 } |
| 186 | 181 |
| 182 this.inputField_.value = this.scalingTicketItem_.getValue(); | |
| 183 this.inputField_.classList.remove('invalid'); | |
| 184 fadeOutElement(this.getChildElement('.hint')); | |
| 187 } | 185 } |
| 188 this.updateUiStateInternal(); | 186 this.updateUiStateInternal(); |
| 189 }, | 187 }, |
| 190 | 188 |
| 191 /** | 189 /** |
| 192 * Helper function to adjust the scaling display if fit to page is checked | 190 * Helper function to adjust the scaling display if fit to page is checked |
| 193 * by the user. | 191 * by the user. |
| 194 * @private | 192 * @private |
| 195 */ | 193 */ |
| 196 onFitToPageChange_: function() { | 194 onFitToPageChange_: function() { |
| 197 if (this.isFitToPageSelected()) { | 195 if (this.isFitToPageSelected()) { |
| 198 // Fit to page was checked. Set scaling to the fit to page scaling. | 196 // Fit to page was checked. Set scaling to the fit to page scaling. |
| 199 this.displayFitToPageScaling(); | 197 this.displayFitToPageScaling(); |
| 200 } else if (this.fitToPageTicketItem_.isCapabilityAvailable() && | 198 } else if (this.fitToPageTicketItem_.isCapabilityAvailable() && |
| 201 (this.getInputAsNumber() == this.fitToPageScaling_ || | 199 this.displayMatchesFitToPage()) { |
| 202 this.inputField_.value == '')) { | |
| 203 // Fit to page unchecked. Return to last scaling. | 200 // Fit to page unchecked. Return to last scaling. |
| 204 this.inputField_.value = this.scalingTicketItem_.getValue(); | 201 this.inputField_.value = this.scalingTicketItem_.getValue(); |
| 205 } | 202 } |
| 206 }, | 203 }, |
| 207 | 204 |
| 208 /** | 205 /** |
| 209 * Called after a timeout after user input into the textfield. | 206 * Called after a timeout after user input into the textfield. |
| 210 * @private | 207 * @private |
| 211 */ | 208 */ |
| 212 onTextfieldTimeout_: function() { | 209 onTextfieldTimeout_: function() { |
| 213 this.textfieldTimeout_ = null; | 210 this.textfieldTimeout_ = null; |
| 214 var scalingValue = this.inputField_.value; | 211 if (!this.inputField_.validity.valid){ |
| 215 if (scalingValue == '') | 212 this.updateState_(); |
| 216 return; | 213 return; |
| 217 if (this.scalingTicketItem_.wouldValueBeValid(scalingValue)) { | 214 } |
| 218 if (scalingValue === this.scalingTicketItem_.getValue()) { | 215 if (this.isFitToPageSelected() && !this.displayMatchesFitToPage()) { |
| 219 // Make sure this still gets called in case returning to a valid | 216 // User modified value away from fit to page. |
| 220 // value. | 217 this.fitToPageTicketItem_.updateValue(false); |
| 221 this.updateState_(); | 218 } |
| 222 } | 219 // Check this after checking for fit to page in case the user went |
| 223 if (this.isFitToPageSelected() && | 220 // back to their old value. |
| 224 parseInt(scalingValue, 10) != this.fitToPageScaling_) { | 221 if (Number(this.inputField_.value).toFixed(0) === |
|
dpapad
2017/02/28 00:36:42
Is the change from parseInt() to Number(...).toFix
rbpotter
2017/02/28 02:34:05
Done. Switched this to use the valueAsNumber field
| |
| 225 // User modified value away from fit to page. | 222 this.scalingTicketItem_.getValue()) { |
| 226 this.fitToPageTicketItem_.updateValue(false); | |
| 227 } | |
| 228 this.scalingTicketItem_.updateValue(scalingValue); | |
| 229 this.inputField_.value = scalingValue; | |
| 230 } else { | |
| 231 this.updateState_(); | 223 this.updateState_(); |
| 224 return; | |
| 232 } | 225 } |
| 226 if (this.inputField_.value == '') | |
| 227 return; | |
| 228 this.scalingTicketItem_.updateValue( | |
| 229 Number(this.inputField_.value).toFixed(0)); | |
| 233 }, | 230 }, |
| 234 | 231 |
| 235 /** | 232 /** |
| 236 * Called when a key is pressed on the custom input. | 233 * Called when a key is pressed on the custom input. |
| 237 * @param {!Event} event Contains the key that was pressed. | 234 * @param {!Event} event Contains the key that was pressed. |
| 238 * @private | 235 * @private |
| 239 */ | 236 */ |
| 240 onTextfieldKeyDown_: function(event) { | 237 onTextfieldKeyDown_: function(event) { |
| 241 if (event.keyCode != 'Enter') | 238 if (event.keyCode != 'Enter') |
| 242 return; | 239 return; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 260 ScalingSettings.TEXTFIELD_DELAY_MS_); | 257 ScalingSettings.TEXTFIELD_DELAY_MS_); |
| 261 }, | 258 }, |
| 262 | 259 |
| 263 /** | 260 /** |
| 264 * Called when the focus leaves the textfield. If the textfield is empty, | 261 * Called when the focus leaves the textfield. If the textfield is empty, |
| 265 * its value is set to the fit to page value if fit to page is checked and | 262 * its value is set to the fit to page value if fit to page is checked and |
| 266 * 100 otherwise. | 263 * 100 otherwise. |
| 267 * @private | 264 * @private |
| 268 */ | 265 */ |
| 269 onTextfieldBlur_: function() { | 266 onTextfieldBlur_: function() { |
| 270 if (this.inputField_.value == '') { | 267 if (this.inputField_.value == '' && this.inputField_.validity.valid) { |
|
dpapad
2017/02/27 21:56:02
I am slightly confused about this condition. It se
rbpotter
2017/02/28 00:12:59
They can actually both be true. This is the case f
dpapad
2017/02/28 00:36:42
Thanks for the explanation.
| |
| 271 if (this.isFitToPageSelected()) { | 268 if (this.isFitToPageSelected()) { |
| 272 this.displayFitToPageScaling(); | 269 this.displayFitToPageScaling(); |
| 273 } else { | 270 } else { |
| 274 if (this.scalingTicketItem_.getValue() == '100') | 271 if (this.scalingTicketItem_.getValue() == '100') |
| 275 // No need to update the ticket, but change the display to match. | 272 // No need to update the ticket, but change the display to match. |
| 276 this.inputField_.value = '100'; | 273 this.inputField_.value = '100'; |
| 277 else | 274 else |
| 278 this.scalingTicketItem_.updateValue('100'); | 275 this.scalingTicketItem_.updateValue('100'); |
| 279 } | 276 } |
| 280 } | 277 } |
| 281 }, | 278 }, |
| 282 | 279 |
| 283 }; | 280 }; |
| 284 | 281 |
| 285 // Export | 282 // Export |
| 286 return { | 283 return { |
| 287 ScalingSettings: ScalingSettings | 284 ScalingSettings: ScalingSettings |
| 288 }; | 285 }; |
| 289 }); | 286 }); |
| OLD | NEW |