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

Side by Side Diff: chrome/browser/resources/print_preview/margins_ui_pair.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
(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 /**
9 * @constructor
10 * This class represents a margin line and a textbox corresponding to that
11 * margin.
12 */
13 function MarginsUIPair(groupName) {
14 var marginsUIPair = document.createElement('div');
15 marginsUIPair.__proto__ = MarginsUIPair.prototype;
16 marginsUIPair.className = MarginsUIPair.CSS_CLASS;
17 // @type {string} Specifies which margin this line refers to.
18 marginsUIPair.marginGroup = groupName;
19 marginsUIPair.classList.add(groupName);
20
21 // @type {print_preview.Rect} A rectangle describing the dimensions of
22 // the draggable area.
23 marginsUIPair.rectangle = null;
24 // @type {print_preview.Rect} A rectangle describing the four margins.
25 marginsUIPair.marginsRectangle = null;
26 // @type {print_preview.MarginLine} The line representing the margin.
27 marginsUIPair.line_ = new print_preview.MarginLine(groupName);
28 // @type {print_preview.MarginTextbox} The textbox corresponding to this
29 // margin.
30 marginsUIPair.box_ = new print_preview.MarginTextbox(groupName);
31 // @type {{x: number, y: number}} The point where mousedown occured within
32 // the draggable area with respect the top-left corner of |this|.
33 marginsUIPair.mousePointerOffset = null;
34 // @type {{x: number, y:number}} The position of the origin before any
35 // dragging in progress occured.
36 marginsUIPair.originBeforeDragging = null;
37
38 marginsUIPair.appendChild(marginsUIPair.line_);
39 marginsUIPair.appendChild(marginsUIPair.box_);
40
41 marginsUIPair.addEventListeners_();
42 return marginsUIPair;
43 }
44
45 MarginsUIPair.CSS_CLASS = 'margins-ui-pair';
46 // Width of the clickable region around each margin line in screen pixels.
47 MarginsUIPair.CLICKABLE_REGION = 20;
48
49 MarginsUIPair.prototype = {
50 __proto__: HTMLDivElement.prototype,
51
52 /**
53 * Updates the state.
54 */
55 update: function(marginsRectangle, value, valueLimit, keepDisplayedValue,
56 valueLimitInPercent) {
57 this.marginsRectangle = marginsRectangle;
58 this.valueLimitInPercent = valueLimitInPercent;
59 this.rectangle = this.getCoordinates_(this.marginsRectangle);
60 this.box_.update(value, valueLimit, keepDisplayedValue);
61 },
62
63 /**
64 * Draws |this| based on the state.
65 */
66 draw: function() {
67 this.drawDraggableArea_();
68 this.line_.draw();
69 this.box_.draw();
70 },
71
72 /**
73 * Draws the area that can be clicked in order to start dragging.
74 * @private
75 */
76 drawDraggableArea_: function() {
77 var width = previewArea.pdfPlugin_.offsetWidth;
78 var height = previewArea.pdfPlugin_.offsetHeight;
79
80 this.style.left = Math.round(this.rectangle.x * width) + 'px';
81 this.style.top = Math.round(this.rectangle.y * height) + 'px';
82 this.style.width = Math.round(this.rectangle.width * width) + 'px';
83 this.style.height = Math.round(this.rectangle.height * height) + 'px';
84 },
85
86 /**
87 * Calculates the coordinates and size of |this|.
88 * @param {print_preview.Rect} marginsRectangle A rectangle describing the
89 * selected margins values in percentages.
90 * @private
91 */
92 getCoordinates_: function(marginsRectangle) {
93 var pageLocation = previewArea.pageLocationNormalized;
94 var totalWidth = previewArea.pdfPlugin_.offsetWidth;
95 var totalHeight = previewArea.pdfPlugin_.offsetHeight;
96 var offsetY = (MarginsUIPair.CLICKABLE_REGION / 2) / totalHeight;
97 var offsetX = (MarginsUIPair.CLICKABLE_REGION / 2) / totalWidth;
98
99 if (this.isTop_()) {
100 var lineCoordinates = new print_preview.Rect(
101 pageLocation.x,
102 marginsRectangle.y - offsetY,
103 pageLocation.width,
104 MarginsUIPair.CLICKABLE_REGION / totalHeight);
105 } else if (this.isBottom_()) {
106 var lineCoordinates = new print_preview.Rect(
107 pageLocation.x,
108 marginsRectangle.bottom - offsetY,
109 pageLocation.width,
110 MarginsUIPair.CLICKABLE_REGION / totalHeight);
111 } else if (this.isRight_()) {
112 var lineCoordinates = new print_preview.Rect(
113 marginsRectangle.right - offsetX,
114 pageLocation.y,
115 MarginsUIPair.CLICKABLE_REGION / totalWidth,
116 pageLocation.height);
117 } else if (this.isLeft_()) {
118 var lineCoordinates = new print_preview.Rect(
119 marginsRectangle.x - offsetX,
120 pageLocation.y,
121 MarginsUIPair.CLICKABLE_REGION / totalWidth,
122 pageLocation.height);
123 }
124 return lineCoordinates;
125 },
126
127 /**
128 * @return {boolean} True if |this| refers to the top margin.
129 * @private
130 */
131 isTop_: function() {
132 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP;
133 },
134
135 /**
136 * @return {boolean} True if |this| refers to the bottom margin.
137 * @private
138 */
139 isBottom_: function() {
140 return this.marginGroup == print_preview.MarginSettings.BOTTOM_GROUP;
141 },
142
143 /**
144 * @return {boolean} True if |this| refers to the left margin.
145 * @private
146 */
147 isLeft_: function() {
148 return this.marginGroup == print_preview.MarginSettings.LEFT_GROUP;
149 },
150
151 /**
152 * @return {boolean} True if |this| refers to the bottom margin.
153 * @private
154 */
155 isRight_: function() {
156 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP;
157 },
158
159 /**
160 * Adds event listeners for events related to dragging.
161 */
162 addEventListeners_: function() {
163 this.onmousedown = this.onMouseDown_.bind(this);
164 },
165
166 /**
167 * Executes whenever a mousedown event occurs on |this| or its child nodes.
168 * @parameter {MouseEvent} e The event that occured.
169 */
170 onMouseDown_: function(e) {
171 if (e.target != this)
172 return;
173 var point = print_preview.MarginsUI.convert({x: e.x, y: e.y});
174 this.originBeforeDragging = { x: this.offsetLeft, y: this.offsetTop };
175 this.mousePointerOffset =
176 { x: point.x - this.offsetLeft, y: point.y - this.offsetTop };
177 cr.dispatchSimpleEvent(this, 'MarginsLineMouseDown');
178 },
179
180 /**
181 * Executes whenever a mouseup event occurs while |this| is dragged.
182 */
183 onMouseUp: function() {
184 this.box_.onTextValueMayHaveChanged();
185 },
186
187 /**
188 * Moves |this| including all its children to |point|.
189 * @param {{x: number, y: number}} point The point where |this| should be
190 * moved.
191 */
192 moveTo: function(point) {
193 if (this.isTop_() || this.isBottom_())
194 this.style.top = point.y - this.mousePointerOffset.y + 'px';
195 else
196 this.style.left = point.x - this.mousePointerOffset.x + 'px';
197 },
198
199 /**
200 * @param {{x: number, y: number}} rhs The point to compare with.
201 * @return {number} The horizontal or vertical displacement in pixels
202 * between |this.originBeforeDragging| and |rhs|. Note: Bottom margin
203 * grows upwards, right margin grows when going to the left.
204 */
205 getDragDisplacementFrom: function(rhs) {
206 var dragDisplacement = 0;
207 if (this.isTop_() || this.isBottom_()) {
208 dragDisplacement = (rhs.y - this.originBeforeDragging.y -
209 this.mousePointerOffset.y) / previewArea.height;
210 } else {
211 dragDisplacement = (rhs.x - this.originBeforeDragging.x -
212 this.mousePointerOffset.x) / previewArea.width;
213 }
214
215 if (this.isBottom_() || this.isRight_())
216 dragDisplacement *= -1;
217 return dragDisplacement;
218 },
219
220 /**
221 * Updates |this| while dragging is in progress. Takes care of rejecting
222 * invalid margin values.
223 * @param {number} dragDeltaInPoints The difference in points between the
224 * currently applied margin and the margin indicated by
225 * |destinationPoint|.
226 * @param {{x: number, y: number}} destinationPoint The point where the
227 * margin line should be drawn if |dragDeltaInPoints| is applied.
228 */
229 updateWhileDragging: function(dragDeltaInPoints, destinationPoint) {
230 this.box_.updateWhileDragging(dragDeltaInPoints);
231 var validity = this.box_.validateDelta(dragDeltaInPoints);
232 if (validity == 0)
233 this.moveTo(destinationPoint);
234 else if (validity == -1)
235 this.snapToMinValue_();
236 else if (validity == 1)
237 this.snapToMaxValue_();
238 },
239
240 /**
241 * Snaps |this| to the minimum allowed margin value (0). Happens whenever
242 * the user drags the margin line to a smaller value than the minimum
243 * allowed.
244 * @private
245 */
246 snapToMinValue_: function() {
247 var pageInformation = previewArea.pageLocationNormalized;
248 var newMarginsRectangle = this.marginsRectangle.clone();
249 if (this.isTop_()) {
250 newMarginsRectangle.y = pageInformation.y;
251 } else if (this.isLeft_()) {
252 newMarginsRectangle.x = pageInformation.x;
253 } else if (this.isRight_()) {
254 newMarginsRectangle.x = pageInformation.x;
255 newMarginsRectangle.width = pageInformation.width;
256 } else if (this.isBottom_()) {
257 newMarginsRectangle.y = pageInformation.y;
258 newMarginsRectangle.height = pageInformation.height;
259 }
260
261 this.rectangle = this.getCoordinates_(newMarginsRectangle);
262 this.drawDraggableArea_();
263 },
264
265 /**
266 * Snaps |this| to the maximum allowed margin value. Happens whenever
267 * the user drags the margin line to a larger value than the maximum
268 * allowed.
269 * @private
270 */
271 snapToMaxValue_: function() {
272 var newMarginsRectangle = this.marginsRectangle.clone();
273
274 if (this.isTop_()) {
275 newMarginsRectangle.y = this.valueLimitInPercent;
276 } else if (this.isLeft_()) {
277 newMarginsRectangle.x = this.valueLimitInPercent;
278 } else if (this.isRight_()) {
279 newMarginsRectangle.x = 0;
280 newMarginsRectangle.width = this.valueLimitInPercent;
281 } else if (this.isBottom_()) {
282 newMarginsRectangle.y = 0;
283 newMarginsRectangle.height = this.valueLimitInPercent;
284 }
285
286 this.rectangle = this.getCoordinates_(newMarginsRectangle);
287 this.drawDraggableArea_();
288 },
289 };
290
291 return {
292 MarginsUIPair: MarginsUIPair
293 };
294 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698