Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / margins.js
blob2b5497151c852f1b7e677f0c701d0404ccd9dfff
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() {
6 'use strict';
8 /**
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.
14 * @constructor
16 function Margins(top, right, bottom, left) {
17 /**
18 * Backing store for the margin values in points.
19 * @type {!Object.<
20 * !print_preview.ticket_items.CustomMargins.Orientation, number>}
21 * @private
23 this.value_ = {};
24 this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top;
25 this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] =
26 right;
27 this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] =
28 bottom;
29 this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] =
30 left;
33 /**
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);
47 Margins.prototype = {
48 /**
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];
57 /**
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;
67 return new Margins(
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]);
74 /**
75 * @param {print_preview.Margins} other The other margins object to compare
76 * against.
77 * @return {boolean} Whether this margins object is equal to another.
79 equals: function(other) {
80 if (other == null) {
81 return false;
83 for (var orientation in this.value_) {
84 if (this.value_[orientation] != other.value_[orientation]) {
85 return false;
88 return true;
91 /** @return {Object} A serialized representation of the margins. */
92 serialize: function() {
93 return this.clone_();
96 /**
97 * @return {Object} Cloned state of the margins.
98 * @private
100 clone_: function() {
101 var clone = {};
102 for (var o in this.value_) {
103 clone[o] = this.value_[o];
105 return clone;
109 // Export
110 return {
111 Margins: Margins