OLD | NEW |
---|---|
(Empty) | |
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 | |
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 {boolean} true if the |text| represents a valid margin value | |
17 */ | |
18 function extractMarginValue(text) { | |
19 // Remove whitespace anywhere in the string. | |
20 text.replace(/\s*/g, ''); | |
21 // TODO(dpapad): Return -1 instead of null to indicate an invalid value. | |
Evan Stade
2011/10/05 22:42:14
is this todo still valid? what's wrong with what i
dpapad
2011/10/06 00:05:39
This TODO is not necessary, i just though it is be
| |
22 if (text.length == 0) | |
23 return null; | |
24 // Remove the inch(") symbol at end of string if present. | |
25 if (text.charAt(text.length - 1) == '\"') | |
26 text = text.slice(0, text.length - 1); | |
27 var regex = /^\d*(\.\d+)?$/ | |
28 if (regex.test(text)) | |
29 return text; | |
30 return null; | |
31 } | |
32 | |
33 /** | |
34 * @param {sting} text The text to check (in inches). | |
35 * @param {number} limit The upper bound of the valid margin range (in | |
36 * points). | |
37 * @return {boolean} True of |text| can be parsed and it is within the allowed | |
38 * range. | |
39 */ | |
40 function isMarginTextValid(text, limit) { | |
41 var value = extractMarginValue(text); | |
42 if (value == null) | |
43 return false; | |
44 value = print_preview.MarginSettings.convertInchesToPoints(value); | |
45 return value <= limit; | |
46 } | |
47 | |
48 /** | |
49 * Creates a Rect object. This object describes a rectangle in a 2D plane. | |
Evan Stade
2011/10/05 22:42:14
here is where you would mention that the units can
dpapad
2011/10/06 00:05:39
Done.
| |
50 * @constructor | |
51 */ | |
52 function Rect(x, y, width, height) { | |
53 this.x_ = x; | |
54 this.y_ = y; | |
55 this.width_ = width; | |
56 this.height_ = height; | |
57 }; | |
58 | |
59 Rect.prototype = { | |
60 get x() { | |
Evan Stade
2011/10/05 22:42:14
remove the getters you don't need (all but origin)
dpapad
2011/10/06 00:05:39
Done. Removed origin too since it is not used in t
| |
61 return this.x_; | |
62 }, | |
63 get y() { | |
64 return this.y_; | |
65 }, | |
66 get origin() { | |
67 return this.origin_; | |
68 }, | |
69 get width() { | |
70 return this.width_; | |
71 }, | |
72 get height() { | |
73 return this.height_; | |
74 }, | |
75 }; | |
76 | |
77 return { | |
78 extractMarginValue: extractMarginValue, | |
79 isMarginTextValid: isMarginTextValid, | |
80 Rect: Rect, | |
81 }; | |
82 }); | |
OLD | NEW |