Chromium Code Reviews| Index: chrome/browser/resources/print_preview/margin_utils.js |
| diff --git a/chrome/browser/resources/print_preview/margin_utils.js b/chrome/browser/resources/print_preview/margin_utils.js |
| index 73a925a3cf15a673c74abcfad04f92b78ecd434f..3dc7fec9d52e134f87cc4b37294f598f39a1ad31 100644 |
| --- a/chrome/browser/resources/print_preview/margin_utils.js |
| +++ b/chrome/browser/resources/print_preview/margin_utils.js |
| @@ -31,18 +31,70 @@ cr.define('print_preview', function() { |
| } |
| /** |
| + * Checks whether |value| is within range [0, limit]. |
| + * @return {number} 0 if within the range, -1 if smaller, 1 if larger. |
| + */ |
| + function validateMarginValue(value, limit) { |
| + if (value <= limit && value >= 0) |
| + return 0; |
| + else if (value < 0) |
| + return -1; |
| + else (value > limit) |
| + return 1; |
| + } |
| + |
| + /** |
| * @param {sting} text The text to check (in inches). |
|
Evan Stade
2011/10/14 21:58:36
can you file a bug to i18n-ize the measurements
dpapad
2011/10/14 23:14:38
Done, filed bug 100416.
|
| * @param {number} limit The upper bound of the valid margin range (in |
| * points). |
| - * @return {boolean} True of |text| can be parsed and it is within the allowed |
| - * range. |
| + * @return {number} -2 if |text| can't be converted to number, 0 if it |
| + * represents a valid margin value, -1 if the value is less than the |
| + * minimum margin, 1 if the value is larger than the maximum margin. |
| */ |
| - function isMarginTextValid(text, limit) { |
| + function validateMarginText(text, limit) { |
| var value = extractMarginValue(text); |
| if (value == -1) |
| - return false; |
| + return -2; |
| value = convertInchesToPoints(value); |
| - return value <= limit; |
| + return validateMarginValue(value, limit); |
| + } |
| + |
| + /** |
| + * @param {number} toConvert The value to convert in points |
| + * @return {string} The equivalent text in inches. |
| + */ |
| + function convertPointsToInchesText(toConvert) { |
| + var inInches = convertPointsToInches(toConvert); |
| + return convertInchesToInchesText(inInches); |
| + } |
| + |
| + /** |
| + * @param {number} toConvert The value to convert in inches. |
| + * @return {string} The equivalent text in inches with precision of two |
| + * digits. |
| + */ |
| + function convertInchesToInchesText(toConvert) { |
| + return toConvert.toFixed(2) + '"'; |
| + } |
| + |
| + /** |
| + * Converts |value| to inches text (keeping 2 decimal digits) and then back to |
| + * points. Note: Because of the precision the return value might be different |
| + * than |value|. |
| + * @param {number} value The value in points to convert. |
| + * @param {number} The value in points after converting to inches with a |
| + * certain precision and back. |
|
Evan Stade
2011/10/14 21:58:36
@return
dpapad
2011/10/14 23:14:38
Done.
|
| + */ |
| + function convertPointsToInchesTextAndBack(value) { |
| + var text = convertPointsToInchesText(value); |
| + var inches = extractMarginValue(text); |
| + return convertInchesToPoints(inches); |
| + } |
| + |
| + |
| + function Point(x, y) { |
|
Evan Stade
2011/10/14 21:58:36
once again, not a useful class
dpapad
2011/10/14 23:14:38
I was using distanceXFrom, distanceYFrom before ad
Evan Stade
2011/10/17 21:36:45
but now you aren't
dpapad
2011/10/17 23:28:06
Done.
|
| + this.x = x; |
| + this.y = y; |
| } |
| /** |
| @@ -76,12 +128,21 @@ cr.define('print_preview', function() { |
| get middleY() { |
| return this.y + this.height / 2; |
| + }, |
| + |
|
Evan Stade
2011/10/14 21:58:36
@return docs for all these functions
dpapad
2011/10/14 23:14:38
Done.
|
| + clone: function() { |
| + return new Rect(this.x, this.y, this.width, this.height); |
| } |
| }; |
| return { |
| extractMarginValue: extractMarginValue, |
| - isMarginTextValid: isMarginTextValid, |
| + validateMarginText: validateMarginText, |
| + validateMarginValue: validateMarginValue, |
|
Evan Stade
2011/10/14 21:58:36
keep the list alphabetical
dpapad
2011/10/14 23:14:38
Done.
|
| + convertPointsToInchesText: convertPointsToInchesText, |
| + convertInchesToInchesText: convertInchesToInchesText, |
| + convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, |
| Rect: Rect, |
| + Point: Point |
| }; |
| }); |