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