/web/viewer.js

http://github.com/mozilla/pdf.js · JavaScript · 253 lines · 204 code · 15 blank · 34 comment · 23 complexity · 6b1cda4519aa05a887df46f08ac8494c MD5 · raw file

  1. /* Copyright 2016 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { AppOptions } from "./app_options.js";
  16. import { PDFViewerApplication } from "./app.js";
  17. /* eslint-disable-next-line no-unused-vars */
  18. const pdfjsVersion =
  19. typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_VERSION") : void 0;
  20. /* eslint-disable-next-line no-unused-vars */
  21. const pdfjsBuild =
  22. typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_BUILD") : void 0;
  23. window.PDFViewerApplication = PDFViewerApplication;
  24. window.PDFViewerApplicationOptions = AppOptions;
  25. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
  26. var defaultUrl; // eslint-disable-line no-var
  27. (function rewriteUrlClosure() {
  28. // Run this code outside DOMContentLoaded to make sure that the URL
  29. // is rewritten as soon as possible.
  30. const queryString = document.location.search.slice(1);
  31. const m = /(^|&)file=([^&]*)/.exec(queryString);
  32. defaultUrl = m ? decodeURIComponent(m[2]) : "";
  33. // Example: chrome-extension://.../http://example.com/file.pdf
  34. const humanReadableUrl = "/" + defaultUrl + location.hash;
  35. history.replaceState(history.state, "", humanReadableUrl);
  36. if (top === window) {
  37. // eslint-disable-next-line no-undef
  38. chrome.runtime.sendMessage("showPageAction");
  39. }
  40. })();
  41. }
  42. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
  43. require("./firefoxcom.js");
  44. require("./firefox_print_service.js");
  45. }
  46. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
  47. require("./genericcom.js");
  48. }
  49. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
  50. require("./chromecom.js");
  51. }
  52. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME || GENERIC")) {
  53. require("./pdf_print_service.js");
  54. }
  55. function getViewerConfiguration() {
  56. let errorWrapper = null;
  57. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  58. errorWrapper = {
  59. container: document.getElementById("errorWrapper"),
  60. errorMessage: document.getElementById("errorMessage"),
  61. closeButton: document.getElementById("errorClose"),
  62. errorMoreInfo: document.getElementById("errorMoreInfo"),
  63. moreInfoButton: document.getElementById("errorShowMore"),
  64. lessInfoButton: document.getElementById("errorShowLess"),
  65. };
  66. }
  67. return {
  68. appContainer: document.body,
  69. mainContainer: document.getElementById("viewerContainer"),
  70. viewerContainer: document.getElementById("viewer"),
  71. eventBus: null,
  72. toolbar: {
  73. container: document.getElementById("toolbarViewer"),
  74. numPages: document.getElementById("numPages"),
  75. pageNumber: document.getElementById("pageNumber"),
  76. scaleSelect: document.getElementById("scaleSelect"),
  77. customScaleOption: document.getElementById("customScaleOption"),
  78. previous: document.getElementById("previous"),
  79. next: document.getElementById("next"),
  80. zoomIn: document.getElementById("zoomIn"),
  81. zoomOut: document.getElementById("zoomOut"),
  82. viewFind: document.getElementById("viewFind"),
  83. openFile: document.getElementById("openFile"),
  84. print: document.getElementById("print"),
  85. presentationModeButton: document.getElementById("presentationMode"),
  86. download: document.getElementById("download"),
  87. viewBookmark: document.getElementById("viewBookmark"),
  88. },
  89. secondaryToolbar: {
  90. toolbar: document.getElementById("secondaryToolbar"),
  91. toggleButton: document.getElementById("secondaryToolbarToggle"),
  92. toolbarButtonContainer: document.getElementById(
  93. "secondaryToolbarButtonContainer"
  94. ),
  95. presentationModeButton: document.getElementById(
  96. "secondaryPresentationMode"
  97. ),
  98. openFileButton: document.getElementById("secondaryOpenFile"),
  99. printButton: document.getElementById("secondaryPrint"),
  100. downloadButton: document.getElementById("secondaryDownload"),
  101. viewBookmarkButton: document.getElementById("secondaryViewBookmark"),
  102. firstPageButton: document.getElementById("firstPage"),
  103. lastPageButton: document.getElementById("lastPage"),
  104. pageRotateCwButton: document.getElementById("pageRotateCw"),
  105. pageRotateCcwButton: document.getElementById("pageRotateCcw"),
  106. cursorSelectToolButton: document.getElementById("cursorSelectTool"),
  107. cursorHandToolButton: document.getElementById("cursorHandTool"),
  108. scrollPageButton: document.getElementById("scrollPage"),
  109. scrollVerticalButton: document.getElementById("scrollVertical"),
  110. scrollHorizontalButton: document.getElementById("scrollHorizontal"),
  111. scrollWrappedButton: document.getElementById("scrollWrapped"),
  112. spreadNoneButton: document.getElementById("spreadNone"),
  113. spreadOddButton: document.getElementById("spreadOdd"),
  114. spreadEvenButton: document.getElementById("spreadEven"),
  115. documentPropertiesButton: document.getElementById("documentProperties"),
  116. },
  117. sidebar: {
  118. // Divs (and sidebar button)
  119. outerContainer: document.getElementById("outerContainer"),
  120. viewerContainer: document.getElementById("viewerContainer"),
  121. toggleButton: document.getElementById("sidebarToggle"),
  122. // Buttons
  123. thumbnailButton: document.getElementById("viewThumbnail"),
  124. outlineButton: document.getElementById("viewOutline"),
  125. attachmentsButton: document.getElementById("viewAttachments"),
  126. layersButton: document.getElementById("viewLayers"),
  127. // Views
  128. thumbnailView: document.getElementById("thumbnailView"),
  129. outlineView: document.getElementById("outlineView"),
  130. attachmentsView: document.getElementById("attachmentsView"),
  131. layersView: document.getElementById("layersView"),
  132. // View-specific options
  133. outlineOptionsContainer: document.getElementById(
  134. "outlineOptionsContainer"
  135. ),
  136. currentOutlineItemButton: document.getElementById("currentOutlineItem"),
  137. },
  138. sidebarResizer: {
  139. outerContainer: document.getElementById("outerContainer"),
  140. resizer: document.getElementById("sidebarResizer"),
  141. },
  142. findBar: {
  143. bar: document.getElementById("findbar"),
  144. toggleButton: document.getElementById("viewFind"),
  145. findField: document.getElementById("findInput"),
  146. highlightAllCheckbox: document.getElementById("findHighlightAll"),
  147. caseSensitiveCheckbox: document.getElementById("findMatchCase"),
  148. entireWordCheckbox: document.getElementById("findEntireWord"),
  149. findMsg: document.getElementById("findMsg"),
  150. findResultsCount: document.getElementById("findResultsCount"),
  151. findPreviousButton: document.getElementById("findPrevious"),
  152. findNextButton: document.getElementById("findNext"),
  153. },
  154. passwordOverlay: {
  155. overlayName: "passwordOverlay",
  156. container: document.getElementById("passwordOverlay"),
  157. label: document.getElementById("passwordText"),
  158. input: document.getElementById("password"),
  159. submitButton: document.getElementById("passwordSubmit"),
  160. cancelButton: document.getElementById("passwordCancel"),
  161. },
  162. documentProperties: {
  163. overlayName: "documentPropertiesOverlay",
  164. container: document.getElementById("documentPropertiesOverlay"),
  165. closeButton: document.getElementById("documentPropertiesClose"),
  166. fields: {
  167. fileName: document.getElementById("fileNameField"),
  168. fileSize: document.getElementById("fileSizeField"),
  169. title: document.getElementById("titleField"),
  170. author: document.getElementById("authorField"),
  171. subject: document.getElementById("subjectField"),
  172. keywords: document.getElementById("keywordsField"),
  173. creationDate: document.getElementById("creationDateField"),
  174. modificationDate: document.getElementById("modificationDateField"),
  175. creator: document.getElementById("creatorField"),
  176. producer: document.getElementById("producerField"),
  177. version: document.getElementById("versionField"),
  178. pageCount: document.getElementById("pageCountField"),
  179. pageSize: document.getElementById("pageSizeField"),
  180. linearized: document.getElementById("linearizedField"),
  181. },
  182. },
  183. errorWrapper,
  184. printContainer: document.getElementById("printContainer"),
  185. openFileInputName: "fileInput",
  186. debuggerScriptPath: "./debugger.js",
  187. };
  188. }
  189. function webViewerLoad() {
  190. const config = getViewerConfiguration();
  191. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
  192. Promise.all([
  193. import("pdfjs-web/genericcom.js"),
  194. import("pdfjs-web/pdf_print_service.js"),
  195. ]).then(function ([genericCom, pdfPrintService]) {
  196. PDFViewerApplication.run(config);
  197. });
  198. } else {
  199. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
  200. AppOptions.set("defaultUrl", defaultUrl);
  201. }
  202. if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
  203. // Give custom implementations of the default viewer a simpler way to
  204. // set various `AppOptions`, by dispatching an event once all viewer
  205. // files are loaded but *before* the viewer initialization has run.
  206. const event = document.createEvent("CustomEvent");
  207. event.initCustomEvent("webviewerloaded", true, true, {
  208. source: window,
  209. });
  210. try {
  211. // Attempt to dispatch the event at the embedding `document`,
  212. // in order to support cases where the viewer is embedded in
  213. // a *dynamically* created <iframe> element.
  214. parent.document.dispatchEvent(event);
  215. } catch (ex) {
  216. // The viewer could be in e.g. a cross-origin <iframe> element,
  217. // fallback to dispatching the event at the current `document`.
  218. console.error(`webviewerloaded: ${ex}`);
  219. document.dispatchEvent(event);
  220. }
  221. }
  222. PDFViewerApplication.run(config);
  223. }
  224. }
  225. // Block the "load" event until all pages are loaded, to ensure that printing
  226. // works in Firefox; see https://bugzilla.mozilla.org/show_bug.cgi?id=1618553
  227. if (document.blockUnblockOnload) {
  228. document.blockUnblockOnload(true);
  229. }
  230. if (
  231. document.readyState === "interactive" ||
  232. document.readyState === "complete"
  233. ) {
  234. webViewerLoad();
  235. } else {
  236. document.addEventListener("DOMContentLoaded", webViewerLoad, true);
  237. }
  238. export { PDFViewerApplication, AppOptions as PDFViewerApplicationOptions };