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