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 'use strict'; |
6 | 7 |
| 8 <include src="../../../../ui/webui/resources/js/util.js"></include> |
| 9 <include src="viewport.js"></include> |
| 10 |
| 11 // The plugin element is sized to fill the entire window and is set to be fixed |
| 12 // positioning, acting as a viewport. The plugin renders into this viewport |
| 13 // according to the scroll position of the window. |
7 var plugin; | 14 var plugin; |
| 15 |
| 16 // This element is placed behind the plugin element to cause scrollbars to be |
| 17 // displayed in the window. It is sized according to the document size of the |
| 18 // pdf and zoom level. |
8 var sizer; | 19 var sizer; |
9 | 20 |
10 function onScroll() { | 21 // The toolbar element. |
11 var scrollMessage = { | 22 var viewerToolbar; |
12 type: 'scroll', | 23 |
13 xOffset: window.pageXOffset, | 24 // The viewport object. |
14 yOffset: window.pageYOffset | 25 var viewport; |
15 }; | 26 |
16 plugin.postMessage(scrollMessage); | 27 // Returns true if the fit-to-page button is enabled. |
| 28 function isFitToPageEnabled() { |
| 29 return $('fit-to-page-button').classList.contains('polymer-selected'); |
17 } | 30 } |
18 | 31 |
| 32 // Called when a message is received from the plugin. |
19 function handleMessage(message) { | 33 function handleMessage(message) { |
20 if (message.data['type'] == 'document_dimensions') { | 34 if (message.data.type == 'documentDimensions') { |
21 if (sizer.style.height != message.data['document_height'] + 'px') { | 35 viewport.setDocumentDimensions(message.data); |
22 sizer.style.height = message.data['document_height'] + 'px'; | |
23 sizer.style.width = message.data['document_width'] + 'px'; | |
24 } | |
25 } | 36 } |
26 } | 37 } |
27 | 38 |
| 39 // Callback that's called when the viewport changes. |
| 40 function viewportChangedCallback(zoom, x, y, scrollbarWidth, hasScrollbars) { |
| 41 // Offset the toolbar position so that it doesn't move if scrollbars appear. |
| 42 var toolbarRight = hasScrollbars.y ? 0 : scrollbarWidth; |
| 43 var toolbarBottom = hasScrollbars.x ? 0 : scrollbarWidth; |
| 44 viewerToolbar.style.right = toolbarRight + 'px'; |
| 45 viewerToolbar.style.bottom = toolbarBottom + 'px'; |
| 46 |
| 47 // Notify the plugin of the viewport change. |
| 48 plugin.postMessage({ |
| 49 type: 'viewport', |
| 50 zoom: zoom, |
| 51 xOffset: x, |
| 52 yOffset: y |
| 53 }); |
| 54 } |
| 55 |
28 function load() { | 56 function load() { |
29 window.addEventListener('scroll', | 57 sizer = $('sizer'); |
30 function() { webkitRequestAnimationFrame(onScroll); }); | 58 viewerToolbar = $('toolbar'); |
31 | 59 |
| 60 // Create the viewport. |
| 61 viewport = new Viewport(window, |
| 62 sizer, |
| 63 isFitToPageEnabled, |
| 64 viewportChangedCallback); |
| 65 |
| 66 // Create the plugin object dynamically so we can set its src. |
| 67 plugin = document.createElement('object'); |
| 68 plugin.id = 'plugin'; |
| 69 plugin.type = 'application/x-google-chrome-pdf'; |
| 70 plugin.addEventListener('message', handleMessage, false); |
32 // The pdf location is passed in the document url in the format: | 71 // The pdf location is passed in the document url in the format: |
33 // http://<.../pdf.html>?<pdf location>. | 72 // http://<.../pdf.html>?<pdf location>. |
34 var url = window.location.search.substring(1); | 73 var url = window.location.search.substring(1); |
35 plugin = document.createElement('object'); | |
36 plugin.setAttribute('width', '100%'); | |
37 plugin.setAttribute('height', '100%'); | |
38 plugin.setAttribute('type', 'application/x-google-chrome-pdf'); | |
39 plugin.setAttribute('src', url); | 74 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); | 75 document.body.appendChild(plugin); |
44 | 76 |
45 sizer = document.createElement('div'); | 77 // Setup the button event listeners. |
46 sizer.style.zIndex = '0'; | 78 $('fit-to-width-button').addEventListener('click', |
47 sizer.style.position = 'absolute'; | 79 viewport.fitToWidth.bind(viewport)); |
48 sizer.style.width = '100%'; | 80 $('fit-to-page-button').addEventListener('click', |
49 sizer.style.height = '100%'; | 81 viewport.fitToPage.bind(viewport)); |
50 document.body.appendChild(sizer); | 82 $('zoom-in-button').addEventListener('click', |
| 83 viewport.zoomIn.bind(viewport)); |
| 84 $('zoom-out-button').addEventListener('click', |
| 85 viewport.zoomOut.bind(viewport)); |
| 86 |
| 87 // Setup keyboard event listeners. |
| 88 document.onkeydown = function(e) { |
| 89 switch (e.keyCode) { |
| 90 case 37: // Left arrow key. |
| 91 // Go to the previous page if there are no horizontal scrollbars. |
| 92 if (!viewport.documentHasScrollbars().x) { |
| 93 viewport.goToPage(viewport.getMostVisiblePage() - 1); |
| 94 // Since we do the movement of the page |
| 95 e.preventDefault(); |
| 96 } |
| 97 return; |
| 98 case 33: // Page up key. |
| 99 // Go to the previous page if we are fit-to-page. |
| 100 if (isFitToPageEnabled()) { |
| 101 viewport.goToPage(viewport.getMostVisiblePage() - 1); |
| 102 e.preventDefault(); |
| 103 } |
| 104 return; |
| 105 case 39: // Right arrow key. |
| 106 // Go to the next page if there are no horizontal scrollbars. |
| 107 if (!viewport.documentHasScrollbars().x) { |
| 108 viewport.goToPage(viewport.getMostVisiblePage() + 1); |
| 109 e.preventDefault(); |
| 110 } |
| 111 return; |
| 112 case 34: // Page down key. |
| 113 // Go to the next page if we are fit-to-page. |
| 114 if (isFitToPageEnabled()) { |
| 115 viewport.goToPage(viewport.getMostVisiblePage() + 1); |
| 116 e.preventDefault(); |
| 117 } |
| 118 return; |
| 119 } |
| 120 }; |
51 } | 121 } |
52 | 122 |
53 load(); | 123 load(); |
54 | 124 |
55 })(); | 125 })(); |
OLD | NEW |