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.ticket_items.StringMargins}. The margin values need to |
| 11 * be represented by a string since the user can textually input the values. |
| 12 * @param {!print_preview.DocumentInfo} documentInfo Information about the |
| 13 * document to print. |
| 14 * @param {!print_preview.MeasurementSystem} measurementSystem Used to convert |
| 15 * from string input into measurements in points. |
| 16 * @constructor |
| 17 * @extends {print_preview.ticket_items.TicketItem} |
| 18 */ |
| 19 function CustomMargins(documentInfo, measurementSystem) { |
| 20 print_preview.ticket_items.TicketItem.call(this); |
| 21 |
| 22 /** |
| 23 * Information about the document to print. |
| 24 * @type {!print_preview.DocumentInfo} |
| 25 * @private |
| 26 */ |
| 27 this.documentInfo_ = documentInfo; |
| 28 |
| 29 /** |
| 30 * Used to convert from string input to measurements in points. |
| 31 * @type {!print_preview.MeasurementSystem} |
| 32 * @private |
| 33 */ |
| 34 this.measurementSystem_ = measurementSystem; |
| 35 |
| 36 /** |
| 37 * Default value of the ticket item in points. |
| 38 * @type {!print_preview.Margins} |
| 39 * @private |
| 40 */ |
| 41 this.defaultValueInPts_ = new print_preview.Margins(72, 72, 72, 72); |
| 42 |
| 43 /** |
| 44 * Cache of the the ticket item's value in points. |
| 45 * @type {print_preview.Margins} |
| 46 * @private |
| 47 */ |
| 48 this.valueInPts_ = null; |
| 49 }; |
| 50 |
| 51 /** |
| 52 * Enumeration of the orientations of margins. |
| 53 * @enum {string} |
| 54 */ |
| 55 CustomMargins.Orientation = { |
| 56 TOP: 'top', |
| 57 RIGHT: 'right', |
| 58 BOTTOM: 'bottom', |
| 59 LEFT: 'left' |
| 60 }; |
| 61 |
| 62 /** |
| 63 * Precision used to convert from number in points to string in local units. |
| 64 * @type {number} |
| 65 * @const |
| 66 * @private |
| 67 */ |
| 68 CustomMargins.PRECISION_ = 3; |
| 69 |
| 70 /** |
| 71 * Minimum distance in points two margins can be separated by. |
| 72 * @type {number} |
| 73 * @const |
| 74 * @private |
| 75 */ |
| 76 CustomMargins.MINIMUM_MARGINS_DISTANCE_ = 72; // 1 inch. |
| 77 |
| 78 CustomMargins.prototype = { |
| 79 __proto__: print_preview.ticket_items.TicketItem.prototype, |
| 80 |
| 81 /** @override */ |
| 82 wouldValueBeValid: function(value) { |
| 83 var stringMargins = |
| 84 /** @type {!print_preview.ticket_items.StringMargins} */ (value); |
| 85 var marginsInPts = this.parseStringMarginsToPts_(stringMargins); |
| 86 if (marginsInPts == null) { |
| 87 return false; |
| 88 } |
| 89 for (var key in CustomMargins.Orientation) { |
| 90 var o = CustomMargins.Orientation[key]; |
| 91 var max = this.getMarginMaxInPts_(o, marginsInPts.getOpposite(o)); |
| 92 max = this.measurementSystem_.convertFromPoints(max, true); |
| 93 var margin = |
| 94 this.measurementSystem_.convertFromPoints(marginsInPts.get(o)); |
| 95 if (margin > max || margin < 0) { |
| 96 return false; |
| 97 } |
| 98 } |
| 99 return true; |
| 100 }, |
| 101 |
| 102 /** @override */ |
| 103 isCapabilityAvailable: function() { |
| 104 return this.documentInfo_.isModifiable; |
| 105 }, |
| 106 |
| 107 /** |
| 108 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 109 * Specifies which margin to check if parseable. |
| 110 * @return {boolean} Whether the specified margin is parseable. |
| 111 */ |
| 112 isMarginParseable: function(orientation) { |
| 113 return this.parseToPts_(this.getValue().get(orientation)) != null; |
| 114 }, |
| 115 |
| 116 /** |
| 117 * @return {!print_preview.Margins} The ticket items custom margins in |
| 118 * points. |
| 119 */ |
| 120 getValueInPts: function() { |
| 121 if (this.valueInPts_ == null) { |
| 122 var stringMargins = |
| 123 /** @type {!print_preview.ticket_items.StringMargins} */ ( |
| 124 this.getValue()); |
| 125 this.valueInPts_ = this.parseStringMarginsToPts_(stringMargins); |
| 126 if (this.valueInPts_ == null) { |
| 127 throw Error('Error while parsing margin values into points'); |
| 128 } |
| 129 } |
| 130 return this.valueInPts_; |
| 131 }, |
| 132 |
| 133 /** |
| 134 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 135 * Specifies the margin to get the maximum value for. |
| 136 * @return {number} Maximum value in points of the specified margin. |
| 137 */ |
| 138 getMarginMaxInPts: function(orientation) { |
| 139 var marginsInPts = this.getValueInPts(); |
| 140 return this.getMarginMaxInPts_( |
| 141 orientation, marginsInPts.getOpposite(orientation)); |
| 142 }, |
| 143 |
| 144 /** @override */ |
| 145 updateValue: function(value) { |
| 146 print_preview.ticket_items.TicketItem.prototype.updateValue.call( |
| 147 this, value); |
| 148 this.valueInPts_ = null; |
| 149 }, |
| 150 |
| 151 /** |
| 152 * Updates the value of the ticket item with the given margins in points. |
| 153 * @param {!print_preview.Margins} valueInPts Updated margins in points. |
| 154 */ |
| 155 updateValueInPts: function(valueInPts) { |
| 156 var o = CustomMargins.Orientation; |
| 157 this.updateValue(new StringMargins( |
| 158 this.serializeMarginFromPts(valueInPts.get(o.TOP)), |
| 159 this.serializeMarginFromPts(valueInPts.get(o.RIGHT)), |
| 160 this.serializeMarginFromPts(valueInPts.get(o.BOTTOM)), |
| 161 this.serializeMarginFromPts(valueInPts.get(o.LEFT)))); |
| 162 this.valueInPts_ = valueInPts; |
| 163 }, |
| 164 |
| 165 /** |
| 166 * Updates the specified margin with a value in points. |
| 167 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 168 * Specifies the margin to update. |
| 169 * @param {number} valueInPts Updated margin in points. |
| 170 */ |
| 171 updateMarginInPts: function(orientation, valueInPts) { |
| 172 this.updateMargin(orientation, this.serializeMarginFromPts(valueInPts)); |
| 173 }, |
| 174 |
| 175 /** |
| 176 * Updates the specified margin with the given string input. |
| 177 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 178 * Specifies the margin to update. |
| 179 * @param {string} value Updated string value of the margin. |
| 180 */ |
| 181 updateMargin: function(orientation, value) { |
| 182 var stringMargins = |
| 183 /** @type {!print_preview.ticket_items.StringMargins} */ ( |
| 184 this.getValue()); |
| 185 var newStringMargins = stringMargins.set(orientation, value); |
| 186 var newMarginsInPts = this.parseStringMarginsToPts_(newStringMargins); |
| 187 if (newMarginsInPts != null) { |
| 188 // Put updated margin within the valid range (between min and max). |
| 189 var max = this.getMarginMaxInPts_( |
| 190 orientation, newMarginsInPts.getOpposite(orientation)); |
| 191 max = this.measurementSystem_.convertFromPoints(max); |
| 192 var newValue = this.measurementSystem_.convertFromPoints( |
| 193 newMarginsInPts.get(orientation)); |
| 194 newValue = Math.max(0, Math.min(max, newValue)); |
| 195 this.updateValueInPts(newMarginsInPts.set( |
| 196 orientation, this.measurementSystem_.convertToPoints(newValue))); |
| 197 } else { |
| 198 this.updateValue(newStringMargins); |
| 199 } |
| 200 }, |
| 201 |
| 202 /** |
| 203 * Updates the default value of the margins ticket item. This value is used |
| 204 * if the ticket item has not been edited by the user. |
| 205 * @param {!print_preview.Margins} valueInPts Updated margin default values. |
| 206 */ |
| 207 updateDefaultValueInPts: function(valueInPts) { |
| 208 this.defaultValueInPts_ = valueInPts; |
| 209 this.valueInPts_ = null; |
| 210 }, |
| 211 |
| 212 /** |
| 213 * @param {number} marginInPts Value of in points to serialize to a locale |
| 214 * string. |
| 215 * @return {string} Serialized form of the given value in points. The |
| 216 * serialized value is in the user's local measurement system. |
| 217 */ |
| 218 serializeMarginFromPts: function(marginInPts) { |
| 219 return this.measurementSystem_.convertFromPoints(marginInPts) + |
| 220 this.measurementSystem_.unitSymbol; |
| 221 }, |
| 222 |
| 223 /** @override */ |
| 224 getDefaultValueInternal: function() { |
| 225 return new StringMargins( |
| 226 this.serializeMarginFromPts( |
| 227 this.defaultValueInPts_.get(CustomMargins.Orientation.TOP)), |
| 228 this.serializeMarginFromPts( |
| 229 this.defaultValueInPts_.get(CustomMargins.Orientation.RIGHT)), |
| 230 this.serializeMarginFromPts( |
| 231 this.defaultValueInPts_.get(CustomMargins.Orientation.BOTTOM)), |
| 232 this.serializeMarginFromPts( |
| 233 this.defaultValueInPts_.get(CustomMargins.Orientation.LEFT))); |
| 234 }, |
| 235 |
| 236 /** @override */ |
| 237 getCapabilityNotAvailableValueInternal: function() { |
| 238 return this.getDefaultValueInternal(); |
| 239 }, |
| 240 |
| 241 /** |
| 242 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 243 * Specifies which margin to get the maximum value of. |
| 244 * @param {number} oppositeMarginInPts Value of the margin in points |
| 245 * opposite the specified margin. |
| 246 * @return {number} Maximum value in points of the specified margin. |
| 247 * @private |
| 248 */ |
| 249 getMarginMaxInPts_: function(orientation, oppositeMarginInPts) { |
| 250 if (orientation == CustomMargins.Orientation.TOP || |
| 251 orientation == CustomMargins.Orientation.BOTTOM) { |
| 252 return this.documentInfo_.pageSize.height - oppositeMarginInPts - |
| 253 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 254 } else { |
| 255 return this.documentInfo_.pageSize.width - oppositeMarginInPts - |
| 256 CustomMargins.MINIMUM_MARGINS_DISTANCE_; |
| 257 } |
| 258 }, |
| 259 |
| 260 /** |
| 261 * Parses the size of a margin in points from the given string. |
| 262 * Example: "1.00", "1", ".5", "1.1" are valid values. |
| 263 * Example: "1.4dsf", "-1" are invalid. |
| 264 * Note: The inch symbol (") at the end of |text| is allowed. |
| 265 * @param {string} text The text to parse. |
| 266 * @param {print_preview.MeasurementSystem} measurementSystem Used to handle |
| 267 * parsing local units. |
| 268 * @return {?number} The margin value represented by |text| in points or |
| 269 * {@code null} if |text| does not represent a valid value. |
| 270 * @private |
| 271 */ |
| 272 parseToPts_: function(text) { |
| 273 // Removing whitespace anywhere in the string. |
| 274 text = text.replace(/\s*/g, ''); |
| 275 if (text.length == 0) { |
| 276 return null; |
| 277 } |
| 278 var validationRegex = new RegExp('^(^-?)(\\d)+(\\' + |
| 279 this.measurementSystem_.thousandsDelimeter + '\\d{3})*(\\' + |
| 280 this.measurementSystem_.decimalDelimeter + '\\d*)?' + |
| 281 '(' + this.measurementSystem_.unitSymbol + ')?$'); |
| 282 if (validationRegex.test(text)) { |
| 283 // Replacing decimal point with the dot symbol in order to use |
| 284 // parseFloat() properly. |
| 285 var replacementRegex = |
| 286 new RegExp('\\' + this.measurementSystem_.decimalDelimeter + '{1}'); |
| 287 text = text.replace(replacementRegex, '.'); |
| 288 return this.measurementSystem_.convertToPoints(parseFloat(text)); |
| 289 } |
| 290 return null; |
| 291 }, |
| 292 |
| 293 /** |
| 294 * Parses the given string margins into margins in points. |
| 295 * @param {!print_preview.ticket_items.StringMargins} stringMargins String |
| 296 * margins to parse. |
| 297 * @return {print_preview.Margins} The parsed margins in points, or |
| 298 * {@code null} if the string margins could not be parsed. |
| 299 * @private |
| 300 */ |
| 301 parseStringMarginsToPts_: function(stringMargins) { |
| 302 var topInPts = |
| 303 this.parseToPts_(stringMargins.get(CustomMargins.Orientation.TOP)); |
| 304 var rightInPts = |
| 305 this.parseToPts_(stringMargins.get(CustomMargins.Orientation.RIGHT)); |
| 306 var bottomInPts = |
| 307 this.parseToPts_(stringMargins.get(CustomMargins.Orientation.BOTTOM)); |
| 308 var leftInPts = |
| 309 this.parseToPts_(stringMargins.get(CustomMargins.Orientation.LEFT)); |
| 310 if (topInPts == null || |
| 311 rightInPts == null || |
| 312 bottomInPts == null || |
| 313 leftInPts == null) { |
| 314 return null; |
| 315 } |
| 316 return new print_preview.Margins( |
| 317 topInPts, rightInPts, bottomInPts, leftInPts); |
| 318 } |
| 319 }; |
| 320 |
| 321 /** |
| 322 * An immutable object that represents page margins as strings of values in |
| 323 * the system's local measurement system. |
| 324 * @param {string} top Top margin expressed as a string in the local |
| 325 * measurement system. |
| 326 * @param {string} right Right margin expressed as a string in the local |
| 327 * measurement system. |
| 328 * @param {string} bottom Bottom margin expressed as a string in the local |
| 329 * measurement system. |
| 330 * @param {string} left Left margin expressed as a string in the local |
| 331 * measurement system. |
| 332 * @constructor |
| 333 */ |
| 334 function StringMargins(top, right, bottom, left) { |
| 335 /** |
| 336 * Backing store for the margin values. |
| 337 * @type {Object.< |
| 338 * print_preview.ticket_items.CustomMargins.Orientation, |
| 339 * string>} |
| 340 * @private |
| 341 */ |
| 342 this.value_ = {}; |
| 343 this.value_[print_preview.ticket_items.CustomMargins.Orientation.TOP] = top; |
| 344 this.value_[print_preview.ticket_items.CustomMargins.Orientation.RIGHT] = |
| 345 right; |
| 346 this.value_[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM] = |
| 347 bottom; |
| 348 this.value_[print_preview.ticket_items.CustomMargins.Orientation.LEFT] = |
| 349 left; |
| 350 }; |
| 351 |
| 352 StringMargins.prototype = { |
| 353 /** |
| 354 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 355 * Specifies which margin value to get. |
| 356 * @return {string} The value of the specified margin. |
| 357 */ |
| 358 get: function(orientation) { |
| 359 return this.value_[orientation]; |
| 360 }, |
| 361 |
| 362 /** |
| 363 * @param {print_preview.ticket_items.CustomMargins.Orientation} orientation |
| 364 * Specifies the margin to modify. |
| 365 * @param {string} value Updated value of the margin to modify. |
| 366 * @return {!print_preview.ticket_items.StringMargins} A new copy of |this| |
| 367 * with the modification made to the specified margin. |
| 368 */ |
| 369 set: function(orientation, value) { |
| 370 var newValue = {}; |
| 371 for (var o in this.value_) { |
| 372 newValue[o] = this.value_[o]; |
| 373 } |
| 374 newValue[orientation] = value; |
| 375 return new StringMargins( |
| 376 newValue[print_preview.ticket_items.CustomMargins.Orientation.TOP], |
| 377 newValue[print_preview.ticket_items.CustomMargins.Orientation.RIGHT], |
| 378 newValue[print_preview.ticket_items.CustomMargins.Orientation.BOTTOM], |
| 379 newValue[print_preview.ticket_items.CustomMargins.Orientation.LEFT]); |
| 380 } |
| 381 }; |
| 382 |
| 383 // Export |
| 384 return { |
| 385 CustomMargins: CustomMargins, |
| 386 StringMargins: StringMargins |
| 387 }; |
| 388 }); |
OLD | NEW |