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

Side by Side Diff: chrome/browser/resources/print_preview/margin_line.js

Issue 7891016: Print Preview: Adding UI for margin settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nit 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 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 var offsetY = (MarginLine.CLICKABLE_REGION / 2) / totalHeight;
83 var offsetX = (MarginLine.CLICKABLE_REGION / 2) / totalWidth;
84
85 if (this.isTop_()) {
86 var lineCoordinates = new print_preview.Rect(
87 pageLocation.x,
88 marginsRectangle.y - offsetY,
89 pageLocation.width,
90 MarginLine.CLICKABLE_REGION / totalHeight);
91 } else if (this.isBottom_()) {
92 var lineCoordinates = new print_preview.Rect(
93 pageLocation.x,
94 marginsRectangle.bottom - offsetY,
95 pageLocation.width,
96 MarginLine.CLICKABLE_REGION / totalHeight);
97 } else if (this.isRight_()) {
98 var lineCoordinates = new print_preview.Rect(
99 marginsRectangle.right - offsetX,
100 pageLocation.y,
101 MarginLine.CLICKABLE_REGION / totalWidth,
102 pageLocation.height);
103 } else if (this.isLeft_()) {
104 var lineCoordinates = new print_preview.Rect(
105 marginsRectangle.x - offsetX,
106 pageLocation.y,
107 MarginLine.CLICKABLE_REGION / totalWidth,
108 pageLocation.height);
109 }
110 return lineCoordinates;
111 },
112
113 /**
114 * Calculates the coordinates in percentages and size of the visible margin
115 * line, with respect to |this| div element.
116 * @return {print_preview.Rect} A rectangle describing the position of the
117 * visible line in percentages.
118 * @private
119 */
120 getVisibleLineCoordinates_: function() {
121 if (this.isHorizontal_())
122 var innerMarginsRect = new print_preview.Rect(0, 0.5, 1, 0);
123 else if (this.isVertical_())
124 var innerMarginsRect = new print_preview.Rect(0.5, 0, 0, 1);
125 return innerMarginsRect;
126 },
127
128 /**
129 * @return {boolean} True if |this| refers to the top margin.
130 * @private
131 */
132 isTop_: function() {
133 return this.marginGroup == print_preview.MarginSettings.TOP_GROUP;
134 },
135
136 /**
137 * @return {boolean} True if |this| refers to the bottom margin.
138 * @private
139 */
140 isBottom_: function() {
141 return this.marginGroup == print_preview.MarginSettings.BOTTOM_GROUP;
142 },
143
144 /**
145 * @return {boolean} True if |this| refers to the left margin.
146 * @private
147 */
148 isLeft_: function() {
149 return this.marginGroup == print_preview.MarginSettings.LEFT_GROUP;
150 },
151
152 /**
153 * @return {boolean} True if |this| refers to the bottom margin.
154 * @private
155 */
156 isRight_: function() {
157 return this.marginGroup == print_preview.MarginSettings.RIGHT_GROUP;
158 },
159
160 /**
161 * @return {boolean} True if |this| is a horizontal line.
162 * @private
163 */
164 isHorizontal_: function() {
165 return this.isTop_() || this.isBottom_() ;
166 },
167
168 /**
169 * @return {boolean} True if |this| is a vertical line.
170 * @private
171 */
172 isVertical_: function() {
173 return this.isLeft_() || this.isRight_();
174 },
175
176 };
177
178 return {
179 MarginLine: MarginLine
180 };
181 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698