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 function MarginLine(groupName) { |
| 9 var line = document.createElement('div'); |
| 10 line.__proto__ = MarginLine.prototype; |
| 11 line.className = MarginLine.CSS_CLASS_DRAGGABLE_AREA; |
| 12 // @type {string} Specifies which margin this line refers to. |
| 13 line.marginGroup = groupName; |
| 14 // @type {print_preview.Rect} A rectangle describing the values of all |
| 15 // margins. |
| 16 line.rectangle = null; |
| 17 // @type {HTMLDivElement} The dotted line representing the margin. |
| 18 line.visibleLine = document.createElement('div'); |
| 19 line.visibleLine.className = MarginLine.CSS_CLASS_MARGIN_LINE; |
| 20 |
| 21 line.appendChild(line.visibleLine); |
| 22 return line; |
| 23 } |
| 24 |
| 25 MarginLine.CSS_CLASS_MARGIN_LINE = 'margin-line'; |
| 26 MarginLine.CSS_CLASS_DRAGGABLE_AREA = 'draggable-area'; |
| 27 // Width of the clickable region around each margin line in screen pixels. |
| 28 MarginLine.CLICKABLE_REGION = 20; |
| 29 |
| 30 MarginLine.prototype = { |
| 31 __proto__: HTMLDivElement.prototype, |
| 32 |
| 33 update: function(marginsRectangle) { |
| 34 this.rectangle = this.getCoordinates_(marginsRectangle); |
| 35 }, |
| 36 |
| 37 /** |
| 38 * Draws |this| on screen. Essentially two divs are being drawn, the drag |
| 39 * control area (invisible) and the dotted margin line (visible). |
| 40 */ |
| 41 draw: function() { |
| 42 this.drawDraggableArea_(); |
| 43 this.drawDottedLine_(); |
| 44 }, |
| 45 |
| 46 /** |
| 47 * Draws the dotted line representing the margin. |
| 48 * @private |
| 49 */ |
| 50 drawDottedLine_ : function() { |
| 51 var rectangle = this.getVisibleLineCoordinates_(); |
| 52 this.visibleLine.style.left = 100 * rectangle.x + '%'; |
| 53 this.visibleLine.style.top = 100 * rectangle.y + '%'; |
| 54 this.visibleLine.style.width = 100 * rectangle.width + '%'; |
| 55 this.visibleLine.style.height = 100 * rectangle.height + '%'; |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Draws the area the draggable area (not visible). |
| 60 * @private |
| 61 */ |
| 62 drawDraggableArea_: function() { |
| 63 var width = previewArea.pdfPlugin_.offsetWidth; |
| 64 var height = previewArea.pdfPlugin_.offsetHeight; |
| 65 |
| 66 this.style.left = Math.round(this.rectangle.x * width) + 'px'; |
| 67 this.style.top = Math.round(this.rectangle.y * height) + 'px'; |
| 68 this.style.width = Math.round(this.rectangle.width * width) + 'px'; |
| 69 this.style.height = Math.round(this.rectangle.height * height) + 'px'; |
| 70 }, |
| 71 |
| 72 /** |
| 73 * Calculates the coordinates and size of |this|. |
| 74 * @param {print_preview.Rect} marginsRectangle A rectangle describing the |
| 75 * selected margins values in percentages. |
| 76 * @private |
| 77 */ |
| 78 getCoordinates_: function(marginsRectangle) { |
| 79 var pageLocation = previewArea.getPageLocationNormalized(); |
| 80 var totalWidth = previewArea.pdfPlugin_.offsetWidth; |
| 81 var totalHeight = previewArea.pdfPlugin_.offsetHeight; |
| 82 |
| 83 if (this.isTop_()) { |
| 84 var lineCoordinates = new print_preview.Rect( |
| 85 pageLocation.x, |
| 86 marginsRectangle.y - |
| 87 (MarginLine.CLICKABLE_REGION / 2) / totalHeight, |
| 88 pageLocation.width, |
| 89 MarginLine.CLICKABLE_REGION / totalHeight); |
| 90 } else if (this.isBottom_()) { |
| 91 var lineCoordinates = new print_preview.Rect( |
| 92 pageLocation.x, |
| 93 marginsRectangle.y + marginsRectangle.height - |
| 94 (MarginLine.CLICKABLE_REGION / 2) / totalHeight, |
| 95 pageLocation.width, |
| 96 MarginLine.CLICKABLE_REGION / totalHeight); |
| 97 } else if (this.isRight_()) { |
| 98 var lineCoordinates = new print_preview.Rect( |
| 99 marginsRectangle.x + marginsRectangle.width - |
| 100 (MarginLine.CLICKABLE_REGION / 2) / totalWidth, |
| 101 pageLocation.y, |
| 102 MarginLine.CLICKABLE_REGION / totalWidth, |
| 103 pageLocation.height); |
| 104 } else if (this.isLeft_()) { |
| 105 var lineCoordinates = new print_preview.Rect( |
| 106 marginsRectangle.x - (MarginLine.CLICKABLE_REGION / 2) / totalWidth, |
| 107 pageLocation.y, |
| 108 MarginLine.CLICKABLE_REGION / totalWidth, |
| 109 pageLocation.height); |
| 110 } |
| 111 return lineCoordinates; |
| 112 }, |
| 113 |
| 114 /** |
| 115 * Calculates the coordinates in percentages and size of the visible margin |
| 116 * line, with respect to |this| div element. |
| 117 * @return {print_preview.Rect} A rectangle describing the position of the |
| 118 * visible line in percentages. |
| 119 * @private |
| 120 */ |
| 121 getVisibleLineCoordinates_: function() { |
| 122 if (this.isHorizontal_()) |
| 123 var innerMarginsRect = new print_preview.Rect(0, 0.5, 1, 0); |
| 124 else if (this.isVertical_()) |
| 125 var innerMarginsRect = new print_preview.Rect(0.5, 0, 0, 1); |
| 126 return innerMarginsRect; |
| 127 }, |
| 128 |
| 129 /** |
| 130 * @return {boolean} True if |this| refers to the top margin. |
| 131 * @private |
| 132 */ |
| 133 isTop_: function() { |
| 134 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP; |
| 135 }, |
| 136 |
| 137 /** |
| 138 * @return {boolean} True if |this| refers to the bottom margin. |
| 139 * @private |
| 140 */ |
| 141 isBottom_: function() { |
| 142 return this.marginGroup == print_preview.MarginSettings.BOTTOM_GROUP; |
| 143 }, |
| 144 |
| 145 /** |
| 146 * @return {boolean} True if |this| refers to the left margin. |
| 147 * @private |
| 148 */ |
| 149 isLeft_: function() { |
| 150 return this.marginGroup == print_preview.MarginSettings.LEFT_GROUP; |
| 151 }, |
| 152 |
| 153 /** |
| 154 * @return {boolean} True if |this| refers to the bottom margin. |
| 155 * @private |
| 156 */ |
| 157 isRight_: function() { |
| 158 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP; |
| 159 }, |
| 160 |
| 161 /** |
| 162 * @return {boolean} True if |this| is a horizontal line. |
| 163 * @private |
| 164 */ |
| 165 isHorizontal_: function() { |
| 166 return this.isTop_() || this.isBottom_() ; |
| 167 }, |
| 168 |
| 169 /** |
| 170 * @return {boolean} True if |this| is a vertical line. |
| 171 * @private |
| 172 */ |
| 173 isVertical_: function() { |
| 174 return this.isLeft_() || this.isRight_(); |
| 175 }, |
| 176 |
| 177 }; |
| 178 |
| 179 return { |
| 180 MarginLine: MarginLine |
| 181 }; |
| 182 }); |
OLD | NEW |