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 var offsetY = (MarginTextbox.totalHeight() / 2) / totalHeight; |
| 173 var offsetX = (MarginTextbox.totalWidth() / 2) / totalWidth; |
| 174 |
| 175 if (this.isTop_()) { |
| 176 x = this.marginsRectangle_.middleX - offsetX; |
| 177 y = this.marginsRectangle_.y; |
| 178 } else if (this.isBottom_()) { |
| 179 x = this.marginsRectangle_.middleX - offsetX; |
| 180 y = this.marginsRectangle_.bottom - 2 * offsetY; |
| 181 } else if (this.isRight_()) { |
| 182 x = this.marginsRectangle_.right - 2 * offsetX; |
| 183 y = this.marginsRectangle_.middleY - offsetY; |
| 184 } else if (this.isLeft_()) { |
| 185 x = this.marginsRectangle_.x; |
| 186 y = this.marginsRectangle_.middleY - offsetY; |
| 187 } |
| 188 |
| 189 return { x: x, y: y }; |
| 190 }, |
| 191 |
| 192 /** |
| 193 * Adds event listeners for various events. |
| 194 * @private |
| 195 */ |
| 196 addEventListeners_: function() { |
| 197 this.oninput = this.resetTimer_.bind(this); |
| 198 this.onblur = this.onBlur_.bind(this); |
| 199 this.onkeypress = this.onKeyPressed_.bind(this); |
| 200 this.onkeyup = this.onKeyUp_.bind(this); |
| 201 }, |
| 202 |
| 203 /** |
| 204 * Executes whenever a blur event occurs. |
| 205 * @private |
| 206 */ |
| 207 onBlur_: function() { |
| 208 clearTimeout(this.timerId_); |
| 209 this.validate(); |
| 210 if (!this.isValid) { |
| 211 this.value = MarginTextbox.convertPointsToInchesText( |
| 212 this.lastValidValueInPoints); |
| 213 this.validate(); |
| 214 } |
| 215 |
| 216 this.updateColor(); |
| 217 cr.dispatchSimpleEvent(document, 'updateSummary'); |
| 218 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
| 219 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); |
| 220 }, |
| 221 |
| 222 /** |
| 223 * Executes whenever a keypressed event occurs. Note: Only the "Enter" key |
| 224 * event is handled. The "Escape" key does not result in such event, |
| 225 * therefore it is handled by |this.onKeyUp_|. |
| 226 * @param {KeyboardEvent} e The event that triggered this listener. |
| 227 * @private |
| 228 */ |
| 229 onKeyPressed_: function(e) { |
| 230 if (e.keyCode == MarginTextbox.ENTER_KEYCODE) |
| 231 this.blur(); |
| 232 }, |
| 233 |
| 234 /** |
| 235 * Executes whenever a keyup event occurs. Note: Only the "Escape" |
| 236 * key event is handled. |
| 237 * @param {KeyboardEvent} e The event that triggered this listener. |
| 238 * @private |
| 239 */ |
| 240 onKeyUp_: function(e) { |
| 241 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { |
| 242 this.value = MarginTextbox.convertPointsToInchesText( |
| 243 this.lastValidValueInPoints); |
| 244 this.validate(); |
| 245 this.updateColor(); |
| 246 cr.dispatchSimpleEvent(document, 'updateSummary'); |
| 247 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
| 248 } |
| 249 }, |
| 250 |
| 251 /** |
| 252 * Resetting the timer used to detect when the user stops typing in order |
| 253 * to update the print preview. |
| 254 * @private |
| 255 */ |
| 256 resetTimer_: function() { |
| 257 clearTimeout(this.timerId_); |
| 258 this.timerId_ = window.setTimeout( |
| 259 this.onTextValueMayHaveChanged_.bind(this), 500); |
| 260 }, |
| 261 |
| 262 /** |
| 263 * Executes whenever the user stops typing. |
| 264 * @private |
| 265 */ |
| 266 onTextValueMayHaveChanged_: function() { |
| 267 this.validate(); |
| 268 this.updateColor(); |
| 269 cr.dispatchSimpleEvent(document, 'updateSummary'); |
| 270 cr.dispatchSimpleEvent(document, 'updatePrintButton'); |
| 271 |
| 272 if (!this.isValid) |
| 273 return; |
| 274 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); |
| 275 } |
| 276 |
| 277 }; |
| 278 |
| 279 return { |
| 280 MarginTextbox: MarginTextbox |
| 281 }; |
| 282 }); |
OLD | NEW |