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