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', function() { | |
6 'use strict'; | |
7 | |
8 /** | |
9 * Creates a Margins object that holds four margin values. The units in which | |
10 * the values are expressed can be any numeric value. | |
11 * @constructor | |
12 * @param {number} left The left margin. | |
13 * @param {number} top The top margin. | |
14 * @param {number} right The right margin. | |
15 * @param {number} bottom The bottom margin. | |
16 */ | |
17 function Margins(left, top, right, bottom) { | |
18 this[MarginSettings.LEFT_GROUP] = left; | |
19 this[MarginSettings.TOP_GROUP] = top; | |
20 this[MarginSettings.RIGHT_GROUP] = right; | |
21 this[MarginSettings.BOTTOM_GROUP] = bottom; | |
22 } | |
23 | |
24 /** | |
25 * Rounds |value| keeping |precision| decimal numbers. Example: 32.76643 | |
26 * becomes 32.77. | |
27 * @param {number} value The number to round. | |
28 * @param {number} precision The desired precision. | |
29 * @return {number} The rounded number. | |
30 */ | |
31 Margins.roundToPrecision = function(value, precision) { | |
32 return Math.round(value * Math.pow(10, precision)) / | |
33 Math.pow(10, precision); | |
34 }; | |
35 | |
36 Margins.prototype = { | |
37 /** | |
38 * Checks if |rhs| is equal to |this|. | |
39 * @param {Margins} rhs The Margins object to compare against. | |
40 * @return {boolean} true if they are equal. | |
41 */ | |
42 isEqual: function(rhs) { | |
43 if (!rhs) | |
44 return false; | |
45 return this[MarginSettings.TOP_GROUP] === rhs[MarginSettings.TOP_GROUP] && | |
46 this[MarginSettings.LEFT_GROUP] === rhs[MarginSettings.LEFT_GROUP] && | |
47 this[MarginSettings.RIGHT_GROUP] === | |
48 rhs[MarginSettings.RIGHT_GROUP] && | |
49 this[MarginSettings.BOTTOM_GROUP] === | |
50 rhs[MarginSettings.BOTTOM_GROUP]; | |
51 }, | |
52 | |
53 clone: function() { | |
54 return new Margins(this[MarginSettings.LEFT_GROUP], | |
55 this[MarginSettings.TOP_GROUP], | |
56 this[MarginSettings.RIGHT_GROUP], | |
57 this[MarginSettings.BOTTOM_GROUP]); | |
58 }, | |
59 | |
60 /** | |
61 * Helper method returning an array of the string indices used for accessing | |
62 * all margins. | |
63 * @return {array} An array of string indices. | |
64 * @private | |
65 */ | |
66 indicesAsArray_: function() { | |
67 return [MarginSettings.LEFT_GROUP, MarginSettings.TOP_GROUP, | |
68 MarginSettings.RIGHT_GROUP, MarginSettings.BOTTOM_GROUP]; | |
69 }, | |
70 | |
71 /** | |
72 * Rounds |this| based on the precision used when displaying the margins in | |
73 * the user's prefered units. This is done by converting from points to | |
74 * those units (mm/inches) and back to points. | |
75 */ | |
76 roundToLocaleUnits: function() { | |
77 var indicesAsArray = this.indicesAsArray_(); | |
78 for (var i = 0; i < indicesAsArray.length; i++) { | |
79 this[indicesAsArray[i]] = | |
80 print_preview.convertPointsToLocaleUnitsAndBack( | |
81 this[indicesAsArray[i]]); | |
82 } | |
83 } | |
84 }; | |
85 | |
86 /** | |
87 * @constructor | |
88 * Class describing the layout of the page. | |
89 */ | |
90 function PageLayout(width, height, left, top, right, bottom) { | |
91 this.contentWidth_ = width; | |
92 this.contentHeight_ = height; | |
93 this.margins_ = new Margins(left, top, right, bottom); | |
94 this.margins_.roundToLocaleUnits(); | |
95 } | |
96 | |
97 PageLayout.prototype = { | |
98 /** | |
99 * @type {number} The width of the page. | |
100 */ | |
101 get pageWidth() { | |
102 return this.margins_.left + this.margins_.right + this.contentWidth_; | |
103 }, | |
104 | |
105 /** | |
106 * @type {number} The height of the page. | |
107 */ | |
108 get pageHeight() { | |
109 return this.margins_.top + this.margins_.bottom + this.contentHeight_; | |
110 } | |
111 }; | |
112 | |
113 /** | |
114 * Creates a MarginSettings object. This object encapsulates all settings and | |
115 * logic related to the margins mode. | |
116 * @constructor | |
117 */ | |
118 function MarginSettings() { | |
119 this.marginsOption_ = $('margins-option'); | |
120 this.marginList_ = $('margin-list'); | |
121 this.marginsUI_ = null; | |
122 | |
123 // Holds the custom margin values in points (if set). | |
124 this.customMargins_ = null; | |
125 // Holds the previous custom margin values in points. | |
126 this.previousCustomMargins_ = null; | |
127 // Holds the width of the page in points. | |
128 this.pageWidth_ = -1; | |
129 // Holds the height of the page in points. | |
130 this.pageHeight_ = -1; | |
131 // @type {boolean} True if the margins UI should be diplayed when the next | |
132 // |customEvents.PDF_LOADED| event occurs. | |
133 this.forceMarginsUIOnPDFLoad_ = false; | |
134 // Holds the currently updated default page layout values. | |
135 this.currentDefaultPageLayout = null; | |
136 | |
137 // True if the margins UI should be shown regardless of mouse position. | |
138 this.forceDisplayingMarginLines_ = true; | |
139 | |
140 // @type {EventTracker} Used to keep track of certain event listeners. | |
141 this.eventTracker_ = new EventTracker(); | |
142 | |
143 this.addEventListeners_(); | |
144 } | |
145 | |
146 // Number of points per inch. | |
147 MarginSettings.POINTS_PER_INCH = 72; | |
148 // Minimum allowed distance in points between top-bottom, left-right margins. | |
149 MarginSettings.MINIMUM_MARGINS_DISTANCE = 36; | |
150 // Margin list values. Matches enum MarginType in | |
151 // printing/print_job_constants.h. | |
152 MarginSettings.MARGINS_VALUE_DEFAULT = 0; | |
153 MarginSettings.MARGINS_VALUE_NO_MARGINS = 1; | |
154 MarginSettings.MARGINS_VALUE_MINIMUM = 2; | |
155 MarginSettings.MARGINS_VALUE_CUSTOM = 3; | |
156 // Default Margins option index. | |
157 MarginSettings.OPTION_INDEX_DEFAULT = 0; | |
158 // Group name corresponding to the top margin. | |
159 MarginSettings.TOP_GROUP = 'top'; | |
160 // Group name corresponding to the left margin. | |
161 MarginSettings.LEFT_GROUP = 'left'; | |
162 // Group name corresponding to the right margin. | |
163 MarginSettings.RIGHT_GROUP = 'right'; | |
164 // Group name corresponding to the bottom margin. | |
165 MarginSettings.BOTTOM_GROUP = 'bottom'; | |
166 | |
167 /** | |
168 * Extracts the number formatting and measurement system for the current | |
169 * locale. | |
170 * @param {string} numberFormat Is the formatted version of a sample number | |
171 * sent from the backend. | |
172 * @param {number} measurementSystem 0 for SI (aka metric system), 1 for the | |
173 * system used in the US. Note: Mathces UMeasurementSystem enum in | |
174 * third_party/icu/public/i18n/unicode/ulocdata.h. | |
175 */ | |
176 MarginSettings.setNumberFormatAndMeasurementSystem = function( | |
177 numberFormat, measurementSystem) { | |
178 var symbols = parseNumberFormat(numberFormat); | |
179 MarginSettings.thousandsPoint = symbols[0]; | |
180 MarginSettings.decimalPoint = symbols[1]; | |
181 MarginSettings.useMetricSystem = measurementSystem == 0; | |
182 }; | |
183 | |
184 cr.addSingletonGetter(MarginSettings); | |
185 | |
186 MarginSettings.prototype = { | |
187 /** | |
188 * Returns a dictionary containing the four custom margin values. | |
189 * @return {{marginLeft: number, marginTop: number, marginRight: number, | |
190 * marginBottom: number}} The dictionary. | |
191 */ | |
192 get customMargins() { | |
193 var margins = {}; | |
194 margins.marginLeft = this.customMargins_.left; | |
195 margins.marginTop = this.customMargins_.top; | |
196 margins.marginRight = this.customMargins_.right; | |
197 margins.marginBottom = this.customMargins_.bottom; | |
198 return margins; | |
199 }, | |
200 | |
201 /** | |
202 * Sets |this.customMargins_| according to |margins|. | |
203 * @param {{marginLeft: number, marginTop: number, marginRight: number, | |
204 * marginBottom: number}} margins An object holding the four margin | |
205 * values. | |
206 */ | |
207 set customMargins(margins) { | |
208 this.customMargins_.left = margins.marginLeft; | |
209 this.customMargins_.top = margins.marginTop; | |
210 this.customMargins_.right = margins.marginRight; | |
211 this.customMargins_.bottom = margins.marginBottom; | |
212 }, | |
213 | |
214 /** | |
215 * @return {number} The value of the selected margin option. | |
216 */ | |
217 get selectedMarginsValue() { | |
218 var val = this.marginList_.options[this.marginList_.selectedIndex].value; | |
219 return parseInt(val, 10); | |
220 }, | |
221 | |
222 /** | |
223 * Sets the current margin selection to |lastUsedMarginType|. | |
224 * @param {number} lastUsedMarginType An integer value identifying a margin | |
225 * type according to MarginType enum in printing/print_job_constants.h. | |
226 * @param {Object} lastUsedCustomMargins The last used custom margins. If | |
227 * custom margins have not been used before | |
228 * |margin{Top|Bottom|Left|Right}| attributes are missing. | |
229 */ | |
230 setLastUsedMargins: function(lastUsedMarginsSettings) { | |
231 var lastUsedMarginsType = lastUsedMarginsSettings['marginsType']; | |
232 this.forceMarginsUIOnPDFLoad_ = | |
233 lastUsedMarginsType == MarginSettings.MARGINS_VALUE_CUSTOM; | |
234 this.marginList_.selectedIndex = | |
235 this.getMarginOptionIndexByValue_(lastUsedMarginsType); | |
236 if (lastUsedMarginsSettings.hasOwnProperty('marginTop') && | |
237 lastUsedMarginsSettings.hasOwnProperty('marginBottom') && | |
238 lastUsedMarginsSettings.hasOwnProperty('marginRight') && | |
239 lastUsedMarginsSettings.hasOwnProperty('marginLeft')) { | |
240 this.customMargins_ = new Margins(-1, -1, -1 , -1); | |
241 this.customMargins = lastUsedMarginsSettings; | |
242 } | |
243 }, | |
244 | |
245 /** | |
246 * @return {number} The total width of the plugin in points. | |
247 */ | |
248 get totalWidthInPoints() { | |
249 var pageInformation = previewArea.pageLocationNormalized; | |
250 return this.pageWidth_ / pageInformation.width; | |
251 }, | |
252 | |
253 /** | |
254 * @return {number} The total height of the plugin in points. | |
255 */ | |
256 get totalHeightInPoints() { | |
257 var pageInformation = previewArea.pageLocationNormalized; | |
258 return this.pageHeight_ / pageInformation.height; | |
259 }, | |
260 | |
261 /** | |
262 * Maps margin type values to indices within |this.marginList_|. | |
263 * @param {number} marginTypeValue An integer value identifying a margin | |
264 * type according to MarginType enum in printing/print_job_constants.h. | |
265 * @return {number} The index within |this.marginList_| that corrsponds to | |
266 * |marginTypeValue|. | |
267 * @private | |
268 */ | |
269 getMarginOptionIndexByValue_: function(marginTypeValue) { | |
270 var options = this.marginList_.options; | |
271 for (var i = 0; i < options.length; i++) { | |
272 if (options[i].getAttribute('value') == marginTypeValue) | |
273 return i; | |
274 } | |
275 return MarginSettings.OPTION_INDEX_DEFAULT; | |
276 }, | |
277 | |
278 /** | |
279 * @return {boolean} True if default margins are selected. | |
280 */ | |
281 isDefaultMarginsSelected: function() { | |
282 return this.selectedMarginsValue == MarginSettings.MARGINS_VALUE_DEFAULT; | |
283 }, | |
284 | |
285 /** | |
286 * @return {boolean} True if no margins are selected. | |
287 */ | |
288 isNoMarginsSelected: function() { | |
289 return this.selectedMarginsValue == | |
290 MarginSettings.MARGINS_VALUE_NO_MARGINS; | |
291 }, | |
292 | |
293 /** | |
294 * @return {boolean} True if custom margins are selected. | |
295 */ | |
296 isCustomMarginsSelected: function() { | |
297 return this.selectedMarginsValue == MarginSettings.MARGINS_VALUE_CUSTOM; | |
298 }, | |
299 | |
300 /** | |
301 * @return {boolean} True if minimum margins are selected. | |
302 */ | |
303 isMinimumMarginsSelected: function() { | |
304 return this.selectedMarginsValue == MarginSettings.MARGINS_VALUE_MINIMUM; | |
305 }, | |
306 | |
307 /** | |
308 * If the custom margin values have changed then request a new preview based | |
309 * on the newly set margins. | |
310 * @private | |
311 */ | |
312 requestPreviewIfNeeded_: function() { | |
313 if (!this.areMarginSettingsValid()) | |
314 return; | |
315 | |
316 if (this.customMargins_.isEqual(this.previousCustomMargins_)) | |
317 return; | |
318 | |
319 this.previousCustomMargins_ = this.customMargins_.clone(); | |
320 setDefaultValuesAndRegeneratePreview(false); | |
321 }, | |
322 | |
323 /** | |
324 * Listener executed when the mouse is over the sidebar. If the custom | |
325 * margin lines are displayed, then, it fades them out. | |
326 * @private | |
327 */ | |
328 onSidebarMouseOver_: function(e) { | |
329 $('mainview').onmouseover = this.onMainviewMouseOver_.bind(this); | |
330 $('navbar-container').onmouseover = null; | |
331 if (!this.forceDisplayingMarginLines_) | |
332 this.marginsUI.hide(false); | |
333 }, | |
334 | |
335 /** | |
336 * Listener executed when the mouse is over the main view. If the custom | |
337 * margin lines are hidden, then, it fades them in. | |
338 * @private | |
339 */ | |
340 onMainviewMouseOver_: function() { | |
341 $('mainview').onmouseover = null; | |
342 $('navbar-container').onmouseover = this.onSidebarMouseOver_.bind(this); | |
343 this.forceDisplayingMarginLines_ = false; | |
344 this.marginsUI.show(); | |
345 }, | |
346 | |
347 /** | |
348 * Adds listeners to all margin related controls. | |
349 * @private | |
350 */ | |
351 addEventListeners_: function() { | |
352 this.marginList_.onchange = this.onMarginsChanged_.bind(this); | |
353 document.addEventListener(customEvents.PDF_LOADED, | |
354 this.onPDFLoaded_.bind(this)); | |
355 document.addEventListener(customEvents.PDF_GENERATION_ERROR, | |
356 this.onPDFGenerationError_.bind(this)); | |
357 }, | |
358 | |
359 /** | |
360 * Executes when a |customEvents.PDF_GENERATION_ERROR| event occurs. | |
361 * @private | |
362 */ | |
363 onPDFGenerationError_: function() { | |
364 if (this.isCustomMarginsSelected()) { | |
365 this.removeCustomMarginEventListeners_(); | |
366 this.marginsUI.hide(true); | |
367 } | |
368 }, | |
369 | |
370 /** | |
371 * Executes whenever a |customEvents.MARGIN_LINE_DRAG| occurs. | |
372 * @param {cr.Event} e The event that triggered this listener. | |
373 */ | |
374 onMarginsLineDrag_: function(e) { | |
375 var dragDeltaInPoints = this.convertDragDeltaToPoints_(e.dragDelta); | |
376 this.marginsUI.lastClickedMarginsUIPair.updateWhileDragging( | |
377 dragDeltaInPoints, e.destinationPoint); | |
378 }, | |
379 | |
380 /** | |
381 * @param {number} dragDelta The difference in pixels between the original | |
382 * and current postion of the last clicked margin line. | |
383 * @return {number} The difference in points. | |
384 * @private | |
385 */ | |
386 convertDragDeltaToPoints_: function(dragDelta) { | |
387 if (this.marginsUI.lastClickedMarginsUIPair.isTop_() || | |
388 this.marginsUI.lastClickedMarginsUIPair.isBottom_()) { | |
389 return dragDelta * this.totalHeightInPoints; | |
390 } else { | |
391 return dragDelta * this.totalWidthInPoints; | |
392 } | |
393 }, | |
394 | |
395 /** | |
396 * @return {boolean} True if the margin settings are valid. | |
397 */ | |
398 areMarginSettingsValid: function() { | |
399 if (!this.isCustomMarginsSelected() || !this.marginsUI_) | |
400 return true; | |
401 | |
402 var pairs = this.marginsUI.pairsAsList; | |
403 return pairs.every(function(pair) { return pair.box_.isValid; }); | |
404 }, | |
405 | |
406 /** | |
407 * Calculates the maximum allowable value of the selected margin text for | |
408 * every margin. | |
409 * @return {array} The maximum allowable value in points in order top, left, | |
410 * right, bottom. | |
411 * @private | |
412 */ | |
413 getMarginValueLimits_: function() { | |
414 var marginValueLimits = []; | |
415 marginValueLimits[0] = this.pageHeight_ - this.customMargins_.bottom - | |
416 MarginSettings.MINIMUM_MARGINS_DISTANCE; | |
417 marginValueLimits[1] = this.pageWidth_ - this.customMargins_.right - | |
418 MarginSettings.MINIMUM_MARGINS_DISTANCE; | |
419 marginValueLimits[2] = this.pageWidth_ - this.customMargins_.left - | |
420 MarginSettings.MINIMUM_MARGINS_DISTANCE; | |
421 marginValueLimits[3] = this.pageHeight_ - this.customMargins_.top - | |
422 MarginSettings.MINIMUM_MARGINS_DISTANCE; | |
423 | |
424 for (var i = 0; i < marginValueLimits.length; i++) { | |
425 marginValueLimits[i] = Math.max(marginValueLimits[i], 0); | |
426 marginValueLimits[i] = print_preview.convertPointsToLocaleUnitsAndBack( | |
427 marginValueLimits[i]); | |
428 } | |
429 return marginValueLimits; | |
430 }, | |
431 | |
432 /** | |
433 * @return {array} The margin value limits positions normalized to the total | |
434 * width and height of the plugin and with respect to the top left | |
435 * corner of the plugin. | |
436 */ | |
437 getMarginValueLimitsInPercent_: function() { | |
438 var pageInformation = previewArea.pageLocationNormalized; | |
439 var totalWidthInPoints = this.pageWidth_ / pageInformation.width; | |
440 var totalHeightInPoints = this.pageHeight_ / pageInformation.height; | |
441 var marginValueLimits = this.getMarginValueLimits_(); | |
442 var marginValueLimitsInPercent = []; | |
443 marginValueLimitsInPercent[0] = pageInformation.y + marginValueLimits[0] / | |
444 totalHeightInPoints; | |
445 marginValueLimitsInPercent[1] = pageInformation.x + marginValueLimits[1] / | |
446 totalWidthInPoints; | |
447 marginValueLimitsInPercent[2] = pageInformation.x + | |
448 pageInformation.width - marginValueLimits[2] / totalWidthInPoints; | |
449 marginValueLimitsInPercent[3] = pageInformation.y + | |
450 pageInformation.height - marginValueLimits[3] / totalHeightInPoints; | |
451 return marginValueLimitsInPercent; | |
452 }, | |
453 | |
454 /** | |
455 * When the user stops typing in the margin text box a new print preview is | |
456 * requested, only if | |
457 * 1) The input is compeletely valid (it can be parsed in its entirety). | |
458 * 2) The newly selected margins differ from the previously selected. | |
459 * @param {cr.Event} event The change event holding information about what | |
460 * changed. | |
461 * @private | |
462 */ | |
463 onMarginTextValueMayHaveChanged_: function(event) { | |
464 var marginBox = event.target; | |
465 var marginBoxValue = | |
466 print_preview.convertLocaleUnitsToPoints(marginBox.margin); | |
467 this.customMargins_[marginBox.marginGroup] = marginBoxValue; | |
468 this.requestPreviewIfNeeded_(); | |
469 }, | |
470 | |
471 /** | |
472 * @type {print_preview.MarginsUI} The object holding the UI for specifying | |
473 * custom margins. | |
474 */ | |
475 get marginsUI() { | |
476 if (!this.marginsUI_) { | |
477 this.marginsUI_ = new print_preview.MarginsUI(); | |
478 $('mainview').appendChild(this.marginsUI_); | |
479 this.marginsUI_.addObserver( | |
480 this.onMarginTextValueMayHaveChanged_.bind(this)); | |
481 } | |
482 return this.marginsUI_; | |
483 }, | |
484 | |
485 /** | |
486 * Adds listeners when the custom margins option is selected. | |
487 * @private | |
488 */ | |
489 addCustomMarginEventListeners_: function() { | |
490 $('mainview').onmouseover = this.onMainviewMouseOver_.bind(this); | |
491 $('navbar-container').onmouseover = this.onSidebarMouseOver_.bind(this); | |
492 this.eventTracker_.add(this.marginsUI, | |
493 customEvents.MARGIN_LINE_DRAG, | |
494 this.onMarginsLineDrag_.bind(this), | |
495 false); | |
496 this.eventTracker_.add(document, customEvents.MARGIN_TEXTBOX_FOCUSED, | |
497 this.onMarginTextboxFocused_.bind(this), false); | |
498 }, | |
499 | |
500 /** | |
501 * Removes the event listeners associated with the custom margins option. | |
502 * @private | |
503 */ | |
504 removeCustomMarginEventListeners_: function() { | |
505 if (!this.marginsUI_) | |
506 return; | |
507 $('mainview').onmouseover = null; | |
508 $('navbar-container').onmouseover = null; | |
509 this.eventTracker_.remove(this.marginsUI, customEvents.MARGIN_LINE_DRAG); | |
510 this.eventTracker_.remove(document, customEvents.MARGIN_TEXTBOX_FOCUSED); | |
511 this.marginsUI.hide(true); | |
512 }, | |
513 | |
514 /** | |
515 * Updates |this.marginsUI| depending on the specified margins and the | |
516 * position of the page within the plugin. | |
517 * @private | |
518 */ | |
519 drawCustomMarginsUI_: function() { | |
520 // TODO(dpapad): find out why passing |!this.areMarginsSettingsValid()| | |
521 // directly produces the opposite value even though | |
522 // |this.getMarginsRectangleInPercent_()| and | |
523 // |this.getMarginValueLimits_()| have no side effects. | |
524 previewArea.update(); | |
525 var keepDisplayedValue = !this.areMarginSettingsValid(); | |
526 this.marginsUI.update(this.getMarginsRectangleInPercent_(), | |
527 this.customMargins_, | |
528 this.getMarginValueLimits_(), | |
529 keepDisplayedValue, | |
530 this.getMarginValueLimitsInPercent_()); | |
531 this.marginsUI.draw(); | |
532 }, | |
533 | |
534 /** | |
535 * Called when there is change in the preview position or size. | |
536 */ | |
537 onPreviewPositionChanged: function() { | |
538 if (!previewArea.pdfPlugin) | |
539 return; | |
540 if (this.isCustomMarginsSelected() && previewArea.pdfLoaded && | |
541 pageSettings.totalPageCount != undefined) { | |
542 this.updatePageData_(); | |
543 this.drawCustomMarginsUI_(); | |
544 } | |
545 }, | |
546 | |
547 /** | |
548 * Executes when a margin textbox is focused. Used for improved | |
549 * accessibility. | |
550 * @private | |
551 */ | |
552 onMarginTextboxFocused_: function() { | |
553 this.forceDisplayingMarginLines_ = true; | |
554 this.marginsUI.show(); | |
555 }, | |
556 | |
557 /** | |
558 * Executes when user selects a different margin option, ie, | |
559 * |this.marginList_.selectedIndex| is changed. | |
560 * @private | |
561 */ | |
562 onMarginsChanged_: function() { | |
563 if (this.isDefaultMarginsSelected() || this.isMinimumMarginsSelected() || | |
564 this.isNoMarginsSelected()) | |
565 this.onDefaultMinimumNoMarginsSelected_(); | |
566 else if (this.isCustomMarginsSelected()) | |
567 this.onCustomMarginsSelected_(); | |
568 }, | |
569 | |
570 /** | |
571 * Executes when the default or minimum or no margins option is selected. | |
572 * @private | |
573 */ | |
574 onDefaultMinimumNoMarginsSelected_: function() { | |
575 this.removeCustomMarginEventListeners_(); | |
576 this.forceDisplayingMarginLines_ = true; | |
577 this.previousCustomMargins_ = null; | |
578 setDefaultValuesAndRegeneratePreview(false); | |
579 }, | |
580 | |
581 /** | |
582 * Executes when the custom margins option is selected. | |
583 * @private | |
584 */ | |
585 onCustomMarginsSelected_: function() { | |
586 if (!previewArea.pdfPlugin) { | |
587 this.forceMarginsUIOnPDFLoad_ = true; | |
588 return; | |
589 } | |
590 var customMarginsNotSpecified = !this.customMargins_; | |
591 this.updatePageData_(); | |
592 | |
593 if (customMarginsNotSpecified) { | |
594 this.previousCustomMargins_ = this.customMargins_.clone(); | |
595 this.drawCustomMarginsUI_(); | |
596 this.addCustomMarginEventListeners_(); | |
597 this.marginsUI.show(); | |
598 } else { | |
599 this.forceMarginsUIOnPDFLoad_ = true; | |
600 this.requestPreviewIfNeeded_(); | |
601 } | |
602 }, | |
603 | |
604 /** | |
605 * Calculates the coordinates of the four margin lines. These are the | |
606 * coordinates where the margin lines should be displayed. The coordinates | |
607 * are expressed in terms of percentages with respect to the total width | |
608 * and height of the plugin. | |
609 * @return {print_preview.Rect} A rectnangle that describes the position of | |
610 * the four margin lines. | |
611 * @private | |
612 */ | |
613 getMarginsRectangleInPercent_: function() { | |
614 var pageLocation = previewArea.pageLocationNormalized; | |
615 var marginsInPercent = this.getMarginsInPercent_(); | |
616 var leftX = pageLocation.x + marginsInPercent.left; | |
617 var topY = pageLocation.y + marginsInPercent.top; | |
618 var contentWidth = pageLocation.width - (marginsInPercent.left + | |
619 marginsInPercent.right); | |
620 var contentHeight = pageLocation.height - (marginsInPercent.top + | |
621 marginsInPercent.bottom); | |
622 return new print_preview.Rect( | |
623 leftX, topY, contentWidth, contentHeight); | |
624 }, | |
625 | |
626 /** | |
627 * @return {print_preview.Margins} The currently selected margin values | |
628 * normalized to the total width and height of the plugin. | |
629 * @private | |
630 */ | |
631 getMarginsInPercent_: function() { | |
632 return this.convertMarginsInPointsToPercent(this.customMargins_); | |
633 }, | |
634 | |
635 /** | |
636 * Converts |marginsToConvert| to points and normalizes it to the height and | |
637 * width of the plugin. | |
638 * @return {print_preview.Margins} The margins in percent. | |
639 * @private | |
640 */ | |
641 convertMarginsInPointsToPercent: function(marginsToConvert) { | |
642 var pageInformation = previewArea.pageLocationNormalized; | |
643 var totalWidthInPoints = this.pageWidth_ / pageInformation.width; | |
644 var totalHeightInPoints = this.pageHeight_ / pageInformation.height; | |
645 var marginsInPercent = new Margins( | |
646 marginsToConvert.left / totalWidthInPoints, | |
647 marginsToConvert.top / totalHeightInPoints, | |
648 marginsToConvert.right / totalWidthInPoints, | |
649 marginsToConvert.bottom / totalHeightInPoints); | |
650 return marginsInPercent; | |
651 }, | |
652 | |
653 /** | |
654 * If custom margins is the currently selected option then change to the | |
655 * default margins option. | |
656 * @private | |
657 */ | |
658 resetMarginsIfNeeded: function() { | |
659 if (this.isCustomMarginsSelected()) { | |
660 this.marginList_.options[ | |
661 MarginSettings.OPTION_INDEX_DEFAULT].selected = true; | |
662 this.removeCustomMarginEventListeners_(); | |
663 this.forceDisplayingMarginLines_ = true; | |
664 this.customMargins_ = null; | |
665 this.previousCustomMargins_ = null; | |
666 } | |
667 }, | |
668 | |
669 /** | |
670 * Executes when a |customEvents.PDF_LOADED| event occurs. | |
671 * @private | |
672 */ | |
673 onPDFLoaded_: function() { | |
674 if (!previewModifiable) { | |
675 fadeOutOption(this.marginsOption_); | |
676 return; | |
677 } | |
678 | |
679 if (this.forceMarginsUIOnPDFLoad_) { | |
680 this.updatePageData_(); | |
681 this.drawCustomMarginsUI_(); | |
682 this.addCustomMarginEventListeners_(); | |
683 this.marginsUI.show(); | |
684 this.forceMarginsUIOnPDFLoad_ = false; | |
685 } | |
686 }, | |
687 | |
688 /** | |
689 * Updates |this.customMargins_|, |this.pageWidth_|, |this.pageHeight_|. | |
690 * @private | |
691 */ | |
692 updatePageData_: function() { | |
693 if (!this.customMargins_) | |
694 this.customMargins_ = this.currentDefaultPageLayout.margins_.clone(); | |
695 | |
696 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; | |
697 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; | |
698 } | |
699 }; | |
700 | |
701 return { | |
702 MarginSettings: MarginSettings, | |
703 PageLayout: PageLayout | |
704 }; | |
705 }); | |
OLD | NEW |