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

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

Issue 2862203002: Print Preview: Fix data/ errors (Closed)
Patch Set: Make tests pass Created 3 years, 7 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.exportPath('print_preview.ticket_items');
6
7 /**
8 * Enumeration of the orientations of margins.
9 * @enum {string}
10 */
11 print_preview.ticket_items.CustomMarginsOrientation = {
12 TOP: 'top',
13 RIGHT: 'right',
14 BOTTOM: 'bottom',
15 LEFT: 'left'
16 };
17
5 cr.define('print_preview.ticket_items', function() { 18 cr.define('print_preview.ticket_items', function() {
6 'use strict'; 19 'use strict';
7 20
dpapad 2017/05/05 17:32:50 Nit (optional): You might be able to create a shor
rbpotter 2017/05/05 18:25:46 Done.
8 /** 21 /**
9 * Custom page margins ticket item whose value is a 22 * Custom page margins ticket item whose value is a
10 * {@code print_preview.Margins}. 23 * {@code print_preview.Margins}.
11 * @param {!print_preview.AppState} appState App state used to persist custom 24 * @param {!print_preview.AppState} appState App state used to persist custom
12 * margins. 25 * margins.
13 * @param {!print_preview.DocumentInfo} documentInfo Information about the 26 * @param {!print_preview.DocumentInfo} documentInfo Information about the
14 * document to print. 27 * document to print.
15 * @constructor 28 * @constructor
16 * @extends {print_preview.ticket_items.TicketItem} 29 * @extends {print_preview.ticket_items.TicketItem}
17 */ 30 */
18 function CustomMargins(appState, documentInfo) { 31 function CustomMargins(appState, documentInfo) {
19 print_preview.ticket_items.TicketItem.call( 32 print_preview.ticket_items.TicketItem.call(
20 this, 33 this,
21 appState, 34 appState,
22 print_preview.AppState.Field.CUSTOM_MARGINS, 35 print_preview.AppStateField.CUSTOM_MARGINS,
23 null /*destinationStore*/, 36 null /*destinationStore*/,
24 documentInfo); 37 documentInfo);
25 }; 38 }
26
27 /**
28 * Enumeration of the orientations of margins.
29 * @enum {string}
30 */
31 CustomMargins.Orientation = {
32 TOP: 'top',
33 RIGHT: 'right',
34 BOTTOM: 'bottom',
35 LEFT: 'left'
36 };
37 39
38 /** 40 /**
39 * Mapping of a margin orientation to its opposite. 41 * Mapping of a margin orientation to its opposite.
40 * @type {!Object<!print_preview.ticket_items.CustomMargins.Orientation, 42 * @type {!Object<!print_preview.ticket_items.CustomMarginsOrientation,
41 * !print_preview.ticket_items.CustomMargins.Orientation>} 43 * !print_preview.ticket_items.CustomMarginsOrientation>}
42 * @private 44 * @private
43 */ 45 */
44 CustomMargins.OppositeOrientation_ = {}; 46 CustomMargins.OppositeOrientation_ = {};
45 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.TOP] = 47 CustomMargins.OppositeOrientation_[
46 CustomMargins.Orientation.BOTTOM; 48 print_preview.ticket_items.CustomMarginsOrientation.TOP] =
47 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.RIGHT] = 49 print_preview.ticket_items.CustomMarginsOrientation.BOTTOM;
48 CustomMargins.Orientation.LEFT; 50 CustomMargins.OppositeOrientation_[
49 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.BOTTOM] = 51 print_preview.ticket_items.CustomMarginsOrientation.RIGHT] =
50 CustomMargins.Orientation.TOP; 52 print_preview.ticket_items.CustomMarginsOrientation.LEFT;
51 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.LEFT] = 53 CustomMargins.OppositeOrientation_[
52 CustomMargins.Orientation.RIGHT; 54 print_preview.ticket_items.CustomMarginsOrientation.BOTTOM] =
55 print_preview.ticket_items.CustomMarginsOrientation.TOP;
56 CustomMargins.OppositeOrientation_[
57 print_preview.ticket_items.CustomMarginsOrientation.LEFT] =
58 print_preview.ticket_items.CustomMarginsOrientation.RIGHT;
53 59
54 /** 60 /**
55 * Minimum distance in points that two margins can be separated by. 61 * Minimum distance in points that two margins can be separated by.
56 * @type {number} 62 * @type {number}
57 * @const 63 * @const
58 * @private 64 * @private
59 */ 65 */
60 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch. 66 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch.
61 67
62 CustomMargins.prototype = { 68 CustomMargins.prototype = {
63 __proto__: print_preview.ticket_items.TicketItem.prototype, 69 __proto__: print_preview.ticket_items.TicketItem.prototype,
64 70
65 /** @override */ 71 /** @override */
66 wouldValueBeValid: function(value) { 72 wouldValueBeValid: function(value) {
67 var margins = /** @type {!print_preview.Margins} */ (value); 73 var margins = /** @type {!print_preview.Margins} */ (value);
68 for (var key in CustomMargins.Orientation) { 74 for (var key in print_preview.ticket_items.CustomMarginsOrientation) {
69 var o = CustomMargins.Orientation[key]; 75 var o = print_preview.ticket_items.CustomMarginsOrientation[key];
70 var max = this.getMarginMax_( 76 var max = this.getMarginMax_(
71 o, margins.get(CustomMargins.OppositeOrientation_[o])); 77 o, margins.get(CustomMargins.OppositeOrientation_[o]));
72 if (margins.get(o) > max || margins.get(o) < 0) { 78 if (margins.get(o) > max || margins.get(o) < 0) {
73 return false; 79 return false;
74 } 80 }
75 } 81 }
76 return true; 82 return true;
77 }, 83 },
78 84
79 /** @override */ 85 /** @override */
80 isCapabilityAvailable: function() { 86 isCapabilityAvailable: function() {
81 return this.getDocumentInfoInternal().isModifiable; 87 return this.getDocumentInfoInternal().isModifiable;
82 }, 88 },
83 89
84 /** @override */ 90 /** @override */
85 isValueEqual: function(value) { 91 isValueEqual: function(value) {
86 return this.getValue().equals(value); 92 return this.getValue().equals(value);
87 }, 93 },
88 94
89 /** 95 /**
90 * @param {!print_preview.ticket_items.CustomMargins.Orientation} 96 * @param {!print_preview.ticket_items.CustomMarginsOrientation}
91 * orientation Specifies the margin to get the maximum value for. 97 * orientation Specifies the margin to get the maximum value for.
92 * @return {number} Maximum value in points of the specified margin. 98 * @return {number} Maximum value in points of the specified margin.
93 */ 99 */
94 getMarginMax: function(orientation) { 100 getMarginMax: function(orientation) {
95 var oppositeOrient = CustomMargins.OppositeOrientation_[orientation]; 101 var oppositeOrient = CustomMargins.OppositeOrientation_[orientation];
96 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); 102 var margins = /** @type {!print_preview.Margins} */ (this.getValue());
97 return this.getMarginMax_(orientation, margins.get(oppositeOrient)); 103 return this.getMarginMax_(orientation, margins.get(oppositeOrient));
98 }, 104 },
99 105
100 /** @override */ 106 /** @override */
101 updateValue: function(value) { 107 updateValue: function(value) {
102 var margins = /** @type {!print_preview.Margins} */ (value); 108 var margins = /** @type {!print_preview.Margins} */ (value);
103 if (margins != null) { 109 if (margins != null) {
104 margins = new print_preview.Margins( 110 margins = new print_preview.Margins(
105 Math.round(margins.get(CustomMargins.Orientation.TOP)), 111 Math.round(margins.get(print_preview.ticket_items.
106 Math.round(margins.get(CustomMargins.Orientation.RIGHT)), 112 CustomMarginsOrientation.TOP)),
107 Math.round(margins.get(CustomMargins.Orientation.BOTTOM)), 113 Math.round(margins.get(print_preview.ticket_items.
108 Math.round(margins.get(CustomMargins.Orientation.LEFT))); 114 CustomMarginsOrientation.RIGHT)),
115 Math.round(margins.get(print_preview.ticket_items.
116 CustomMarginsOrientation.BOTTOM)),
117 Math.round(margins.get(print_preview.ticket_items.
118 CustomMarginsOrientation.LEFT)));
109 } 119 }
110 print_preview.ticket_items.TicketItem.prototype.updateValue.call( 120 print_preview.ticket_items.TicketItem.prototype.updateValue.call(
111 this, margins); 121 this, margins);
112 }, 122 },
113 123
114 /** 124 /**
115 * Updates the specified margin in points while keeping the value within 125 * Updates the specified margin in points while keeping the value within
116 * a maximum and minimum. 126 * a maximum and minimum.
117 * @param {!print_preview.ticket_items.CustomMargins.Orientation} 127 * @param {!print_preview.ticket_items.CustomMarginsOrientation}
118 * orientation Specifies the margin to update. 128 * orientation Specifies the margin to update.
119 * @param {number} value Updated margin value in points. 129 * @param {number} value Updated margin value in points.
120 */ 130 */
121 updateMargin: function(orientation, value) { 131 updateMargin: function(orientation, value) {
122 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); 132 var margins = /** @type {!print_preview.Margins} */ (this.getValue());
123 var oppositeOrientation = CustomMargins.OppositeOrientation_[orientation]; 133 var oppositeOrientation = CustomMargins.OppositeOrientation_[orientation];
124 var max = 134 var max =
125 this.getMarginMax_(orientation, margins.get(oppositeOrientation)); 135 this.getMarginMax_(orientation, margins.get(oppositeOrientation));
126 value = Math.max(0, Math.min(max, value)); 136 value = Math.max(0, Math.min(max, value));
127 this.updateValue(margins.set(orientation, value)); 137 this.updateValue(margins.set(orientation, value));
128 }, 138 },
129 139
130 /** @override */ 140 /** @override */
131 getDefaultValueInternal: function() { 141 getDefaultValueInternal: function() {
132 return this.getDocumentInfoInternal().margins || 142 return this.getDocumentInfoInternal().margins ||
133 new print_preview.Margins(72, 72, 72, 72); 143 new print_preview.Margins(72, 72, 72, 72);
134 }, 144 },
135 145
136 /** @override */ 146 /** @override */
137 getCapabilityNotAvailableValueInternal: function() { 147 getCapabilityNotAvailableValueInternal: function() {
138 return this.getDocumentInfoInternal().margins || 148 return this.getDocumentInfoInternal().margins ||
139 new print_preview.Margins(72, 72, 72, 72); 149 new print_preview.Margins(72, 72, 72, 72);
140 }, 150 },
141 151
142 /** 152 /**
143 * @param {!print_preview.ticket_items.CustomMargins.Orientation} 153 * @param {!print_preview.ticket_items.CustomMarginsOrientation}
144 * orientation Specifies which margin to get the maximum value of. 154 * orientation Specifies which margin to get the maximum value of.
145 * @param {number} oppositeMargin Value of the margin in points 155 * @param {number} oppositeMargin Value of the margin in points
146 * opposite the specified margin. 156 * opposite the specified margin.
147 * @return {number} Maximum value in points of the specified margin. 157 * @return {number} Maximum value in points of the specified margin.
148 * @private 158 * @private
149 */ 159 */
150 getMarginMax_: function(orientation, oppositeMargin) { 160 getMarginMax_: function(orientation, oppositeMargin) {
151 var dimensionLength = (orientation == CustomMargins.Orientation.TOP || 161 var dimensionLength =
152 orientation == CustomMargins.Orientation.BOTTOM) ? 162 (orientation ==
153 this.getDocumentInfoInternal().pageSize.height : 163 print_preview.ticket_items.CustomMarginsOrientation.TOP ||
154 this.getDocumentInfoInternal().pageSize.width; 164 orientation ==
165 print_preview.ticket_items.CustomMarginsOrientation.BOTTOM) ?
166 this.getDocumentInfoInternal().pageSize.height :
167 this.getDocumentInfoInternal().pageSize.width;
155 168
156 var totalMargin = dimensionLength - 169 var totalMargin = dimensionLength -
157 CustomMargins.MINIMUM_MARGINS_DISTANCE_; 170 CustomMargins.MINIMUM_MARGINS_DISTANCE_;
158 return Math.round(totalMargin > 0 ? totalMargin - oppositeMargin : 0); 171 return Math.round(totalMargin > 0 ? totalMargin - oppositeMargin : 0);
159 } 172 }
160 }; 173 };
161 174
162 // Export 175 // Export
163 return { 176 return {
164 CustomMargins: CustomMargins 177 CustomMargins: CustomMargins
165 }; 178 };
166 }); 179 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698