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. |
11 var scrollMessage = { | 11 var gPlugin; |
12 type: 'scroll', | 12 |
13 xOffset: window.pageXOffset, | 13 // The sizer element. This represents the zoomed size of the document in the |
14 yOffset: window.pageYOffset | 14 // page. |
15 }; | 15 var gSizer; |
koz (OOO until 15th September)
2014/02/03 03:40:43
Maybe instead of saying that it represents the siz
raymes
2014/02/05 06:05:17
Done.
| |
16 plugin.postMessage(scrollMessage); | 16 |
17 // The toolbar element. | |
18 var gViewerToolbar; | |
19 | |
20 // The viewport object. | |
21 var gViewport; | |
22 | |
23 function isFitToPageEnabled() { | |
24 return $('btn-ftp').classList.contains( | |
25 'polymer-selected'); | |
17 } | 26 } |
18 | 27 |
28 // Called when a message is received from the plugin. | |
19 function handleMessage(message) { | 29 function handleMessage(message) { |
20 if (message.data['type'] == 'document_dimensions') { | 30 if (message.data.type == 'documentDimensions') { |
21 if (sizer.style.height != message.data['document_height'] + 'px') { | 31 gViewport.setDocumentDimensions(message.data); |
22 sizer.style.height = message.data['document_height'] + 'px'; | |
23 sizer.style.width = message.data['document_width'] + 'px'; | |
24 } | |
25 } | 32 } |
26 } | 33 } |
27 | 34 |
35 // Callback that's called when the viewport changes. | |
36 function viewportChangedCallback(zoom, x, y, scrollbarWidth, hasScrollbars) { | |
37 // Offset the toolbar position so that it doesn't move if scrollbars appear. | |
38 var toolbarRight = hasScrollbars.y ? 0 : scrollbarWidth; | |
39 var toolbarBottom = hasScrollbars.x ? 0 : scrollbarWidth; | |
40 gViewerToolbar.style.right = toolbarRight + 'px'; | |
41 gViewerToolbar.style.bottom = toolbarBottom + 'px'; | |
42 | |
43 // Notify the plugin of the viewport change. | |
44 gPlugin.postMessage({ | |
45 type: 'viewport', | |
46 zoom: zoom, | |
47 xOffset: x, | |
48 yOffset: y | |
49 }); | |
50 } | |
51 | |
28 function load() { | 52 function load() { |
29 window.addEventListener('scroll', | 53 // Create the plugin and sizer elements. |
30 function() { webkitRequestAnimationFrame(onScroll); }); | 54 gPlugin = document.createElement('object'); |
55 gPlugin.style.width = '100%'; | |
56 gPlugin.style.height = '100%'; | |
57 gPlugin.setAttribute('type', 'application/x-google-chrome-pdf'); | |
58 gPlugin.style.zIndex = 1; | |
59 gPlugin.style.position = 'fixed'; | |
60 gPlugin.addEventListener('message', handleMessage, false); | |
31 | 61 |
62 gSizer = document.createElement('div'); | |
63 gSizer.style.zIndex = 0; | |
64 gSizer.style.position = 'absolute'; | |
65 | |
66 // Create the viewport. | |
67 gViewport = new Viewport(window, | |
68 gSizer, | |
69 isFitToPageEnabled, | |
70 viewportChangedCallback); | |
71 | |
72 // Set the plugin URL which will initialize the plugin. | |
32 // The pdf location is passed in the document url in the format: | 73 // The pdf location is passed in the document url in the format: |
33 // http://<.../pdf.html>?<pdf location>. | 74 // http://<.../pdf.html>?<pdf location>. |
34 var url = window.location.search.substring(1); | 75 var url = window.location.search.substring(1); |
35 plugin = document.createElement('object'); | 76 gPlugin.setAttribute('src', url); |
36 plugin.setAttribute('width', '100%'); | 77 // Add the elements to the document. |
37 plugin.setAttribute('height', '100%'); | 78 document.body.appendChild(gPlugin); |
38 plugin.setAttribute('type', 'application/x-google-chrome-pdf'); | 79 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 | 80 |
45 sizer = document.createElement('div'); | 81 // Setup the button event listeners. |
46 sizer.style.zIndex = '0'; | 82 $('btn-ftw').addEventListener('click', |
47 sizer.style.position = 'absolute'; | 83 function() { gViewport.fitToWidth(); }); |
48 sizer.style.width = '100%'; | 84 $('btn-ftp').addEventListener('click', function() { gViewport.fitToPage(); }); |
49 sizer.style.height = '100%'; | 85 $('btn-zoom-in').addEventListener('click', |
50 document.body.appendChild(sizer); | 86 function() { gViewport.zoomIn(); }); |
87 $('btn-zoom-out').addEventListener('click', | |
88 function() { gViewport.zoomOut(); }); | |
89 | |
90 gViewerToolbar = $('toolbar'); | |
91 | |
92 // Setup keyboard event listeners. | |
93 document.onkeydown = function(e) { | |
94 switch (e.keyCode) { | |
95 case 37: // Left arrow key. | |
96 // Go to the previous page if there are no horizontal scrollbars. | |
97 if (!gViewport.documentHasScrollbars().x) { | |
98 gViewport.goToPage(gViewport.getMostVisiblePage() - 1); | |
99 e.stopPropagation(); | |
100 return false; | |
101 } | |
102 return true; | |
103 case 33: // Page up key. | |
104 // Go to the previous page if we are fit-to-page. | |
105 if (isFitToPageEnabled()) { | |
106 gViewport.goToPage(gViewport.getMostVisiblePage() - 1); | |
107 e.stopPropagation(); | |
108 return false; | |
109 } | |
110 return true; | |
111 case 39: // Right arrow key. | |
112 // Go to the next page if there are no horizontal scrollbars. | |
113 if (!gViewport.documentHasScrollbars().y) { | |
114 gViewport.goToPage(gViewport.getMostVisiblePage() + 1); | |
115 e.stopPropagation(); | |
116 return false; | |
117 } | |
118 return true; | |
119 case 34: // Page down key. | |
120 // Go to the next page if we are fit-to-page. | |
121 if (isFitToPageEnabled()) { | |
122 gViewport.goToPage(gViewport.getMostVisiblePage() + 1); | |
123 e.stopPropagation(); | |
124 return false; | |
125 } | |
126 return true; | |
127 } | |
128 }; | |
51 } | 129 } |
52 | 130 |
53 load(); | 131 load(); |
54 | 132 |
55 })(); | 133 })(); |
OLD | NEW |