Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/resources/print_preview/margin_textbox.js

Issue 8233030: Print Preview: Making margin lines draggable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addresing comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_();
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(); 142 this.updateColor();
127 }, 143 },
128 144
129 /** 145 /**
130 * @return {boolean} True if |this| refers to the top margin. 146 * @return {boolean} True if |this| refers to the top margin.
131 * @private 147 * @private
132 */ 148 */
133 isTop_: function() { 149 isTop_: function() {
134 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP; 150 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP;
135 }, 151 },
(...skipping 16 matching lines...) Expand all
152 168
153 /** 169 /**
154 * @return {boolean} True if |this| refers to the bottom margin. 170 * @return {boolean} True if |this| refers to the bottom margin.
155 * @private 171 * @private
156 */ 172 */
157 isRight_: function() { 173 isRight_: function() {
158 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP; 174 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP;
159 }, 175 },
160 176
161 /** 177 /**
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. 178 * Adds event listeners for various events.
194 * @private 179 * @private
195 */ 180 */
196 addEventListeners_: function() { 181 addEventListeners_: function() {
197 this.oninput = this.resetTimer_.bind(this); 182 this.oninput = this.resetTimer_.bind(this);
198 this.onblur = this.onBlur_.bind(this); 183 this.onblur = this.onBlur_.bind(this);
199 this.onkeypress = this.onKeyPressed_.bind(this); 184 this.onkeypress = this.onKeyPressed_.bind(this);
200 this.onkeyup = this.onKeyUp_.bind(this); 185 this.onkeyup = this.onKeyUp_.bind(this);
201 }, 186 },
202 187
203 /** 188 /**
204 * Executes whenever a blur event occurs. 189 * Executes whenever a blur event occurs.
205 * @private 190 * @private
206 */ 191 */
207 onBlur_: function() { 192 onBlur_: function() {
208 clearTimeout(this.timerId_); 193 clearTimeout(this.timerId_);
209 this.validate(); 194 this.validate();
210 if (!this.isValid) { 195 if (!this.isValid) {
211 this.value = MarginTextbox.convertPointsToInchesText( 196 this.value = print_preview.convertPointsToInchesText(
212 this.lastValidValueInPoints); 197 this.lastValidValueInPoints);
213 this.validate(); 198 this.validate();
214 } 199 }
215 200
216 this.updateColor(); 201 this.updateColor();
217 cr.dispatchSimpleEvent(document, 'updateSummary'); 202 cr.dispatchSimpleEvent(document, 'updateSummary');
218 cr.dispatchSimpleEvent(document, 'updatePrintButton'); 203 cr.dispatchSimpleEvent(document, 'updatePrintButton');
219 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); 204 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged');
220 }, 205 },
221 206
(...skipping 10 matching lines...) Expand all
232 }, 217 },
233 218
234 /** 219 /**
235 * Executes whenever a keyup event occurs. Note: Only the "Escape" 220 * Executes whenever a keyup event occurs. Note: Only the "Escape"
236 * key event is handled. 221 * key event is handled.
237 * @param {KeyboardEvent} e The event that triggered this listener. 222 * @param {KeyboardEvent} e The event that triggered this listener.
238 * @private 223 * @private
239 */ 224 */
240 onKeyUp_: function(e) { 225 onKeyUp_: function(e) {
241 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) { 226 if (e.keyCode == MarginTextbox.ESCAPE_KEYCODE) {
242 this.value = MarginTextbox.convertPointsToInchesText( 227 this.value = print_preview.convertPointsToInchesText(
243 this.lastValidValueInPoints); 228 this.lastValidValueInPoints);
244 this.validate(); 229 this.validate();
245 this.updateColor(); 230 this.updateColor();
246 cr.dispatchSimpleEvent(document, 'updateSummary'); 231 cr.dispatchSimpleEvent(document, 'updateSummary');
247 cr.dispatchSimpleEvent(document, 'updatePrintButton'); 232 cr.dispatchSimpleEvent(document, 'updatePrintButton');
248 } 233 }
249 }, 234 },
250 235
251 /** 236 /**
252 * Resetting the timer used to detect when the user stops typing in order 237 * Resetting the timer used to detect when the user stops typing in order
253 * to update the print preview. 238 * to update the print preview.
254 * @private 239 * @private
255 */ 240 */
256 resetTimer_: function() { 241 resetTimer_: function() {
257 clearTimeout(this.timerId_); 242 clearTimeout(this.timerId_);
258 this.timerId_ = window.setTimeout( 243 this.timerId_ = window.setTimeout(
259 this.onTextValueMayHaveChanged_.bind(this), 500); 244 this.onTextValueMayHaveChanged.bind(this), 500);
260 }, 245 },
261 246
262 /** 247 /**
263 * Executes whenever the user stops typing. 248 * Executes whenever the user stops typing or when a drag session associated
264 * @private 249 * with |this| ends.
265 */ 250 */
266 onTextValueMayHaveChanged_: function() { 251 onTextValueMayHaveChanged: function() {
267 this.validate(); 252 this.validate();
268 this.updateColor(); 253 this.updateColor();
269 cr.dispatchSimpleEvent(document, 'updateSummary'); 254 cr.dispatchSimpleEvent(document, 'updateSummary');
270 cr.dispatchSimpleEvent(document, 'updatePrintButton'); 255 cr.dispatchSimpleEvent(document, 'updatePrintButton');
271 256
272 if (!this.isValid) 257 if (!this.isValid)
273 return; 258 return;
274 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged'); 259 cr.dispatchSimpleEvent(this, 'MarginsMayHaveChanged');
275 } 260 }
276 261
277 }; 262 };
278 263
279 return { 264 return {
280 MarginTextbox: MarginTextbox 265 MarginTextbox: MarginTextbox
281 }; 266 };
282 }); 267 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698