Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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). |
|
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.
| |
| 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. | |
|
Evan Stade
2011/10/14 21:58:36
@return
dpapad
2011/10/14 23:14:38
Done.
| |
| 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) { | |
|
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.
| |
| 96 this.x = x; | |
| 97 this.y = y; | |
| 98 } | |
| 99 | |
| 100 /** | |
| 49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The | 101 * 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. | 102 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. |
| 51 * @constructor | 103 * @constructor |
| 52 */ | 104 */ |
| 53 function Rect(x, y, width, height) { | 105 function Rect(x, y, width, height) { |
| 54 // @type {number} Horizontal distance of the upper left corner from origin. | 106 // @type {number} Horizontal distance of the upper left corner from origin. |
| 55 this.x = x; | 107 this.x = x; |
| 56 // @type {number} Vertical distance of the upper left corner from origin. | 108 // @type {number} Vertical distance of the upper left corner from origin. |
| 57 this.y = y; | 109 this.y = y; |
| 58 // @type {number} Width of |this| rectangle. | 110 // @type {number} Width of |this| rectangle. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 69 get bottom() { | 121 get bottom() { |
| 70 return this.y + this.height; | 122 return this.y + this.height; |
| 71 }, | 123 }, |
| 72 | 124 |
| 73 get middleX() { | 125 get middleX() { |
| 74 return this.x + this.width / 2; | 126 return this.x + this.width / 2; |
| 75 }, | 127 }, |
| 76 | 128 |
| 77 get middleY() { | 129 get middleY() { |
| 78 return this.y + this.height / 2; | 130 return this.y + this.height / 2; |
| 131 }, | |
| 132 | |
|
Evan Stade
2011/10/14 21:58:36
@return docs for all these functions
dpapad
2011/10/14 23:14:38
Done.
| |
| 133 clone: function() { | |
| 134 return new Rect(this.x, this.y, this.width, this.height); | |
| 79 } | 135 } |
| 80 }; | 136 }; |
| 81 | 137 |
| 82 return { | 138 return { |
| 83 extractMarginValue: extractMarginValue, | 139 extractMarginValue: extractMarginValue, |
| 84 isMarginTextValid: isMarginTextValid, | 140 validateMarginText: validateMarginText, |
| 141 validateMarginValue: validateMarginValue, | |
|
Evan Stade
2011/10/14 21:58:36
keep the list alphabetical
dpapad
2011/10/14 23:14:38
Done.
| |
| 142 convertPointsToInchesText: convertPointsToInchesText, | |
| 143 convertInchesToInchesText: convertInchesToInchesText, | |
| 144 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, | |
| 85 Rect: Rect, | 145 Rect: Rect, |
| 146 Point: Point | |
| 86 }; | 147 }; |
| 87 }); | 148 }); |
| OLD | NEW |