OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 * @param {print_preview.MeasurementSystem} measurementSystem Used to handle |
| 17 * parsing local units. |
16 * @return {number} The margin value represented by |text| or null if |text| | 18 * @return {number} The margin value represented by |text| or null if |text| |
17 * does not represent a valid number. | 19 * does not represent a valid number. |
18 */ | 20 */ |
19 function extractMarginValue(text) { | 21 function extractMarginValue(text, measurementSystem) { |
20 // Removing whitespace anywhere in the string. | 22 // Removing whitespace anywhere in the string. |
21 text = text.replace(/\s*/g, ''); | 23 text = text.replace(/\s*/g, ''); |
22 if (text.length == 0) | 24 if (text.length == 0) |
23 return -1; | 25 return -1; |
24 var validationRegex = getValidationRegExp(); | 26 var validationRegex = getValidationRegExp(measurementSystem); |
25 if (validationRegex.test(text)) { | 27 if (validationRegex.test(text)) { |
26 // Replacing decimal point with the dot symbol in order to use | 28 // Replacing decimal point with the dot symbol in order to use |
27 // parseFloat() properly. | 29 // parseFloat() properly. |
28 var replacementRegex = new RegExp('\\' + | 30 var replacementRegex = |
29 print_preview.MarginSettings.decimalPoint + '{1}'); | 31 new RegExp('\\' + measurementSystem.decimalDelimeter + '{1}'); |
30 text = text.replace(replacementRegex, '.'); | 32 text = text.replace(replacementRegex, '.'); |
31 return parseFloat(text); | 33 return parseFloat(text); |
32 } | 34 } |
33 return -1; | 35 return -1; |
34 } | 36 } |
35 | 37 |
36 /** | 38 /** |
| 39 * @param {print_preview.MeasurementSystem} measurementSystem Contains the |
| 40 * delimeters used in this system. |
37 * @return {RegExp} A regular expression for validating the input of the user. | 41 * @return {RegExp} A regular expression for validating the input of the user. |
38 * It takes into account the user's locale. | 42 * It takes into account the user's locale. |
39 */ | 43 */ |
40 function getValidationRegExp() { | 44 function getValidationRegExp(measurementSystem) { |
41 var regex = new RegExp('(^\\d+)(\\' + | 45 var regex = new RegExp('(^\\d+)(\\' + |
42 print_preview.MarginSettings.thousandsPoint + '\\d{3})*(\\' + | 46 measurementSystem.thousandsDelimeter + '\\d{3})*(\\' + |
43 print_preview.MarginSettings.decimalPoint + '\\d+)*' + | 47 measurementSystem.decimalDelimeter + '\\d+)*' + |
44 (!print_preview.MarginSettings.useMetricSystem ? '"?' : '(mm)?') + '$'); | 48 '(' + measurementSystem.unitSymbol + ')?' + '$'); |
45 return regex; | 49 return regex; |
46 } | 50 } |
47 | 51 |
48 var marginValidationStates = { | 52 var marginValidationStates = { |
49 TOO_SMALL: 0, | 53 TOO_SMALL: 0, |
50 WITHIN_RANGE: 1, | 54 WITHIN_RANGE: 1, |
51 TOO_BIG: 2, | 55 TOO_BIG: 2, |
52 NOT_A_NUMBER: 3 | 56 NOT_A_NUMBER: 3 |
53 }; | 57 }; |
54 | 58 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 convertPointsToLocaleUnitsAndBack, | 182 convertPointsToLocaleUnitsAndBack, |
179 convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText, | 183 convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText, |
180 convertLocaleUnitsToPoints: convertLocaleUnitsToPoints, | 184 convertLocaleUnitsToPoints: convertLocaleUnitsToPoints, |
181 extractMarginValue: extractMarginValue, | 185 extractMarginValue: extractMarginValue, |
182 marginValidationStates: marginValidationStates, | 186 marginValidationStates: marginValidationStates, |
183 Rect: Rect, | 187 Rect: Rect, |
184 validateMarginText: validateMarginText, | 188 validateMarginText: validateMarginText, |
185 validateMarginValue: validateMarginValue | 189 validateMarginValue: validateMarginValue |
186 }; | 190 }; |
187 }); | 191 }); |
OLD | NEW |