OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 cr.define('print_preview', function() { | |
6 'use strict'; | |
7 | |
8 /** | |
9 * Checks if |text| has a valid margin value format. A valid format is | |
10 * parsable as a number and is greater than zero. | |
11 * Example: "1.00", "1", ".5", "1.1" are valid values. | |
12 * Example: "1.4dsf", "-1" are invalid. | |
13 * Note: The inch symbol (") at the end of |text| is allowed. | |
14 * | |
15 * @param {string} text The text to check. | |
16 * @return {number} The margin value represented by |text| or null if |text| | |
17 * does not represent a valid number. | |
18 */ | |
19 function extractMarginValue(text) { | |
20 // Removing whitespace anywhere in the string. | |
21 text = text.replace(/\s*/g, ''); | |
22 if (text.length == 0) | |
23 return -1; | |
24 var validationRegex = getValidationRegExp(); | |
25 if (validationRegex.test(text)) { | |
26 // Replacing decimal point with the dot symbol in order to use | |
27 // parseFloat() properly. | |
28 var replacementRegex = new RegExp('\\' + | |
29 print_preview.MarginSettings.decimalPoint + '{1}'); | |
30 text = text.replace(replacementRegex, '.'); | |
31 return parseFloat(text); | |
32 } | |
33 return -1; | |
34 } | |
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 | |
48 var marginValidationStates = { | |
49 TOO_SMALL: 0, | |
50 WITHIN_RANGE: 1, | |
51 TOO_BIG: 2, | |
52 NOT_A_NUMBER: 3 | |
53 }; | |
54 | |
55 /** | |
56 * Checks whether |value| is within range [0, limit]. | |
57 * @return {number} An appropriate value from enum |marginValidationStates|. | |
58 */ | |
59 function validateMarginValue(value, limit) { | |
60 if (value <= limit && value >= 0) | |
61 return marginValidationStates.WITHIN_RANGE; | |
62 else if (value < 0) | |
63 return marginValidationStates.TOO_SMALL; | |
64 else | |
65 return marginValidationStates.TOO_BIG; | |
66 } | |
67 | |
68 /** | |
69 * @param {string} text The text to check in user's locale units. | |
70 * @param {number} limit The upper bound of the valid margin range (in | |
71 * points). | |
72 * @return {number} An appropriate value from enum |marginValidationStates|. | |
73 */ | |
74 function validateMarginText(text, limit) { | |
75 var value = extractMarginValue(text); | |
76 if (value == -1) | |
77 return marginValidationStates.NOT_A_NUMBER; | |
78 value = print_preview.convertLocaleUnitsToPoints(value); | |
79 return validateMarginValue(value, limit); | |
80 } | |
81 | |
82 /** | |
83 * @param {number} value The value to convert in points. | |
84 * @return {number} The corresponding value after converting to user's locale | |
85 * units. | |
86 */ | |
87 function convertPointsToLocaleUnits(value) { | |
88 return print_preview.MarginSettings.useMetricSystem ? | |
89 convertPointsToMillimeters(value) : convertPointsToInches(value); | |
90 } | |
91 | |
92 /** | |
93 * @param {number} value The value to convert in user's locale units. | |
94 * @return {number} The corresponding value after converting to points. | |
95 */ | |
96 function convertLocaleUnitsToPoints(value) { | |
97 return print_preview.MarginSettings.useMetricSystem ? | |
98 convertMillimetersToPoints(value) : convertInchesToPoints(value); | |
99 } | |
100 | |
101 /** | |
102 * Converts |value| to user's locale units and then back to points. Note: | |
103 * Because of the precision the return value might be different than |value|. | |
104 * @param {number} value The value in points to convert. | |
105 * @return {number} The value in points after converting to user's locale | |
106 * units with a certain precision and back. | |
107 */ | |
108 function convertPointsToLocaleUnitsAndBack(value) { | |
109 var inLocaleUnits = | |
110 convertPointsToLocaleUnits(value).toFixed(getDesiredPrecision()); | |
111 return convertLocaleUnitsToPoints(inLocaleUnits); | |
112 } | |
113 | |
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} value 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 /** | |
137 * Creates a Rect object. This object describes a rectangle in a 2D plane. The | |
138 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. | |
139 * @constructor | |
140 */ | |
141 function Rect(x, y, width, height) { | |
142 // @type {number} Horizontal distance of the upper left corner from origin. | |
143 this.x = x; | |
144 // @type {number} Vertical distance of the upper left corner from origin. | |
145 this.y = y; | |
146 // @type {number} Width of |this| rectangle. | |
147 this.width = width; | |
148 // @type {number} Height of |this| rectangle. | |
149 this.height = height; | |
150 }; | |
151 | |
152 Rect.prototype = { | |
153 /** | |
154 * @type {number} The x coordinate of the right-most point. | |
155 */ | |
156 get right() { | |
157 return this.x + this.width; | |
158 }, | |
159 | |
160 /** | |
161 * @type {number} The y coordinate of the lower-most point. | |
162 */ | |
163 get bottom() { | |
164 return this.y + this.height; | |
165 }, | |
166 | |
167 /** | |
168 * Clones |this| and returns the cloned object. | |
169 * @return {Rect} A copy of |this|. | |
170 */ | |
171 clone: function() { | |
172 return new Rect(this.x, this.y, this.width, this.height); | |
173 } | |
174 }; | |
175 | |
176 return { | |
177 convertPointsToLocaleUnitsAndBack: | |
178 convertPointsToLocaleUnitsAndBack, | |
179 convertPointsToLocaleUnitsText: convertPointsToLocaleUnitsText, | |
180 convertLocaleUnitsToPoints: convertLocaleUnitsToPoints, | |
181 extractMarginValue: extractMarginValue, | |
182 marginValidationStates: marginValidationStates, | |
183 Rect: Rect, | |
184 validateMarginText: validateMarginText, | |
185 validateMarginValue: validateMarginValue | |
186 }; | |
187 }); | |
OLD | NEW |