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..eef0461156dcaa0ff52590d73d5cdfd9f950f429 100644 |
| --- a/chrome/browser/resources/print_preview/margin_utils.js |
| +++ b/chrome/browser/resources/print_preview/margin_utils.js |
| @@ -31,18 +31,64 @@ 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). |
| * @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. |
|
Evan Stade
2011/10/18 01:04:40
dang, this is getting complicated. You should crea
dpapad
2011/10/18 02:18:11
Done.
|
| */ |
| - 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. |
| + * @return {number} The value in points after converting to inches with a |
| + * certain precision and back. |
| + */ |
| + function convertPointsToInchesTextAndBack(value) { |
| + var text = convertPointsToInchesText(value); |
| + var inches = extractMarginValue(text); |
| + return convertInchesToPoints(inches); |
| } |
| /** |
| @@ -62,26 +108,36 @@ cr.define('print_preview', function() { |
| }; |
| Rect.prototype = { |
| + /** |
| + * @type {number} The x coordinate of the right-most point. |
| + */ |
| get right() { |
| return this.x + this.width; |
| }, |
| + /** |
| + * @type {number} The y coordinate of the lower-most point. |
| + */ |
| get bottom() { |
| return this.y + this.height; |
| }, |
| - get middleX() { |
| - return this.x + this.width / 2; |
| - }, |
| - |
| - get middleY() { |
| - return this.y + this.height / 2; |
| + /** |
| + * Clones |this| and returns the cloned object. |
| + * @return {Rect} A copy of |this|. |
| + */ |
| + clone: function() { |
| + return new Rect(this.x, this.y, this.width, this.height); |
| } |
| }; |
| return { |
| + convertInchesToInchesText: convertInchesToInchesText, |
| + convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, |
| + convertPointsToInchesText: convertPointsToInchesText, |
| extractMarginValue: extractMarginValue, |
| - isMarginTextValid: isMarginTextValid, |
| Rect: Rect, |
| + validateMarginText: validateMarginText, |
| + validateMarginValue: validateMarginValue |
| }; |
| }); |