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

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

Issue 8233030: Print Preview: Making margin lines draggable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing mouse events outside the plugin area, adding documentations 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 cr.define('print_preview', function() { 5 cr.define('print_preview', function() {
6 'use strict'; 6 'use strict';
7 7
8 /** 8 /**
9 * Checks if |text| has a valid margin value format. A valid format is 9 * Checks if |text| has a valid margin value format. A valid format is
10 * parsable as a number and is greater than zero. 10 * parsable as a number and is greater than zero.
(...skipping 13 matching lines...) Expand all
24 // Remove the inch(") symbol at end of string if present. 24 // Remove the inch(") symbol at end of string if present.
25 if (text.charAt(text.length - 1) == '\"') 25 if (text.charAt(text.length - 1) == '\"')
26 text = text.slice(0, text.length - 1); 26 text = text.slice(0, text.length - 1);
27 var regex = /^\d*(\.\d+)?$/ 27 var regex = /^\d*(\.\d+)?$/
28 if (regex.test(text)) 28 if (regex.test(text))
29 return parseFloat(text); 29 return parseFloat(text);
30 return -1; 30 return -1;
31 } 31 }
32 32
33 /** 33 /**
34 * Checks whether |value| is within range [0, limit].
35 * @return {number} 0 if within the range, -1 if smaller, 1 if larger.
36 */
37 function validateMarginValue(value, limit) {
38 if (value <= limit && value >= 0)
39 return 0;
40 else if (value < 0)
41 return -1;
42 else (value > limit)
43 return 1;
44 }
45
46 /**
34 * @param {sting} text The text to check (in inches). 47 * @param {sting} text The text to check (in inches).
35 * @param {number} limit The upper bound of the valid margin range (in 48 * @param {number} limit The upper bound of the valid margin range (in
36 * points). 49 * points).
37 * @return {boolean} True of |text| can be parsed and it is within the allowed 50 * @return {number} -2 if |text| can't be converted to number, 0 if it
38 * range. 51 * represents a valid margin value, -1 if the value is less than the
52 * minimum margin, 1 if the value is larger than the maximum margin.
39 */ 53 */
40 function isMarginTextValid(text, limit) { 54 function validateMarginText(text, limit) {
41 var value = extractMarginValue(text); 55 var value = extractMarginValue(text);
42 if (value == -1) 56 if (value == -1)
43 return false; 57 return -2;
44 value = convertInchesToPoints(value); 58 value = convertInchesToPoints(value);
45 return value <= limit; 59 return validateMarginValue(value, limit);
46 } 60 }
47 61
48 /** 62 /**
63 * @param {number} toConvert The value to convert in points
64 * @return {string} The equivalent text in inches.
65 */
66 function convertPointsToInchesText(toConvert) {
67 var inInches = convertPointsToInches(toConvert);
68 return convertInchesToInchesText(inInches);
69 }
70
71 /**
72 * @param {number} toConvert The value to convert in inches.
73 * @return {string} The equivalent text in inches with precision of two
74 * digits.
75 */
76 function convertInchesToInchesText(toConvert) {
77 return toConvert.toFixed(2) + '"';
78 }
79
80 /**
81 * Converts |value| to inches text (keeping 2 decimal digits) and then back to
82 * points. Note: Because of the precision the return value might be different
83 * than |value|.
84 * @param {number} value The value in points to convert.
85 * @param {number} The value in points after converting to inches with a
86 * certain precision and back.
87 */
88 function convertPointsToInchesTextAndBack(value) {
89 var text = convertPointsToInchesText(value);
90 var inches = extractMarginValue(text);
91 return convertInchesToPoints(inches);
92 }
93
94
95 function Point(x, y) {
96 this.x = x;
97 this.y = y;
98 }
99
100 Point.prototype = {
101 /**
102 * @param {Point} rhs The point to compare with.
103 * @return {number} The horizontal distance between |this| point and |rhs|.
104 */
105 distanceXFrom: function(rhs) {
106 return rhs.x - this.x;
107 },
108
109 /**
110 * @param {Point} rhs The point to compare with.
111 * @return {number} The vertical distance between |this| point and |rhs|.
112 */
113 distanceYFrom: function(rhs) {
114 return rhs.y - this.y;
115 },
116 }
117
118 /**
49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The 119 * Creates a Rect object. This object describes a rectangle in a 2D plane. The
50 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. 120 * units of |x|, |y|, |width|, |height| are chosen by clients of this class.
51 * @constructor 121 * @constructor
52 */ 122 */
53 function Rect(x, y, width, height) { 123 function Rect(x, y, width, height) {
54 // @type {number} Horizontal distance of the upper left corner from origin. 124 // @type {number} Horizontal distance of the upper left corner from origin.
55 this.x = x; 125 this.x = x;
56 // @type {number} Vertical distance of the upper left corner from origin. 126 // @type {number} Vertical distance of the upper left corner from origin.
57 this.y = y; 127 this.y = y;
58 // @type {number} Width of |this| rectangle. 128 // @type {number} Width of |this| rectangle.
(...skipping 10 matching lines...) Expand all
69 get bottom() { 139 get bottom() {
70 return this.y + this.height; 140 return this.y + this.height;
71 }, 141 },
72 142
73 get middleX() { 143 get middleX() {
74 return this.x + this.width / 2; 144 return this.x + this.width / 2;
75 }, 145 },
76 146
77 get middleY() { 147 get middleY() {
78 return this.y + this.height / 2; 148 return this.y + this.height / 2;
149 },
150
151 clone: function() {
152 return new Rect(this.x, this.y, this.width, this.height);
79 } 153 }
80 }; 154 };
81 155
82 return { 156 return {
83 extractMarginValue: extractMarginValue, 157 extractMarginValue: extractMarginValue,
84 isMarginTextValid: isMarginTextValid, 158 validateMarginText: validateMarginText,
159 validateMarginValue: validateMarginValue,
160 convertPointsToInchesText: convertPointsToInchesText,
161 convertInchesToInchesText: convertInchesToInchesText,
162 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack,
85 Rect: Rect, 163 Rect: Rect,
164 Point: Point
86 }; 165 };
87 }); 166 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698