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 * Creates a Margins object that holds four margin values. The units in which |
| 10 * the values are expressed can be any numeric value. |
| 11 * @param {number} top The top margin. |
| 12 * @param {number} right The right margin. |
| 13 * @param {number} bottom The bottom margin. |
| 14 * @param {number} left The left margin. |
| 15 * @param {print_preview.MeasurementSystem} measurementSystem The measurement |
| 16 * system. |
| 17 * @constructor |
| 18 */ |
| 19 function Margins(top, right, bottom, left, measurementSystem) { |
| 20 this[Margins.TOP_GROUP] = top; |
| 21 this[Margins.RIGHT_GROUP] = right; |
| 22 this[Margins.BOTTOM_GROUP] = bottom; |
| 23 this[Margins.LEFT_GROUP] = left; |
| 24 this.measurementSystem_ = measurementSystem; |
| 25 }; |
| 26 |
| 27 /** |
| 28 * Enumeration of margin types. Matches enum MarginType in |
| 29 * printing/print_job_constants.h. |
| 30 * @enum {number} |
| 31 */ |
| 32 Margins.Type = { |
| 33 DEFAULT: 0, |
| 34 NO_MARGINS: 1, |
| 35 MINIMUM: 2, |
| 36 CUSTOM: 3 |
| 37 }, |
| 38 |
| 39 // Group name corresponding to the top margin. |
| 40 Margins.TOP_GROUP = 'top'; |
| 41 // Group name corresponding to the left margin. |
| 42 Margins.LEFT_GROUP = 'left'; |
| 43 // Group name corresponding to the right margin. |
| 44 Margins.RIGHT_GROUP = 'right'; |
| 45 // Group name corresponding to the bottom margin. |
| 46 Margins.BOTTOM_GROUP = 'bottom'; |
| 47 |
| 48 /** |
| 49 * Rounds |value| keeping |precision| decimal numbers. Example: 32.76643 |
| 50 * becomes 32.77. |
| 51 * @param {number} value The number to round. |
| 52 * @param {number} precision The desired precision. |
| 53 * @return {number} The rounded number. |
| 54 */ |
| 55 Margins.roundToPrecision = function(value, precision) { |
| 56 return Math.round(value * Math.pow(10, precision)) / |
| 57 Math.pow(10, precision); |
| 58 }; |
| 59 |
| 60 Margins.prototype = { |
| 61 /** |
| 62 * Checks if |rhs| is equal to |this|. |
| 63 * @param {Margins} rhs The Margins object to compare against. |
| 64 * @return {boolean} true if they are equal. |
| 65 */ |
| 66 isEqual: function(rhs) { |
| 67 if (!rhs) |
| 68 return false; |
| 69 return this[Margins.TOP_GROUP] === rhs[Margins.TOP_GROUP] && |
| 70 this[Margins.LEFT_GROUP] === rhs[Margins.LEFT_GROUP] && |
| 71 this[Margins.RIGHT_GROUP] === |
| 72 rhs[Margins.RIGHT_GROUP] && |
| 73 this[Margins.BOTTOM_GROUP] === |
| 74 rhs[Margins.BOTTOM_GROUP]; |
| 75 }, |
| 76 |
| 77 clone: function() { |
| 78 return new Margins(this[Margins.LEFT_GROUP], |
| 79 this[Margins.TOP_GROUP], |
| 80 this[Margins.RIGHT_GROUP], |
| 81 this[Margins.BOTTOM_GROUP]); |
| 82 }, |
| 83 |
| 84 /** |
| 85 * Helper method returning an array of the string indices used for accessing |
| 86 * all margins. |
| 87 * @return {array} An array of string indices. |
| 88 * @private |
| 89 */ |
| 90 indicesAsArray_: function() { |
| 91 return [Margins.LEFT_GROUP, Margins.TOP_GROUP, |
| 92 Margins.RIGHT_GROUP, Margins.BOTTOM_GROUP]; |
| 93 }, |
| 94 |
| 95 /** |
| 96 * Rounds |this| based on the precision used when displaying the margins in |
| 97 * the user's prefered units. This is done by converting from points to |
| 98 * those units (mm/inches) and back to points. |
| 99 */ |
| 100 roundToLocaleUnits: function() { |
| 101 var indicesAsArray = this.indicesAsArray_(); |
| 102 for (var i = 0; i < indicesAsArray.length; i++) { |
| 103 this[indicesAsArray[i]] = |
| 104 print_preview.convertPointsToLocaleUnitsAndBack( |
| 105 this[indicesAsArray[i]]); |
| 106 } |
| 107 } |
| 108 }; |
| 109 |
| 110 // Export |
| 111 return { |
| 112 Margins: Margins |
| 113 }; |
| 114 }); |
OLD | NEW |