Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/resources/print_preview/margin_settings.js

Issue 8345025: Print Preview: Adding support for localized margin units and format. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 roundToUserLocaleUnits: 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.convertPointsToUserLocaleUnitsAndBack(
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
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
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
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] =
366 marginValueLimits[i]); 362 print_preview.convertPointsToUserLocaleUnitsAndBack(
Evan Stade 2011/10/21 01:33:02 nit: s/UserLocale/Locale/ here and elsewhere
dpapad 2011/10/21 16:12:48 Done.
363 marginValueLimits[i]);
367 } 364 }
368 return marginValueLimits; 365 return marginValueLimits;
369 }, 366 },
370 367
371 /** 368 /**
372 * @return {array} The margin value limits positions normalized to the total 369 * @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 370 * width and height of the plugin and with respect to the top left
374 * corner of the plugin. 371 * corner of the plugin.
375 */ 372 */
376 getMarginValueLimitsInPercent_: function() { 373 getMarginValueLimitsInPercent_: function() {
(...skipping 17 matching lines...) Expand all
394 * When the user stops typing in the margin text box a new print preview is 391 * When the user stops typing in the margin text box a new print preview is
395 * requested, only if 392 * requested, only if
396 * 1) The input is compeletely valid (it can be parsed in its entirety). 393 * 1) The input is compeletely valid (it can be parsed in its entirety).
397 * 2) The newly selected margins differ from the previously selected. 394 * 2) The newly selected margins differ from the previously selected.
398 * @param {cr.Event} event The change event holding information about what 395 * @param {cr.Event} event The change event holding information about what
399 * changed. 396 * changed.
400 * @private 397 * @private
401 */ 398 */
402 onMarginTextValueMayHaveChanged_: function(event) { 399 onMarginTextValueMayHaveChanged_: function(event) {
403 var marginBox = event.target; 400 var marginBox = event.target;
404 var marginBoxValue = convertInchesToPoints(marginBox.margin); 401 var marginBoxValue =
402 print_preview.convertUserLocaleUnitsToPoints(marginBox.margin);
405 this.customMargins_[marginBox.marginGroup] = marginBoxValue; 403 this.customMargins_[marginBox.marginGroup] = marginBoxValue;
406 this.requestPreviewIfNeeded_(); 404 this.requestPreviewIfNeeded_();
407 }, 405 },
408 406
409 /** 407 /**
410 * @type {print_preview.MarginsUI} The object holding the UI for specifying 408 * @type {print_preview.MarginsUI} The object holding the UI for specifying
411 * custom margins. 409 * custom margins.
412 */ 410 */
413 get marginsUI() { 411 get marginsUI() {
414 if (!this.marginsUI_) { 412 if (!this.marginsUI_) {
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 509
512 /** 510 /**
513 * Executes when the custom margins option is selected. 511 * Executes when the custom margins option is selected.
514 * @private 512 * @private
515 */ 513 */
516 onCustomMarginsSelected_: function() { 514 onCustomMarginsSelected_: function() {
517 this.addCustomMarginEventListeners_(); 515 this.addCustomMarginEventListeners_();
518 516
519 if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) { 517 if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) {
520 this.customMargins_ = this.currentDefaultPageLayout.margins_; 518 this.customMargins_ = this.currentDefaultPageLayout.margins_;
521 this.customMargins_.roundToInches(); 519 this.customMargins_.roundToUserLocaleUnits();
522 } 520 }
523 this.previousCustomMargins_.copy(this.customMargins_); 521 this.previousCustomMargins_.copy(this.customMargins_);
524 522
525 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) { 523 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) {
526 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; 524 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth;
527 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; 525 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight;
528 } 526 }
529 527
530 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout; 528 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout;
531 this.drawCustomMarginsUI_(); 529 this.drawCustomMarginsUI_();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 if (!previewModifiable) 602 if (!previewModifiable)
605 fadeOutElement(this.marginsOption_); 603 fadeOutElement(this.marginsOption_);
606 } 604 }
607 }; 605 };
608 606
609 return { 607 return {
610 MarginSettings: MarginSettings, 608 MarginSettings: MarginSettings,
611 PageLayout: PageLayout, 609 PageLayout: PageLayout,
612 }; 610 };
613 }); 611 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698