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

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

Issue 8233030: Print Preview: Making margin lines draggable. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing rounding error corner case. 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.
(...skipping 13 matching lines...) Expand all
24 // Remove the inch(") symbol at end of string if present. 24 // Remove the inch(") symbol at end of string if present.
25 if (text.charAt(text.length - 1) == '\"') 25 if (text.charAt(text.length - 1) == '\"')
26 text = text.slice(0, text.length - 1); 26 text = text.slice(0, text.length - 1);
27 var regex = /^\d*(\.\d+)?$/ 27 var regex = /^\d*(\.\d+)?$/
28 if (regex.test(text)) 28 if (regex.test(text))
29 return parseFloat(text); 29 return parseFloat(text);
30 return -1; 30 return -1;
31 } 31 }
32 32
33 /** 33 /**
34 * Checks whether |value| is within range [0, limit].
35 * @return {number} 0 if within the range, -1 if smaller, 1 if larger.
36 */
37 function validateMarginValue(value, limit) {
38 if (value <= limit && value >= 0)
39 return 0;
40 else if (value < 0)
41 return -1;
42 else (value > limit)
43 return 1;
44 }
45
46 /**
34 * @param {sting} text The text to check (in inches). 47 * @param {sting} text The text to check (in inches).
35 * @param {number} limit The upper bound of the valid margin range (in 48 * @param {number} limit The upper bound of the valid margin range (in
36 * points). 49 * points).
37 * @return {boolean} True of |text| can be parsed and it is within the allowed 50 * @return {number} -2 if |text| can't be converted to number, 0 if it
38 * range. 51 * represents a valid margin value, -1 if the value is less than the
52 * minimum margin, 1 if the value is larger than the maximum margin.
39 */ 53 */
40 function isMarginTextValid(text, limit) { 54 function validateMarginText(text, limit) {
41 var value = extractMarginValue(text); 55 var value = extractMarginValue(text);
42 if (value == -1) 56 if (value == -1)
43 return false; 57 return -2;
44 value = convertInchesToPoints(value); 58 value = convertInchesToPoints(value);
45 return value <= limit; 59 return validateMarginValue(value, limit);
46 } 60 }
47 61
48 /** 62 /**
63 * @param {number} toConvert The value to convert in points
64 * @return {string} The equivalent text in inches.
65 */
66 function convertPointsToInchesText(toConvert) {
67 var inInches = convertPointsToInches(toConvert);
68 return convertInchesToInchesText(inInches);
69 }
70
71 /**
72 * @param {number} toConvert The value to convert in inches.
73 * @return {string} The equivalent text in inches with precision of two
74 * digits.
75 */
76 function convertInchesToInchesText(toConvert) {
77 return toConvert.toFixed(2) + '"';
78 }
79
80 /**
81 * Converts |value| to inches text (keeping 2 decimal digits) and then back to
82 * points. Note: Because of the precision the return value might be different
83 * than |value|.
84 * @param {number} value The value in points to convert.
85 * @return {number} The value in points after converting to inches with a
86 * certain precision and back.
87 */
88 function convertPointsToInchesTextAndBack(value) {
89 var text = convertPointsToInchesText(value);
90 var inches = extractMarginValue(text);
91 return convertInchesToPoints(inches);
92 }
93
94
95 function Point(x, y) {
96 this.x = x;
97 this.y = y;
98 }
99
100 /**
49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The 101 * Creates a Rect object. This object describes a rectangle in a 2D plane. The
50 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. 102 * units of |x|, |y|, |width|, |height| are chosen by clients of this class.
51 * @constructor 103 * @constructor
52 */ 104 */
53 function Rect(x, y, width, height) { 105 function Rect(x, y, width, height) {
54 // @type {number} Horizontal distance of the upper left corner from origin. 106 // @type {number} Horizontal distance of the upper left corner from origin.
55 this.x = x; 107 this.x = x;
56 // @type {number} Vertical distance of the upper left corner from origin. 108 // @type {number} Vertical distance of the upper left corner from origin.
57 this.y = y; 109 this.y = y;
58 // @type {number} Width of |this| rectangle. 110 // @type {number} Width of |this| rectangle.
59 this.width = width; 111 this.width = width;
60 // @type {number} Height of |this| rectangle. 112 // @type {number} Height of |this| rectangle.
61 this.height = height; 113 this.height = height;
62 }; 114 };
63 115
64 Rect.prototype = { 116 Rect.prototype = {
117 /**
118 * @type {number} The x coordinate of the right-most point.
119 */
65 get right() { 120 get right() {
66 return this.x + this.width; 121 return this.x + this.width;
67 }, 122 },
68 123
124 /**
125 * @type {number} The y coordinate of the lower-most point.
126 */
69 get bottom() { 127 get bottom() {
70 return this.y + this.height; 128 return this.y + this.height;
71 }, 129 },
72 130
73 get middleX() { 131 /**
74 return this.x + this.width / 2; 132 * Clones |this| and returns the cloned object.
75 }, 133 * @return {Rect} A copy of |this|.
76 134 */
77 get middleY() { 135 clone: function() {
78 return this.y + this.height / 2; 136 return new Rect(this.x, this.y, this.width, this.height);
79 } 137 }
80 }; 138 };
81 139
82 return { 140 return {
141 convertInchesToInchesText: convertInchesToInchesText,
142 convertPointsToInchesTextAndBack:convertPointsToInchesTextAndBack,
143 convertPointsToInchesText: convertPointsToInchesText,
83 extractMarginValue: extractMarginValue, 144 extractMarginValue: extractMarginValue,
84 isMarginTextValid: isMarginTextValid, 145 Point: Point,
85 Rect: Rect, 146 Rect: Rect,
147 validateMarginText: validateMarginText,
148 validateMarginValue: validateMarginValue
86 }; 149 };
87 }); 150 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698