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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 }; | 86 }; |
87 | 87 |
88 /** | 88 /** |
89 * @constructor | 89 * @constructor |
90 * Class describing the layout of the page. | 90 * Class describing the layout of the page. |
91 */ | 91 */ |
92 function PageLayout(width, height, left, top, right, bottom) { | 92 function PageLayout(width, height, left, top, right, bottom) { |
93 this.contentWidth_ = width; | 93 this.contentWidth_ = width; |
94 this.contentHeight_ = height; | 94 this.contentHeight_ = height; |
95 this.margins_ = new Margins(left, top, right, bottom); | 95 this.margins_ = new Margins(left, top, right, bottom); |
96 this.margins_.roundToLocaleUnits(); | |
96 } | 97 } |
97 | 98 |
98 PageLayout.prototype = { | 99 PageLayout.prototype = { |
99 /** | 100 /** |
100 * @type {number} The width of the page. | 101 * @type {number} The width of the page. |
101 */ | 102 */ |
102 get pageWidth() { | 103 get pageWidth() { |
103 return this.margins_.left + this.margins_.right + this.contentWidth_; | 104 return this.margins_.left + this.margins_.right + this.contentWidth_; |
104 }, | 105 }, |
105 | 106 |
106 /** | 107 /** |
107 * @type {number} The height of the page. | 108 * @type {number} The height of the page. |
108 */ | 109 */ |
109 get pageHeight() { | 110 get pageHeight() { |
110 return this.margins_.top + this.margins_.bottom + this.contentHeight_; | 111 return this.margins_.top + this.margins_.bottom + this.contentHeight_; |
111 } | 112 } |
112 }; | 113 }; |
113 | 114 |
114 /** | 115 /** |
115 * Creates a MarginSettings object. This object encapsulates all settings and | 116 * Creates a MarginSettings object. This object encapsulates all settings and |
116 * logic related to the margins mode. | 117 * logic related to the margins mode. |
117 * @constructor | 118 * @constructor |
118 */ | 119 */ |
119 function MarginSettings() { | 120 function MarginSettings() { |
120 this.marginsOption_ = $('margins-option'); | 121 this.marginsOption_ = $('margins-option'); |
121 this.marginList_ = $('margin-list'); | 122 this.marginList_ = $('margin-list'); |
122 this.marginsUI_ = null; | 123 this.marginsUI_ = null; |
123 | 124 |
124 // Holds the custom margin values in points (if set). | 125 // Holds the custom margin values in points (if set). |
125 this.customMargins_ = new Margins(-1, -1, -1, -1); | 126 this.customMargins_ = null; |
126 // Holds the previous custom margin values in points. | 127 // Holds the previous custom margin values in points. |
127 this.previousCustomMargins_ = new Margins(-1, -1, -1, -1); | 128 this.previousCustomMargins_ = null; |
128 // Holds the width of the page in points. | 129 // Holds the width of the page in points. |
129 this.pageWidth_ = -1; | 130 this.pageWidth_ = -1; |
130 // Holds the height of the page in points. | 131 // Holds the height of the page in points. |
131 this.pageHeight_ = -1; | 132 this.pageHeight_ = -1; |
132 // The last selected margin option. | 133 // @type {boolean} True if the margins UI should be diplayed when the next |
133 this.lastSelectedOption_ = MarginSettings.MARGINS_VALUE_DEFAULT; | 134 // |customEvents.PDF_LOADED| event occurs. |
134 | 135 this.forceMarginsUIOnPDFLoad_ = false; |
135 // Holds the currently updated default page layout values. | 136 // Holds the currently updated default page layout values. |
136 this.currentDefaultPageLayout = null; | 137 this.currentDefaultPageLayout = null; |
137 // Holds the default page layout values when the custom margins was last | |
138 // selected. | |
139 this.previousDefaultPageLayout_ = null; | |
140 | 138 |
141 // True if the margins UI should be shown regardless of mouse position. | 139 // True if the margins UI should be shown regardless of mouse position. |
142 this.forceDisplayingMarginLines_ = true; | 140 this.forceDisplayingMarginLines_ = true; |
143 | 141 |
144 // @type {EventTracker} Used to keep track of certain event listeners. | 142 // @type {EventTracker} Used to keep track of certain event listeners. |
145 this.eventTracker_ = new EventTracker(); | 143 this.eventTracker_ = new EventTracker(); |
146 | 144 |
147 this.addEventListeners_(); | 145 this.addEventListeners_(); |
148 } | 146 } |
149 | 147 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 get customMargins() { | 194 get customMargins() { |
197 var margins = {}; | 195 var margins = {}; |
198 margins.marginLeft = this.customMargins_.left; | 196 margins.marginLeft = this.customMargins_.left; |
199 margins.marginTop = this.customMargins_.top; | 197 margins.marginTop = this.customMargins_.top; |
200 margins.marginRight = this.customMargins_.right; | 198 margins.marginRight = this.customMargins_.right; |
201 margins.marginBottom = this.customMargins_.bottom; | 199 margins.marginBottom = this.customMargins_.bottom; |
202 return margins; | 200 return margins; |
203 }, | 201 }, |
204 | 202 |
205 /** | 203 /** |
204 * Sets |this.customMargins_| according to |margins|. | |
205 * @param {{marginLeft: number, marginTop: number, marginRight: bottom, | |
kmadhusu
2011/11/09 00:57:54
"marginRight: bottom" => "marginBottom: number"
dpapad
2011/11/09 03:19:00
Done.
| |
206 * marginRight: number}} margins An object holding the four margin | |
207 * values. | |
208 */ | |
209 set customMargins(margins) { | |
210 this.customMargins_.left = margins.marginLeft; | |
211 this.customMargins_.top = margins.marginTop; | |
212 this.customMargins_.right = margins.marginRight; | |
213 this.customMargins_.bottom = margins.marginBottom; | |
214 }, | |
215 | |
216 /** | |
206 * @return {number} The value of the selected margin option. | 217 * @return {number} The value of the selected margin option. |
207 */ | 218 */ |
208 get selectedMarginsValue() { | 219 get selectedMarginsValue() { |
209 var val = this.marginList_.options[this.marginList_.selectedIndex].value; | 220 var val = this.marginList_.options[this.marginList_.selectedIndex].value; |
210 return parseInt(val, 10); | 221 return parseInt(val, 10); |
211 }, | 222 }, |
212 | 223 |
213 /** | 224 /** |
214 * Sets the current margin selection to |lastUsedMarginsType|. | 225 * Sets the current margin selection to |lastUsedMarginType|. |
215 * @param {number} lastUsedMarginsType An integer value identifying a margin | 226 * @param {number} lastUsedMarginType An integer value identifying a margin |
216 * type according to MarginType enum in printing/print_job_constants.h. | 227 * type according to MarginType enum in printing/print_job_constants.h. |
228 * @param {Object} lastUsedCustomMargins The last used custom margins. If | |
229 * custom margins have not been used before | |
kmadhusu
2011/11/09 00:57:54
nit:
@param {Object} lastUsedCustomMargins The la
dpapad
2011/11/09 03:19:00
As discussed in IM, this is not true anymore, last
| |
230 * |margin{Top|Bottom|Left|Right}| attributes are missing. | |
217 */ | 231 */ |
218 setLastUsedMarginsType: function(lastUsedMarginsType) { | 232 setLastUsedMargins: function(lastUsedMarginType, lastUsedCustomMargins) { |
233 this.forceMarginsUIOnPDFLoad_ = | |
234 lastUsedMarginType == MarginSettings.MARGINS_VALUE_CUSTOM; | |
219 this.marginList_.selectedIndex = | 235 this.marginList_.selectedIndex = |
220 this.getMarginOptionIndexByValue_(lastUsedMarginsType); | 236 this.getMarginOptionIndexByValue_(lastUsedMarginType); |
237 if (lastUsedCustomMargins.hasOwnProperty('marginTop')) { | |
238 this.customMargins_ = new Margins(-1, -1, -1 , -1); | |
239 this.customMargins = lastUsedCustomMargins; | |
kmadhusu
2011/11/09 00:57:54
"this.customMargins" => "this.customMargins_"? Mis
dpapad
2011/11/09 03:19:00
This is on purpose, leaving the underscore out, ca
| |
240 } | |
221 }, | 241 }, |
222 | 242 |
223 /** | 243 /** |
224 * @return {number} The total width of the plugin in points. | 244 * @return {number} The total width of the plugin in points. |
225 */ | 245 */ |
226 get totalWidthInPoints() { | 246 get totalWidthInPoints() { |
227 var pageInformation = previewArea.pageLocationNormalized; | 247 var pageInformation = previewArea.pageLocationNormalized; |
228 return this.pageWidth_ / pageInformation.width; | 248 return this.pageWidth_ / pageInformation.width; |
229 }, | 249 }, |
230 | 250 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
283 }, | 303 }, |
284 | 304 |
285 /** | 305 /** |
286 * If the custom margin values have changed then request a new preview based | 306 * If the custom margin values have changed then request a new preview based |
287 * on the newly set margins. | 307 * on the newly set margins. |
288 * @private | 308 * @private |
289 */ | 309 */ |
290 requestPreviewIfNeeded_: function() { | 310 requestPreviewIfNeeded_: function() { |
291 if (!this.areMarginSettingsValid()) | 311 if (!this.areMarginSettingsValid()) |
292 return; | 312 return; |
293 if (this.customMargins_.isEqual(this.previousCustomMargins_)) | 313 |
314 if (this.previousCustomMargins_ && | |
315 this.customMargins_.isEqual(this.previousCustomMargins_)) | |
294 return; | 316 return; |
317 | |
318 if (!this.previousCustomMargins_) | |
319 this.previousCustomMargins_ = new Margins(-1, -1, -1, -1); | |
295 this.previousCustomMargins_.copy(this.customMargins_); | 320 this.previousCustomMargins_.copy(this.customMargins_); |
kmadhusu
2011/11/09 00:57:54
Do you need to the same in line 239?
this.customM
dpapad
2011/11/09 03:19:00
It is not needed there, because a preview request
| |
296 setDefaultValuesAndRegeneratePreview(false); | 321 setDefaultValuesAndRegeneratePreview(false); |
297 }, | 322 }, |
298 | 323 |
299 /** | 324 /** |
300 * Listener executed when the mouse is over the sidebar. If the custom | 325 * Listener executed when the mouse is over the sidebar. If the custom |
301 * margin lines are displayed, then, it fades them out. | 326 * margin lines are displayed, then, it fades them out. |
302 * @private | 327 * @private |
303 */ | 328 */ |
304 onSidebarMouseOver_: function(e) { | 329 onSidebarMouseOver_: function(e) { |
330 $('mainview').onmouseover = this.onMainviewMouseOver_.bind(this); | |
331 $('sidebar').onmouseover = null; | |
305 if (!this.forceDisplayingMarginLines_) | 332 if (!this.forceDisplayingMarginLines_) |
306 this.marginsUI.hide(false); | 333 this.marginsUI.hide(false); |
307 }, | 334 }, |
308 | 335 |
309 /** | 336 /** |
310 * Listener executed when the mouse is over the main view. If the custom | 337 * Listener executed when the mouse is over the main view. If the custom |
311 * margin lines are hidden, then, it fades them in. | 338 * margin lines are hidden, then, it fades them in. |
312 * @private | 339 * @private |
313 */ | 340 */ |
314 onMainviewMouseOver_: function() { | 341 onMainviewMouseOver_: function() { |
342 $('mainview').onmouseover = null; | |
343 $('sidebar').onmouseover = this.onSidebarMouseOver_.bind(this); | |
315 this.forceDisplayingMarginLines_ = false; | 344 this.forceDisplayingMarginLines_ = false; |
316 this.marginsUI.show(); | 345 this.marginsUI.show(); |
317 }, | 346 }, |
318 | 347 |
319 /** | 348 /** |
320 * Adds listeners to all margin related controls. | 349 * Adds listeners to all margin related controls. |
321 * @private | 350 * @private |
322 */ | 351 */ |
323 addEventListeners_: function() { | 352 addEventListeners_: function() { |
324 this.marginList_.onchange = this.onMarginsChanged_.bind(this); | 353 this.marginList_.onchange = this.onMarginsChanged_.bind(this); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
361 return dragDelta * this.totalHeightInPoints; | 390 return dragDelta * this.totalHeightInPoints; |
362 } else { | 391 } else { |
363 return dragDelta * this.totalWidthInPoints; | 392 return dragDelta * this.totalWidthInPoints; |
364 } | 393 } |
365 }, | 394 }, |
366 | 395 |
367 /** | 396 /** |
368 * @return {boolean} True if the margin settings are valid. | 397 * @return {boolean} True if the margin settings are valid. |
369 */ | 398 */ |
370 areMarginSettingsValid: function() { | 399 areMarginSettingsValid: function() { |
371 if (this.marginsUI_ == null) | 400 if (!this.isCustomMarginsSelected() || !this.marginsUI_) |
372 return true; | 401 return true; |
373 | 402 |
374 var pairs = this.marginsUI.pairsAsList; | 403 var pairs = this.marginsUI.pairsAsList; |
375 return pairs.every(function(pair) { return pair.box_.isValid; }); | 404 return pairs.every(function(pair) { return pair.box_.isValid; }); |
376 }, | 405 }, |
377 | 406 |
378 /** | 407 /** |
379 * Calculates the maximum allowable value of the selected margin text for | 408 * Calculates the maximum allowable value of the selected margin text for |
380 * every margin. | 409 * every margin. |
381 * @return {array} The maximum allowable value in points in order top, left, | 410 * @return {array} The maximum allowable value in points in order top, left, |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 false); | 496 false); |
468 this.eventTracker_.add(document, customEvents.MARGIN_TEXTBOX_FOCUSED, | 497 this.eventTracker_.add(document, customEvents.MARGIN_TEXTBOX_FOCUSED, |
469 this.onMarginTextboxFocused_.bind(this), false); | 498 this.onMarginTextboxFocused_.bind(this), false); |
470 }, | 499 }, |
471 | 500 |
472 /** | 501 /** |
473 * Removes the event listeners associated with the custom margins option. | 502 * Removes the event listeners associated with the custom margins option. |
474 * @private | 503 * @private |
475 */ | 504 */ |
476 removeCustomMarginEventListeners_: function() { | 505 removeCustomMarginEventListeners_: function() { |
506 if (!this.marginsUI_) | |
507 return; | |
477 $('mainview').onmouseover = null; | 508 $('mainview').onmouseover = null; |
478 $('sidebar').onmouseover = null; | 509 $('sidebar').onmouseover = null; |
479 this.eventTracker_.remove(this.marginsUI, customEvents.MARGIN_LINE_DRAG); | 510 this.eventTracker_.remove(this.marginsUI, customEvents.MARGIN_LINE_DRAG); |
480 this.eventTracker_.remove(document, customEvents.MARGIN_TEXTBOX_FOCUSED); | 511 this.eventTracker_.remove(document, customEvents.MARGIN_TEXTBOX_FOCUSED); |
481 this.marginsUI.hide(true); | 512 this.marginsUI.hide(true); |
482 }, | 513 }, |
483 | 514 |
484 /** | 515 /** |
485 * Updates |this.marginsUI| depending on the specified margins and the | 516 * Updates |this.marginsUI| depending on the specified margins and the |
486 * position of the page within the plugin. | 517 * position of the page within the plugin. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
525 * Executes when user selects a different margin option, ie, | 556 * Executes when user selects a different margin option, ie, |
526 * |this.marginList_.selectedIndex| is changed. | 557 * |this.marginList_.selectedIndex| is changed. |
527 * @private | 558 * @private |
528 */ | 559 */ |
529 onMarginsChanged_: function() { | 560 onMarginsChanged_: function() { |
530 if (this.isDefaultMarginsSelected() || this.isMinimumMarginsSelected() || | 561 if (this.isDefaultMarginsSelected() || this.isMinimumMarginsSelected() || |
531 this.isNoMarginsSelected()) | 562 this.isNoMarginsSelected()) |
532 this.onDefaultMinimumNoMarginsSelected_(); | 563 this.onDefaultMinimumNoMarginsSelected_(); |
533 else if (this.isCustomMarginsSelected()) | 564 else if (this.isCustomMarginsSelected()) |
534 this.onCustomMarginsSelected_(); | 565 this.onCustomMarginsSelected_(); |
535 | |
536 this.lastSelectedOption_ = this.selectedMarginsValue; | |
537 }, | 566 }, |
538 | 567 |
539 /** | 568 /** |
540 * Executes when the default or minimum or no margins option is selected. | 569 * Executes when the default or minimum or no margins option is selected. |
541 * @private | 570 * @private |
542 */ | 571 */ |
543 onDefaultMinimumNoMarginsSelected_: function() { | 572 onDefaultMinimumNoMarginsSelected_: function() { |
544 this.removeCustomMarginEventListeners_(); | 573 this.removeCustomMarginEventListeners_(); |
545 this.forceDisplayingMarginLines_ = true; | 574 this.forceDisplayingMarginLines_ = true; |
575 this.previousCustomMargins_ = null; | |
546 setDefaultValuesAndRegeneratePreview(false); | 576 setDefaultValuesAndRegeneratePreview(false); |
547 }, | 577 }, |
548 | 578 |
549 /** | 579 /** |
550 * Executes when the custom margins option is selected. | 580 * Executes when the custom margins option is selected. |
551 * @private | 581 * @private |
552 */ | 582 */ |
553 onCustomMarginsSelected_: function() { | 583 onCustomMarginsSelected_: function() { |
554 this.addCustomMarginEventListeners_(); | 584 var customMarginsNotSpecified = !this.customMargins_; |
585 this.updatePageData_(); | |
555 | 586 |
556 this.customMargins_ = this.currentDefaultPageLayout.margins_; | 587 if (customMarginsNotSpecified) { |
kmadhusu
2011/11/09 00:57:54
customMarginsNotSpecified variable is not required
dpapad
2011/11/09 03:19:00
this.updatePageData_ will initialize |this.customM
| |
557 this.customMargins_.roundToLocaleUnits(); | 588 this.previousCustomMargins_ = new Margins(-1, -1, -1, -1); |
558 this.previousCustomMargins_.copy(this.customMargins_); | 589 this.previousCustomMargins_.copy(this.customMargins_); |
kmadhusu
2011/11/09 00:57:54
this.customMargins_ is null here.
Instead of lin
kmadhusu
2011/11/09 00:57:54
Can you create a helper function to do the followi
dpapad
2011/11/09 03:19:00
|this.customMargins_| is not null here, explained
dpapad
2011/11/09 03:19:00
Done. Created a clone() function which is much mor
| |
559 | 590 this.drawCustomMarginsUI_(); |
560 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) { | 591 this.addCustomMarginEventListeners_(); |
561 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; | 592 this.marginsUI.show(); |
562 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; | 593 } else { |
594 this.forceMarginsUIOnPDFLoad_ = true; | |
595 this.requestPreviewIfNeeded_(); | |
563 } | 596 } |
564 | |
565 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout; | |
566 this.drawCustomMarginsUI_(); | |
567 this.marginsUI.show(); | |
568 }, | 597 }, |
569 | 598 |
570 /** | 599 /** |
571 * Calculates the coordinates of the four margin lines. These are the | 600 * Calculates the coordinates of the four margin lines. These are the |
572 * coordinates where the margin lines should be displayed. The coordinates | 601 * coordinates where the margin lines should be displayed. The coordinates |
573 * are expressed in terms of percentages with respect to the total width | 602 * are expressed in terms of percentages with respect to the total width |
574 * and height of the plugin. | 603 * and height of the plugin. |
575 * @return {print_preview.Rect} A rectnangle that describes the position of | 604 * @return {print_preview.Rect} A rectnangle that describes the position of |
576 * the four margin lines. | 605 * the four margin lines. |
577 * @private | 606 * @private |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
620 * If custom margins is the currently selected option then change to the | 649 * If custom margins is the currently selected option then change to the |
621 * default margins option. | 650 * default margins option. |
622 * @private | 651 * @private |
623 */ | 652 */ |
624 resetMarginsIfNeeded: function() { | 653 resetMarginsIfNeeded: function() { |
625 if (this.isCustomMarginsSelected()) { | 654 if (this.isCustomMarginsSelected()) { |
626 this.marginList_.options[ | 655 this.marginList_.options[ |
627 MarginSettings.OPTION_INDEX_DEFAULT].selected = true; | 656 MarginSettings.OPTION_INDEX_DEFAULT].selected = true; |
628 this.removeCustomMarginEventListeners_(); | 657 this.removeCustomMarginEventListeners_(); |
629 this.forceDisplayingMarginLines_ = true; | 658 this.forceDisplayingMarginLines_ = true; |
630 this.lastSelectedOption_ = MarginSettings.MARGINS_VALUE_DEFAULT; | |
631 } | 659 } |
632 }, | 660 }, |
633 | 661 |
634 /** | 662 /** |
635 * Executes when a |customEvents.PDF_LOADED| event occurs. | 663 * Executes when a |customEvents.PDF_LOADED| event occurs. |
636 * @private | 664 * @private |
637 */ | 665 */ |
638 onPDFLoaded_: function() { | 666 onPDFLoaded_: function() { |
639 if (!previewModifiable) | 667 if (!previewModifiable) { |
640 fadeOutOption(this.marginsOption_); | 668 fadeOutOption(this.marginsOption_); |
669 return; | |
670 } | |
671 | |
672 if (this.forceMarginsUIOnPDFLoad_) { | |
673 this.updatePageData_(); | |
674 this.drawCustomMarginsUI_(); | |
675 this.addCustomMarginEventListeners_(); | |
676 this.marginsUI.show(); | |
677 this.forceMarginsUIOnPDFLoad_ = false; | |
678 } | |
679 }, | |
680 | |
681 /** | |
682 * Updates |this.customMargins_|, |this.pageWidth_|, |this.pageHeight_|. | |
683 * @private | |
684 */ | |
685 updatePageData_: function() { | |
686 if (!this.customMargins_) { | |
687 this.customMargins_ = new Margins(-1, -1, -1. -1); | |
688 this.customMargins_.copy(this.currentDefaultPageLayout.margins_); | |
689 } | |
690 | |
691 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; | |
692 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; | |
641 } | 693 } |
642 }; | 694 }; |
643 | 695 |
644 return { | 696 return { |
645 MarginSettings: MarginSettings, | 697 MarginSettings: MarginSettings, |
646 PageLayout: PageLayout, | 698 PageLayout: PageLayout, |
647 setNumberFormatAndMeasurementSystem: | |
648 MarginSettings.setNumberFormatAndMeasurementSystem, | |
649 }; | 699 }; |
650 }); | 700 }); |
OLD | NEW |