| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
| 6 'use strict'; | 6 'use strict'; |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Creates a Margins object that holds four margin values. The units in which | 9 * Creates a Margins object that holds four margin values. The units in which |
| 10 * the values are expressed can be any numeric value. | 10 * the values are expressed can be any numeric value. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 * @return {array} An array of string indices. | 65 * @return {array} An array of string indices. |
| 66 * @private | 66 * @private |
| 67 */ | 67 */ |
| 68 indicesAsArray_: function() { | 68 indicesAsArray_: function() { |
| 69 return [MarginSettings.LEFT_GROUP, MarginSettings.TOP_GROUP, | 69 return [MarginSettings.LEFT_GROUP, MarginSettings.TOP_GROUP, |
| 70 MarginSettings.RIGHT_GROUP, MarginSettings.BOTTOM_GROUP]; | 70 MarginSettings.RIGHT_GROUP, MarginSettings.BOTTOM_GROUP]; |
| 71 }, | 71 }, |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * Rounds |this| based on the precision used when displaying the margins in | 74 * Rounds |this| based on the precision used when displaying the margins in |
| 75 * inches. This is done by converting from points to inches and back to | 75 * the user's prefered units. This is done by converting from points to |
| 76 * points. | 76 * those units (mm/inches) and back to points. |
| 77 */ | 77 */ |
| 78 roundToInches: function() { | 78 roundToLocaleUnits: function() { |
| 79 var indicesAsArray = this.indicesAsArray_(); | 79 var indicesAsArray = this.indicesAsArray_(); |
| 80 for (var i = 0; i < indicesAsArray.length; i++) { | 80 for (var i = 0; i < indicesAsArray.length; i++) { |
| 81 this[indicesAsArray[i]] = | 81 this[indicesAsArray[i]] = |
| 82 print_preview.convertPointsToInchesTextAndBack( | 82 print_preview.convertPointsToLocaleUnitsAndBack( |
| 83 this[indicesAsArray[i]]); | 83 this[indicesAsArray[i]]); |
| 84 } | 84 } |
| 85 }, | 85 }, |
| 86 | |
| 87 /** | |
| 88 * Converts |this| to inches and returns the result in a new Margins object. | |
| 89 * |this| is not affected. It assumes that |this| is currently expressed in | |
| 90 * points. | |
| 91 * @param {number} The number of decimal points to keep. | |
| 92 * @return {Margins} The equivalent of |this| in inches. | |
| 93 */ | |
| 94 toInches: function(precision) { | |
| 95 return new Margins( | |
| 96 Margins.roundToPrecision(convertPointsToInches( | |
| 97 this[MarginSettings.LEFT_GROUP]), precision), | |
| 98 Margins.roundToPrecision(convertPointsToInches( | |
| 99 this[MarginSettings.TOP_GROUP]), precision), | |
| 100 Margins.roundToPrecision(convertPointsToInches( | |
| 101 this[MarginSettings.RIGHT_GROUP]), precision), | |
| 102 Margins.roundToPrecision(convertPointsToInches( | |
| 103 this[MarginSettings.BOTTOM_GROUP]), precision) | |
| 104 ); | |
| 105 } | |
| 106 }; | 86 }; |
| 107 | 87 |
| 108 /** | 88 /** |
| 109 * @constructor | 89 * @constructor |
| 110 * Class describing the layout of the page. | 90 * Class describing the layout of the page. |
| 111 */ | 91 */ |
| 112 function PageLayout(width, height, left, top, right, bottom) { | 92 function PageLayout(width, height, left, top, right, bottom) { |
| 113 this.contentWidth_ = width; | 93 this.contentWidth_ = width; |
| 114 this.contentHeight_ = height; | 94 this.contentHeight_ = height; |
| 115 this.margins_ = new Margins(left, top, right, bottom); | 95 this.margins_ = new Margins(left, top, right, bottom); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 MarginSettings.DEFAULT_MARGINS_OPTION_INDEX = 0; | 159 MarginSettings.DEFAULT_MARGINS_OPTION_INDEX = 0; |
| 180 // Group name corresponding to the top margin. | 160 // Group name corresponding to the top margin. |
| 181 MarginSettings.TOP_GROUP = 'top'; | 161 MarginSettings.TOP_GROUP = 'top'; |
| 182 // Group name corresponding to the left margin. | 162 // Group name corresponding to the left margin. |
| 183 MarginSettings.LEFT_GROUP = 'left'; | 163 MarginSettings.LEFT_GROUP = 'left'; |
| 184 // Group name corresponding to the right margin. | 164 // Group name corresponding to the right margin. |
| 185 MarginSettings.RIGHT_GROUP = 'right'; | 165 MarginSettings.RIGHT_GROUP = 'right'; |
| 186 // Group name corresponding to the bottom margin. | 166 // Group name corresponding to the bottom margin. |
| 187 MarginSettings.BOTTOM_GROUP = 'bottom'; | 167 MarginSettings.BOTTOM_GROUP = 'bottom'; |
| 188 | 168 |
| 169 /** |
| 170 * Extracts the number formatting and measurement system for the current |
| 171 * locale. |
| 172 * @param {string} numberFormat Is the formatted version of a sample number, |
| 173 * sent from the backend. |
| 174 * @oaram {number} measurementSystem 0 for SI (aka metric system), 1 for the |
| 175 * system used in the US. Note: Mathces UMeasurementSystem enum in |
| 176 * third_party/icu/public/i18n/unicode/ulocdata.h. |
| 177 */ |
| 178 MarginSettings.setNumberFormatAndMeasurementSystem = function( |
| 179 numberFormat, measurementSystem) { |
| 180 var regex = /^(\d+)(\.|\,)(\d+)(\.|\,)(\d+)$/; |
| 181 var matches = numberFormat.match(regex); |
| 182 MarginSettings.thousandsPoint = matches[2]; |
| 183 MarginSettings.decimalPoint = matches[4]; |
| 184 MarginSettings.useMetricSystem = measurementSystem == 0; |
| 185 }; |
| 186 |
| 189 cr.addSingletonGetter(MarginSettings); | 187 cr.addSingletonGetter(MarginSettings); |
| 190 | 188 |
| 191 MarginSettings.prototype = { | 189 MarginSettings.prototype = { |
| 192 /** | 190 /** |
| 193 * Returns a dictionary of the four custom margin values. | 191 * Returns a dictionary of the four custom margin values. |
| 194 * @return {object} | 192 * @return {object} |
| 195 */ | 193 */ |
| 196 get customMargins() { | 194 get customMargins() { |
| 197 var margins = {}; | 195 var margins = {}; |
| 198 margins.marginLeft = this.customMargins_.left; | 196 margins.marginLeft = this.customMargins_.left; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 }, | 247 }, |
| 250 | 248 |
| 251 /** | 249 /** |
| 252 * If the custom margin values have changed then request a new preview based | 250 * If the custom margin values have changed then request a new preview based |
| 253 * on the newly set margins. | 251 * on the newly set margins. |
| 254 * @private | 252 * @private |
| 255 */ | 253 */ |
| 256 requestPreviewIfNeeded_: function() { | 254 requestPreviewIfNeeded_: function() { |
| 257 if (!this.areMarginSettingsValid()) | 255 if (!this.areMarginSettingsValid()) |
| 258 return; | 256 return; |
| 259 if (this.customMargins_.toInches(2).isEqual( | 257 if (this.customMargins_.isEqual(this.previousCustomMargins_)) |
| 260 this.previousCustomMargins_.toInches(2))) { | |
| 261 return; | 258 return; |
| 262 } | |
| 263 this.previousCustomMargins_.copy(this.customMargins_); | 259 this.previousCustomMargins_.copy(this.customMargins_); |
| 264 setDefaultValuesAndRegeneratePreview(false); | 260 setDefaultValuesAndRegeneratePreview(false); |
| 265 }, | 261 }, |
| 266 | 262 |
| 267 /** | 263 /** |
| 268 * Listener executed when the mouse is over the sidebar. If the custom | 264 * Listener executed when the mouse is over the sidebar. If the custom |
| 269 * margin lines are displayed, then, it fades them out. | 265 * margin lines are displayed, then, it fades them out. |
| 270 * @private | 266 * @private |
| 271 */ | 267 */ |
| 272 onSidebarMouseOver_: function(e) { | 268 onSidebarMouseOver_: function(e) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 MarginSettings.MINIMUM_MARGINS_DISTANCE; | 351 MarginSettings.MINIMUM_MARGINS_DISTANCE; |
| 356 marginValueLimits[1] = this.pageWidth_ - this.customMargins_.right - | 352 marginValueLimits[1] = this.pageWidth_ - this.customMargins_.right - |
| 357 MarginSettings.MINIMUM_MARGINS_DISTANCE; | 353 MarginSettings.MINIMUM_MARGINS_DISTANCE; |
| 358 marginValueLimits[2] = this.pageWidth_ - this.customMargins_.left - | 354 marginValueLimits[2] = this.pageWidth_ - this.customMargins_.left - |
| 359 MarginSettings.MINIMUM_MARGINS_DISTANCE; | 355 MarginSettings.MINIMUM_MARGINS_DISTANCE; |
| 360 marginValueLimits[3] = this.pageHeight_ - this.customMargins_.top - | 356 marginValueLimits[3] = this.pageHeight_ - this.customMargins_.top - |
| 361 MarginSettings.MINIMUM_MARGINS_DISTANCE; | 357 MarginSettings.MINIMUM_MARGINS_DISTANCE; |
| 362 | 358 |
| 363 for (var i = 0; i < marginValueLimits.length; i++) { | 359 for (var i = 0; i < marginValueLimits.length; i++) { |
| 364 marginValueLimits[i] = Math.max(marginValueLimits[i], 0); | 360 marginValueLimits[i] = Math.max(marginValueLimits[i], 0); |
| 365 marginValueLimits[i] = print_preview.convertPointsToInchesTextAndBack( | 361 marginValueLimits[i] = print_preview.convertPointsToLocaleUnitsAndBack( |
| 366 marginValueLimits[i]); | 362 marginValueLimits[i]); |
| 367 } | 363 } |
| 368 return marginValueLimits; | 364 return marginValueLimits; |
| 369 }, | 365 }, |
| 370 | 366 |
| 371 /** | 367 /** |
| 372 * @return {array} The margin value limits positions normalized to the total | 368 * @return {array} The margin value limits positions normalized to the total |
| 373 * width and height of the plugin and with respect to the top left | 369 * width and height of the plugin and with respect to the top left |
| 374 * corner of the plugin. | 370 * corner of the plugin. |
| 375 */ | 371 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 394 * When the user stops typing in the margin text box a new print preview is | 390 * When the user stops typing in the margin text box a new print preview is |
| 395 * requested, only if | 391 * requested, only if |
| 396 * 1) The input is compeletely valid (it can be parsed in its entirety). | 392 * 1) The input is compeletely valid (it can be parsed in its entirety). |
| 397 * 2) The newly selected margins differ from the previously selected. | 393 * 2) The newly selected margins differ from the previously selected. |
| 398 * @param {cr.Event} event The change event holding information about what | 394 * @param {cr.Event} event The change event holding information about what |
| 399 * changed. | 395 * changed. |
| 400 * @private | 396 * @private |
| 401 */ | 397 */ |
| 402 onMarginTextValueMayHaveChanged_: function(event) { | 398 onMarginTextValueMayHaveChanged_: function(event) { |
| 403 var marginBox = event.target; | 399 var marginBox = event.target; |
| 404 var marginBoxValue = convertInchesToPoints(marginBox.margin); | 400 var marginBoxValue = |
| 401 print_preview.convertLocaleUnitsToPoints(marginBox.margin); |
| 405 this.customMargins_[marginBox.marginGroup] = marginBoxValue; | 402 this.customMargins_[marginBox.marginGroup] = marginBoxValue; |
| 406 this.requestPreviewIfNeeded_(); | 403 this.requestPreviewIfNeeded_(); |
| 407 }, | 404 }, |
| 408 | 405 |
| 409 /** | 406 /** |
| 410 * @type {print_preview.MarginsUI} The object holding the UI for specifying | 407 * @type {print_preview.MarginsUI} The object holding the UI for specifying |
| 411 * custom margins. | 408 * custom margins. |
| 412 */ | 409 */ |
| 413 get marginsUI() { | 410 get marginsUI() { |
| 414 if (!this.marginsUI_) { | 411 if (!this.marginsUI_) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 | 508 |
| 512 /** | 509 /** |
| 513 * Executes when the custom margins option is selected. | 510 * Executes when the custom margins option is selected. |
| 514 * @private | 511 * @private |
| 515 */ | 512 */ |
| 516 onCustomMarginsSelected_: function() { | 513 onCustomMarginsSelected_: function() { |
| 517 this.addCustomMarginEventListeners_(); | 514 this.addCustomMarginEventListeners_(); |
| 518 | 515 |
| 519 if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) { | 516 if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) { |
| 520 this.customMargins_ = this.currentDefaultPageLayout.margins_; | 517 this.customMargins_ = this.currentDefaultPageLayout.margins_; |
| 521 this.customMargins_.roundToInches(); | 518 this.customMargins_.roundToLocaleUnits(); |
| 522 } | 519 } |
| 523 this.previousCustomMargins_.copy(this.customMargins_); | 520 this.previousCustomMargins_.copy(this.customMargins_); |
| 524 | 521 |
| 525 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) { | 522 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) { |
| 526 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; | 523 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; |
| 527 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; | 524 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; |
| 528 } | 525 } |
| 529 | 526 |
| 530 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout; | 527 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout; |
| 531 this.drawCustomMarginsUI_(); | 528 this.drawCustomMarginsUI_(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 */ | 599 */ |
| 603 onPDFLoaded_: function() { | 600 onPDFLoaded_: function() { |
| 604 if (!previewModifiable) | 601 if (!previewModifiable) |
| 605 fadeOutElement(this.marginsOption_); | 602 fadeOutElement(this.marginsOption_); |
| 606 } | 603 } |
| 607 }; | 604 }; |
| 608 | 605 |
| 609 return { | 606 return { |
| 610 MarginSettings: MarginSettings, | 607 MarginSettings: MarginSettings, |
| 611 PageLayout: PageLayout, | 608 PageLayout: PageLayout, |
| 609 setNumberFormatAndMeasurementSystem: |
| 610 MarginSettings.setNumberFormatAndMeasurementSystem, |
| 612 }; | 611 }; |
| 613 }); | 612 }); |
| OLD | NEW |