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

Side by Side Diff: chrome/browser/resources/print_preview/data/margins.js

Issue 10909124: Improves application state persistance. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Updates unit tests. Created 8 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 * Creates a Margins object that holds four margin values in points. 9 * Creates a Margins object that holds four margin values in points.
10 * @param {number} top The top margin in pts. 10 * @param {number} top The top margin in pts.
(...skipping 12 matching lines...) Expand all
23 this.value_ = {}; 23 this.value_ = {};
24 this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top; 24 this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top;
25 this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] = 25 this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] =
26 right; 26 right;
27 this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] = 27 this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] =
28 bottom; 28 bottom;
29 this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] = 29 this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] =
30 left; 30 left;
31 }; 31 };
32 32
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.
38 */
39 Margins.parse = function(state) {
40 return new print_preview.Margins(
41 state[print_preview.ticket_items.CustomMargins.Orientation.TOP],
42 state[print_preview.ticket_items.CustomMargins.Orientation.RIGHT],
43 state[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM],
44 state[print_preview.ticket_items.CustomMargins.Orientation.LEFT]);
45 };
46
33 Margins.prototype = { 47 Margins.prototype = {
34 /** 48 /**
35 * @param {!print_preview.ticket_items.CustomMargins.Orientation} 49 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
36 * orientation Specifies the margin value to get. 50 * orientation Specifies the margin value to get.
37 * @return {number} Value of the margin of the given orientation. 51 * @return {number} Value of the margin of the given orientation.
38 */ 52 */
39 get: function(orientation) { 53 get: function(orientation) {
40 return this.value_[orientation]; 54 return this.value_[orientation];
41 }, 55 },
42 56
43 /** 57 /**
44 * @param {!print_preview.ticket_items.CustomMargins.Orientation} 58 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
45 * orientation Specifies the margin to set. 59 * orientation Specifies the margin to set.
46 * @param {number} value Updated value of the margin in points to modify. 60 * @param {number} value Updated value of the margin in points to modify.
47 * @return {!print_preview.Margins} A new copy of |this| with the 61 * @return {!print_preview.Margins} A new copy of |this| with the
48 * modification made to the specified margin. 62 * modification made to the specified margin.
49 */ 63 */
50 set: function(orientation, value) { 64 set: function(orientation, value) {
51 var newValue = {}; 65 var newValue = this.clone_();
52 for (var o in this.value_) {
53 newValue[o] = this.value_[o];
54 }
55 newValue[orientation] = value; 66 newValue[orientation] = value;
56 return new Margins( 67 return new Margins(
57 newValue[print_preview.ticket_items.CustomMargins.Orientation.TOP], 68 newValue[print_preview.ticket_items.CustomMargins.Orientation.TOP],
58 newValue[print_preview.ticket_items.CustomMargins.Orientation.RIGHT], 69 newValue[print_preview.ticket_items.CustomMargins.Orientation.RIGHT],
59 newValue[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM], 70 newValue[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM],
60 newValue[print_preview.ticket_items.CustomMargins.Orientation.LEFT]); 71 newValue[print_preview.ticket_items.CustomMargins.Orientation.LEFT]);
61 }, 72 },
62 73
63 /** 74 /**
64 * @param {print_preview.Margins} other The other margins object to compare 75 * @param {print_preview.Margins} other The other margins object to compare
65 * against. 76 * against.
66 * @return {boolean} Whether this margins object is equal to another. 77 * @return {boolean} Whether this margins object is equal to another.
67 */ 78 */
68 equals: function(other) { 79 equals: function(other) {
69 if (other == null) { 80 if (other == null) {
70 return false; 81 return false;
71 } 82 }
72 for (var orientation in this.value_) { 83 for (var orientation in this.value_) {
73 if (this.value_[orientation] != other.value_[orientation]) { 84 if (this.value_[orientation] != other.value_[orientation]) {
74 return false; 85 return false;
75 } 86 }
76 } 87 }
77 return true; 88 return true;
89 },
90
91 /** @return {Object} A serialized representation of the margins. */
92 serialize: function() {
93 return this.clone_();
94 },
95
96 /**
97 * @return {Object} Cloned state of the margins.
98 * @private
99 */
100 clone_: function() {
101 var clone = {};
102 for (var o in this.value_) {
103 clone[o] = this.value_[o];
104 }
105 return clone;
78 } 106 }
79 }; 107 };
80 108
81 // Export 109 // Export
82 return { 110 return {
83 Margins: Margins 111 Margins: Margins
84 }; 112 };
85 }); 113 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698