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. | |
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 // TODO(dpapad): convert |value| to number before comparing. | |
Evan Stade
2011/10/05 03:05:23
I don't understand this comment, if value is not a
dpapad
2011/10/05 16:39:49
It is already of type number. The comment is obsol
| |
46 return value <= limit; | |
47 } | |
48 | |
49 /** | |
50 * Creates a Point object. This object describes a point in a 2D plane. | |
51 * @constructor | |
52 */ | |
53 function Point(x, y) { | |
Evan Stade
2011/10/05 03:05:23
what is the point of point? why not just do var po
dpapad
2011/10/05 16:39:49
Having a class instead of creating an object on th
| |
54 this.x_ = x; | |
55 this.y_ = y; | |
56 }; | |
57 | |
58 Point.prototype = { | |
59 get x() { | |
60 return this.x_; | |
61 }, | |
62 | |
63 get y() { | |
64 return this.y_; | |
65 }, | |
66 | |
67 set x(x) { | |
68 this.x_ = x;; | |
69 }, | |
70 | |
71 set y(y) { | |
72 return this.y_ = y; | |
73 } | |
74 }; | |
75 | |
76 /** | |
77 * Creates a Rect object. This object describes a rectangle in a 2D plane. | |
78 * @constructor | |
79 */ | |
80 function Rect(x, y, width, height) { | |
81 this.origin_ = new Point(x, y); | |
82 this.width_ = width; | |
83 this.height_ = height; | |
84 }; | |
85 | |
86 Rect.prototype = { | |
87 get x() { | |
88 return this.origin_.x; | |
89 }, | |
90 get y() { | |
91 return this.origin_.y; | |
92 }, | |
93 get origin() { | |
94 return this.origin_; | |
95 }, | |
96 get width() { | |
97 return this.width_; | |
98 }, | |
99 get height() { | |
100 return this.height_; | |
101 }, | |
102 }; | |
103 | |
104 return { | |
105 extractMarginValue: extractMarginValue, | |
106 isMarginTextValid: isMarginTextValid, | |
107 Point: Point, | |
108 Rect: Rect, | |
109 }; | |
110 }); | |
OLD | NEW |