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.
5 cr.define('print_preview', function() {
9 * Creates a Margins object that holds four margin values in points.
10 * @param {number} top The top margin in pts.
11 * @param {number} right The right margin in pts.
12 * @param {number} bottom The bottom margin in pts.
13 * @param {number} left The left margin in pts.
16 function Margins(top, right, bottom, left) {
18 * Backing store for the margin values in points.
20 * !print_preview.ticket_items.CustomMargins.Orientation, number>}
24 this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top;
25 this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] =
27 this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] =
29 this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] =
34 * Parses a margins object from the given serialized state.
35 * @param {Object} state Serialized representation of the margins created by
36 * the {@code serialize} method.
37 * @return {!print_preview.Margins} New margins instance.
39 Margins.parse = function(state) {
40 return new print_preview.Margins(
41 state[print_preview.ticket_items.CustomMargins.Orientation.TOP] || 0,
42 state[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] || 0,
43 state[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] || 0,
44 state[print_preview.ticket_items.CustomMargins.Orientation.LEFT] || 0);
49 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
50 * orientation Specifies the margin value to get.
51 * @return {number} Value of the margin of the given orientation.
53 get: function(orientation) {
54 return this.value_[orientation];
58 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
59 * orientation Specifies the margin to set.
60 * @param {number} value Updated value of the margin in points to modify.
61 * @return {!print_preview.Margins} A new copy of |this| with the
62 * modification made to the specified margin.
64 set: function(orientation, value) {
65 var newValue = this.clone_();
66 newValue[orientation] = value;
68 newValue[print_preview.ticket_items.CustomMargins.Orientation.TOP],
69 newValue[print_preview.ticket_items.CustomMargins.Orientation.RIGHT],
70 newValue[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM],
71 newValue[print_preview.ticket_items.CustomMargins.Orientation.LEFT]);
75 * @param {print_preview.Margins} other The other margins object to compare
77 * @return {boolean} Whether this margins object is equal to another.
79 equals: function(other) {
83 for (var orientation in this.value_) {
84 if (this.value_[orientation] != other.value_[orientation]) {
91 /** @return {Object} A serialized representation of the margins. */
92 serialize: function() {
97 * @return {Object} Cloned state of the margins.
102 for (var o in this.value_) {
103 clone[o] = this.value_[o];