| 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 'use strict'; |
| 7 | 7 |
| 8 <include src="../../../../ui/webui/resources/js/util.js"></include> | 8 <include src="../../../../ui/webui/resources/js/util.js"></include> |
| 9 <include src="viewport.js"></include> | 9 <include src="viewport.js"></include> |
| 10 | 10 |
| 11 // The plugin element is sized to fill the entire window and is set to be fixed | 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 | 12 // positioning, acting as a viewport. The plugin renders into this viewport |
| 13 // according to the scroll position of the window. | 13 // according to the scroll position of the window. |
| 14 var plugin; | 14 var plugin; |
| 15 | 15 |
| 16 // This element is placed behind the plugin element to cause scrollbars to be | 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 | 17 // displayed in the window. It is sized according to the document size of the |
| 18 // pdf and zoom level. | 18 // pdf and zoom level. |
| 19 var sizer; | 19 var sizer; |
| 20 | 20 |
| 21 // The toolbar element. | 21 // The toolbar element. |
| 22 var viewerToolbar; | 22 var viewerToolbar; |
| 23 | 23 |
| 24 // The page indicator element. |
| 25 var viewerPageIndicator; |
| 26 |
| 27 // The progress bar element. |
| 28 var viewerProgressBar; |
| 29 |
| 24 // The viewport object. | 30 // The viewport object. |
| 25 var viewport; | 31 var viewport; |
| 26 | 32 |
| 33 // The document dimensions. |
| 34 var documentDimensions; |
| 35 |
| 27 // Returns true if the fit-to-page button is enabled. | 36 // Returns true if the fit-to-page button is enabled. |
| 28 function isFitToPageEnabled() { | 37 function isFitToPageEnabled() { |
| 29 return $('fit-to-page-button').classList.contains('polymer-selected'); | 38 return $('fit-to-page-button').classList.contains('polymer-selected'); |
| 30 } | 39 } |
| 31 | 40 |
| 41 function updateProgress(progress) { |
| 42 viewerProgressBar.progress = progress; |
| 43 if (progress == -1) { |
| 44 // Document load failed. |
| 45 sizer.style.display = 'none'; |
| 46 viewerToolbar.style.visibility = 'hidden'; |
| 47 } |
| 48 } |
| 49 |
| 32 // Called when a message is received from the plugin. | 50 // Called when a message is received from the plugin. |
| 33 function handleMessage(message) { | 51 function handleMessage(message) { |
| 34 if (message.data.type == 'documentDimensions') { | 52 if (message.data.type == 'documentDimensions') { |
| 35 viewport.setDocumentDimensions(message.data); | 53 documentDimensions = message.data; |
| 54 viewport.setDocumentDimensions(documentDimensions); |
| 55 viewerToolbar.style.visibility = 'visible'; |
| 56 viewerPageIndicator.initialFadeIn(); |
| 57 viewerToolbar.initialFadeIn(); |
| 58 } else if (message.data.type == 'loadProgress') { |
| 59 updateProgress(message.data['progress']); |
| 36 } | 60 } |
| 37 } | 61 } |
| 38 | 62 |
| 39 // Callback that's called when the viewport changes. | 63 // Callback that's called when the viewport changes. |
| 40 function viewportChangedCallback(zoom, x, y, scrollbarWidth, hasScrollbars) { | 64 function viewportChangedCallback(zoom, |
| 65 x, |
| 66 y, |
| 67 scrollbarWidth, |
| 68 hasScrollbars, |
| 69 page) { |
| 41 // Offset the toolbar position so that it doesn't move if scrollbars appear. | 70 // Offset the toolbar position so that it doesn't move if scrollbars appear. |
| 42 var toolbarRight = hasScrollbars.y ? 0 : scrollbarWidth; | 71 var toolbarRight = hasScrollbars.y ? 0 : scrollbarWidth; |
| 43 var toolbarBottom = hasScrollbars.x ? 0 : scrollbarWidth; | 72 var toolbarBottom = hasScrollbars.x ? 0 : scrollbarWidth; |
| 44 viewerToolbar.style.right = toolbarRight + 'px'; | 73 viewerToolbar.style.right = toolbarRight + 'px'; |
| 45 viewerToolbar.style.bottom = toolbarBottom + 'px'; | 74 viewerToolbar.style.bottom = toolbarBottom + 'px'; |
| 46 | 75 |
| 76 // Show or hide the page indicator. |
| 77 if (documentDimensions.pageDimensions.length > 1 && hasScrollbars.y) |
| 78 viewerPageIndicator.style.visibility = 'visible'; |
| 79 else |
| 80 viewerPageIndicator.style.visibility = 'hidden'; |
| 81 |
| 82 // Update the most visible page. |
| 83 viewerPageIndicator.text = page + 1; |
| 84 |
| 47 // Notify the plugin of the viewport change. | 85 // Notify the plugin of the viewport change. |
| 48 plugin.postMessage({ | 86 plugin.postMessage({ |
| 49 type: 'viewport', | 87 type: 'viewport', |
| 50 zoom: zoom, | 88 zoom: zoom, |
| 51 xOffset: x, | 89 xOffset: x, |
| 52 yOffset: y | 90 yOffset: y |
| 53 }); | 91 }); |
| 54 } | 92 } |
| 55 | 93 |
| 56 function load() { | 94 function load() { |
| 57 sizer = $('sizer'); | 95 sizer = $('sizer'); |
| 58 viewerToolbar = $('toolbar'); | 96 viewerToolbar = $('toolbar'); |
| 97 viewerPageIndicator = $('page-indicator'); |
| 98 viewerProgressBar = $('progress-bar'); |
| 59 | 99 |
| 60 // Create the viewport. | 100 // Create the viewport. |
| 61 viewport = new Viewport(window, | 101 viewport = new Viewport(window, |
| 62 sizer, | 102 sizer, |
| 63 isFitToPageEnabled, | 103 isFitToPageEnabled, |
| 64 viewportChangedCallback); | 104 viewportChangedCallback); |
| 65 | 105 |
| 66 // Create the plugin object dynamically so we can set its src. | 106 // Create the plugin object dynamically so we can set its src. |
| 67 plugin = document.createElement('object'); | 107 plugin = document.createElement('object'); |
| 68 plugin.id = 'plugin'; | 108 plugin.id = 'plugin'; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 e.preventDefault(); | 156 e.preventDefault(); |
| 117 } | 157 } |
| 118 return; | 158 return; |
| 119 } | 159 } |
| 120 }; | 160 }; |
| 121 } | 161 } |
| 122 | 162 |
| 123 load(); | 163 load(); |
| 124 | 164 |
| 125 })(); | 165 })(); |
| OLD | NEW |