OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 (function() { | 5 (function() { |
6 | 6 |
7 var plugin; | 7 <include src="../../../../ui/webui/resources/js/util.js"></include> |
8 var sizer; | 8 <include src="viewport.js"></include> |
9 | 9 |
10 function onScroll() { | 10 // The plugin element is sized to fill the entire window and is set to be fixed |
11 var scrollMessage = { | 11 // positioning, acting as a viewport. The plugin renders into this viewport |
12 type: 'scroll', | 12 // according to the scroll position of the window. |
13 xOffset: window.pageXOffset, | 13 var gPlugin; |
arv (Not doing code reviews)
2014/02/13 23:47:41
Why gPlugin instead of plugin?
raymes
2014/02/17 00:54:03
Done.
| |
14 yOffset: window.pageYOffset | 14 |
15 }; | 15 // This element is placed behind the plugin element to cause scrollbars to be |
16 plugin.postMessage(scrollMessage); | 16 // displayed in the window. It is sized according to the document size of the |
17 // pdf and zoom level. | |
18 var gSizer; | |
19 | |
20 // The toolbar element. | |
21 var gViewerToolbar; | |
22 | |
23 // The viewport object. | |
24 var gViewport; | |
25 | |
26 // Returns true if the fit-to-page button is enabled. | |
27 function isFitToPageEnabled() { | |
28 return $('btn-ftp').classList.contains('polymer-selected'); | |
17 } | 29 } |
18 | 30 |
31 // Called when a message is received from the plugin. | |
19 function handleMessage(message) { | 32 function handleMessage(message) { |
20 if (message.data['type'] == 'document_dimensions') { | 33 if (message.data.type == 'documentDimensions') { |
21 if (sizer.style.height != message.data['document_height'] + 'px') { | 34 gViewport.setDocumentDimensions(message.data); |
22 sizer.style.height = message.data['document_height'] + 'px'; | |
23 sizer.style.width = message.data['document_width'] + 'px'; | |
24 } | |
25 } | 35 } |
26 } | 36 } |
27 | 37 |
38 // Callback that's called when the viewport changes. | |
39 function viewportChangedCallback(zoom, x, y, scrollbarWidth, hasScrollbars) { | |
40 // Offset the toolbar position so that it doesn't move if scrollbars appear. | |
41 var toolbarRight = hasScrollbars.y ? 0 : scrollbarWidth; | |
42 var toolbarBottom = hasScrollbars.x ? 0 : scrollbarWidth; | |
43 gViewerToolbar.style.right = toolbarRight + 'px'; | |
44 gViewerToolbar.style.bottom = toolbarBottom + 'px'; | |
45 | |
46 // Notify the plugin of the viewport change. | |
47 gPlugin.postMessage({ | |
48 type: 'viewport', | |
49 zoom: zoom, | |
50 xOffset: x, | |
51 yOffset: y | |
52 }); | |
53 } | |
54 | |
28 function load() { | 55 function load() { |
29 window.addEventListener('scroll', | 56 // Create the plugin and sizer elements. |
30 function() { webkitRequestAnimationFrame(onScroll); }); | 57 gPlugin = document.createElement('object'); |
arv (Not doing code reviews)
2014/02/13 23:47:41
Any reason why this is created in script instead o
raymes
2014/02/17 00:54:03
The plugin gets loaded when the the object node is
| |
58 gPlugin.style.width = '100%'; | |
59 gPlugin.style.height = '100%'; | |
60 gPlugin.setAttribute('type', 'application/x-google-chrome-pdf'); | |
61 gPlugin.style.zIndex = 1; | |
62 gPlugin.style.position = 'fixed'; | |
63 gPlugin.addEventListener('message', handleMessage, false); | |
31 | 64 |
65 gSizer = document.createElement('div'); | |
66 gSizer.style.zIndex = 0; | |
67 gSizer.style.position = 'absolute'; | |
68 | |
69 // Create the viewport. | |
70 gViewport = new Viewport(window, | |
71 gSizer, | |
72 isFitToPageEnabled, | |
73 viewportChangedCallback); | |
74 | |
75 // Set the plugin URL which will initialize the plugin. | |
32 // The pdf location is passed in the document url in the format: | 76 // The pdf location is passed in the document url in the format: |
33 // http://<.../pdf.html>?<pdf location>. | 77 // http://<.../pdf.html>?<pdf location>. |
34 var url = window.location.search.substring(1); | 78 var url = window.location.search.substring(1); |
35 plugin = document.createElement('object'); | 79 gPlugin.setAttribute('src', url); |
arv (Not doing code reviews)
2014/02/13 23:47:41
plugin.src =
raymes
2014/02/17 00:54:03
That doesn't work, for some reason. I suspect it's
| |
36 plugin.setAttribute('width', '100%'); | 80 // Add the elements to the document. |
37 plugin.setAttribute('height', '100%'); | 81 document.body.appendChild(gPlugin); |
38 plugin.setAttribute('type', 'application/x-google-chrome-pdf'); | 82 document.body.appendChild(gSizer); |
39 plugin.setAttribute('src', url); | |
40 plugin.style.zIndex = '1'; | |
41 plugin.style.position = 'fixed'; | |
42 plugin.addEventListener('message', handleMessage, false); | |
43 document.body.appendChild(plugin); | |
44 | 83 |
45 sizer = document.createElement('div'); | 84 // Setup the button event listeners. |
46 sizer.style.zIndex = '0'; | 85 $('fit-to-width-button').addEventListener('click', |
47 sizer.style.position = 'absolute'; | 86 function() { gViewport.fitToWidth(); }); |
arv (Not doing code reviews)
2014/02/13 23:47:41
or
$('fit-to-width-button').addEventListener('cli
raymes
2014/02/17 00:54:03
Done.
| |
48 sizer.style.width = '100%'; | 87 $('fit-to-page-button').addEventListener('click', |
49 sizer.style.height = '100%'; | 88 function() { gViewport.fitToPage(); }); |
50 document.body.appendChild(sizer); | 89 $('zoom-in-button').addEventListener('click', |
90 function() { gViewport.zoomIn(); }); | |
91 $('zoom-out-button').addEventListener('click', | |
92 function() { gViewport.zoomOut(); }); | |
93 | |
94 gViewerToolbar = $('toolbar'); | |
95 | |
96 // Setup keyboard event listeners. | |
97 document.onkeydown = function(e) { | |
98 switch (e.keyCode) { | |
99 case 37: // Left arrow key. | |
100 // Go to the previous page if there are no horizontal scrollbars. | |
101 if (!gViewport.documentHasScrollbars().x) { | |
102 gViewport.goToPage(gViewport.getMostVisiblePage() - 1); | |
103 e.stopPropagation(); | |
arv (Not doing code reviews)
2014/02/13 23:47:41
why do you need to stop the event propagation. Ple
raymes
2014/02/17 00:54:03
It's not needed. Thanks.
| |
104 return false; | |
arv (Not doing code reviews)
2014/02/13 23:47:41
Please use `event.preventDefault()`
raymes
2014/02/17 00:54:03
Done.
| |
105 } | |
106 return true; | |
107 case 33: // Page up key. | |
108 // Go to the previous page if we are fit-to-page. | |
109 if (isFitToPageEnabled()) { | |
110 gViewport.goToPage(gViewport.getMostVisiblePage() - 1); | |
111 e.stopPropagation(); | |
112 return false; | |
113 } | |
114 return true; | |
115 case 39: // Right arrow key. | |
116 // Go to the next page if there are no horizontal scrollbars. | |
117 if (!gViewport.documentHasScrollbars().y) { | |
118 gViewport.goToPage(gViewport.getMostVisiblePage() + 1); | |
119 e.stopPropagation(); | |
120 return false; | |
121 } | |
122 return true; | |
123 case 34: // Page down key. | |
124 // Go to the next page if we are fit-to-page. | |
125 if (isFitToPageEnabled()) { | |
126 gViewport.goToPage(gViewport.getMostVisiblePage() + 1); | |
127 e.stopPropagation(); | |
128 return false; | |
129 } | |
130 return true; | |
131 } | |
132 }; | |
51 } | 133 } |
52 | 134 |
53 load(); | 135 load(); |
54 | 136 |
55 })(); | 137 })(); |
OLD | NEW |