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 12 matching lines...) Expand all Loading... |
23 return -1; | 23 return -1; |
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 var marginValidationStates = { |
| 34 TOO_SMALL: 0, |
| 35 WITHIN_RANGE: 1, |
| 36 TOO_BIG: 2, |
| 37 NOT_A_NUMBER: 3 |
| 38 } |
| 39 |
| 40 /** |
| 41 * Checks whether |value| is within range [0, limit]. |
| 42 * @return {number} An appropriate value from enum |marginValidationStates|. |
| 43 */ |
| 44 function validateMarginValue(value, limit) { |
| 45 if (value <= limit && value >= 0) |
| 46 return marginValidationStates.WITHIN_RANGE; |
| 47 else if (value < 0) |
| 48 return marginValidationStates.TOO_SMALL; |
| 49 else (value > limit) |
| 50 return marginValidationStates.TOO_BIG; |
| 51 } |
| 52 |
33 /** | 53 /** |
34 * @param {sting} text The text to check (in inches). | 54 * @param {sting} text The text to check (in inches). |
35 * @param {number} limit The upper bound of the valid margin range (in | 55 * @param {number} limit The upper bound of the valid margin range (in |
36 * points). | 56 * points). |
37 * @return {boolean} True of |text| can be parsed and it is within the allowed | 57 * @return {number} An appropriate value from enum |marginValidationStates|. |
38 * range. | |
39 */ | 58 */ |
40 function isMarginTextValid(text, limit) { | 59 function validateMarginText(text, limit) { |
41 var value = extractMarginValue(text); | 60 var value = extractMarginValue(text); |
42 if (value == -1) | 61 if (value == -1) |
43 return false; | 62 return marginValidationStates.NOT_A_NUMBER; |
44 value = convertInchesToPoints(value); | 63 value = convertInchesToPoints(value); |
45 return value <= limit; | 64 return validateMarginValue(value, limit); |
46 } | 65 } |
47 | 66 |
48 /** | 67 /** |
| 68 * @param {number} toConvert The value to convert in points |
| 69 * @return {string} The equivalent text in inches. |
| 70 */ |
| 71 function convertPointsToInchesText(toConvert) { |
| 72 var inInches = convertPointsToInches(toConvert); |
| 73 return convertInchesToInchesText(inInches); |
| 74 } |
| 75 |
| 76 /** |
| 77 * @param {number} toConvert The value to convert in inches. |
| 78 * @return {string} The equivalent text in inches with precision of two |
| 79 * digits. |
| 80 */ |
| 81 function convertInchesToInchesText(toConvert) { |
| 82 return toConvert.toFixed(2) + '"'; |
| 83 } |
| 84 |
| 85 /** |
| 86 * Converts |value| to inches text (keeping 2 decimal digits) and then back to |
| 87 * points. Note: Because of the precision the return value might be different |
| 88 * than |value|. |
| 89 * @param {number} value The value in points to convert. |
| 90 * @return {number} The value in points after converting to inches with a |
| 91 * certain precision and back. |
| 92 */ |
| 93 function convertPointsToInchesTextAndBack(value) { |
| 94 var text = convertPointsToInchesText(value); |
| 95 var inches = extractMarginValue(text); |
| 96 return convertInchesToPoints(inches); |
| 97 } |
| 98 |
| 99 /** |
49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The | 100 * 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. | 101 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. |
51 * @constructor | 102 * @constructor |
52 */ | 103 */ |
53 function Rect(x, y, width, height) { | 104 function Rect(x, y, width, height) { |
54 // @type {number} Horizontal distance of the upper left corner from origin. | 105 // @type {number} Horizontal distance of the upper left corner from origin. |
55 this.x = x; | 106 this.x = x; |
56 // @type {number} Vertical distance of the upper left corner from origin. | 107 // @type {number} Vertical distance of the upper left corner from origin. |
57 this.y = y; | 108 this.y = y; |
58 // @type {number} Width of |this| rectangle. | 109 // @type {number} Width of |this| rectangle. |
59 this.width = width; | 110 this.width = width; |
60 // @type {number} Height of |this| rectangle. | 111 // @type {number} Height of |this| rectangle. |
61 this.height = height; | 112 this.height = height; |
62 }; | 113 }; |
63 | 114 |
64 Rect.prototype = { | 115 Rect.prototype = { |
| 116 /** |
| 117 * @type {number} The x coordinate of the right-most point. |
| 118 */ |
65 get right() { | 119 get right() { |
66 return this.x + this.width; | 120 return this.x + this.width; |
67 }, | 121 }, |
68 | 122 |
| 123 /** |
| 124 * @type {number} The y coordinate of the lower-most point. |
| 125 */ |
69 get bottom() { | 126 get bottom() { |
70 return this.y + this.height; | 127 return this.y + this.height; |
71 }, | 128 }, |
72 | 129 |
73 get middleX() { | 130 /** |
74 return this.x + this.width / 2; | 131 * Clones |this| and returns the cloned object. |
75 }, | 132 * @return {Rect} A copy of |this|. |
76 | 133 */ |
77 get middleY() { | 134 clone: function() { |
78 return this.y + this.height / 2; | 135 return new Rect(this.x, this.y, this.width, this.height); |
79 } | 136 } |
80 }; | 137 }; |
81 | 138 |
82 return { | 139 return { |
| 140 convertInchesToInchesText: convertInchesToInchesText, |
| 141 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, |
| 142 convertPointsToInchesText: convertPointsToInchesText, |
83 extractMarginValue: extractMarginValue, | 143 extractMarginValue: extractMarginValue, |
84 isMarginTextValid: isMarginTextValid, | 144 marginValidationStates: marginValidationStates, |
85 Rect: Rect, | 145 Rect: Rect, |
| 146 validateMarginText: validateMarginText, |
| 147 validateMarginValue: validateMarginValue |
86 }; | 148 }; |
87 }); | 149 }); |
OLD | NEW |