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. |
11 * Example: "1.00", "1", ".5", "1.1" are valid values. | 11 * Example: "1.00", "1", ".5", "1.1" are valid values. |
12 * Example: "1.4dsf", "-1" are invalid. | 12 * Example: "1.4dsf", "-1" are invalid. |
13 * Note: The inch symbol (") at the end of |text| is allowed. | 13 * Note: The inch symbol (") at the end of |text| is allowed. |
14 * | 14 * |
15 * @param {string} text The text to check. | 15 * @param {string} text The text to check. |
16 * @return {number} The margin value represented by |text| or null if |text| | 16 * @return {number} The margin value represented by |text| or null if |text| |
17 * does not represent a valid number. | 17 * does not represent a valid number. |
18 */ | 18 */ |
19 function extractMarginValue(text) { | 19 function extractMarginValue(text) { |
20 // Remove whitespace anywhere in the string. | 20 // Removing whitespace anywhere in the string. |
21 text.replace(/\s*/g, ''); | 21 text = text.replace(/\s*/g, ''); |
22 if (text.length == 0) | 22 if (text.length == 0) |
23 return -1; | 23 return -1; |
24 // Remove the inch(") symbol at end of string if present. | 24 var validationRegex = getValidationRegExp(); |
25 if (text.charAt(text.length - 1) == '\"') | 25 if (validationRegex.test(text)) { |
26 text = text.slice(0, text.length - 1); | 26 // Replacing decimal point with the dot symbol in order to use |
27 var regex = /^\d*(\.\d+)?$/ | 27 // parseFloat() properly. |
28 if (regex.test(text)) | 28 var replacementRegex = new RegExp('\\' + |
| 29 print_preview.MarginSettings.decimalPoint + '{1}'); |
| 30 text = text.replace(replacementRegex, '.'); |
29 return parseFloat(text); | 31 return parseFloat(text); |
| 32 } |
30 return -1; | 33 return -1; |
31 } | 34 } |
32 | 35 |
| 36 /** |
| 37 * @return {RegExp} A regular expression for validating the input of the user. |
| 38 * It takes into account the user's locale. |
| 39 */ |
| 40 function getValidationRegExp() { |
| 41 var regex = new RegExp('(^\\d+)(\\' + |
| 42 print_preview.MarginSettings.thousandsPoint + '\\d{3})*(\\' + |
| 43 print_preview.MarginSettings.decimalPoint + '\\d+)*' + |
| 44 (!print_preview.MarginSettings.useMetricSystem ? '"?' : '(mm)?') + '$'); |
| 45 return regex; |
| 46 } |
| 47 |
33 var marginValidationStates = { | 48 var marginValidationStates = { |
34 TOO_SMALL: 0, | 49 TOO_SMALL: 0, |
35 WITHIN_RANGE: 1, | 50 WITHIN_RANGE: 1, |
36 TOO_BIG: 2, | 51 TOO_BIG: 2, |
37 NOT_A_NUMBER: 3 | 52 NOT_A_NUMBER: 3 |
38 } | 53 } |
39 | 54 |
40 /** | 55 /** |
41 * Checks whether |value| is within range [0, limit]. | 56 * Checks whether |value| is within range [0, limit]. |
42 * @return {number} An appropriate value from enum |marginValidationStates|. | 57 * @return {number} An appropriate value from enum |marginValidationStates|. |
43 */ | 58 */ |
44 function validateMarginValue(value, limit) { | 59 function validateMarginValue(value, limit) { |
45 if (value <= limit && value >= 0) | 60 if (value <= limit && value >= 0) |
46 return marginValidationStates.WITHIN_RANGE; | 61 return marginValidationStates.WITHIN_RANGE; |
47 else if (value < 0) | 62 else if (value < 0) |
48 return marginValidationStates.TOO_SMALL; | 63 return marginValidationStates.TOO_SMALL; |
49 else (value > limit) | 64 else (value > limit) |
50 return marginValidationStates.TOO_BIG; | 65 return marginValidationStates.TOO_BIG; |
51 } | 66 } |
52 | 67 |
53 /** | 68 /** |
54 * @param {sting} text The text to check (in inches). | 69 * @param {sting} text The text to check in user's locale units. |
55 * @param {number} limit The upper bound of the valid margin range (in | 70 * @param {number} limit The upper bound of the valid margin range (in |
56 * points). | 71 * points). |
57 * @return {number} An appropriate value from enum |marginValidationStates|. | 72 * @return {number} An appropriate value from enum |marginValidationStates|. |
58 */ | 73 */ |
59 function validateMarginText(text, limit) { | 74 function validateMarginText(text, limit) { |
60 var value = extractMarginValue(text); | 75 var value = extractMarginValue(text); |
61 if (value == -1) | 76 if (value == -1) |
62 return marginValidationStates.NOT_A_NUMBER; | 77 return marginValidationStates.NOT_A_NUMBER; |
63 value = convertInchesToPoints(value); | 78 value = print_preview.convertLocaleUnitsToPoints(value); |
64 return validateMarginValue(value, limit); | 79 return validateMarginValue(value, limit); |
65 } | 80 } |
66 | 81 |
67 /** | 82 /** |
68 * @param {number} toConvert The value to convert in points | 83 * @param {number} value The value to convert in points. |
69 * @return {string} The equivalent text in inches. | 84 * @return {number} The corresponding value after converting to user's locale |
| 85 * units. |
70 */ | 86 */ |
71 function convertPointsToInchesText(toConvert) { | 87 function convertPointsToLocaleUnits(value) { |
72 var inInches = convertPointsToInches(toConvert); | 88 return print_preview.MarginSettings.useMetricSystem ? |
73 return convertInchesToInchesText(inInches); | 89 convertPointsToMillimeters(value) : convertPointsToInches(value); |
74 } | 90 } |
75 | 91 |
76 /** | 92 /** |
77 * @param {number} toConvert The value to convert in inches. | 93 * @param {number} value The value to convert in user's locale units. |
78 * @return {string} The equivalent text in inches with precision of two | 94 * @return {number} The corresponding value after converting to points. |
79 * digits. | |
80 */ | 95 */ |
81 function convertInchesToInchesText(toConvert) { | 96 function convertLocaleUnitsToPoints(value) { |
82 return toConvert.toFixed(2) + '"'; | 97 return print_preview.MarginSettings.useMetricSystem ? |
| 98 convertMillimetersToPoints(value) : convertInchesToPoints(value); |
83 } | 99 } |
84 | 100 |
85 /** | 101 /** |
86 * Converts |value| to inches text (keeping 2 decimal digits) and then back to | 102 * Converts |value| to user's locale units and then back to points. Note: |
87 * points. Note: Because of the precision the return value might be different | 103 * Because of the precision the return value might be different than |value|. |
88 * than |value|. | |
89 * @param {number} value The value in points to convert. | 104 * @param {number} value The value in points to convert. |
90 * @return {number} The value in points after converting to inches with a | 105 * @return {number} The value in points after converting to user's locale |
91 * certain precision and back. | 106 * units with a certain precision and back. |
92 */ | 107 */ |
93 function convertPointsToInchesTextAndBack(value) { | 108 function convertPointsToLocaleUnitsAndBack(value) { |
94 var text = convertPointsToInchesText(value); | 109 var inLocaleUnits = |
95 var inches = extractMarginValue(text); | 110 convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision()); |
96 return convertInchesToPoints(inches); | 111 return convertLocaleUnitsToPoints(inLocaleUnits); |
97 } | 112 } |
98 | 113 |
99 /** | 114 /** |
| 115 * @return {number} The number of decimal digits to keep based on the |
| 116 * measurement system used. |
| 117 */ |
| 118 function getDesiredPrecision() { |
| 119 return print_preview.MarginSettings.useMetricSystem ? 0 : 2; |
| 120 } |
| 121 |
| 122 /** |
| 123 * @param {number} toConvert The value to convert in points. |
| 124 * @return {string} The equivalent text in user locale units with precision |
| 125 * of two digits. |
| 126 */ |
| 127 function convertPointsToLocaleUnitsText(value) { |
| 128 var inLocaleUnits = |
| 129 convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision()); |
| 130 var inLocaleUnitsText = inLocaleUnits.toString(10).replace( |
| 131 /\./g, print_preview.MarginSettings.decimalPoint); |
| 132 return !print_preview.MarginSettings.useMetricSystem ? |
| 133 inLocaleUnitsText + '"' : inLocaleUnitsText + 'mm'; |
| 134 } |
| 135 |
| 136 /** |
100 * Creates a Rect object. This object describes a rectangle in a 2D plane. The | 137 * Creates a Rect object. This object describes a rectangle in a 2D plane. The |
101 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. | 138 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. |
102 * @constructor | 139 * @constructor |
103 */ | 140 */ |
104 function Rect(x, y, width, height) { | 141 function Rect(x, y, width, height) { |
105 // @type {number} Horizontal distance of the upper left corner from origin. | 142 // @type {number} Horizontal distance of the upper left corner from origin. |
106 this.x = x; | 143 this.x = x; |
107 // @type {number} Vertical distance of the upper left corner from origin. | 144 // @type {number} Vertical distance of the upper left corner from origin. |
108 this.y = y; | 145 this.y = y; |
109 // @type {number} Width of |this| rectangle. | 146 // @type {number} Width of |this| rectangle. |
(...skipping 20 matching lines...) Expand all Loading... |
130 /** | 167 /** |
131 * Clones |this| and returns the cloned object. | 168 * Clones |this| and returns the cloned object. |
132 * @return {Rect} A copy of |this|. | 169 * @return {Rect} A copy of |this|. |
133 */ | 170 */ |
134 clone: function() { | 171 clone: function() { |
135 return new Rect(this.x, this.y, this.width, this.height); | 172 return new Rect(this.x, this.y, this.width, this.height); |
136 } | 173 } |
137 }; | 174 }; |
138 | 175 |
139 return { | 176 return { |
140 convertInchesToInchesText: convertInchesToInchesText, | 177 convertPointsToLocaleUnitsAndBack: |
141 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, | 178 convertPointsToLocaleUnitsAndBack, |
142 convertPointsToInchesText: convertPointsToInchesText, | 179 convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText, |
| 180 convertLocaleUnitsToPoints: convertLocaleUnitsToPoints, |
143 extractMarginValue: extractMarginValue, | 181 extractMarginValue: extractMarginValue, |
144 marginValidationStates: marginValidationStates, | 182 marginValidationStates: marginValidationStates, |
145 Rect: Rect, | 183 Rect: Rect, |
146 validateMarginText: validateMarginText, | 184 validateMarginText: validateMarginText, |
147 validateMarginValue: validateMarginValue | 185 validateMarginValue: validateMarginValue, |
148 }; | 186 }; |
149 }); | 187 }); |
OLD | NEW |