Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / ticket_items / custom_margins.js
blob38ffc00b5fe0b09230eff36ee144fa02ef3c99bd
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() {
6 'use strict';
8 /**
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
12 * margins.
13 * @param {!print_preview.DocumentInfo} documentInfo Information about the
14 * document to print.
15 * @constructor
16 * @extends {print_preview.ticket_items.TicketItem}
18 function CustomMargins(appState, documentInfo) {
19 print_preview.ticket_items.TicketItem.call(
20 this,
21 appState,
22 print_preview.AppState.Field.CUSTOM_MARGINS,
23 null /*destinationStore*/,
24 documentInfo);
27 /**
28 * Enumeration of the orientations of margins.
29 * @enum {string}
31 CustomMargins.Orientation = {
32 TOP: 'top',
33 RIGHT: 'right',
34 BOTTOM: 'bottom',
35 LEFT: 'left'
38 /**
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>}
42 * @private
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;
54 /**
55 * Minimum distance in points that two margins can be separated by.
56 * @type {number}
57 * @const
58 * @private
60 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch.
62 CustomMargins.prototype = {
63 __proto__: print_preview.ticket_items.TicketItem.prototype,
65 /** @override */
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) {
73 return false;
76 return true;
79 /** @override */
80 isCapabilityAvailable: function() {
81 return this.getDocumentInfoInternal().isModifiable;
84 /** @override */
85 isValueEqual: function(value) {
86 return this.getValue().equals(value);
89 /**
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));
100 /** @override */
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(
111 this, margins);
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];
124 var max =
125 this.getMarginMax_(orientation, margins.get(oppositeOrientation));
126 value = Math.max(0, Math.min(max, value));
127 this.updateValue(margins.set(orientation, value));
130 /** @override */
131 getDefaultValueInternal: function() {
132 return this.getDocumentInfoInternal().margins ||
133 new print_preview.Margins(72, 72, 72, 72);
136 /** @override */
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.
148 * @private
150 getMarginMax_: function(orientation, oppositeMargin) {
151 var max;
152 if (orientation == CustomMargins.Orientation.TOP ||
153 orientation == CustomMargins.Orientation.BOTTOM) {
154 max = this.getDocumentInfoInternal().pageSize.height - oppositeMargin -
155 CustomMargins.MINIMUM_MARGINS_DISTANCE_;
156 } else {
157 max = this.getDocumentInfoInternal().pageSize.width - oppositeMargin -
158 CustomMargins.MINIMUM_MARGINS_DISTANCE_;
160 return Math.round(max);
164 // Export
165 return {
166 CustomMargins: CustomMargins