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 the four margin values. | |
10 * @constructor | |
11 */ | |
12 function Margins(left, top, right, bottom) { | |
13 this.left = left; | |
14 this.top = top; | |
15 this.right = right; | |
16 this.bottom = bottom; | |
17 } | |
18 | |
19 Margins.prototype = { | |
20 /** | |
21 * Checks if the Margins object passed is equal to |this|. | |
22 * @param {Margins} rhs The Margins object to compare against. | |
23 * @return {boolean} true if they are equal. | |
24 */ | |
25 isEqual: function(rhs) { | |
26 return this.top == rhs.top && | |
Evan Stade
2011/10/04 02:49:18
use ===
dpapad
2011/10/04 20:41:21
Done.
| |
27 this.left == rhs.left && | |
28 this.right == rhs.right && | |
29 this.bottom == rhs.bottom; | |
30 }, | |
31 | |
32 /** | |
33 * Copies the four margin values from |rhs|. | |
34 * @param {Margins} rhs The Margins object values to be used. | |
35 */ | |
36 copy: function(rhs) { | |
37 this.top = rhs.top; | |
38 this.left = rhs.left; | |
39 this.right = rhs.right; | |
40 this.bottom = rhs.bottom; | |
41 }, | |
42 | |
43 /** | |
44 * Updates one of the members based on |marginGroupName| with |value|. | |
45 * @param {string} marginGroupName The group name of the margin for which | |
46 * the member variable has to be updated. | |
47 */ | |
48 update: function(marginGroupName, value) { | |
49 if (marginGroupName == MarginSettings.TOP_GROUP_NAME) | |
50 this.top = value; | |
51 else if (marginGroupName == MarginSettings.LEFT_GROUP_NAME) | |
52 this.left = value; | |
53 else if (marginGroupName == MarginSettings.RIGHT_GROUP_NAME) | |
54 this.right = value; | |
55 else if (marginGroupName == MarginSettings.BOTTOM_GROUP_NAME) | |
56 this.bottom = value; | |
57 }, | |
58 | |
59 /** | |
60 * Gets the value of the member variable based on |marginGroupName|. | |
61 * @param {string} marginGroupName The group name of the margin for which | |
62 * the member variable has to be retrieved. | |
63 */ | |
64 getValue: function(marginGroupName) { | |
65 if (marginGroupName == MarginSettings.TOP_GROUP_NAME) | |
66 return this.top; | |
67 else if (marginGroupName == MarginSettings.LEFT_GROUP_NAME) | |
68 return this.left; | |
69 else if (marginGroupName == MarginSettings.RIGHT_GROUP_NAME) | |
70 return this.right; | |
71 else if (marginGroupName == MarginSettings.BOTTOM_GROUP_NAME) | |
72 return this.bottom; | |
73 } | |
74 }; | |
75 | |
76 /** | |
77 * @constructor | |
78 * Class describing the layout of the page. | |
79 */ | |
80 function PageLayout(width, height, left, top, right, bottom) { | |
81 this.contentWidth_ = width; | |
82 this.contentHeight_ = height; | |
83 this.margins_ = new Margins(left, top, right, bottom); | |
84 } | |
85 | |
86 PageLayout.prototype = { | |
87 /** | |
88 * @type {number} The width of the page. | |
89 */ | |
90 get pageWidth() { | |
91 return this.margins_.left + this.margins_.right + this.contentWidth_; | |
92 }, | |
93 | |
94 /** | |
95 * @type {number} The height of the page. | |
96 */ | |
97 get pageHeight() { | |
98 return this.margins_.top + this.margins_.bottom + this.contentHeight_; | |
99 } | |
100 | |
Evan Stade
2011/10/04 02:49:18
extra line
dpapad
2011/10/04 20:41:21
Done.
| |
101 }; | |
102 | |
103 /** | |
9 * Creates a MarginSettings object. This object encapsulates all settings and | 104 * Creates a MarginSettings object. This object encapsulates all settings and |
10 * logic related to the margins mode. | 105 * logic related to the margins mode. |
11 * @constructor | 106 * @constructor |
12 */ | 107 */ |
13 function MarginSettings() { | 108 function MarginSettings() { |
14 this.marginsOption_ = $('margins-option'); | 109 this.marginsOption_ = $('margins-option'); |
15 this.marginList_ = $('margin-list'); | 110 this.marginList_ = $('margin-list'); |
16 // Holds the custom left margin value (if set). | 111 this.marginsUI_ = null; |
17 this.customMarginLeft_ = -1; | 112 |
18 // Holds the custom right margin value (if set). | 113 // Holds the custom margin values in points (if set). |
19 this.customMarginRight_ = -1; | 114 this.customMargins_ = new Margins(-1, -1, -1, -1); |
20 // Holds the custom top margin value (if set). | 115 // Holds the previous custom margin values in points. |
21 this.customMarginTop_ = -1; | 116 this.previousCustomMargins_ = new Margins(-1, -1, -1, -1); |
22 // Holds the custom bottom margin value (if set). | 117 // Holds the width of the page in points. |
23 this.customMarginBottom_ = -1; | 118 this.pageWidth_ = -1; |
24 // Margin list values. | 119 // Holds the height of the page in points. |
25 this.customMarginsValue_ = 2; | 120 this.pageHeight_ = -1; |
26 this.defaultMarginsValue_ = 0; | 121 // The last selected margin option. |
27 this.noMarginsValue_ = 1; | 122 this.lastSelectedOption_ = MarginSettings.MARGINS_VALUE_DEFAULT; |
28 // Default Margins option index. | 123 |
29 this.defaultMarginsIndex_ = 0; | 124 // Holds the currently updated default page layout values. |
125 this.currentDefaultPageLayout = null; | |
126 // Holds the default page layout values when the custom margins was last | |
127 // selected. | |
128 this.previousDefaultPageLayout_ = null; | |
129 | |
130 // True if the margins UI should be shown regardless of mouse position. | |
131 this.forceDisplayingMarginLines_ = true; | |
30 } | 132 } |
31 | 133 |
134 // Number of points per inch. | |
135 MarginSettings.POINTS_PER_INCH = 72; | |
136 // Margin list values. | |
137 MarginSettings.MARGINS_VALUE_CUSTOM = 2; | |
Evan Stade
2011/10/04 02:49:18
order these by value, not alphabet
dpapad
2011/10/04 20:41:21
Done.
| |
138 MarginSettings.MARGINS_VALUE_DEFAULT = 0; | |
139 MarginSettings.MARGINS_VALUE_NO_MARGINS = 1; | |
140 // Default Margins option index. | |
141 MarginSettings.DEFAULT_MARGINS_OPTION_INDEX = 0; | |
142 // Group name corresponding to the top margin. | |
143 MarginSettings.TOP_GROUP_NAME = 'margin-top'; | |
144 // Group name corresponding to the left margin. | |
145 MarginSettings.LEFT_GROUP_NAME = 'margin-left'; | |
146 // Group name corresponding to the right margin. | |
147 MarginSettings.RIGHT_GROUP_NAME = 'margin-right'; | |
148 // Group name corresponding to the bottom margin. | |
149 MarginSettings.BOTTOM_GROUP_NAME = 'margin-bottom'; | |
150 | |
151 /** | |
152 * Converts |value| from points to inches. | |
153 * @param {number} value The number in points. | |
154 * @return {number} |value| in inches. | |
155 */ | |
156 MarginSettings.convertPointsToInches = function(value) { | |
157 return value / MarginSettings.POINTS_PER_INCH; | |
158 }; | |
159 | |
160 /** | |
161 * Converts |value| from inches to points. | |
162 * @param {number} value The number in inches. | |
163 * @return {number} |value| in points. | |
164 */ | |
165 MarginSettings.convertInchesToPoints = function(value) { | |
166 return value * MarginSettings.POINTS_PER_INCH; | |
167 }; | |
168 | |
32 cr.addSingletonGetter(MarginSettings); | 169 cr.addSingletonGetter(MarginSettings); |
33 | 170 |
34 MarginSettings.prototype = { | 171 MarginSettings.prototype = { |
35 /** | 172 /** |
36 * The selection list corresponding to the margins option. | |
37 * @return {HTMLInputElement} | |
38 */ | |
39 get marginList() { | |
40 return this.marginList_; | |
41 }, | |
42 | |
43 /** | |
44 * Returns a dictionary of the four custom margin values. | 173 * Returns a dictionary of the four custom margin values. |
45 * @return {object} | 174 * @return {object} |
46 */ | 175 */ |
47 get customMargins() { | 176 get customMargins() { |
48 return {'marginLeft': this.customMarginLeft_, | 177 var margins = {}; |
49 'marginTop': this.customMarginTop_, | 178 margins.marginLeft = this.customMargins_.left; |
50 'marginRight': this.customMarginRight_, | 179 margins.marginTop = this.customMargins_.top; |
51 'marginBottom': this.customMarginBottom_}; | 180 margins.marginRight = this.customMargins_.right; |
181 margins.marginBottom = this.customMargins_.bottom; | |
182 return margins; | |
52 }, | 183 }, |
53 | 184 |
54 /** | 185 /** |
55 * Gets the value of the selected margin option. | |
56 * @private | 186 * @private |
57 * @return {number} | 187 * @return {number} The value of the selected margin option. |
58 */ | 188 */ |
59 get selectedMarginsValue_() { | 189 get selectedMarginsValue_() { |
60 return this.marginList_.options[this.marginList_.selectedIndex].value; | 190 return this.marginList_.options[this.marginList_.selectedIndex].value; |
61 }, | 191 }, |
62 | 192 |
63 /** | 193 /** |
64 * Checks whether user has selected the Default Margins option or not. | 194 * @return {boolean} True if default margins are selected. |
65 * | |
66 * @return {boolean} true if default margins are selected. | |
67 */ | 195 */ |
68 isDefaultMarginsSelected: function() { | 196 isDefaultMarginsSelected: function() { |
69 return this.selectedMarginsValue_ == this.defaultMarginsValue_; | 197 return this.selectedMarginsValue_ == MarginSettings.MARGINS_VALUE_DEFAULT; |
70 }, | 198 }, |
71 | 199 |
72 /** | 200 /** |
73 * Adds listeners to all margin related controls. The listeners take care | 201 * @return {boolean} True if no margins are selected. |
74 * of altering their behavior depending on |hasPendingPreviewRequest|. | 202 */ |
203 isNoMarginsSelected: function() { | |
204 return this.selectedMarginsValue_ == | |
205 MarginSettings.MARGINS_VALUE_NO_MARGINS; | |
206 }, | |
207 | |
208 /** | |
209 * @return {boolean} True if custom margins are selected. | |
210 */ | |
211 isCustomMarginsSelected: function() { | |
212 return this.selectedMarginsValue_ == MarginSettings.MARGINS_VALUE_CUSTOM; | |
213 }, | |
214 | |
215 /** | |
216 * If the custom margin values have changed then request a new preview based | |
217 * on the newly set margins. | |
218 * @private | |
219 */ | |
220 requestPreviewIfNeeded_: function() { | |
221 if (this.customMargins_.isEqual(this.previousCustomMargins_)) | |
222 return; | |
223 this.previousCustomMargins_.copy(this.customMargins_); | |
224 setDefaultValuesAndRegeneratePreview(false); | |
225 }, | |
226 | |
227 /** | |
228 * Listener executed when the mouse is over the sidebar. If the custom | |
229 * margin lines are displayed, then, it fades them out. | |
230 * @private | |
231 */ | |
232 onSidebarMouseOver_: function(e) { | |
233 if (!this.forceDisplayingMarginLines_) | |
234 this.marginsUI.hide(); | |
235 }, | |
236 | |
237 /** | |
238 * Listener executed when the mouse is over the main view. If the custom | |
239 * margin lines are hidden, then, it fades them in. | |
240 * @private | |
241 */ | |
242 onMainviewMouseOver_: function() { | |
243 this.forceDisplayingMarginLines_ = false; | |
244 this.marginsUI.show(); | |
245 }, | |
246 | |
247 /** | |
248 * Adds listeners to all margin related controls. | |
75 */ | 249 */ |
76 addEventListeners: function() { | 250 addEventListeners: function() { |
77 this.marginList_.onchange = this.onMarginsChanged_.bind(this); | 251 this.marginList_.onchange = this.onMarginsChanged_.bind(this); |
78 document.addEventListener('PDFLoaded', this.onPDFLoaded_.bind(this)); | 252 document.addEventListener('PDFLoaded', this.onPDFLoaded_.bind(this)); |
79 }, | 253 }, |
80 | 254 |
81 /** | 255 /** |
256 * @return {boolean} True if the margin settings are valid. | |
257 */ | |
258 areMarginSettingsValid: function() { | |
259 if (this.marginsUI_ == null) | |
260 return true; | |
261 | |
262 var pairs = this.marginsUI.pairsAsList; | |
263 for (var i = 0; i < pairs.length; i++) { | |
264 if (!pairs[i].box_.isValid) | |
265 return false; | |
266 } | |
267 return true; | |
268 }, | |
269 | |
270 /** | |
271 * Calculates the maximum allowable value of the selected margin text for | |
272 * every margin. | |
273 * @private | |
274 * @return {array} The maximum allowable value in order top, left, right, | |
275 * bottom. | |
276 */ | |
277 getMarginValueLimits_: function() { | |
278 var marginValueLimits = []; | |
279 marginValueLimits[0] = this.pageHeight_ - this.customMargins_.bottom; | |
280 marginValueLimits[1] = this.pageWidth_ - this.customMargins_.right; | |
281 marginValueLimits[2] = this.pageWidth_ - this.customMargins_.left; | |
282 marginValueLimits[3] = this.pageHeight_ - this.customMargins_.top; | |
283 return marginValueLimits; | |
284 }, | |
285 | |
286 /** | |
287 * When the user stops typing in the margin text box a new print preview is | |
288 * requested, only if | |
289 * 1) The input is compeletely valid (it can be parsed in its entirety). | |
290 * 2) The newly selected margins differ from the previous selected margins. | |
291 * @param {cr.Event} event The change event holding information about what | |
292 * changed. | |
293 * @private | |
294 */ | |
295 onMarginTextValueMayHaveChanged_: function(event) { | |
296 var marginBox = event.target; | |
297 var marginGroupName = marginBox.getAttribute('margin-group'); | |
298 var marginBoxValue = marginBox.margin; | |
299 | |
300 marginBoxValue = MarginSettings.convertInchesToPoints(marginBoxValue); | |
301 this.customMargins_.update(marginGroupName, marginBoxValue); | |
302 | |
303 if(!this.areMarginSettingsValid()) | |
304 return; | |
305 if (this.customMargins_.isEqual(this.previousCustomMargins_)) | |
306 return; | |
307 | |
308 this.requestPreviewIfNeeded_(); | |
309 }, | |
310 | |
311 /** | |
312 * @type {print_preview.MarginsUI} The object holding the UI for specifying | |
313 * custom margins. | |
314 */ | |
315 get marginsUI() { | |
316 if (this.marginsUI_ == null) { | |
317 this.marginsUI_ = new print_preview.MarginsUI($('mainview')); | |
318 this.marginsUI_.addObserver( | |
319 this.onMarginTextValueMayHaveChanged_.bind(this)); | |
320 } | |
321 return this.marginsUI_; | |
322 }, | |
323 | |
324 /** | |
325 * Adds listeners when the custom margins option is selected. | |
326 * @private | |
327 */ | |
328 addCustomMarginEventListeners_: function() { | |
329 $('mainview').onmouseover = this.onMainviewMouseOver_.bind(this); | |
330 $('sidebar').onmouseover = this.onSidebarMouseOver_.bind(this); | |
331 }, | |
332 | |
333 /** | |
334 * Removes the event listeners associated with the custom margins option. | |
335 * @private | |
336 */ | |
337 removeCustomMarginEventListeners_: function() { | |
338 $('mainview').onmouseover = null; | |
339 $('sidebar').onmouseover = null; | |
340 this.marginsUI.hide(); | |
341 }, | |
342 | |
343 /** | |
344 * Updates |this.marginsUI| depending on the specified margins and the | |
345 * position of the page within the plugin. | |
346 * @private | |
347 */ | |
348 drawCustomMarginsUI_: function() { | |
349 // TODO(dpapad): find out why passing |!this.areMarginsSettingsValid()| | |
350 // directly produces the opposite value even though | |
351 // |this.getMarginsRectangleInPercent_()| and | |
352 // |this.getMarginValueLimits_()| have no side effects. | |
353 var keepDisplayedValue = !this.areMarginSettingsValid(); | |
354 this.marginsUI.update(this.getMarginsRectangleInPercent_(), | |
355 this.customMargins_, | |
356 this.getMarginValueLimits_(), | |
357 keepDisplayedValue); | |
358 this.marginsUI.draw(); | |
359 }, | |
360 | |
361 /** | |
362 * Called when there is change in the preview position or size. | |
363 */ | |
364 onPreviewPositionChanged: function() { | |
365 if (this.isCustomMarginsSelected() && previewArea.pdfLoaded && | |
366 pageSettings.totalPageCount != undefined) { | |
367 this.drawCustomMarginsUI_(); | |
368 } | |
369 }, | |
370 | |
371 /** | |
82 * Listener executing when user selects a different margin option, ie, | 372 * Listener executing when user selects a different margin option, ie, |
83 * |this.marginList_| is changed. | 373 * |this.marginList_.selectedIndex| is changed. |
84 * @private | 374 * @private |
85 */ | 375 */ |
86 onMarginsChanged_: function() { | 376 onMarginsChanged_: function() { |
87 if (this.selectedMarginsValue_ == this.defaultMarginsValue_) { | 377 if (this.isDefaultMarginsSelected()) |
88 setDefaultValuesAndRegeneratePreview(false); | 378 this.onDefaultMarginsSelected_(); |
89 } else if (this.selectedMarginsValue_ == this.noMarginsValue_) { | 379 else if (this.isNoMarginsSelected()) |
90 this.customMarginLeft_ = 0; | 380 this.onNoMarginsSelected_(); |
91 this.customMarginTop_ = 0; | 381 else if (this.isCustomMarginsSelected()) |
92 this.customMarginRight_ = 0; | 382 this.onCustomMarginsSelected_(); |
93 this.customMarginBottom_ = 0; | 383 |
94 setDefaultValuesAndRegeneratePreview(false); | 384 this.lastSelectedOption_ = this.selectedMarginsValue_; |
95 } | 385 }, |
96 // TODO(aayushkumar): Add handler for custom margins | 386 |
387 /** | |
388 * Listener executing when the default margins option is selected. | |
389 * @private | |
390 */ | |
391 onDefaultMarginsSelected_: function() { | |
392 this.removeCustomMarginEventListeners_(); | |
393 this.forceDisplayingMarginLines_ = true; | |
394 setDefaultValuesAndRegeneratePreview(false); | |
395 }, | |
396 | |
397 /** | |
398 * Listener executing when the no margins option is selected. | |
399 * @private | |
400 */ | |
401 onNoMarginsSelected_: function() { | |
402 this.removeCustomMarginEventListeners_(); | |
403 this.forceDisplayingMarginLines_ = true; | |
404 this.customMargins_ = new Margins(0, 0, 0, 0); | |
405 setDefaultValuesAndRegeneratePreview(false); | |
406 }, | |
407 | |
408 /** | |
409 * Listener executing when the custom margins option is selected. | |
410 * @private | |
411 */ | |
412 onCustomMarginsSelected_: function() { | |
413 this.addCustomMarginEventListeners_(); | |
414 | |
415 if (this.lastSelectedOption_ == MarginSettings.MARGINS_VALUE_DEFAULT) | |
416 this.customMargins_ = this.currentDefaultPageLayout.margins_; | |
417 this.previousCustomMargins_.copy(this.customMargins_); | |
418 | |
419 if (this.previousDefaultPageLayout_ != this.currentDefaultPageLayout) { | |
420 this.pageWidth_ = this.currentDefaultPageLayout.pageWidth; | |
421 this.pageHeight_ = this.currentDefaultPageLayout.pageHeight; | |
422 } | |
423 | |
424 this.previousDefaultPageLayout_ = this.currentDefaultPageLayout; | |
425 this.drawCustomMarginsUI_(); | |
426 this.marginsUI.show(); | |
427 }, | |
428 | |
429 /** | |
430 * Calculates the coordinates of the four margin lines. These are the | |
431 * coordinates where the margin lines should be displayed. The coordinates | |
432 * are expressed in terms of percentages with respect to the total width | |
433 * and height of the plugin. | |
434 * @private | |
435 * @return {print_preview.Rect} A rectnangle that describes the position of | |
436 * the four margin lines. | |
437 */ | |
438 getMarginsRectangleInPercent_: function() { | |
439 var pageLocation = previewArea.getPageLocationNormalized(); | |
440 var marginsInPercent = this.getMarginsInPercent_(); | |
441 var leftX = pageLocation.x + marginsInPercent.left; | |
442 var topY = pageLocation.y + marginsInPercent.top; | |
443 var contentWidth = pageLocation.width - (marginsInPercent.left + | |
444 marginsInPercent.right); | |
445 var contentHeight = pageLocation.height - (marginsInPercent.top + | |
446 marginsInPercent.bottom); | |
447 return new print_preview.Rect( | |
448 leftX, topY, contentWidth, contentHeight); | |
449 }, | |
450 | |
451 /** | |
452 * @private | |
453 * @return {print_preview.Margins} The currently selected margin values | |
454 * normalized to the total width and height of the plugin. | |
455 */ | |
456 getMarginsInPercent_: function() { | |
457 var pageInformation = previewArea.getPageLocationNormalized(); | |
458 var totalWidthInPoints = this.pageWidth_ / pageInformation.width; | |
459 var totalHeightInPoints = this.pageHeight_ / pageInformation.height; | |
460 var marginsInPercent = new Margins( | |
461 this.customMargins_.left / totalWidthInPoints, | |
462 this.customMargins_.top / totalHeightInPoints, | |
463 this.customMargins_.right / totalWidthInPoints, | |
464 this.customMargins_.bottom / totalHeightInPoints); | |
465 return marginsInPercent; | |
97 }, | 466 }, |
98 | 467 |
99 /** | 468 /** |
100 * If custom margins is the currently selected option then change to the | 469 * If custom margins is the currently selected option then change to the |
101 * default margins option. | 470 * default margins option. |
471 * @private | |
102 */ | 472 */ |
103 resetMarginsIfNeeded: function() { | 473 resetMarginsIfNeeded: function() { |
104 if (this.selectedMarginsValue_ == this.customMarginsValue_) | 474 if (this.isCustomMarginsSelected()) { |
105 this.marginList_.options[this.defaultMarginsIndex_].selected = true; | 475 this.marginList_.options[ |
106 }, | 476 MarginSettings.DEFAULT_MARGINS_OPTION_INDEX].selected = true; |
107 | 477 this.removeCustomMarginEventListeners_(); |
108 /** | 478 this.lastSelectedOption_ = MarginSettings.MARGINS_VALUE_DEFAULT; |
479 } | |
480 }, | |
481 | |
482 /** | |
109 * Listener executing when a PDFLoaded event occurs. | 483 * Listener executing when a PDFLoaded event occurs. |
110 * @private | 484 * @private |
111 */ | 485 */ |
112 onPDFLoaded_: function() { | 486 onPDFLoaded_: function() { |
113 if (!previewModifiable) | 487 if (!previewModifiable) |
114 fadeOutElement(this.marginsOption_); | 488 fadeOutElement(this.marginsOption_); |
115 } | 489 } |
116 }; | 490 }; |
117 | 491 |
118 return { | 492 return { |
119 MarginSettings: MarginSettings, | 493 MarginSettings: MarginSettings, |
494 PageLayout: PageLayout, | |
120 }; | 495 }; |
121 }); | 496 }); |
OLD | NEW |