OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 cr.define('print_preview.ticket_items', function() { |
| 6 'use strict'; |
| 7 |
| 8 /** |
| 9 * Custom page margins ticket item whose value is a |
| 10 * {@code print_preview.Margins}. |
| 11 * @param {!print_preview.DocumentInfo} documentInfo Information about the |
| 12 * document to print. |
| 13 * @param {!print_preview.MeasurementSystem} measurementSystem Used to convert |
| 14 * from string input into measurements in points. |
| 15 * @constructor |
| 16 * @extends {print_preview.ticket_items.TicketItem} |
| 17 */ |
| 18 function CustomMargins(documentInfo, measurementSystem) { |
| 19 print_preview.ticket_items.TicketItem.call(this); |
| 20 |
| 21 /** |
| 22 * Information about the document to print. |
| 23 * @type {!print_preview.DocumentInfo} |
| 24 * @private |
| 25 */ |
| 26 this.documentInfo_ = documentInfo; |
| 27 |
| 28 /** |
| 29 * Used to convert from string input to measurements in points. |
| 30 * @type {!print_preview.MeasurementSystem} |
| 31 * @private |
| 32 */ |
| 33 this.measurementSystem_ = measurementSystem; |
| 34 |
| 35 /** |
| 36 * Default margins used if user has not made any changes to the margins. |
| 37 * @type {!print_preview.Margins} |
| 38 * @private |
| 39 */ |
| 40 this.defaultValue_ = new print_preview.Margins(72, 72, 72, 72); |
| 41 }; |
| 42 |
| 43 /** |
| 44 * Enumeration of the orientations of margins. |
| 45 * @enum {string} |
| 46 */ |
| 47 CustomMargins.Orientation = { |
| 48 TOP: 'top', |
| 49 RIGHT: 'right', |
| 50 BOTTOM: 'bottom', |
| 51 LEFT: 'left' |
| 52 }; |
| 53 |
| 54 /** |
| 55 * Mapping of a margin orientation to its opposite. |
| 56 * @type {object.<CustomMargins.Orientation, CustomMargins.Orientation>} |
| 57 * @private |
| 58 */ |
| 59 CustomMargins.OppositeOrientation_ = {}; |
| 60 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.TOP] = |
| 61 CustomMargins.Orientation.BOTTOM; |
| 62 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.RIGHT] = |
| 63 CustomMargins.Orientation.LEFT; |
| 64 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.BOTTOM] = |
| 65 CustomMargins.Orientation.TOP; |
| 66 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.LEFT] = |
| 67 CustomMargins.Orientation.RIGHT; |
| 68 |
| 69 /** |
| 70 * Minimum distance in points that two margins can be separated by. |
| 71 * @type {number} |
| 72 * @const |
| 73 * @private |
| 74 */ |
| 75 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch. |
| 76 |
| 77 CustomMargins.prototype = { |
| 78 __proto__: print_preview.ticket_items.TicketItem.prototype, |
| 79 |
| 80 /** @override */ |
| 81 wouldValueBeValid: function(value) { |
| 82 var margins = /** @type {!print_preview.Margins} */ (value); |
| 83 for (var key in CustomMargins.Orientation) { |
| 84 var o = CustomMargins.Orientation[key]; |
| 85 var max = this.getMarginMax_( |
| 86 o, margins.get(CustomMargins.OppositeOrientation_[o])); |
| 87 if (margins.get(o) > max || margins.get(o) < 0) { |
| 88 return false; |
| 89 } |
| 90 } |
| 91 return true; |
| 92 }, |
| 93 |
| 94 /** @override */ |
| 95 isCapabilityAvailable: function() { |
| 96 return this.documentInfo_.isModifiable; |
| 97 }, |
| 98 |
| 99 /** |
| 100 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 101 * Specifies the margin to get the maximum value for. |
| 102 * @return {number} Maximum value in points of the specified margin. |
| 103 */ |
| 104 getMarginMax: function(orientation) { |
| 105 var oppositeOrient = CustomMargins.OppositeOrientation_[orientation]; |
| 106 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); |
| 107 return this.getMarginMax_(orientation, margins.get(oppositeOrient)); |
| 108 }, |
| 109 |
| 110 /** @override */ |
| 111 updateValue: function(value) { |
| 112 var margins = /** @type {!InputMargins} */ (value); |
| 113 if (margins != null) { |
| 114 margins = new print_preview.Margins( |
| 115 Math.round(margins.get(CustomMargins.Orientation.TOP)), |
| 116 Math.round(margins.get(CustomMargins.Orientation.RIGHT)), |
| 117 Math.round(margins.get(CustomMargins.Orientation.BOTTOM)), |
| 118 Math.round(margins.get(CustomMargins.Orientation.LEFT))); |
| 119 } |
| 120 print_preview.ticket_items.TicketItem.prototype.updateValue.call( |
| 121 this, margins); |
| 122 }, |
| 123 |
| 124 /** |
| 125 * Updates the specified margin in points while keeping the value within |
| 126 * a maximum and minimum. |
| 127 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 128 * Specifies the margin to update. |
| 129 * @param {number} value Updated margin value in points. |
| 130 */ |
| 131 updateMargin: function(orientation, value) { |
| 132 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); |
| 133 var oppositeOrientation = CustomMargins.OppositeOrientation_[orientation]; |
| 134 var max = |
| 135 this.getMarginMax_(orientation, margins.get(oppositeOrientation)); |
| 136 value = Math.max(0, Math.min(max, value)); |
| 137 this.updateValue(margins.set(orientation, value)); |
| 138 }, |
| 139 |
| 140 /** |
| 141 * Updates the custom margins used if the user has not edited them. |
| 142 * @param {!print_preview.Margins} defaultValue Updated default margins. |
| 143 */ |
| 144 updateDefaultValue: function(defaultValue) { |
| 145 this.defaultValue_ = defaultValue; |
| 146 }, |
| 147 |
| 148 /** @override */ |
| 149 getDefaultValueInternal: function() { |
| 150 return this.defaultValue_; |
| 151 }, |
| 152 |
| 153 /** @override */ |
| 154 getCapabilityNotAvailableValueInternal: function() { |
| 155 return this.defaultValue_; |
| 156 }, |
| 157 |
| 158 /** |
| 159 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 160 * Specifies which margin to get the maximum value of. |
| 161 * @param {number} oppositeMargin Value of the margin in points |
| 162 * opposite the specified margin. |
| 163 * @return {number} Maximum value in points of the specified margin. |
| 164 * @private |
| 165 */ |
| 166 getMarginMax_: function(orientation, oppositeMargin) { |
| 167 var max; |
| 168 if (orientation == CustomMargins.Orientation.TOP || |
| 169 orientation == CustomMargins.Orientation.BOTTOM) { |
| 170 max = this.documentInfo_.pageSize.height - oppositeMargin - |
| 171 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 172 } else { |
| 173 max = this.documentInfo_.pageSize.width - oppositeMargin - |
| 174 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 175 } |
| 176 return Math.round(max); |
| 177 } |
| 178 }; |
| 179 |
| 180 // Export |
| 181 return { |
| 182 CustomMargins: CustomMargins |
| 183 }; |
| 184 }); |
OLD | NEW |