Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: chrome/browser/resources/print_preview/margin_utils.js

Issue 8345025: Print Preview: Adding support for localized margin units and format. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Localizing units used for margins. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.convertUserLocaleUnitsToPoints(value);
64 return validateMarginValue(value, limit); 79 return validateMarginValue(value, limit);
65 } 80 }
66 81
67 /** 82 function convertPointsToUserLocaleUnits(value) {
68 * @param {number} toConvert The value to convert in points 83 if (!print_preview.MarginSettings.useMetricSystem)
69 * @return {string} The equivalent text in inches. 84 return convertPointsToInches(value);
70 */ 85 else
71 function convertPointsToInchesText(toConvert) { 86 return convertPointsToMillimeters(value);
72 var inInches = convertPointsToInches(toConvert); 87 }
73 return convertInchesToInchesText(inInches); 88
89 function convertUserLocaleUnitsToPoints(value) {
90 if (!print_preview.MarginSettings.useMetricSystem)
91 return convertInchesToPoints(value);
92 else
93 return convertMillimetersToPoints(value);
74 } 94 }
75 95
76 /** 96 /**
77 * @param {number} toConvert The value to convert in inches. 97 * Converts |value| to user's locale units and then back to points. Note:
78 * @return {string} The equivalent text in inches with precision of two 98 * Because of the precision the return value might be different than |value|.
79 * digits. 99 * @param {number} value The value in points to convert.
100 * @return {number} The value in points after converting to user's locale
101 * units with a certain precision and back.
80 */ 102 */
81 function convertInchesToInchesText(toConvert) { 103 function convertPointsToUserLocaleUnitsAndBack(value) {
82 return toConvert.toFixed(2) + '"'; 104 var inUserLocaleUnits =
105 convertPointsToUserLocaleUnits(value).toFixed(getDesiredPrecision());
106 return convertUserLocaleUnitsToPoints(inUserLocaleUnits);
83 } 107 }
84 108
85 /** 109 /**
86 * Converts |value| to inches text (keeping 2 decimal digits) and then back to 110 * @return {number} The number of decimal digits to keep based on the
87 * points. Note: Because of the precision the return value might be different 111 * measurement system used.
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 */ 112 */
93 function convertPointsToInchesTextAndBack(value) { 113 function getDesiredPrecision() {
94 var text = convertPointsToInchesText(value); 114 return print_preview.MarginSettings.useMetricSystem ? 0 : 2;
95 var inches = extractMarginValue(text);
96 return convertInchesToPoints(inches);
97 } 115 }
98 116
99 /** 117 /**
118 * @param {number} toConvert The value to convert in points.
119 * @return {string} The equivalent text in user locale units with precision
120 * of two digits.
121 */
122 function convertPointsToUserLocaleUnitsText(value) {
123 var inUserLocaleUnits =
124 convertPointsToUserLocaleUnits(value).toFixed(getDesiredPrecision());
125 var inUserLocaleUnitsText = inUserLocaleUnits.toString(10).replace(
126 /\./g, print_preview.MarginSettings.decimalPoint);
127 return !print_preview.MarginSettings.useMetricSystem ?
128 inUserLocaleUnitsText + '"' : inUserLocaleUnitsText + 'mm';
129 }
130
131 /**
100 * Creates a Rect object. This object describes a rectangle in a 2D plane. The 132 * 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. 133 * units of |x|, |y|, |width|, |height| are chosen by clients of this class.
102 * @constructor 134 * @constructor
103 */ 135 */
104 function Rect(x, y, width, height) { 136 function Rect(x, y, width, height) {
105 // @type {number} Horizontal distance of the upper left corner from origin. 137 // @type {number} Horizontal distance of the upper left corner from origin.
106 this.x = x; 138 this.x = x;
107 // @type {number} Vertical distance of the upper left corner from origin. 139 // @type {number} Vertical distance of the upper left corner from origin.
108 this.y = y; 140 this.y = y;
109 // @type {number} Width of |this| rectangle. 141 // @type {number} Width of |this| rectangle.
(...skipping 20 matching lines...) Expand all
130 /** 162 /**
131 * Clones |this| and returns the cloned object. 163 * Clones |this| and returns the cloned object.
132 * @return {Rect} A copy of |this|. 164 * @return {Rect} A copy of |this|.
133 */ 165 */
134 clone: function() { 166 clone: function() {
135 return new Rect(this.x, this.y, this.width, this.height); 167 return new Rect(this.x, this.y, this.width, this.height);
136 } 168 }
137 }; 169 };
138 170
139 return { 171 return {
140 convertInchesToInchesText: convertInchesToInchesText, 172 convertPointsToUserLocaleUnitsAndBack:
141 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack, 173 convertPointsToUserLocaleUnitsAndBack,
142 convertPointsToInchesText: convertPointsToInchesText, 174 convertPointsToUserLocaleUnitsText: convertPointsToUserLocaleUnitsText,
175 convertUserLocaleUnitsToPoints: convertUserLocaleUnitsToPoints,
143 extractMarginValue: extractMarginValue, 176 extractMarginValue: extractMarginValue,
144 marginValidationStates: marginValidationStates, 177 marginValidationStates: marginValidationStates,
145 Rect: Rect, 178 Rect: Rect,
146 validateMarginText: validateMarginText, 179 validateMarginText: validateMarginText,
147 validateMarginValue: validateMarginValue 180 validateMarginValue: validateMarginValue,
181 getValidationRegExp: getValidationRegExp,
148 }; 182 };
149 }); 183 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698