Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 '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 | |
| 15 // @type {string} Specifies which margin this line refers to. | |
| 16 box.marginGroup = groupName; | |
| 17 // @type {boolean} True if the displayed value is valid. | |
| 18 box.isValid = true; | |
| 19 // @type {number} Timer used to detect when the user stops typing. | |
| 20 box.timerId_ = null; | |
| 21 // @type {number} The last valid value in points. | |
| 22 box.lastValidValueInPoints = 0; | |
| 23 // @type {print_preview.Rect} A rectangle describing the four margins. | |
| 24 box.marginsRectangle_ = null; | |
| 25 // @type {number} The upper allowed limit for the corresponding margin. | |
| 26 box.valueLimit = null; | |
| 27 | |
| 28 box.addEventListeners_(); | |
| 29 return box; | |
| 30 } | |
| 31 | |
| 32 MarginTextbox.CSS_CLASS_MARGIN_TEXTBOX = 'margin-box'; | |
| 33 MarginTextbox.MARGIN_BOX_HEIGHT = 15; | |
| 34 MarginTextbox.MARGIN_BOX_VERTICAL_PADDING = 5; | |
| 35 MarginTextbox.MARGIN_BOX_WIDTH = 40; | |
| 36 MarginTextbox.MARGIN_BOX_HORIZONTAL_PADDING = 10; | |
| 37 | |
| 38 // Keycode for the "Escape" key. | |
| 39 MarginTextbox.ESCAPE_KEYCODE = 27; | |
| 40 // Keycode for the "Enter" key. | |
| 41 MarginTextbox.ENTER_KEYCODE = 13; | |
| 42 | |
| 43 MarginTextbox.convertPointsToInchesText = function(toConvert) { | |
| 44 var inInches = convertPointsToInches(toConvert); | |
| 45 return MarginTextbox.convertInchesToInchesText(inInches); | |
| 46 }; | |
| 47 | |
| 48 MarginTextbox.convertInchesToInchesText = function(toConvert) { | |
| 49 return toConvert.toFixed(2) + '"'; | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 53 * @return {number} The total height of a margin textbox (including padding). | |
| 54 */ | |
| 55 MarginTextbox.totalHeight = function() { | |
| 56 return MarginTextbox.MARGIN_BOX_HEIGHT + | |
| 57 2 * MarginTextbox.MARGIN_BOX_VERTICAL_PADDING; | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * @return {number} The total width of a margin textbox (including padding). | |
| 62 */ | |
| 63 MarginTextbox.totalWidth = function() { | |
| 64 return MarginTextbox.MARGIN_BOX_WIDTH + | |
| 65 2 * MarginTextbox.MARGIN_BOX_HORIZONTAL_PADDING; | |
| 66 } | |
| 67 | |
| 68 MarginTextbox.prototype = { | |
| 69 __proto__: HTMLInputElement.prototype, | |
| 70 | |
| 71 /** | |
| 72 * Updates the state of |this|. | |
| 73 * @param {print_preview.Rect} marginsRectangle A rectangle describing the | |
| 74 * margins in percentages. | |
| 75 * @param {number} value The margin value in points. | |
| 76 * @param {number} valueLimit The upper allowed value for the margin. | |
| 77 * @param {boolean} keepDisplayedValue True if the currently displayed value | |
| 78 * should not be updated. | |
| 79 */ | |
| 80 update: function(marginsRectangle, value, valueLimit, keepDisplayedValue) { | |
| 81 this.marginsRectangle_ = marginsRectangle; | |
| 82 this.lastValidValueInPoints = value; | |
| 83 if (!keepDisplayedValue) { | |
| 84 this.value = MarginTextbox.convertPointsToInchesText( | |
| 85 this.lastValidValueInPoints); | |
| 86 } | |
| 87 | |
| 88 this.valueLimit = valueLimit; | |
| 89 this.validate(); | |
| 90 }, | |
| 91 | |
| 92 get margin() { | |
| 93 return print_preview.extractMarginValue(this.value); | |
| 94 }, | |
| 95 | |
| 96 /** | |
| 97 * Updates |this.isValid|. | |
| 98 */ | |
| 99 validate: function() { | |
| 100 this.isValid = print_preview.isMarginTextValid(this.value, | |
| 101 this.valueLimit); | |
| 102 if (this.isValid) | |
| 103 this.value = MarginTextbox.convertInchesToInchesText(this.margin); | |
| 104 }, | |
| 105 | |
| 106 /** | |
| 107 * Updates the background color depending on |isValid| by adding/removing | |
| 108 * the appropriate CSS class. | |
| 109 * @param {boolean} isValid True if the margin is valid. | |
| 110 */ | |
| 111 updateColor: function() { | |
| 112 this.isValid ? this.classList.remove('invalid') : | |
| 113 this.classList.add('invalid'); | |
| 114 }, | |
| 115 | |
| 116 /** | |
| 117 * Draws this textbox. | |
| 118 */ | |
| 119 draw: function() { | |
| 120 var topLeft = this.getCoordinates_(); | |
| 121 var totalWidth = previewArea.pdfPlugin_.offsetWidth; | |
| 122 var totalHeight = previewArea.pdfPlugin_.offsetHeight; | |
| 123 | |
| 124 this.style.left = Math.round(topLeft.x * totalWidth) + 'px'; | |
| 125 this.style.top = Math.round(topLeft.y * totalHeight) + 'px'; | |
| 126 this.updateColor(); | |
| 127 }, | |
| 128 | |
| 129 /** | |
| 130 * @return {boolean} True if |this| refers to the top margin. | |
| 131 * @private | |
| 132 */ | |
| 133 isTop_: function() { | |
| 134 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP; | |
| 135 }, | |
| 136 | |
| 137 /** | |
| 138 * @return {boolean} True if |this| refers to the bottom margin. | |
| 139 * @private | |
| 140 */ | |
| 141 isBottom_: function() { | |
| 142 return this.marginGroup == print_preview.MarginSettings.BOTTOM_GROUP; | |
| 143 }, | |
| 144 | |
| 145 /** | |
| 146 * @return {boolean} True if |this| refers to the left margin. | |
| 147 * @private | |
| 148 */ | |
| 149 isLeft_: function() { | |
| 150 return this.marginGroup == print_preview.MarginSettings.LEFT_GROUP; | |
| 151 }, | |
| 152 | |
| 153 /** | |
| 154 * @return {boolean} True if |this| refers to the bottom margin. | |
| 155 * @private | |
| 156 */ | |
| 157 isRight_: function() { | |
| 158 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP; | |
| 159 }, | |
| 160 | |
| 161 /** | |
| 162 * Calculates the coordinates where |this| should be displayed. | |
| 163 * @return {{x: number, y: number}} The coordinates (in percent) where | |
| 164 * |this| should be drawn relative to the upper left corner of the | |
| 165 * plugin. | |
| 166 * @private | |
| 167 */ | |
| 168 getCoordinates_: function() { | |
| 169 var x = 0, y = 0; | |
| 170 var totalWidth = previewArea.pdfPlugin_.offsetWidth; | |
| 171 var totalHeight = previewArea.pdfPlugin_.offsetHeight; | |
| 172 | |
| 173 if (this.isTop_()) { | |
| 174 x = this.marginsRectangle_.x + | |
|
Evan Stade
2011/10/07 03:29:58
this block will get a lot more readable if you sav
dpapad
2011/10/07 16:49:20
Done. I created getters for middleX, middleY, bott
| |
| 175 this.marginsRectangle_.width / 2 - | |
| 176 (MarginTextbox.totalWidth() / 2) / totalWidth; | |
| 177 y = this.marginsRectangle_.y; | |
| 178 } else if (this.isBottom_()) { | |
| 179 x = this.marginsRectangle_.x + | |
| 180 this.marginsRectangle_.width / 2 - | |
| 181 (MarginTextbox.totalWidth() / 2) / totalWidth; | |
| 182 y = this.marginsRectangle_.y + this.marginsRectangle_.height - | |
| 183 (MarginTextbox.totalHeight() / totalHeight); | |
| 184 } else if (this.isRight_()) { | |
| 185 x = this.marginsRectangle_.x + this.marginsRectangle_.width - | |
| 186 (MarginTextbox.totalWidth() / totalWidth); | |
|
Evan Stade
2011/10/07 03:29:58
indent
dpapad
2011/10/07 16:49:20
Done.
| |
| 187 y = this.marginsRectangle_.y + this.marginsRectangle_.height / 2 - | |
| 188 (MarginTextbox.totalHeight() / 2) / totalHeight; | |
| 189 } else if (this.isLeft_()) { | |
| 190 x = this.marginsRectangle_.x; | |
| 191 y = this.marginsRectangle_.y + this.marginsRectangle_.height / 2 - | |
| 192 (MarginTextbox.totalHeight() / 2) / totalHeight; | |
| 193 } | |
| 194 | |
| 195 return { x: x, y: y }; | |
| 196 }, | |
| 197 | |
| 198 /** | |
| 199 * Adds event listeners for various events. | |
| 200 * @private | |
| 201 */ | |
| 202 addEventListeners_: function() { | |
| 203 this.oninput = this.resetTimer_.bind(this); | |
| 204 this.onblur = this.onBlur_.bind(this); | |
| 205 this.onkeypress = this.onKeyPressed_.bind(this); | |
| 206 this.onkeyup = this.onKeyUp_.bind(this); | |
| 207 }, | |
| 208 | |
| 209 /** | |
| 210 * Executes whenever a blur event occurs. | |
| 211 * @private | |
| 212 */ | |
| 213 onBlur_: function() { | |
| 214 clearTimeout(this.timerId_); | |
| 215 this.validate(); | |
| 216 if (!this.isValid) { | |
| 217 this.value = MarginTextbox.convertPointsToInchesText( | |
| 218 this.lastValidValueInPoints); | |
| 219 this.validate(); | |
| 220 } | |
| 221 | |
| 222 this.updateColor(); | |
| 223 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
| 224 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | |
| 225 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); | |
| 226 }, | |
| 227 | |
| 228 /** | |
| 229 * Executes whenever a keypressed event occurs. Note: Only the "Enter" key | |
| 230 * event is handled. The "Escape" key does not result in such event, | |
| 231 * therefore it is handled by |this.onKeyUp_|. | |
| 232 * @param {KeyboardEvent} e The event that triggered this listener. | |
| 233 * @private | |
| 234 */ | |
| 235 onKeyPressed_: function(e) { | |
| 236 if (e.keyCode == MarginTextbox.ENTER_KEYCODE) | |
| 237 this.blur(); | |
| 238 }, | |
| 239 | |
| 240 /** | |
| 241 * Executes whenever a keyup event occurs. Note: Only the "Escape" | |
| 242 * key event is handled. | |
| 243 * @param {KeyboardEvent} e The event that triggered this listener. | |
| 244 * @private | |
| 245 */ | |
| 246 onKeyUp_: function(e) { | |
| 247 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { | |
| 248 this.value = MarginTextbox.convertPointsToInchesText( | |
| 249 this.lastValidValueInPoints); | |
| 250 this.validate(); | |
| 251 this.updateColor(); | |
| 252 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
| 253 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | |
| 254 } | |
| 255 }, | |
| 256 | |
| 257 /** | |
| 258 * Resetting the timer used to detect when the user stops typing in order | |
| 259 * to update the print preview. | |
| 260 * @private | |
| 261 */ | |
| 262 resetTimer_: function() { | |
| 263 clearTimeout(this.timerId_); | |
| 264 this.timerId_ = window.setTimeout( | |
| 265 this.onTextValueMayHaveChanged_.bind(this), 500); | |
| 266 }, | |
| 267 | |
| 268 /** | |
| 269 * Executes whenever the user stops typing. | |
| 270 * @private | |
| 271 */ | |
| 272 onTextValueMayHaveChanged_: function() { | |
| 273 this.validate(); | |
| 274 this.updateColor(); | |
| 275 cr.dispatchSimpleEvent(document, 'updateSummary'); | |
| 276 cr.dispatchSimpleEvent(document, 'updatePrintButton'); | |
| 277 | |
| 278 if (!this.isValid) | |
| 279 return; | |
| 280 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); | |
| 281 } | |
| 282 | |
| 283 }; | |
| 284 | |
| 285 return { | |
| 286 MarginTextbox: MarginTextbox | |
| 287 }; | |
| 288 }); | |
| OLD | NEW |