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