OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 'use strict'; |
| 7 |
| 8 /** |
| 9 * UI component used for setting custom print margins. |
| 10 * |
| 11 * @param {!print_preview.PrintTicketStore} printTicketStore Used to read and |
| 12 * write custom margin values. |
| 13 * @constructor |
| 14 * @extends {print_preview.Component} |
| 15 */ |
| 16 function CustomMargins(printTicketStore) { |
| 17 print_preview.Component.call(this); |
| 18 |
| 19 /** |
| 20 * Used to read and write custom margin values. |
| 21 * @type {!print_preview.PrintTicketStore} |
| 22 * @private |
| 23 */ |
| 24 this.printTicketStore_ = printTicketStore; |
| 25 |
| 26 /** |
| 27 * Convenience array that contains all of the margin controls. |
| 28 * @type {!Array<!print_preview.MarginControl>} |
| 29 * @private |
| 30 */ |
| 31 this.controls_ = {}; |
| 32 for (var orientation in print_preview.MarginControl.Orientation) { |
| 33 orientation = print_preview.MarginControl.Orientation[orientation]; |
| 34 var control = new print_preview.MarginControl(orientation); |
| 35 this.controls_[orientation] = control; |
| 36 this.addChild(control); |
| 37 } |
| 38 |
| 39 /** |
| 40 * Margin control currently being dragged. Null if no control is being |
| 41 * dragged. |
| 42 * @type {print_preview.MarginControl} |
| 43 * @private |
| 44 */ |
| 45 this.draggedControl_ = null; |
| 46 |
| 47 /** |
| 48 * Translation transformation in pixels to translate from the origin of the |
| 49 * custom margins component to the top-left corner of the most visible |
| 50 * preview page. |
| 51 * @type {!print_preview.Coordinate2d} |
| 52 * @private |
| 53 */ |
| 54 this.translateTransform_ = new print_preview.Coordinate2d(0, 0); |
| 55 |
| 56 /** |
| 57 * Scaling transformation to scale from pixels to the units which the |
| 58 * print preview is in. The scaling factor is the same in both dimensions, |
| 59 * so this field is just a single number. |
| 60 * @type {number} |
| 61 * @private |
| 62 */ |
| 63 this.scaleTransform_ = 1; |
| 64 }; |
| 65 |
| 66 /** |
| 67 * CSS classes used by the custom margins component. |
| 68 * @enum {string} |
| 69 * @private |
| 70 */ |
| 71 CustomMargins.Classes_ = { |
| 72 DRAGGING_HORIZONTAL: 'custom-margins-dragging-horizontal', |
| 73 DRAGGING_VERTICAL: 'custom-margins-dragging-vertical' |
| 74 }; |
| 75 |
| 76 /** |
| 77 * Minimum distance in points between two margins. |
| 78 * @type {number} |
| 79 * @const |
| 80 * @private |
| 81 */ |
| 82 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; |
| 83 |
| 84 CustomMargins.prototype = { |
| 85 __proto__: print_preview.Component.prototype, |
| 86 |
| 87 updateBoundingBox: function(boundingBox) { |
| 88 this.boundingBox_ = boundingBox; |
| 89 // TODO Update UI |
| 90 }, |
| 91 |
| 92 /** |
| 93 * Updates the translation transformation that translates pixel values in |
| 94 * the space of the HTML DOM. |
| 95 * @param {print_preview.Coordinate2d} translateTransform Updated value of |
| 96 * the translation transformation. |
| 97 */ |
| 98 updateTranslationTransform: function(translateTransform) { |
| 99 if (!translateTransform.equals(this.translateTransform_)) { |
| 100 this.translateTransform_ = translateTransform; |
| 101 for (var orientation in this.controls_) { |
| 102 this.controls_[orientation].translateTransform = translateTransform; |
| 103 } |
| 104 } |
| 105 }, |
| 106 |
| 107 updateScaleTransform: function(scaleTransform) { |
| 108 if (scaleTransform != this.scaleTransform_) { |
| 109 this.scaleTransform_ = scaleTransform; |
| 110 for (var orientation in this.controls_) { |
| 111 this.controls_[orientation].scaleTransform = scaleTransform; |
| 112 } |
| 113 } |
| 114 }, |
| 115 |
| 116 /** @override */ |
| 117 decorateInternal: function() { |
| 118 for (var orientation in this.controls_) { |
| 119 this.controls_[orientation].render(this.getElement()); |
| 120 } |
| 121 }, |
| 122 |
| 123 /** @override */ |
| 124 enterDocument: function() { |
| 125 print_preview.Component.prototype.enterDocument.call(this); |
| 126 this.tracker.add( |
| 127 this.getElement(), 'mousemove', this.onMouseMove_.bind(this)); |
| 128 this.tracker.add( |
| 129 this.getElement(), 'mouseup', this.onMouseUp_.bind(this)); |
| 130 this.tracker.add( |
| 131 this.getElement(), 'mouseover', this.onMouseOver_.bind(this)); |
| 132 this.tracker.add( |
| 133 this.getElement(), 'mouseout', this.onMouseOut_.bind(this)); |
| 134 |
| 135 this.tracker.add( |
| 136 this.printTicketStore_, |
| 137 print_preview.PrintTicketStore.Event.INITIALIZE, |
| 138 this.onTicketInitialize_.bind(this)); |
| 139 this.tracker.add( |
| 140 this.printTicketStore_, |
| 141 print_preview.PrintTicketStore.Event.TICKET_CHANGE, |
| 142 this.onTicketChange_.bind(this)); |
| 143 this.tracker.add( |
| 144 this.printTicketStore_, |
| 145 print_preview.PrintTicketStore.Event.DOCUMENT_CHANGE, |
| 146 this.onDocumentChange_.bind(this)); |
| 147 this.tracker.add( |
| 148 this.printTicketStore_, |
| 149 print_preview.PrintTicketStore.Event.CAPABILITIES_CHANGE, |
| 150 this.onCapabilitiesChange_.bind(this)); |
| 151 |
| 152 for (var orientation in this.controls_) { |
| 153 var control = this.controls_[orientation]; |
| 154 this.tracker.add( |
| 155 control, |
| 156 print_preview.MarginControl.Event.DRAG_START, |
| 157 this.onControlDragStart_.bind(this, control)); |
| 158 this.tracker.add( |
| 159 control, |
| 160 print_preview.MarginControl.Event.TEXT_CHANGE, |
| 161 this.updatePrintTicket_.bind(this, control)); |
| 162 } |
| 163 }, |
| 164 |
| 165 setIsMarginControlsVisible_: function(isVisible) { |
| 166 for (var orientation in this.controls_) { |
| 167 this.controls_[orientation].isVisible = isVisible; |
| 168 } |
| 169 }, |
| 170 |
| 171 moveControlWithinConstraints_: function(control, position) { |
| 172 var orientationEnum = print_preview.MarginControl.Orientation; |
| 173 |
| 174 // Get desired position |
| 175 var newPosInPts; |
| 176 var isTopOrBottom = control.orientation == orientationEnum.TOP || |
| 177 control.orientation == orientationEnum.BOTTOM; |
| 178 if (isTopOrBottom) { |
| 179 newPosInPts = control.convertPixelsToPts(position.y); |
| 180 } else { |
| 181 newPosInPts = control.convertPixelsToPts(position.x); |
| 182 } |
| 183 |
| 184 // Apply constraints between controls |
| 185 var constraintControl; |
| 186 if (control.orientation == orientationEnum.TOP) { |
| 187 constraintControl = this.controls_[orientationEnum.BOTTOM]; |
| 188 } else if (control.orientation == orientationEnum.RIGHT) { |
| 189 constraintControl = this.controls_[orientationEnum.LEFT]; |
| 190 } else if (control.orientation == orientationEnum.BOTTOM) { |
| 191 constraintControl = this.controls_[orientationEnum.TOP]; |
| 192 } else { |
| 193 constraintControl = this.controls_[orientationEnum.RIGHT]; |
| 194 } |
| 195 var sum = constraintControl.positionInPts + newPosInPts + |
| 196 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 197 if (isTopOrBottom && sum > this.printTicketStore_.pageSize.height) { |
| 198 newPosInPts = this.printTicketStore_.pageSize.height - |
| 199 CustomMargins.MINIMUM_MARGINS_DISTANCE_ - |
| 200 constraintControl.positionInPts; |
| 201 } |
| 202 if (!isTopOrBottom && sum > this.printTicketStore_.pageSize.width) { |
| 203 newPosInPts = this.printTicketStore_.pageSize.width - |
| 204 CustomMargins.MINIMUM_MARGINS_DISTANCE_ - |
| 205 constraintControl.positionInPts; |
| 206 } |
| 207 |
| 208 // Make sure margin is above minimum |
| 209 var minimum; |
| 210 if (control.orientation == orientationEnum.TOP) { |
| 211 minimum = this.printTicketStore_.printableArea.origin.y; |
| 212 } else if (control.orientation == orientationEnum.RIGHT) { |
| 213 minimum = this.printTicketStore_.pageSize.width - |
| 214 this.printTicketStore_.printableArea.origin.x - |
| 215 this.printTicketStore_.printableArea.size.width; |
| 216 } else if (control.orientation == orientationEnum.BOTTOM) { |
| 217 minimum = this.printTicketStore_.pageSize.height - |
| 218 this.printTicketStore_.printableArea.origin.y - |
| 219 this.printTicketStore_.printableArea.size.height; |
| 220 } else { |
| 221 minimum = this.printTicketStore_.printableArea.origin.x; |
| 222 } |
| 223 newPosInPts = Math.max(minimum, newPosInPts); |
| 224 |
| 225 // Set new position |
| 226 control.positionInPts = newPosInPts; |
| 227 }, |
| 228 |
| 229 updatePrintTicket_: function(control) { |
| 230 var orientationEnum = print_preview.MarginControl.Orientation; |
| 231 var oldMargins = this.printTicketStore_.getCustomMargins(); |
| 232 var top = control.orientation == orientationEnum.TOP ? |
| 233 control.positionInPts : oldMargins.top; |
| 234 var right = control.orientation == orientationEnum.RIGHT ? |
| 235 control.positionInPts : oldMargins.right; |
| 236 var bottom = control.orientation == orientationEnum.BOTTOM ? |
| 237 control.positionInPts : oldMargins.bottom; |
| 238 var left = control.orientation == orientationEnum.LEFT ? |
| 239 control.positionInPts : oldMargins.left; |
| 240 this.printTicketStore_.updateCustomMargins( |
| 241 new print_preview.Margins(top, right, bottom, left)); |
| 242 }, |
| 243 |
| 244 /** |
| 245 * Called when a margin control starts to drag. |
| 246 * @param {print_preview.MarginControl} control The control which started to |
| 247 * drag. |
| 248 * @private |
| 249 */ |
| 250 onControlDragStart_: function(control) { |
| 251 this.draggedControl_ = control; |
| 252 var isControlTopOrBottom = |
| 253 control.orientation == print_preview.MarginControl.Orientation.TOP || |
| 254 control.orientation == print_preview.MarginControl.Orientation.BOTTOM; |
| 255 this.getElement().classList.add( |
| 256 isControlTopOrBottom ? |
| 257 CustomMargins.Classes_.DRAGGING_VERTICAL : |
| 258 CustomMargins.Classes_.DRAGGING_HORIZONTAL); |
| 259 }, |
| 260 |
| 261 /** |
| 262 * Called when the mouse moves in the custom margins component. Moves the |
| 263 * dragged margin control. |
| 264 * @param {MouseEvent} evt Contains the position of the mouse. |
| 265 * @private |
| 266 */ |
| 267 onMouseMove_: function(evt) { |
| 268 if (this.draggedControl_) { |
| 269 this.moveControlWithinConstraints_( |
| 270 this.draggedControl_, |
| 271 this.draggedControl_.translateMouseToPositionInPixels( |
| 272 new print_preview.Coordinate2d(evt.x, evt.y))); |
| 273 } |
| 274 }, |
| 275 |
| 276 /** |
| 277 * Called when the mouse is released in the custom margins component. |
| 278 * Releases the dragged margin control. |
| 279 * @param {MouseEvent} evt Contains the position of the mouse. |
| 280 * @private |
| 281 */ |
| 282 onMouseUp_: function(evt) { |
| 283 if (this.draggedControl_) { |
| 284 this.getElement().classList.remove( |
| 285 CustomMargins.Classes_.DRAGGING_VERTICAL); |
| 286 this.getElement().classList.remove( |
| 287 CustomMargins.Classes_.DRAGGING_HORIZONTAL); |
| 288 this.moveControlWithinConstraints_( |
| 289 this.draggedControl_, |
| 290 this.draggedControl_.translateMouseToPositionInPixels( |
| 291 new print_preview.Coordinate2d(evt.x, evt.y))); |
| 292 this.updatePrintTicket_(this.draggedControl_); |
| 293 this.draggedControl_ = null; |
| 294 } |
| 295 }, |
| 296 |
| 297 /** |
| 298 * Called when the mouse moves onto the component. Shows the margin |
| 299 * controls. |
| 300 * @private |
| 301 */ |
| 302 onMouseOver_: function() { |
| 303 if (this.printTicketStore_.isInitialized && |
| 304 this.printTicketStore_.getMarginsType() == |
| 305 print_preview.Margins.Type.CUSTOM) { |
| 306 this.setIsMarginControlsVisible_(true); |
| 307 } |
| 308 }, |
| 309 |
| 310 /** |
| 311 * Called when the mouse moves off of the component. Hides the margin |
| 312 * controls. |
| 313 * @private |
| 314 */ |
| 315 onMouseOut_: function() { |
| 316 var isAnyControlInFocus = false; |
| 317 for (var orientation in this.controls_) { |
| 318 if (this.controls_[orientation].isInFocus) { |
| 319 isAnyControlInFocus = true; |
| 320 break; |
| 321 } |
| 322 } |
| 323 if (!isAnyControlInFocus) { |
| 324 this.setIsMarginControlsVisible_(false); |
| 325 } |
| 326 }, |
| 327 |
| 328 /** |
| 329 * Called when the print ticket is initialized. Creates and updates the |
| 330 * margin controls. |
| 331 * @private |
| 332 */ |
| 333 onTicketInitialize_: function() { |
| 334 for (var orientation in this.controls_) { |
| 335 this.controls_[orientation].measurementSystem = |
| 336 this.printTicketStore_.measurementSystem; |
| 337 this.controls_[orientation].pageSize = this.printTicketStore_.pageSize; |
| 338 } |
| 339 this.onTicketChange_(); |
| 340 }, |
| 341 |
| 342 /** |
| 343 * Called when the print ticket changes. Updates the position of the margin |
| 344 * controls. |
| 345 * @private |
| 346 */ |
| 347 onTicketChange_: function() { |
| 348 var marginsType = this.printTicketStore_.getMarginsType(); |
| 349 var margins = this.printTicketStore_.getCustomMargins(); |
| 350 if (marginsType == print_preview.Margins.Type.CUSTOM && margins != null) { |
| 351 var orientEnum = print_preview.MarginControl.Orientation; |
| 352 this.controls_[orientEnum.TOP].positionInPts = margins.top; |
| 353 this.controls_[orientEnum.RIGHT].positionInPts = margins.right; |
| 354 this.controls_[orientEnum.BOTTOM].positionInPts = margins.bottom; |
| 355 this.controls_[orientEnum.LEFT].positionInPts = margins.left; |
| 356 } |
| 357 this.setIsMarginControlsVisible_( |
| 358 marginsType == print_preview.Margins.Type.CUSTOM); |
| 359 }, |
| 360 |
| 361 /** |
| 362 * Called when the document changes. Updates margin controls if the page |
| 363 * size has changed. |
| 364 * @private |
| 365 */ |
| 366 onDocumentChange_: function() { |
| 367 for (var orientation in this.controls_) { |
| 368 this.controls_[orientation].pageSize = this.printTicketStore_.pageSize; |
| 369 } |
| 370 }, |
| 371 |
| 372 /** |
| 373 * Called when the destination print capabilities have changed. Shows or |
| 374 * hides the custom margins. |
| 375 * @private |
| 376 */ |
| 377 onCapabilitiesChange_: function() { |
| 378 if (!this.printTicketStore_.hasMarginsCapability() || |
| 379 this.printTicketStore_.getMarginsType() != |
| 380 print_preview.Margins.Type.CUSTOM) { |
| 381 this.setIsMarginControlsVisible_(false); |
| 382 } |
| 383 }, |
| 384 |
| 385 /** |
| 386 * Applies a clipping mask on |this| so that it does not paint on top of the |
| 387 * scrollbars (if any). |
| 388 */ |
| 389 applyClippingMask_: function() { |
| 390 var bottom = previewArea.height; |
| 391 var right = previewArea.width; |
| 392 this.style.clip = 'rect(0, ' + right + 'px, ' + bottom + 'px, 0)'; |
| 393 }, |
| 394 |
| 395 /** |
| 396 * Updates |this.marginsUI| depending on the specified margins and the |
| 397 * position of the page within the plugin. |
| 398 * @private |
| 399 */ |
| 400 drawCustomMarginsUI_: function() { |
| 401 // TODO(dpapad): find out why passing |!this.areMarginsSettingsValid()| |
| 402 // directly produces the opposite value even though |
| 403 // |this.getMarginsRectangleInPercent_()| and |
| 404 // |this.getMarginValueLimits_()| have no side effects. |
| 405 previewArea.update(); |
| 406 var keepDisplayedValue = !this.areMarginSettingsValid(); |
| 407 this.marginsUI.update(this.getMarginsRectangleInPercent_(), |
| 408 this.customMargins_, |
| 409 this.getMarginValueLimits_(), |
| 410 keepDisplayedValue, |
| 411 this.getMarginValueLimitsInPercent_()); |
| 412 this.marginsUI.draw(); |
| 413 }, |
| 414 |
| 415 /** |
| 416 * Executes when user selects a different margin option, ie, |
| 417 * |this.select_.selectedIndex| is changed. |
| 418 * @private |
| 419 */ |
| 420 onMarginsChanged_: function() { |
| 421 if (this.isDefaultMarginsSelected() || this.isMinimumMarginsSelected() || |
| 422 this.isNoMarginsSelected()) |
| 423 this.onDefaultMinimumNoMarginsSelected_(); |
| 424 else if (this.isCustomMarginsSelected()) |
| 425 this.onCustomMarginsSelected_(); |
| 426 }, |
| 427 |
| 428 /** |
| 429 * Calculates the coordinates of the four margin lines. These are the |
| 430 * coordinates where the margin lines should be displayed. The coordinates |
| 431 * are expressed in terms of percentages with respect to the total width |
| 432 * and height of the plugin. |
| 433 * @return {print_preview.Rect} A rectnangle that describes the position of |
| 434 * the four margin lines. |
| 435 * @private |
| 436 */ |
| 437 getMarginsRectangleInPercent_: function() { |
| 438 var pageLocation = previewArea.pageLocationNormalized; |
| 439 var marginsInPercent = this.getMarginsInPercent_(); |
| 440 var leftX = pageLocation.x + marginsInPercent.left; |
| 441 var topY = pageLocation.y + marginsInPercent.top; |
| 442 var contentWidth = pageLocation.width - (marginsInPercent.left + |
| 443 marginsInPercent.right); |
| 444 var contentHeight = pageLocation.height - (marginsInPercent.top + |
| 445 marginsInPercent.bottom); |
| 446 return new print_preview.Rect( |
| 447 leftX, topY, contentWidth, contentHeight); |
| 448 }, |
| 449 |
| 450 /** |
| 451 * @return {print_preview.Margins} The currently selected margin values |
| 452 * normalized to the total width and height of the plugin. |
| 453 * @private |
| 454 */ |
| 455 getMarginsInPercent_: function() { |
| 456 return this.convertMarginsInPointsToPercent(this.customMargins_); |
| 457 }, |
| 458 |
| 459 /** |
| 460 * Converts |marginsToConvert| to points and normalizes it to the height and |
| 461 * width of the plugin. |
| 462 * @return {print_preview.Margins} The margins in percent. |
| 463 * @private |
| 464 */ |
| 465 convertMarginsInPointsToPercent: function(marginsToConvert) { |
| 466 var pageInformation = previewArea.pageLocationNormalized; |
| 467 var totalWidthInPoints = this.pageWidth_ / pageInformation.width; |
| 468 var totalHeightInPoints = this.pageHeight_ / pageInformation.height; |
| 469 var marginsInPercent = new Margins( |
| 470 marginsToConvert.left / totalWidthInPoints, |
| 471 marginsToConvert.top / totalHeightInPoints, |
| 472 marginsToConvert.right / totalWidthInPoints, |
| 473 marginsToConvert.bottom / totalHeightInPoints); |
| 474 return marginsInPercent; |
| 475 }, |
| 476 |
| 477 /** |
| 478 * Executes when a |customEvents.PDF_LOADED| event occurs. |
| 479 * @private |
| 480 */ |
| 481 onPDFLoaded_: function() { |
| 482 if (!previewModifiable) { |
| 483 fadeOutOption(this.marginsOption_); |
| 484 return; |
| 485 } |
| 486 |
| 487 if (this.forceMarginsUIOnPDFLoad_) { |
| 488 this.updatePageData_(); |
| 489 this.drawCustomMarginsUI_(); |
| 490 this.addCustomMarginEventListeners_(); |
| 491 this.marginsUI.show(); |
| 492 this.forceMarginsUIOnPDFLoad_ = false; |
| 493 } |
| 494 }, |
| 495 |
| 496 /** |
| 497 * Updates |this.customMargins_|, |this.pageWidth_|, |this.pageHeight_|. |
| 498 * @private |
| 499 */ |
| 500 updatePageData_: function() { |
| 501 if (!this.customMargins_) |
| 502 this.customMargins_ = this.currentDefaultPageLayout.margins_.clone(); |
| 503 |
| 504 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; |
| 505 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; |
| 506 } |
| 507 }; |
| 508 |
| 509 // Export |
| 510 return { |
| 511 CustomMargins: CustomMargins |
| 512 }; |
| 513 }); |
OLD | NEW |