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 /** |
| 37 * Enumeration of the orientations of margins. |
| 38 * @enum {string} |
| 39 */ |
| 40 CustomMargins.Orientation = { |
| 41 TOP: 'top', |
| 42 RIGHT: 'right', |
| 43 BOTTOM: 'bottom', |
| 44 LEFT: 'left' |
| 45 }; |
| 46 |
| 47 /** |
| 48 * Mapping of a margin orientation to its opposite. |
| 49 * @type {object.<CustomMargins.Orientation, CustomMargins.Orientation>} |
| 50 * @private |
| 51 */ |
| 52 CustomMargins.OppositeOrientation_ = {}; |
| 53 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.TOP] = |
| 54 CustomMargins.Orientation.BOTTOM; |
| 55 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.RIGHT] = |
| 56 CustomMargins.Orientation.LEFT; |
| 57 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.BOTTOM] = |
| 58 CustomMargins.Orientation.TOP; |
| 59 CustomMargins.OppositeOrientation_[CustomMargins.Orientation.LEFT] = |
| 60 CustomMargins.Orientation.RIGHT; |
| 61 |
| 62 /** |
| 63 * Minimum distance in points that two margins can be separated by. |
| 64 * @type {number} |
| 65 * @const |
| 66 * @private |
| 67 */ |
| 68 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch. |
| 69 |
| 70 CustomMargins.prototype = { |
| 71 __proto__: print_preview.ticket_items.TicketItem.prototype, |
| 72 |
| 73 /** @override */ |
| 74 wouldValueBeValid: function(value) { |
| 75 var margins = /** @type {!print_preview.Margins} */ (value); |
| 76 for (var key in CustomMargins.Orientation) { |
| 77 var o = CustomMargins.Orientation[key]; |
| 78 var max = this.getMarginMax_( |
| 79 o, margins.get(CustomMargins.OppositeOrientation_[o])); |
| 80 if (margins.get(o) > max || margins.get(o) < 0) { |
| 81 return false; |
| 82 } |
| 83 } |
| 84 return true; |
| 85 }, |
| 86 |
| 87 /** @override */ |
| 88 isCapabilityAvailable: function() { |
| 89 return this.documentInfo_.isModifiable; |
| 90 }, |
| 91 |
| 92 /** |
| 93 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 94 * Specifies the margin to get the maximum value for. |
| 95 * @return {number} Maximum value in points of the specified margin. |
| 96 */ |
| 97 getMarginMax: function(orientation) { |
| 98 var oppositeOrient = CustomMargins.OppositeOrientation_[orientation]; |
| 99 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); |
| 100 return this.getMarginMax_(orientation, margins.get(oppositeOrient)); |
| 101 }, |
| 102 |
| 103 /** @override */ |
| 104 updateValue: function(value) { |
| 105 var margins = /** @type {!InputMargins} */ (value); |
| 106 if (margins != null) { |
| 107 margins = new print_preview.Margins( |
| 108 Math.round(margins.get(CustomMargins.Orientation.TOP)), |
| 109 Math.round(margins.get(CustomMargins.Orientation.RIGHT)), |
| 110 Math.round(margins.get(CustomMargins.Orientation.BOTTOM)), |
| 111 Math.round(margins.get(CustomMargins.Orientation.LEFT))); |
| 112 } |
| 113 print_preview.ticket_items.TicketItem.prototype.updateValue.call( |
| 114 this, margins); |
| 115 }, |
| 116 |
| 117 /** |
| 118 * Updates the specified margin in points while keeping the value within |
| 119 * a maximum and minimum. |
| 120 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 121 * Specifies the margin to update. |
| 122 * @param {number} value Updated margin value in points. |
| 123 */ |
| 124 updateMargin: function(orientation, value) { |
| 125 var margins = /** @type {!print_preview.Margins} */ (this.getValue()); |
| 126 var oppositeOrientation = CustomMargins.OppositeOrientation_[orientation]; |
| 127 var max = |
| 128 this.getMarginMax_(orientation, margins.get(oppositeOrientation)); |
| 129 value = Math.max(0, Math.min(max, value)); |
| 130 this.updateValue(margins.set(orientation, value)); |
| 131 }, |
| 132 |
| 133 /** @override */ |
| 134 getDefaultValueInternal: function() { |
| 135 return this.documentInfo_.margins || |
| 136 new print_preview.Margins(72, 72, 72, 72); |
| 137 }, |
| 138 |
| 139 /** @override */ |
| 140 getCapabilityNotAvailableValueInternal: function() { |
| 141 return this.documentInfo_.margins || |
| 142 new print_preview.Margins(72, 72, 72, 72); |
| 143 }, |
| 144 |
| 145 /** |
| 146 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 147 * Specifies which margin to get the maximum value of. |
| 148 * @param {number} oppositeMargin Value of the margin in points |
| 149 * opposite the specified margin. |
| 150 * @return {number} Maximum value in points of the specified margin. |
| 151 * @private |
| 152 */ |
| 153 getMarginMax_: function(orientation, oppositeMargin) { |
| 154 var max; |
| 155 if (orientation == CustomMargins.Orientation.TOP || |
| 156 orientation == CustomMargins.Orientation.BOTTOM) { |
| 157 max = this.documentInfo_.pageSize.height - oppositeMargin - |
| 158 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 159 } else { |
| 160 max = this.documentInfo_.pageSize.width - oppositeMargin - |
| 161 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 162 } |
| 163 return Math.round(max); |
| 164 } |
| 165 }; |
| 166 |
| 167 // Export |
| 168 return { |
| 169 CustomMargins: CustomMargins |
| 170 }; |
| 171 }); |
OLD | NEW |