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.ticket_items', function() {
9 * Custom page margins ticket item whose value is a
10 * {@code print_preview.Margins}.
11 * @param {!print_preview.AppState} appState App state used to persist custom
13 * @param {!print_preview.DocumentInfo} documentInfo Information about the
16 * @extends {print_preview.ticket_items.TicketItem}
18 function CustomMargins(appState
, documentInfo
) {
19 print_preview
.ticket_items
.TicketItem
.call(
22 print_preview
.AppState
.Field
.CUSTOM_MARGINS
,
23 null /*destinationStore*/,
28 * Enumeration of the orientations of margins.
31 CustomMargins
.Orientation
= {
39 * Mapping of a margin orientation to its opposite.
40 * @type {!Object<!print_preview.ticket_items.CustomMargins.Orientation,
41 * !print_preview.ticket_items.CustomMargins.Orientation>}
44 CustomMargins
.OppositeOrientation_
= {};
45 CustomMargins
.OppositeOrientation_
[CustomMargins
.Orientation
.TOP
] =
46 CustomMargins
.Orientation
.BOTTOM
;
47 CustomMargins
.OppositeOrientation_
[CustomMargins
.Orientation
.RIGHT
] =
48 CustomMargins
.Orientation
.LEFT
;
49 CustomMargins
.OppositeOrientation_
[CustomMargins
.Orientation
.BOTTOM
] =
50 CustomMargins
.Orientation
.TOP
;
51 CustomMargins
.OppositeOrientation_
[CustomMargins
.Orientation
.LEFT
] =
52 CustomMargins
.Orientation
.RIGHT
;
55 * Minimum distance in points that two margins can be separated by.
60 CustomMargins
.MINIMUM_MARGINS_DISTANCE_
= 72; // 1 inch.
62 CustomMargins
.prototype = {
63 __proto__
: print_preview
.ticket_items
.TicketItem
.prototype,
66 wouldValueBeValid: function(value
) {
67 var margins
= /** @type {!print_preview.Margins} */ (value
);
68 for (var key
in CustomMargins
.Orientation
) {
69 var o
= CustomMargins
.Orientation
[key
];
70 var max
= this.getMarginMax_(
71 o
, margins
.get(CustomMargins
.OppositeOrientation_
[o
]));
72 if (margins
.get(o
) > max
|| margins
.get(o
) < 0) {
80 isCapabilityAvailable: function() {
81 return this.getDocumentInfoInternal().isModifiable
;
85 isValueEqual: function(value
) {
86 return this.getValue().equals(value
);
90 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
91 * orientation Specifies the margin to get the maximum value for.
92 * @return {number} Maximum value in points of the specified margin.
94 getMarginMax: function(orientation
) {
95 var oppositeOrient
= CustomMargins
.OppositeOrientation_
[orientation
];
96 var margins
= /** @type {!print_preview.Margins} */ (this.getValue());
97 return this.getMarginMax_(orientation
, margins
.get(oppositeOrient
));
101 updateValue: function(value
) {
102 var margins
= /** @type {!print_preview.Margins} */ (value
);
103 if (margins
!= null) {
104 margins
= new print_preview
.Margins(
105 Math
.round(margins
.get(CustomMargins
.Orientation
.TOP
)),
106 Math
.round(margins
.get(CustomMargins
.Orientation
.RIGHT
)),
107 Math
.round(margins
.get(CustomMargins
.Orientation
.BOTTOM
)),
108 Math
.round(margins
.get(CustomMargins
.Orientation
.LEFT
)));
110 print_preview
.ticket_items
.TicketItem
.prototype.updateValue
.call(
115 * Updates the specified margin in points while keeping the value within
116 * a maximum and minimum.
117 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
118 * orientation Specifies the margin to update.
119 * @param {number} value Updated margin value in points.
121 updateMargin: function(orientation
, value
) {
122 var margins
= /** @type {!print_preview.Margins} */ (this.getValue());
123 var oppositeOrientation
= CustomMargins
.OppositeOrientation_
[orientation
];
125 this.getMarginMax_(orientation
, margins
.get(oppositeOrientation
));
126 value
= Math
.max(0, Math
.min(max
, value
));
127 this.updateValue(margins
.set(orientation
, value
));
131 getDefaultValueInternal: function() {
132 return this.getDocumentInfoInternal().margins
||
133 new print_preview
.Margins(72, 72, 72, 72);
137 getCapabilityNotAvailableValueInternal: function() {
138 return this.getDocumentInfoInternal().margins
||
139 new print_preview
.Margins(72, 72, 72, 72);
143 * @param {!print_preview.ticket_items.CustomMargins.Orientation}
144 * orientation Specifies which margin to get the maximum value of.
145 * @param {number} oppositeMargin Value of the margin in points
146 * opposite the specified margin.
147 * @return {number} Maximum value in points of the specified margin.
150 getMarginMax_: function(orientation
, oppositeMargin
) {
152 if (orientation
== CustomMargins
.Orientation
.TOP
||
153 orientation
== CustomMargins
.Orientation
.BOTTOM
) {
154 max
= this.getDocumentInfoInternal().pageSize
.height
- oppositeMargin
-
155 CustomMargins
.MINIMUM_MARGINS_DISTANCE_
;
157 max
= this.getDocumentInfoInternal().pageSize
.width
- oppositeMargin
-
158 CustomMargins
.MINIMUM_MARGINS_DISTANCE_
;
160 return Math
.round(max
);
166 CustomMargins
: CustomMargins