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.setAttribute('class', MarginLine.CSS_CLASS_MARGIN_LINE); | |
Evan Stade
2011/10/04 02:49:18
line.className = ...
dpapad
2011/10/04 20:41:21
Done, here and elsewhere.
| |
12 line.setAttribute('margin-group', groupName); | |
Evan Stade
2011/10/04 02:49:18
why not
line.marginGroup = groupName;
dpapad
2011/10/04 20:41:21
Done, here and elsewhere.
| |
13 | |
14 line.visibleLine = document.createElement('div'); | |
Evan Stade
2011/10/04 02:49:18
docs
dpapad
2011/10/04 20:41:21
Done.
| |
15 line.visibleLine.setAttribute('class', 'margin-line-inner'); | |
16 line.visibleLine.setAttribute('margin-group', groupName); | |
17 line.appendChild(line.visibleLine); | |
18 line.rectangle = null; | |
19 return line; | |
20 } | |
21 | |
22 MarginLine.CSS_CLASS_MARGIN_LINE = 'margin-line'; | |
23 // Width of the visible part of MarginLine. | |
24 MarginLine.VISIBLE_LINE_WIDTH = 0; | |
Evan Stade
2011/10/04 02:49:18
this doesn't make sense. How is it 0? isn't it 1?
dpapad
2011/10/04 20:41:21
Removed it. Indeed it does not make sense since it
| |
25 // Width of the clickable region around each margin line in screen pixels. | |
26 MarginLine.CLICKABLE_REGION = 20; | |
27 | |
28 /** | |
29 * Draws |line| on screen. | |
30 * @param {HTMLDivElement} line The line to draw. | |
31 * @param {print_preview.Rectangle} rectangle The rectangle describing where | |
32 * |line| should be drawn in percentages. | |
33 */ | |
34 MarginLine.drawLineUsingPercent = function (line, rectangle) { | |
Evan Stade
2011/10/04 02:49:18
no space after function (here and elsewhere)
dpapad
2011/10/04 20:41:21
Done.
| |
35 line.style.left = 100 * rectangle.x.toFixed(6) + '%'; | |
Evan Stade
2011/10/04 02:49:18
is 6 decimal places the limit css accepts or somet
dpapad
2011/10/04 20:41:21
Done. It accepts more digits (even though it would
| |
36 line.style.top = 100 * rectangle.y.toFixed(6) + '%'; | |
37 line.style.width = 100 * rectangle.width.toFixed(6) + '%'; | |
38 line.style.height = 100 * rectangle.height.toFixed(6) + '%'; | |
39 }; | |
40 | |
41 /** | |
42 * Draws |line| on screen. | |
43 * @param {HTMLDivElement} line The line to draw. | |
44 * @param {print_preview.Rectangle} rectangle The rectangle describing where | |
45 * |line| should be drawn in percentages with respect to |width| and | |
46 * |height|. | |
47 * @param {number} width The width corresponding to 100%. | |
48 * @param {number} height The height corresponding to 100%. | |
49 */ | |
50 MarginLine.drawLineUsingPixels = function (line, rectangle, width, height) { | |
Evan Stade
2011/10/04 02:49:18
drawLineUsingPixels is an odd name. If I understan
dpapad
2011/10/04 20:41:21
Actually these should not be class (as of static i
| |
51 line.style.left = Math.round(rectangle.x * width) + 'px'; | |
52 line.style.top = Math.round(rectangle.y * height) + 'px'; | |
53 line.style.width = Math.round(rectangle.width * width) + 'px'; | |
54 line.style.height = Math.round(rectangle.height * height) + 'px'; | |
55 }; | |
56 | |
57 | |
Evan Stade
2011/10/04 02:49:18
extra line
dpapad
2011/10/04 20:41:21
Done.
| |
58 MarginLine.prototype = { | |
59 __proto__: HTMLDivElement.prototype, | |
60 | |
61 update: function(marginsRectangle) { | |
62 this.rectangle = this.getCoordinates_(marginsRectangle); | |
63 }, | |
64 | |
65 /** | |
66 * Draws |this| on screen. Essentially two divs are being drawn, the outer | |
67 * margin line (not visible) and the inner line (visible). | |
Evan Stade
2011/10/04 02:49:18
outer and inner are not very descriptive, 'drag co
dpapad
2011/10/04 20:41:21
Done.
| |
68 */ | |
69 draw: function() { | |
70 var totalWidth = previewArea.pdfPlugin_.offsetWidth; | |
71 var totalHeight = previewArea.pdfPlugin_.offsetHeight; | |
72 print_preview.MarginLine.drawLineUsingPixels( | |
73 this, this.rectangle, totalWidth, totalHeight); | |
74 print_preview.MarginLine.drawLineUsingPercent( | |
75 this.visibleLine, this.getVisibleLineCoordinates_()); | |
76 }, | |
77 | |
78 /** | |
79 * Calucaltes the coordinates and size of |this|. | |
Evan Stade
2011/10/04 02:49:18
sp
dpapad
2011/10/04 20:41:21
Done.
| |
80 * @param {print_preview.Rect} marginsRectangle A rectangle describing the | |
81 * selected margins values in percentages. | |
82 * @private | |
83 */ | |
84 getCoordinates_: function(marginsRectangle) { | |
85 var pageLocation = previewArea.getPageLocationNormalized(); | |
86 var totalWidth = previewArea.pdfPlugin_.offsetWidth; | |
87 var totalHeight = previewArea.pdfPlugin_.offsetHeight; | |
88 | |
89 if (this.isTop_()) { | |
90 var lineCoordinates = new print_preview.Rect( | |
91 pageLocation.x, | |
92 marginsRectangle.y - | |
93 (MarginLine.CLICKABLE_REGION / 2) / totalHeight, | |
94 pageLocation.width, | |
95 MarginLine.CLICKABLE_REGION / totalHeight); | |
96 } else if (this.isBottom_()) { | |
97 var lineCoordinates = new print_preview.Rect( | |
98 pageLocation.x, | |
99 marginsRectangle.y + marginsRectangle.height - | |
100 (MarginLine.CLICKABLE_REGION / 2) / totalHeight, | |
101 pageLocation.width, | |
102 MarginLine.CLICKABLE_REGION / totalHeight); | |
103 } else if (this.isRight_()) { | |
104 var lineCoordinates = new print_preview.Rect( | |
105 marginsRectangle.x + marginsRectangle.width - | |
106 (MarginLine.CLICKABLE_REGION / 2) / totalWidth, | |
107 pageLocation.y, | |
108 MarginLine.CLICKABLE_REGION / totalWidth, | |
109 pageLocation.height); | |
110 } else if (this.isLeft_()) { | |
111 var lineCoordinates = new print_preview.Rect( | |
112 marginsRectangle.x - (MarginLine.CLICKABLE_REGION / 2) / totalWidth, | |
113 pageLocation.y, | |
114 MarginLine.CLICKABLE_REGION / totalWidth, | |
115 pageLocation.height); | |
116 } | |
117 return lineCoordinates; | |
118 }, | |
119 | |
120 /** | |
121 * Calculates the coordinates in percentages and size of the visible margin | |
122 * line, with respect to |this| div element. | |
123 * @private | |
124 * @return {print_preview.Rect} A rectangle describing the position of the | |
125 * visible line in percentages. | |
126 */ | |
127 getVisibleLineCoordinates_: function() { | |
128 if (this.isHorizontal_()) { | |
129 var innerMarginsRect = new print_preview.Rect( | |
130 0, 0.5, 1, MarginLine.VISIBLE_LINE_WIDTH); | |
131 } else if (this.isVertical_()) { | |
132 var innerMarginsRect = new print_preview.Rect( | |
133 0.5, 0, MarginLine.VISIBLE_LINE_WIDTH, 1); | |
134 } | |
135 return innerMarginsRect; | |
136 }, | |
137 | |
138 /** | |
139 * @private | |
140 * @return {boolean} True if |this| refers to the top margin. | |
141 */ | |
142 isTop_: function() { | |
143 return this.getAttribute('margin-group') == | |
144 print_preview.MarginSettings.TOP_GROUP_NAME; | |
145 }, | |
146 | |
147 /** | |
148 * @private | |
149 * @return {boolean} True if |this| refers to the bottom margin. | |
150 */ | |
151 isBottom_: function() { | |
152 return this.getAttribute('margin-group') == | |
153 print_preview.MarginSettings.BOTTOM_GROUP_NAME; | |
154 }, | |
155 | |
156 /** | |
157 * @private | |
158 * @return {boolean} True if |this| refers to the left margin. | |
159 */ | |
160 isLeft_: function() { | |
161 return this.getAttribute('margin-group') == | |
162 print_preview.MarginSettings.LEFT_GROUP_NAME; | |
163 }, | |
164 | |
165 /** | |
166 * @private | |
167 * @return {boolean} True if |this| refers to the bottom margin. | |
168 */ | |
169 isRight_: function() { | |
170 return this.getAttribute('margin-group') == | |
171 print_preview.MarginSettings.RIGHT_GROUP_NAME; | |
172 }, | |
173 | |
174 /** | |
175 * @private | |
176 * @return {boolean} True if |this| is a horizontal line. | |
177 */ | |
178 isHorizontal_: function() { | |
179 return this.isTop_() || this.isBottom_() ; | |
180 }, | |
181 | |
182 /** | |
183 * @private | |
184 * @return {boolean} True if |this| is a vertical line. | |
185 */ | |
186 isVertical_: function() { | |
187 return this.isLeft_() || this.isRight_(); | |
188 }, | |
189 | |
190 }; | |
191 | |
192 return { | |
193 MarginLine: MarginLine | |
194 }; | |
195 }); | |
OLD | NEW |