pdfview.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
  3. /* FROM PDF.JS, ADAPTED */
  4. 'use strict';
  5. var kDefaultURL = 'compressed.tracemonkey-pldi-09.pdf';
  6. var kDefaultScale = 1.25;
  7. var kDefaultScaleDelta = 1.1;
  8. var kCacheSize = 20;
  9. var kCssUnits = 96.0 / 72.0;
  10. var kScrollbarPadding = 40;
  11. var kMinScale = 0.25;
  12. var kMaxScale = 4.0;
  13. var Cache = function cacheCache(size) {
  14. var data = [];
  15. this.push = function cachePush(view) {
  16. var i = data.indexOf(view);
  17. if (i >= 0)
  18. data.splice(i);
  19. data.push(view);
  20. if (data.length > size)
  21. data.shift().update();
  22. };
  23. };
  24. var cache = new Cache(kCacheSize);
  25. var currentPageNumber = 1;
  26. var PDFView = {
  27. active: false,
  28. pages: [],
  29. thumbnails: [],
  30. currentScale: kDefaultScale,
  31. initialBookmark: document.location.hash.substring(1),
  32. setScale: function pdfViewSetScale(val, resetAutoSettings) {
  33. var pages = this.pages;
  34. for (var i = 0; i < pages.length; i++)
  35. pages[i].update(val * kCssUnits);
  36. if (this.currentScale != val)
  37. this.pages[this.page - 1].scrollIntoView();
  38. this.currentScale = val;
  39. var event = document.createEvent('UIEvents');
  40. event.initUIEvent('scalechange', false, false, window, 0);
  41. event.scale = val;
  42. event.resetAutoSettings = resetAutoSettings;
  43. window.dispatchEvent(event);
  44. },
  45. parseScale: function pdfViewParseScale(value, resetAutoSettings) {
  46. if ('custom' == value)
  47. return;
  48. var scale = parseFloat(value);
  49. if (scale) {
  50. this.setScale(scale, true);
  51. return;
  52. }
  53. var currentPage = this.pages[this.page - 1];
  54. var pageWidthScale = ($("#content").width() - kScrollbarPadding) /
  55. currentPage.width / kCssUnits;
  56. var pageHeightScale = ($("#content").height() - kScrollbarPadding) /
  57. currentPage.height / kCssUnits;
  58. if ('page-width' == value)
  59. this.setScale(pageWidthScale, resetAutoSettings);
  60. if ('page-height' == value)
  61. this.setScale(pageHeightScale, resetAutoSettings);
  62. if ('page-fit' == value) {
  63. this.setScale(
  64. Math.min(pageWidthScale, pageHeightScale), resetAutoSettings);
  65. }
  66. },
  67. zoomIn: function pdfViewZoomIn() {
  68. var newScale = Math.min(kMaxScale, this.currentScale * kDefaultScaleDelta);
  69. this.setScale(newScale, true);
  70. },
  71. zoomOut: function pdfViewZoomOut() {
  72. var newScale = Math.max(kMinScale, this.currentScale / kDefaultScaleDelta);
  73. this.setScale(newScale, true);
  74. },
  75. set page(val) {
  76. var pages = this.pages;
  77. var input = document.getElementById('pageNumber');
  78. if (!(0 < val && val <= pages.length)) {
  79. var event = document.createEvent('UIEvents');
  80. event.initUIEvent('pagechange', false, false, window, 0);
  81. event.pageNumber = this.page;
  82. window.dispatchEvent(event);
  83. return;
  84. }
  85. currentPageNumber = val;
  86. var event = document.createEvent('UIEvents');
  87. event.initUIEvent('pagechange', false, false, window, 0);
  88. event.pageNumber = val;
  89. window.dispatchEvent(event);
  90. // checking if the this.page was called from the updateViewarea function:
  91. // avoiding the creation of two "set page" method (internal and public)
  92. if (updateViewarea.inProgress)
  93. return;
  94. // Avoid scrolling the first page during loading
  95. if (this.loading && val == 1)
  96. return;
  97. pages[val - 1].scrollIntoView();
  98. },
  99. get page() {
  100. return currentPageNumber;
  101. },
  102. open: function pdfViewOpen(url, scale) {
  103. document.title = PDFView.Ptitle;
  104. this.url = url;
  105. var self = this;
  106. PDFJS.getPdf(
  107. {
  108. url: url,
  109. progress: function getPdfProgress(evt) {
  110. if (evt.lengthComputable)
  111. self.progress(evt.loaded / evt.total);
  112. },
  113. error: self.error
  114. },
  115. function getPdfLoad(data) {
  116. self.loading = true;
  117. self.load(data, scale);
  118. self.loading = false;
  119. });
  120. },
  121. download: function pdfViewDownload() {
  122. window.open(this.url + '#pdfjs.action=download', '_parent');
  123. },
  124. navigateTo: function pdfViewNavigateTo(dest) {
  125. if (typeof dest === 'string')
  126. if(!this.destinations || ! this.destinations[dest]){
  127. return; // invalid destination
  128. }
  129. dest = this.destinations[dest];
  130. if (!(dest instanceof Array))
  131. return; // invalid destination
  132. // dest array looks like that: <page-ref> </XYZ|FitXXX> <args..>
  133. var destRef = dest[0];
  134. var pageNumber = destRef instanceof Object ?
  135. this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R'] : (destRef + 1);
  136. if (pageNumber) {
  137. this.page = pageNumber;
  138. var currentPage = this.pages[pageNumber - 1];
  139. currentPage.scrollIntoView(dest);
  140. }
  141. },
  142. getDestinationHash: function pdfViewGetDestinationHash(dest) {
  143. if (typeof dest === 'string')
  144. return '#' + escape(dest);
  145. if (dest instanceof Array) {
  146. var destRef = dest[0]; // see navigateTo method for dest format
  147. var pageNumber = destRef instanceof Object ?
  148. this.pagesRefMap[destRef.num + ' ' + destRef.gen + ' R'] :
  149. (destRef + 1);
  150. if (pageNumber) {
  151. var pdfOpenParams = '#page=' + pageNumber;
  152. if (isName(dest[1], 'XYZ')) {
  153. var scale = (dest[4] || this.currentScale);
  154. pdfOpenParams += '&zoom=' + (scale * 100);
  155. if (dest[2] || dest[3]) {
  156. pdfOpenParams += ',' + (dest[2] || 0) + ',' + (dest[3] || 0);
  157. }
  158. }
  159. return pdfOpenParams;
  160. }
  161. }
  162. return '';
  163. },
  164. error: function pdfViewError() {
  165. var loadingIndicator = document.getElementById('loading');
  166. loadingIndicator.innerHTML = 'Error';
  167. },
  168. progress: function pdfViewProgress(level) {
  169. var percent = Math.round(level * 100);
  170. var loadingIndicator = document.getElementById('loading');
  171. loadingIndicator.innerHTML = 'Loading... ' + percent + '%';
  172. },
  173. load: function pdfViewLoad(data, scale) {
  174. var loadingIndicator = document.getElementById('loading');
  175. loadingIndicator.setAttribute('hidden', 'true');
  176. var container = document.getElementById('viewer');
  177. while (container.hasChildNodes())
  178. container.removeChild(container.lastChild);
  179. var pdf = new PDFJS.PDFDoc(data);
  180. var pagesCount = pdf.numPages;
  181. document.getElementById('numPages').innerHTML = pagesCount;
  182. document.getElementById('pageNumber').max = pagesCount;
  183. var pages = this.pages = [];
  184. var pagesRefMap = {};
  185. var thumbnails = this.thumbnails = [];
  186. for (var i = 1; i <= pagesCount; i++) {
  187. var page = pdf.getPage(i);
  188. pages.push(new PageView(container, page, i, page.width, page.height,
  189. page.stats, this.navigateTo.bind(this)));
  190. /*thumbnails.push(new ThumbnailView(sidebar, page, i,
  191. page.width / page.height));*/
  192. var pageRef = page.ref;
  193. pagesRefMap[pageRef.num + ' ' + pageRef.gen + ' R'] = i;
  194. }
  195. this.setScale(scale || kDefaultScale, true);
  196. /*this.pagesRefMap = pagesRefMap;
  197. this.destinations = pdf.catalog.destinations;
  198. if (pdf.catalog.documentOutline) {
  199. this.outline = new DocumentOutlineView(pdf.catalog.documentOutline);
  200. var outlineSwitchButton = document.getElementById('outlineSwitch');
  201. outlineSwitchButton.removeAttribute('disabled');
  202. this.switchSidebarView('outline');
  203. }*/
  204. this.page = 1;
  205. /*if (this.initialBookmark) {
  206. this.setHash(this.initialBookmark);
  207. this.initialBookmark = null;
  208. }
  209. else
  210. this.page = 1;*/
  211. },
  212. setHash: function pdfViewSetHash(hash) {
  213. if (!hash)
  214. return;
  215. if (hash.indexOf('=') >= 0) {
  216. // parsing query string
  217. var paramsPairs = hash.split('&');
  218. var params = {};
  219. for (var i = 0; i < paramsPairs.length; ++i) {
  220. var paramPair = paramsPairs[i].split('=');
  221. params[paramPair[0]] = paramPair[1];
  222. }
  223. // borrowing syntax from "Parameters for Opening PDF Files"
  224. if ('nameddest' in params) {
  225. PDFView.navigateTo(params.nameddest);
  226. return;
  227. }
  228. if ('page' in params) {
  229. var pageNumber = (params.page | 0) || 1;
  230. this.page = pageNumber;
  231. if ('zoom' in params) {
  232. var zoomArgs = params.zoom.split(','); // scale,left,top
  233. // building destination array
  234. var dest = [null, new Name('XYZ'), (zoomArgs[1] | 0),
  235. (zoomArgs[2] | 0), (zoomArgs[0] | 0) / 100];
  236. var currentPage = this.pages[pageNumber - 1];
  237. currentPage.scrollIntoView(dest);
  238. } else
  239. this.page = params.page; // simple page
  240. return;
  241. }
  242. } else if (/^\d+$/.test(hash)) // page number
  243. this.page = hash;
  244. else // named destination
  245. PDFView.navigateTo(unescape(hash));
  246. },
  247. /*switchSidebarView: function pdfViewSwitchSidebarView(view) {
  248. var thumbsScrollView = document.getElementById('sidebarScrollView');
  249. var outlineScrollView = document.getElementById('outlineScrollView');
  250. var thumbsSwitchButton = document.getElementById('thumbsSwitch');
  251. var outlineSwitchButton = document.getElementById('outlineSwitch');
  252. switch (view) {
  253. case 'thumbs':
  254. thumbsScrollView.removeAttribute('hidden');
  255. outlineScrollView.setAttribute('hidden', 'true');
  256. thumbsSwitchButton.setAttribute('data-selected', true);
  257. outlineSwitchButton.removeAttribute('data-selected');
  258. break;
  259. case 'outline':
  260. thumbsScrollView.setAttribute('hidden', 'true');
  261. outlineScrollView.removeAttribute('hidden');
  262. thumbsSwitchButton.removeAttribute('data-selected');
  263. outlineSwitchButton.setAttribute('data-selected', true);
  264. break;
  265. }
  266. },*/
  267. getVisiblePages: function pdfViewGetVisiblePages() {
  268. var pages = this.pages;
  269. var kBottomMargin = 10;
  270. var visiblePages = [];
  271. var currentHeight = kBottomMargin;
  272. var windowTop = window.pageYOffset;
  273. for (var i = 1; i <= pages.length; ++i) {
  274. var page = pages[i - 1];
  275. var pageHeight = page.height * page.scale + kBottomMargin;
  276. if (currentHeight + pageHeight > windowTop)
  277. break;
  278. currentHeight += pageHeight;
  279. }
  280. var windowBottom = window.pageYOffset + window.innerHeight;
  281. for (; i <= pages.length && currentHeight < windowBottom; ++i) {
  282. var singlePage = pages[i - 1];
  283. visiblePages.push({ id: singlePage.id, y: currentHeight,
  284. view: singlePage });
  285. currentHeight += singlePage.height * singlePage.scale + kBottomMargin;
  286. }
  287. return visiblePages;
  288. }
  289. };
  290. var PageView = function pageView(container, content, id, pageWidth, pageHeight,
  291. stats, navigateTo) {
  292. this.id = id;
  293. this.content = content;
  294. var view = this.content.view;
  295. this.x = view.x;
  296. this.y = view.y;
  297. this.width = view.width;
  298. this.height = view.height;
  299. var anchor = document.createElement('a');
  300. anchor.name = '' + this.id;
  301. var div = document.createElement('div');
  302. div.id = 'pageContainer' + this.id;
  303. div.className = 'page';
  304. container.appendChild(anchor);
  305. container.appendChild(div);
  306. this.update = function pageViewUpdate(scale) {
  307. this.scale = scale || this.scale;
  308. div.style.width = (this.width * this.scale) + 'px';
  309. div.style.height = (this.height * this.scale) + 'px';
  310. var container = document.getElementById('viewer');
  311. container.style.left = (document.getElementById("content").offsetWidth-(this.width))/2 + 'px';
  312. while (div.hasChildNodes())
  313. div.removeChild(div.lastChild);
  314. div.removeAttribute('data-loaded');
  315. };
  316. function setupLinks(content, scale) {
  317. function bindLink(link, dest) {
  318. link.href = PDFView.getDestinationHash(dest);
  319. link.onclick = function pageViewSetupLinksOnclick() {
  320. if (dest)
  321. PDFView.navigateTo(dest);
  322. return false;
  323. };
  324. }
  325. var links = content.getLinks();
  326. for (var i = 0; i < links.length; i++) {
  327. var link = document.createElement('a');
  328. link.style.left = (Math.floor(links[i].x - view.x) * scale) + 'px';
  329. link.style.top = (Math.floor(links[i].y - view.y) * scale) + 'px';
  330. link.style.width = Math.ceil(links[i].width * scale) + 'px';
  331. link.style.height = Math.ceil(links[i].height * scale) + 'px';
  332. link.href = links[i].url || '';
  333. if (!links[i].url)
  334. bindLink(link, ('dest' in links[i]) ? links[i].dest : null);
  335. div.appendChild(link);
  336. }
  337. }
  338. this.getPagePoint = function pageViewGetPagePoint(x, y) {
  339. var scale = PDFView.currentScale;
  340. return this.content.rotatePoint(x / scale, y / scale);
  341. };
  342. this.scrollIntoView = function pageViewScrollIntoView(dest) {
  343. if (!dest) {
  344. div.scrollIntoView(true);
  345. return;
  346. }
  347. var x = 0, y = 0;
  348. var width = 0, height = 0, widthScale, heightScale;
  349. var scale = 0;
  350. switch (dest[1].name) {
  351. case 'XYZ':
  352. x = dest[2];
  353. y = dest[3];
  354. scale = dest[4];
  355. break;
  356. case 'Fit':
  357. case 'FitB':
  358. scale = 'page-fit';
  359. break;
  360. case 'FitH':
  361. case 'FitBH':
  362. y = dest[2];
  363. scale = 'page-width';
  364. break;
  365. case 'FitV':
  366. case 'FitBV':
  367. x = dest[2];
  368. scale = 'page-height';
  369. break;
  370. case 'FitR':
  371. x = dest[2];
  372. y = dest[3];
  373. width = dest[4] - x;
  374. height = dest[5] - y;
  375. widthScale = (window.innerWidth - kScrollbarPadding) /
  376. width / kCssUnits;
  377. heightScale = (window.innerHeight - kScrollbarPadding) /
  378. height / kCssUnits;
  379. scale = Math.min(widthScale, heightScale);
  380. break;
  381. default:
  382. return;
  383. }
  384. var boundingRect = [
  385. this.content.rotatePoint(x, y),
  386. this.content.rotatePoint(x + width, y + height)
  387. ];
  388. if (scale && scale !== PDFView.currentScale)
  389. PDFView.setScale(scale, true);
  390. setTimeout(function pageViewScrollIntoViewRelayout() {
  391. // letting page to re-layout before scrolling
  392. var scale = PDFView.currentScale;
  393. var x = Math.min(boundingRect[0].x, boundingRect[1].x);
  394. var y = Math.min(boundingRect[0].y, boundingRect[1].y);
  395. var width = Math.abs(boundingRect[0].x - boundingRect[1].x);
  396. var height = Math.abs(boundingRect[0].y - boundingRect[1].y);
  397. // using temporary div to scroll it into view
  398. var tempDiv = document.createElement('div');
  399. tempDiv.style.position = 'absolute';
  400. tempDiv.style.left = Math.floor(x * scale) + 'px';
  401. tempDiv.style.top = Math.floor(y * scale) + 'px';
  402. tempDiv.style.width = Math.ceil(width * scale) + 'px';
  403. tempDiv.style.height = Math.ceil(height * scale) + 'px';
  404. div.appendChild(tempDiv);
  405. tempDiv.scrollIntoView(true);
  406. div.removeChild(tempDiv);
  407. }, 0);
  408. };
  409. this.draw = function pageviewDraw() {
  410. if (div.hasChildNodes()) {
  411. this.updateStats();
  412. return false;
  413. }
  414. var canvas = document.createElement('canvas');
  415. canvas.id = 'page' + this.id;
  416. canvas.mozOpaque = true;
  417. div.appendChild(canvas);
  418. var scale = this.scale;
  419. canvas.width = pageWidth * scale;
  420. canvas.height = pageHeight * scale;
  421. var ctx = canvas.getContext('2d');
  422. ctx.save();
  423. ctx.fillStyle = 'rgb(255, 255, 255)';
  424. ctx.fillRect(0, 0, canvas.width, canvas.height);
  425. ctx.restore();
  426. ctx.translate(-this.x * scale, -this.y * scale);
  427. stats.begin = Date.now();
  428. this.content.startRendering(ctx, this.updateStats);
  429. setupLinks(this.content, this.scale);
  430. div.setAttribute('data-loaded', true);
  431. return true;
  432. };
  433. this.updateStats = function pageViewUpdateStats() {
  434. var t1 = stats.compile, t2 = stats.fonts, t3 = stats.render;
  435. var str = 'Time to compile/fonts/render: ' +
  436. (t1 - stats.begin) + '/' + (t2 - t1) + '/' + (t3 - t2) + ' ms';
  437. };
  438. };
  439. var ThumbnailView = function thumbnailView(container, page, id, pageRatio) {
  440. var anchor = document.createElement('a');
  441. anchor.href = '#' + id;
  442. anchor.onclick = function stopNivigation() {
  443. PDFView.page = id;
  444. return false;
  445. };
  446. var div = document.createElement('div');
  447. div.id = 'thumbnailContainer' + id;
  448. div.className = 'thumbnail';
  449. anchor.appendChild(div);
  450. container.appendChild(anchor);
  451. this.draw = function thumbnailViewDraw() {
  452. if (div.hasChildNodes())
  453. return;
  454. var canvas = document.createElement('canvas');
  455. canvas.id = 'thumbnail' + id;
  456. canvas.mozOpaque = true;
  457. var maxThumbSize = 134;
  458. canvas.width = pageRatio >= 1 ? maxThumbSize :
  459. maxThumbSize * pageRatio;
  460. canvas.height = pageRatio <= 1 ? maxThumbSize :
  461. maxThumbSize / pageRatio;
  462. div.setAttribute('data-loaded', true);
  463. div.appendChild(canvas);
  464. var ctx = canvas.getContext('2d');
  465. ctx.save();
  466. ctx.fillStyle = 'rgb(255, 255, 255)';
  467. ctx.fillRect(0, 0, canvas.width, canvas.height);
  468. ctx.restore();
  469. var view = page.view;
  470. var scaleX = (canvas.width / page.width);
  471. var scaleY = (canvas.height / page.height);
  472. ctx.translate(-view.x * scaleX, -view.y * scaleY);
  473. div.style.width = (view.width * scaleX) + 'px';
  474. div.style.height = (view.height * scaleY) + 'px';
  475. div.style.lineHeight = (view.height * scaleY) + 'px';
  476. page.startRendering(ctx, function thumbnailViewDrawStartRendering() {});
  477. };
  478. };
  479. var DocumentOutlineView = function documentOutlineView(outline) {
  480. var outlineView = document.getElementById('outlineView');
  481. function bindItemLink(domObj, item) {
  482. domObj.href = PDFView.getDestinationHash(item.dest);
  483. domObj.onclick = function documentOutlineViewOnclick(e) {
  484. PDFView.navigateTo(item.dest);
  485. return false;
  486. };
  487. }
  488. var queue = [{parent: outlineView, items: outline}];
  489. while (queue.length > 0) {
  490. var levelData = queue.shift();
  491. var i, n = levelData.items.length;
  492. for (i = 0; i < n; i++) {
  493. var item = levelData.items[i];
  494. var div = document.createElement('div');
  495. div.className = 'outlineItem';
  496. var a = document.createElement('a');
  497. bindItemLink(a, item);
  498. a.textContent = item.title;
  499. div.appendChild(a);
  500. if (item.items.length > 0) {
  501. var itemsDiv = document.createElement('div');
  502. itemsDiv.className = 'outlineItems';
  503. div.appendChild(itemsDiv);
  504. queue.push({parent: itemsDiv, items: item.items});
  505. }
  506. levelData.parent.appendChild(div);
  507. }
  508. }
  509. };
  510. function webViewerLoad(url) {
  511. var scale = kDefaultScale;
  512. PDFView.open(url, parseFloat(PDFView.parseScale(scale)));
  513. if (!window.File || !window.FileReader || !window.FileList || !window.Blob)
  514. document.getElementById('fileInput').setAttribute('hidden', 'true');
  515. else
  516. document.getElementById('fileInput').value = null;
  517. }
  518. window.addEventListener('unload', function webViewerUnload(evt) {
  519. window.scrollTo(0, 0);
  520. }, true);
  521. function updateViewarea() {
  522. var visiblePages = PDFView.getVisiblePages();
  523. for (var i = 0; i < visiblePages.length; i++) {
  524. var page = visiblePages[i];
  525. if (PDFView.pages[page.id - 1].draw())
  526. cache.push(page.view);
  527. }
  528. if (!visiblePages.length)
  529. return;
  530. updateViewarea.inProgress = true; // used in "set page"
  531. var currentId = PDFView.page;
  532. var firstPage = visiblePages[0];
  533. PDFView.page = firstPage.id;
  534. updateViewarea.inProgress = false;
  535. var kViewerTopMargin = 52;
  536. var pageNumber = firstPage.id;
  537. var pdfOpenParams = '#page=' + pageNumber;
  538. pdfOpenParams += '&zoom=' + Math.round(PDFView.currentScale * 100);
  539. var currentPage = PDFView.pages[pageNumber - 1];
  540. var topLeft = currentPage.getPagePoint(window.pageXOffset,
  541. window.pageYOffset - firstPage.y - kViewerTopMargin);
  542. pdfOpenParams += ',' + Math.round(topLeft.x) + ',' + Math.round(topLeft.y);
  543. }
  544. window.addEventListener('scroll', function webViewerScroll(evt) {
  545. if(!PDFView.active){
  546. return;
  547. }
  548. updateViewarea();
  549. }, true);
  550. window.addEventListener('resize', function webViewerResize(evt) {
  551. if(!PDFView.active){
  552. return;
  553. }
  554. if (document.getElementById('pageWidthOption').selected ||
  555. document.getElementById('pageFitOption').selected)
  556. PDFView.parseScale(document.getElementById('scaleSelect').value);
  557. updateViewarea();
  558. });
  559. window.addEventListener('hashchange', function webViewerHashchange(evt) {
  560. if(!PDFView.active){
  561. return;
  562. }
  563. PDFView.setHash(document.location.hash.substring(1));
  564. });
  565. window.addEventListener('change', function webViewerChange(evt) {
  566. if(!PDFView.active){
  567. return;
  568. }
  569. var files = evt.target.files;
  570. if (!files || files.length == 0)
  571. return;
  572. // Read the local file into a Uint8Array.
  573. var fileReader = new FileReader();
  574. fileReader.onload = function webViewerChangeFileReaderOnload(evt) {
  575. var data = evt.target.result;
  576. var buffer = new ArrayBuffer(data.length);
  577. var uint8Array = new Uint8Array(buffer);
  578. for (var i = 0; i < data.length; i++)
  579. uint8Array[i] = data.charCodeAt(i);
  580. PDFView.load(uint8Array);
  581. };
  582. // Read as a binary string since "readAsArrayBuffer" is not yet
  583. // implemented in Firefox.
  584. var file = files[0];
  585. fileReader.readAsBinaryString(file);
  586. document.title = PDFView.Ptitle;
  587. // URL does not reflect proper document location - hiding some icons.
  588. document.getElementById('download').setAttribute('hidden', 'true');
  589. }, true);
  590. window.addEventListener('scalechange', function scalechange(evt) {
  591. if(!PDFView.active){
  592. return;
  593. }
  594. var customScaleOption = document.getElementById('customScaleOption');
  595. customScaleOption.selected = false;
  596. if (!evt.resetAutoSettings &&
  597. (document.getElementById('pageWidthOption').selected ||
  598. document.getElementById('pageFitOption').selected)) {
  599. updateViewarea();
  600. return;
  601. }
  602. var options = document.getElementById('scaleSelect').options;
  603. var predefinedValueFound = false;
  604. var value = '' + evt.scale;
  605. for (var i = 0; i < options.length; i++) {
  606. var option = options[i];
  607. if (option.value != value) {
  608. option.selected = false;
  609. continue;
  610. }
  611. option.selected = true;
  612. predefinedValueFound = true;
  613. }
  614. if (!predefinedValueFound) {
  615. customScaleOption.textContent = Math.round(evt.scale * 10000) / 100 + '%';
  616. customScaleOption.selected = true;
  617. }
  618. updateViewarea();
  619. }, true);
  620. window.addEventListener('pagechange', function pagechange(evt) {
  621. if(!PDFView.active){
  622. return;
  623. }
  624. var page = evt.pageNumber;
  625. if (document.getElementById('pageNumber').value != page)
  626. document.getElementById('pageNumber').value = page;
  627. document.getElementById('previous').disabled = (page <= 1);
  628. document.getElementById('next').disabled = (page >= PDFView.pages.length);
  629. }, true);
  630. window.addEventListener('keydown', function keydown(evt) {
  631. if(!PDFView.active){
  632. return;
  633. }
  634. var curElement = document.activeElement;
  635. var controlsElement = document.getElementById('controls2');
  636. while (curElement) {
  637. if (curElement === controlsElement)
  638. return; // ignoring if the 'controls' element is focused
  639. curElement = curElement.parentNode;
  640. }
  641. switch (evt.keyCode) {
  642. case 61: // FF/Mac '='
  643. case 107: // FF '+' and '='
  644. case 187: // Chrome '+'
  645. PDFView.zoomIn();
  646. break;
  647. case 109: // FF '-'
  648. case 189: // Chrome '-'
  649. PDFView.zoomOut();
  650. break;
  651. case 48: // '0'
  652. PDFView.setScale(kDefaultScale, true);
  653. break;
  654. }
  655. });