| 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 function MarginTextbox(groupName) { | |
| 9 var box = document.createElement('input'); | |
| 10 box.__proto__ = MarginTextbox.prototype; | |
| 11 box.setAttribute('type', 'text'); | |
| 12 box.className = MarginTextbox.CSS_CLASS_MARGIN_TEXTBOX; | |
| 13 box.value = '0'; | |
| 14 box.setAttribute('aria-label', localStrings.getString(groupName)); | |
| 15 | |
| 16 // @type {string} Specifies which margin this line refers to. | |
| 17 box.marginGroup = groupName; | |
| 18 // @type {boolean} True if the displayed value is valid. | |
| 19 box.isValid = true; | |
| 20 // @type {number} Timer used to detect when the user stops typing. | |
| 21 box.timerId_ = null; | |
| 22 // @type {number} The last valid value in points. | |
| 23 box.lastValidValueInPoints = 0; | |
| 24 // @type {number} The upper allowed limit for the corresponding margin. | |
| 25 box.valueLimit = null; | |
| 26 | |
| 27 box.addEventListeners_(); | |
| 28 return box; | |
| 29 } | |
| 30 | |
| 31 MarginTextbox.CSS_CLASS_MARGIN_TEXTBOX = 'margin-box'; | |
| 32 // Keycode for the "Escape" key. | |
| 33 MarginTextbox.ESCAPE_KEYCODE = 27; | |
| 34 | |
| 35 MarginTextbox.prototype = { | |
| 36 __proto__: HTMLInputElement.prototype, | |
| 37 | |
| 38 /** | |
| 39 * @type {number} The margin value currently in the textbox. | |
| 40 */ | |
| 41 get margin() { | |
| 42 return print_preview.extractMarginValue(this.value); | |
| 43 }, | |
| 44 | |
| 45 /** | |
| 46 * Sets the contents of the textbox. | |
| 47 * @param {number} newValueInPoints The value to be displayed in points. | |
| 48 * @private | |
| 49 */ | |
| 50 setValue_: function(newValueInPoints) { | |
| 51 this.value = | |
| 52 print_preview.convertPointsToLocaleUnitsText(newValueInPoints); | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * Updates the state of |this|. | |
| 57 * @param {number} value The margin value in points. | |
| 58 * @param {number} valueLimit The upper allowed value for the margin. | |
| 59 * @param {boolean} keepDisplayedValue True if the currently displayed value | |
| 60 * should not be updated. | |
| 61 */ | |
| 62 update: function(value, valueLimit, keepDisplayedValue) { | |
| 63 this.lastValidValueInPoints = value; | |
| 64 if (!keepDisplayedValue) | |
| 65 this.setValue_(this.lastValidValueInPoints); | |
| 66 | |
| 67 this.valueLimit = valueLimit; | |
| 68 this.validate(); | |
| 69 }, | |
| 70 | |
| 71 /** | |
| 72 * Updates |this| while dragging is in progress. | |
| 73 * @param {number} dragDeltaInPoints The difference in points between the | |
| 74 * margin value before dragging started and now. | |
| 75 */ | |
| 76 updateWhileDragging: function(dragDeltaInPoints) { | |
| 77 var validity = this.validateDelta(dragDeltaInPoints); | |
| 78 | |
| 79 if (validity == print_preview.marginValidationStates.WITHIN_RANGE) | |
| 80 this.setValue_(this.lastValidValueInPoints + dragDeltaInPoints); | |
| 81 else if (validity == print_preview.marginValidationStates.TOO_SMALL) | |
| 82 this.setValue_(0); | |
| 83 else if (validity == print_preview.marginValidationStates.TOO_BIG) | |
| 84 this.setValue_(this.valueLimit); | |
| 85 | |
| 86 this.validate(); | |
| 87 this.updateColor(); | |
| 88 }, | |
| 89 | |
| 90 /** | |
| 91 * @param {number} dragDeltaInPoints The difference in points between the | |
| 92 * margin value before dragging started and now. | |
| 93 * @return {number} An appropriate value from enum |marginValidationStates|. | |
| 94 */ | |
| 95 validateDelta: function(dragDeltaInPoints) { | |
| 96 var newValue = this.lastValidValueInPoints + dragDeltaInPoints; | |
| 97 return print_preview.validateMarginValue(newValue, this.valueLimit); | |
| 98 }, | |
| 99 | |
| 100 /** | |
| 101 * Updates |this.isValid|. | |
| 102 */ | |
| 103 validate: function() { | |
| 104 this.isValid = | |
| 105 print_preview.validateMarginText(this.value, this.valueLimit) == | |
| 106 print_preview.marginValidationStates.WITHIN_RANGE; | |
| 107 }, | |
| 108 | |
| 109 /** | |
| 110 * Updates the background color depending on |isValid| by adding/removing | |
| 111 * the appropriate CSS class. | |
| 112 * @param {boolean} isValid True if the margin is valid. | |
| 113 */ | |
| 114 updateColor: function() { | |
| 115 this.isValid ? this.classList.remove('invalid') : | |
| 116 this.classList.add('invalid'); | |
| 117 }, | |
| 118 | |
| 119 /** | |
| 120 * Draws this textbox. | |
| 121 */ | |
| 122 draw: function() { | |
| 123 this.updateColor(); | |
| 124 }, | |
| 125 | |
| 126 /** | |
| 127 * Adds event listeners for various events. | |
| 128 * @private | |
| 129 */ | |
| 130 addEventListeners_: function() { | |
| 131 this.oninput = this.resetTimer_.bind(this); | |
| 132 this.onblur = this.onBlur_.bind(this); | |
| 133 this.onkeypress = this.onKeyPressed_.bind(this); | |
| 134 this.onkeyup = this.onKeyUp_.bind(this); | |
| 135 this.onfocus = function() { | |
| 136 cr.dispatchSimpleEvent(document, customEvents.MARGIN_TEXTBOX_FOCUSED); | |
| 137 }; | |
| 138 }, | |
| 139 | |
| 140 /** | |
| 141 * Executes whenever a blur event occurs. | |
| 142 * @private | |
| 143 */ | |
| 144 onBlur_: function() { | |
| 145 clearTimeout(this.timerId_); | |
| 146 this.validate(); | |
| 147 if (!this.isValid) { | |
| 148 this.setValue_(this.lastValidValueInPoints); | |
| 149 this.validate(); | |
| 150 } | |
| 151 | |
| 152 this.updateColor(); | |
| 153 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 154 cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); | |
| 155 cr.dispatchSimpleEvent(this, customEvents.MARGINS_MAY_HAVE_CHANGED); | |
| 156 }, | |
| 157 | |
| 158 /** | |
| 159 * Executes whenever a keypressed event occurs. Note: Only the "Enter" key | |
| 160 * event is handled. The "Escape" key does not result in such event, | |
| 161 * therefore it is handled by |this.onKeyUp_|. | |
| 162 * @param {KeyboardEvent} e The event that triggered this listener. | |
| 163 * @private | |
| 164 */ | |
| 165 onKeyPressed_: function(e) { | |
| 166 if (e.keyIdentifier == 'Enter') | |
| 167 this.blur(); | |
| 168 }, | |
| 169 | |
| 170 /** | |
| 171 * Executes whenever a keyup event occurs. Note: Only the "Escape" | |
| 172 * key event is handled. | |
| 173 * @param {KeyboardEvent} e The event that triggered this listener. | |
| 174 * @private | |
| 175 */ | |
| 176 onKeyUp_: function(e) { | |
| 177 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { | |
| 178 this.setValue_(this.lastValidValueInPoints); | |
| 179 this.validate(); | |
| 180 this.updateColor(); | |
| 181 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 182 cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); | |
| 183 } | |
| 184 }, | |
| 185 | |
| 186 /** | |
| 187 * Resetting the timer used to detect when the user stops typing in order | |
| 188 * to update the print preview. | |
| 189 * @private | |
| 190 */ | |
| 191 resetTimer_: function() { | |
| 192 clearTimeout(this.timerId_); | |
| 193 this.timerId_ = window.setTimeout( | |
| 194 this.onTextValueMayHaveChanged.bind(this), 1000); | |
| 195 }, | |
| 196 | |
| 197 /** | |
| 198 * Executes whenever the user stops typing or when a drag session associated | |
| 199 * with |this| ends. | |
| 200 */ | |
| 201 onTextValueMayHaveChanged: function() { | |
| 202 this.validate(); | |
| 203 this.updateColor(); | |
| 204 cr.dispatchSimpleEvent(document, customEvents.UPDATE_SUMMARY); | |
| 205 cr.dispatchSimpleEvent(document, customEvents.UPDATE_PRINT_BUTTON); | |
| 206 | |
| 207 if (!this.isValid) | |
| 208 return; | |
| 209 cr.dispatchSimpleEvent(this, customEvents.MARGINS_MAY_HAVE_CHANGED); | |
| 210 } | |
| 211 | |
| 212 }; | |
| 213 | |
| 214 return { | |
| 215 MarginTextbox: MarginTextbox | |
| 216 }; | |
| 217 }); | |
| OLD | NEW |