pdfh5.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. ;
  2. (function(g, fn) {
  3. var version = "1.3.18",
  4. pdfjsVersion = "2.3.200";
  5. console.log("pdfh5.js v" + version + " https://www.gjtool.cn")
  6. if (typeof require !== 'undefined') {
  7. if (g.$ === undefined) {
  8. g.$ = require('./jquery-1.11.3.min.js');
  9. }
  10. g.pdfjsWorker = require('./pdf.worker.js');
  11. g.pdfjsLib = require('./pdf.js');
  12. }
  13. var pdfjsLib = g.pdfjsLib,
  14. $ = g.$,
  15. pdfjsWorker = g.pdfjsWorker;
  16. if (typeof define === 'function' && define.amd) {
  17. define(function() {
  18. return fn(g, pdfjsWorker, pdfjsLib, $, version)
  19. })
  20. } else if (typeof module !== 'undefined' && module.exports) {
  21. module.exports = fn(g, pdfjsWorker, pdfjsLib, $, version)
  22. } else {
  23. g.Pdfh5 = fn(g, pdfjsWorker, pdfjsLib, $, version)
  24. }
  25. })(typeof window !== 'undefined' ? window : this, function(g, pdfjsWorker, pdfjsLib, $, version) {
  26. 'use strict';
  27. var _createClass = function() {
  28. function defineProperties(target, props) {
  29. for (var i = 0; i < props.length; i++) {
  30. var descriptor = props[i];
  31. descriptor.enumerable = descriptor.enumerable || false;
  32. descriptor.configurable = true;
  33. if ("value" in descriptor) descriptor.writable = true;
  34. Object.defineProperty(target, descriptor.key, descriptor);
  35. }
  36. }
  37. return function(Constructor, protoProps, staticProps) {
  38. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  39. if (staticProps) defineProperties(Constructor, staticProps);
  40. return Constructor;
  41. };
  42. }();
  43. function _classCallCheck(instance, Constructor) {
  44. if (!(instance instanceof Constructor)) {
  45. throw new TypeError("Cannot call a class as a function");
  46. }
  47. }
  48. var renderTextLayer = pdfjsLib.renderTextLayer;
  49. var EXPAND_DIVS_TIMEOUT = 300; // ms
  50. var TextLayerBuilder = function() {
  51. function TextLayerBuilder(_ref) {
  52. var textLayerDiv = _ref.textLayerDiv;
  53. var eventBus = _ref.eventBus;
  54. var pageIndex = _ref.pageIndex;
  55. var viewport = _ref.viewport;
  56. var _ref$findController = _ref.findController;
  57. var findController = _ref$findController === undefined ? null : _ref$findController;
  58. var _ref$enhanceTextSelec = _ref.enhanceTextSelection;
  59. var enhanceTextSelection = _ref$enhanceTextSelec === undefined ? false : _ref$enhanceTextSelec;
  60. _classCallCheck(this, TextLayerBuilder);
  61. this.textLayerDiv = textLayerDiv;
  62. this.eventBus = eventBus;
  63. this.textContent = null;
  64. this.textContentItemsStr = [];
  65. this.textContentStream = null;
  66. this.renderingDone = false;
  67. this.pageIdx = pageIndex;
  68. this.pageNumber = this.pageIdx + 1;
  69. this.matches = [];
  70. this.viewport = viewport;
  71. this.textDivs = [];
  72. this.findController = findController;
  73. this.textLayerRenderTask = null;
  74. this.enhanceTextSelection = enhanceTextSelection;
  75. this._onUpdateTextLayerMatches = null;
  76. this._bindMouse();
  77. }
  78. /**
  79. * @private
  80. */
  81. _createClass(TextLayerBuilder, [{
  82. key: "_finishRendering",
  83. value: function _finishRendering() {
  84. this.renderingDone = true;
  85. if (!this.enhanceTextSelection) {
  86. var endOfContent = document.createElement("div");
  87. endOfContent.className = "endOfContent";
  88. this.textLayerDiv.appendChild(endOfContent);
  89. }
  90. if (this.eventBus) {
  91. this.eventBus.dispatch("textlayerrendered", {
  92. source: this,
  93. pageNumber: this.pageNumber,
  94. numTextDivs: this.textDivs.length
  95. });
  96. }
  97. }
  98. /**
  99. * Renders the text layer.
  100. *
  101. * @param {number} [timeout] - Wait for a specified amount of milliseconds
  102. * before rendering.
  103. */
  104. }, {
  105. key: "render",
  106. value: function render() {
  107. var _this = this;
  108. var timeout = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];
  109. if (!(this.textContent || this.textContentStream) || this.renderingDone) {
  110. return;
  111. }
  112. this.cancel();
  113. this.textDivs = [];
  114. var textLayerFrag = document.createDocumentFragment();
  115. this.textLayerRenderTask = renderTextLayer({
  116. textContent: this.textContent,
  117. textContentStream: this.textContentStream,
  118. container: textLayerFrag,
  119. viewport: this.viewport,
  120. textDivs: this.textDivs,
  121. textContentItemsStr: this.textContentItemsStr,
  122. timeout: timeout,
  123. enhanceTextSelection: this.enhanceTextSelection
  124. });
  125. this.textLayerRenderTask.promise.then(function() {
  126. _this.textLayerDiv.appendChild(textLayerFrag);
  127. _this._finishRendering();
  128. _this._updateMatches();
  129. }, function(reason) {
  130. // Cancelled or failed to render text layer; skipping errors.
  131. });
  132. if (!this._onUpdateTextLayerMatches) {
  133. this._onUpdateTextLayerMatches = function(evt) {
  134. if (evt.pageIndex === _this.pageIdx || evt.pageIndex === -1) {
  135. _this._updateMatches();
  136. }
  137. };
  138. if (this.eventBus) {
  139. this.eventBus._on("updatetextlayermatches", this._onUpdateTextLayerMatches);
  140. }
  141. }
  142. }
  143. /**
  144. * Cancel rendering of the text layer.
  145. */
  146. }, {
  147. key: "cancel",
  148. value: function cancel() {
  149. if (this.textLayerRenderTask) {
  150. this.textLayerRenderTask.cancel();
  151. this.textLayerRenderTask = null;
  152. }
  153. if (this._onUpdateTextLayerMatches) {
  154. this.eventBus._off("updatetextlayermatches", this._onUpdateTextLayerMatches);
  155. this._onUpdateTextLayerMatches = null;
  156. }
  157. }
  158. }, {
  159. key: "setTextContentStream",
  160. value: function setTextContentStream(readableStream) {
  161. this.cancel();
  162. this.textContentStream = readableStream;
  163. }
  164. }, {
  165. key: "setTextContent",
  166. value: function setTextContent(textContent) {
  167. this.cancel();
  168. this.textContent = textContent;
  169. }
  170. }, {
  171. key: "_convertMatches",
  172. value: function _convertMatches(matches, matchesLength) {
  173. // Early exit if there is nothing to convert.
  174. if (!matches) {
  175. return [];
  176. }
  177. var findController = this.findController;
  178. var textContentItemsStr = this.textContentItemsStr;
  179. var i = 0,
  180. iIndex = 0;
  181. var end = textContentItemsStr.length - 1;
  182. var queryLen = findController.state.query.length;
  183. var result = [];
  184. for (var m = 0, mm = matches.length; m < mm; m++) {
  185. // Calculate the start position.
  186. var matchIdx = matches[m];
  187. // Loop over the divIdxs.
  188. while (i !== end && matchIdx >= iIndex + textContentItemsStr[i].length) {
  189. iIndex += textContentItemsStr[i].length;
  190. i++;
  191. }
  192. if (i === textContentItemsStr.length) {
  193. console.error("Could not find a matching mapping");
  194. }
  195. var match = {
  196. begin: {
  197. divIdx: i,
  198. offset: matchIdx - iIndex
  199. }
  200. };
  201. // Calculate the end position.
  202. if (matchesLength) {
  203. // Multiterm search.
  204. matchIdx += matchesLength[m];
  205. } else {
  206. // Phrase search.
  207. matchIdx += queryLen;
  208. }
  209. // Somewhat the same array as above, but use > instead of >= to get
  210. // the end position right.
  211. while (i !== end && matchIdx > iIndex + textContentItemsStr[i].length) {
  212. iIndex += textContentItemsStr[i].length;
  213. i++;
  214. }
  215. match.end = {
  216. divIdx: i,
  217. offset: matchIdx - iIndex
  218. };
  219. result.push(match);
  220. }
  221. return result;
  222. }
  223. }, {
  224. key: "_renderMatches",
  225. value: function _renderMatches(matches) {
  226. // Early exit if there is nothing to render.
  227. if (matches.length === 0) {
  228. return;
  229. }
  230. var findController = this.findController;
  231. var pageIdx = this.pageIdx;
  232. var textContentItemsStr = this.textContentItemsStr;
  233. var textDivs = this.textDivs;
  234. var isSelectedPage = pageIdx === findController.selected.pageIdx;
  235. var selectedMatchIdx = findController.selected.matchIdx;
  236. var highlightAll = findController.state.highlightAll;
  237. var prevEnd = null;
  238. var infinity = {
  239. divIdx: -1,
  240. offset: undefined
  241. };
  242. function beginText(begin, className) {
  243. var divIdx = begin.divIdx;
  244. textDivs[divIdx].textContent = "";
  245. appendTextToDiv(divIdx, 0, begin.offset, className);
  246. }
  247. function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
  248. var div = textDivs[divIdx];
  249. var content = textContentItemsStr[divIdx].substring(fromOffset, toOffset);
  250. var node = document.createTextNode(content);
  251. if (className) {
  252. var span = document.createElement("span");
  253. span.className = className;
  254. span.appendChild(node);
  255. div.appendChild(span);
  256. return;
  257. }
  258. div.appendChild(node);
  259. }
  260. var i0 = selectedMatchIdx,
  261. i1 = i0 + 1;
  262. if (highlightAll) {
  263. i0 = 0;
  264. i1 = matches.length;
  265. } else if (!isSelectedPage) {
  266. // Not highlighting all and this isn't the selected page, so do nothing.
  267. return;
  268. }
  269. for (var i = i0; i < i1; i++) {
  270. var match = matches[i];
  271. var begin = match.begin;
  272. var end = match.end;
  273. var isSelected = isSelectedPage && i === selectedMatchIdx;
  274. var highlightSuffix = isSelected ? " selected" : "";
  275. if (isSelected) {
  276. // Attempt to scroll the selected match into view.
  277. findController.scrollMatchIntoView({
  278. element: textDivs[begin.divIdx],
  279. pageIndex: pageIdx,
  280. matchIndex: selectedMatchIdx
  281. });
  282. }
  283. // Match inside new div.
  284. if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
  285. // If there was a previous div, then add the text at the end.
  286. if (prevEnd !== null) {
  287. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  288. }
  289. // Clear the divs and set the content until the starting point.
  290. beginText(begin);
  291. } else {
  292. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
  293. }
  294. if (begin.divIdx === end.divIdx) {
  295. appendTextToDiv(begin.divIdx, begin.offset, end.offset, "highlight" + highlightSuffix);
  296. } else {
  297. appendTextToDiv(begin.divIdx, begin.offset, infinity.offset, "highlight begin" + highlightSuffix);
  298. for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
  299. textDivs[n0].className = "highlight middle" + highlightSuffix;
  300. }
  301. beginText(end, "highlight end" + highlightSuffix);
  302. }
  303. prevEnd = end;
  304. }
  305. if (prevEnd) {
  306. appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
  307. }
  308. }
  309. }, {
  310. key: "_updateMatches",
  311. value: function _updateMatches() {
  312. // Only show matches when all rendering is done.
  313. if (!this.renderingDone) {
  314. return;
  315. }
  316. var findController = this.findController;
  317. var matches = this.matches;
  318. var pageIdx = this.pageIdx;
  319. var textContentItemsStr = this.textContentItemsStr;
  320. var textDivs = this.textDivs;
  321. var clearedUntilDivIdx = -1;
  322. // Clear all current matches.
  323. for (var i = 0, ii = matches.length; i < ii; i++) {
  324. var match = matches[i];
  325. var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
  326. for (var n = begin, end = match.end.divIdx; n <= end; n++) {
  327. var div = textDivs[n];
  328. div.textContent = textContentItemsStr[n];
  329. div.className = "";
  330. }
  331. clearedUntilDivIdx = match.end.divIdx + 1;
  332. }
  333. if (!findController || !findController.highlightMatches) {
  334. return;
  335. }
  336. // Convert the matches on the `findController` into the match format
  337. // used for the textLayer.
  338. var pageMatches = findController.pageMatches[pageIdx] || null;
  339. var pageMatchesLength = findController.pageMatchesLength[pageIdx] || null;
  340. this.matches = this._convertMatches(pageMatches, pageMatchesLength);
  341. this._renderMatches(this.matches);
  342. }
  343. /**
  344. * Improves text selection by adding an additional div where the mouse was
  345. * clicked. This reduces flickering of the content if the mouse is slowly
  346. * dragged up or down.
  347. *
  348. * @private
  349. */
  350. }, {
  351. key: "_bindMouse",
  352. value: function _bindMouse() {
  353. var _this2 = this;
  354. var div = this.textLayerDiv;
  355. var expandDivsTimer = null;
  356. div.addEventListener("mousedown", function(evt) {
  357. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  358. _this2.textLayerRenderTask.expandTextDivs(true);
  359. if ((typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) && expandDivsTimer) {
  360. clearTimeout(expandDivsTimer);
  361. expandDivsTimer = null;
  362. }
  363. return;
  364. }
  365. var end = div.querySelector(".endOfContent");
  366. if (!end) {
  367. return;
  368. }
  369. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  370. // On non-Firefox browsers, the selection will feel better if the height
  371. // of the `endOfContent` div is adjusted to start at mouse click
  372. // location. This avoids flickering when the selection moves up.
  373. // However it does not work when selection is started on empty space.
  374. var adjustTop = evt.target !== div;
  375. if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
  376. adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";
  377. }
  378. if (adjustTop) {
  379. var divBounds = div.getBoundingClientRect();
  380. var r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);
  381. end.style.top = (r * 100).toFixed(2) + "%";
  382. }
  383. }
  384. end.classList.add("active");
  385. });
  386. div.addEventListener("mouseup", function() {
  387. if (_this2.enhanceTextSelection && _this2.textLayerRenderTask) {
  388. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  389. expandDivsTimer = setTimeout(function() {
  390. if (_this2.textLayerRenderTask) {
  391. _this2.textLayerRenderTask.expandTextDivs(false);
  392. }
  393. expandDivsTimer = null;
  394. }, EXPAND_DIVS_TIMEOUT);
  395. } else {
  396. _this2.textLayerRenderTask.expandTextDivs(false);
  397. }
  398. return;
  399. }
  400. var end = div.querySelector(".endOfContent");
  401. if (!end) {
  402. return;
  403. }
  404. if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
  405. end.style.top = "";
  406. }
  407. end.classList.remove("active");
  408. });
  409. }
  410. }]);
  411. return TextLayerBuilder;
  412. }();
  413. /**
  414. * @implements IPDFTextLayerFactory
  415. */
  416. var DefaultTextLayerFactory = function() {
  417. function DefaultTextLayerFactory() {
  418. _classCallCheck(this, DefaultTextLayerFactory);
  419. }
  420. _createClass(DefaultTextLayerFactory, [{
  421. key: "createTextLayerBuilder",
  422. /**
  423. * @param {HTMLDivElement} textLayerDiv
  424. * @param {number} pageIndex
  425. * @param {PageViewport} viewport
  426. * @param {boolean} enhanceTextSelection
  427. * @param {EventBus} eventBus
  428. * @returns {TextLayerBuilder}
  429. */
  430. value: function createTextLayerBuilder(textLayerDiv, pageIndex, viewport) {
  431. var enhanceTextSelection = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
  432. var eventBus = arguments[4];
  433. return new TextLayerBuilder({
  434. textLayerDiv: textLayerDiv,
  435. pageIndex: pageIndex,
  436. viewport: viewport,
  437. enhanceTextSelection: enhanceTextSelection,
  438. eventBus: eventBus
  439. });
  440. }
  441. }]);
  442. return DefaultTextLayerFactory;
  443. }();
  444. g.TextLayerBuilder = TextLayerBuilder;
  445. g.DefaultTextLayerFactory = DefaultTextLayerFactory;
  446. var definePinchZoom = function($) {
  447. var PinchZoom = function(el, options, viewerContainer) {
  448. this.el = $(el);
  449. this.viewerContainer = viewerContainer;
  450. this.zoomFactor = 1;
  451. this.lastScale = 1;
  452. this.offset = {
  453. x: 0,
  454. y: 0
  455. };
  456. this.options = $.extend({}, this.defaults, options);
  457. this.options.zoomOutFactor = isNaN(options.zoomOutFactor) ? 1.2 : options.zoomOutFactor;
  458. this.options.animationDuration = isNaN(options.animationDuration) ? 300 : options.animationDuration;
  459. this.options.maxZoom = isNaN(options.maxZoom) ? 3 : options.maxZoom;
  460. this.options.minZoom = isNaN(options.minZoom) ? 0.8 : options.minZoom;
  461. this.setupMarkup();
  462. this.bindEvents();
  463. this.update();
  464. this.enable();
  465. this.height = 0;
  466. this.load = false;
  467. this.direction = null;
  468. this.clientY = null;
  469. this.lastclientY = null;
  470. },
  471. sum = function(a, b) {
  472. return a + b;
  473. },
  474. isCloseTo = function(value, expected) {
  475. return value > expected - 0.01 && value < expected + 0.01;
  476. };
  477. PinchZoom.prototype = {
  478. defaults: {
  479. tapZoomFactor: 3,
  480. zoomOutFactor: 1.2,
  481. animationDuration: 300,
  482. maxZoom: 5,
  483. minZoom: 0.8,
  484. lockDragAxis: false,
  485. use2d: true,
  486. zoomStartEventName: 'pz_zoomstart',
  487. zoomEndEventName: 'pz_zoomend',
  488. dragStartEventName: 'pz_dragstart',
  489. dragEndEventName: 'pz_dragend',
  490. doubleTapEventName: 'pz_doubletap'
  491. },
  492. handleDragStart: function(event) {
  493. this.el.trigger(this.options.dragStartEventName);
  494. this.stopAnimation();
  495. this.lastDragPosition = false;
  496. this.hasInteraction = true;
  497. this.handleDrag(event);
  498. },
  499. handleDrag: function(event) {
  500. if (this.zoomFactor > 1.0) {
  501. var touch = this.getTouches(event)[0];
  502. this.drag(touch, this.lastDragPosition, event);
  503. this.offset = this.sanitizeOffset(this.offset);
  504. this.lastDragPosition = touch;
  505. }
  506. },
  507. handleDragEnd: function() {
  508. this.el.trigger(this.options.dragEndEventName);
  509. this.end();
  510. },
  511. handleZoomStart: function(event) {
  512. this.el.trigger(this.options.zoomStartEventName);
  513. this.stopAnimation();
  514. this.lastScale = 1;
  515. this.nthZoom = 0;
  516. this.lastZoomCenter = false;
  517. this.hasInteraction = true;
  518. },
  519. handleZoom: function(event, newScale) {
  520. var touchCenter = this.getTouchCenter(this.getTouches(event)),
  521. scale = newScale / this.lastScale;
  522. this.lastScale = newScale;
  523. this.nthZoom += 1;
  524. if (this.nthZoom > 3) {
  525. this.scale(scale, touchCenter);
  526. this.drag(touchCenter, this.lastZoomCenter);
  527. }
  528. this.lastZoomCenter = touchCenter;
  529. },
  530. handleZoomEnd: function() {
  531. this.el.trigger(this.options.zoomEndEventName);
  532. this.end();
  533. },
  534. handleDoubleTap: function(event) {
  535. var center = this.getTouches(event)[0],
  536. zoomFactor = this.zoomFactor > 1 ? 1 : this.options.tapZoomFactor,
  537. startZoomFactor = this.zoomFactor,
  538. updateProgress = (function(progress) {
  539. this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center);
  540. }).bind(this);
  541. if (this.hasInteraction) {
  542. return;
  543. }
  544. if (startZoomFactor > zoomFactor) {
  545. center = this.getCurrentZoomCenter();
  546. }
  547. this.animate(this.options.animationDuration, updateProgress, this.swing);
  548. this.el.trigger(this.options.doubleTapEventName);
  549. },
  550. sanitizeOffset: function(offset) {
  551. var maxX = (this.zoomFactor - 1) * this.getContainerX(),
  552. maxY = (this.zoomFactor - 1) * this.getContainerY(),
  553. maxOffsetX = Math.max(maxX, 0),
  554. maxOffsetY = Math.max(maxY, 0),
  555. minOffsetX = Math.min(maxX, 0),
  556. minOffsetY = Math.min(maxY, 0);
  557. var x = Math.min(Math.max(offset.x, minOffsetX), maxOffsetX),
  558. y = Math.min(Math.max(offset.y, minOffsetY), maxOffsetY);
  559. return {
  560. x: x,
  561. y: y
  562. };
  563. },
  564. scaleTo: function(zoomFactor, center) {
  565. this.scale(zoomFactor / this.zoomFactor, center);
  566. },
  567. scale: function(scale, center) {
  568. scale = this.scaleZoomFactor(scale);
  569. this.addOffset({
  570. x: (scale - 1) * (center.x + this.offset.x),
  571. y: (scale - 1) * (center.y + this.offset.y)
  572. });
  573. this.done && this.done.call(this, this.getInitialZoomFactor() * this.zoomFactor)
  574. },
  575. scaleZoomFactor: function(scale) {
  576. var originalZoomFactor = this.zoomFactor;
  577. this.zoomFactor *= scale;
  578. this.zoomFactor = Math.min(this.options.maxZoom, Math.max(this.zoomFactor, this.options.minZoom));
  579. return this.zoomFactor / originalZoomFactor;
  580. },
  581. drag: function(center, lastCenter, event) {
  582. if (lastCenter) {
  583. if (this.options.lockDragAxis) {
  584. if (Math.abs(center.x - lastCenter.x) > Math.abs(center.y - lastCenter.y)) {
  585. this.addOffset({
  586. x: -(center.x - lastCenter.x),
  587. y: 0
  588. });
  589. } else {
  590. this.addOffset({
  591. y: -(center.y - lastCenter.y),
  592. x: 0
  593. });
  594. }
  595. } else {
  596. if (center.y - lastCenter.y < 0) {
  597. this.direction = "down";
  598. } else if (center.y - lastCenter.y > 10) {
  599. this.direction = "up";
  600. }
  601. this.addOffset({
  602. y: -(center.y - lastCenter.y),
  603. x: -(center.x - lastCenter.x)
  604. });
  605. }
  606. }
  607. },
  608. getTouchCenter: function(touches) {
  609. return this.getVectorAvg(touches);
  610. },
  611. getVectorAvg: function(vectors) {
  612. return {
  613. x: vectors.map(function(v) {
  614. return v.x;
  615. }).reduce(sum) / vectors.length,
  616. y: vectors.map(function(v) {
  617. return v.y;
  618. }).reduce(sum) / vectors.length
  619. };
  620. },
  621. addOffset: function(offset) {
  622. this.offset = {
  623. x: this.offset.x + offset.x,
  624. y: this.offset.y + offset.y
  625. };
  626. },
  627. sanitize: function() {
  628. if (this.zoomFactor < this.options.zoomOutFactor) {
  629. this.zoomOutAnimation();
  630. } else if (this.isInsaneOffset(this.offset)) {
  631. this.sanitizeOffsetAnimation();
  632. }
  633. },
  634. isInsaneOffset: function(offset) {
  635. var sanitizedOffset = this.sanitizeOffset(offset);
  636. return sanitizedOffset.x !== offset.x ||
  637. sanitizedOffset.y !== offset.y;
  638. },
  639. sanitizeOffsetAnimation: function() {
  640. var targetOffset = this.sanitizeOffset(this.offset),
  641. startOffset = {
  642. x: this.offset.x,
  643. y: this.offset.y
  644. },
  645. updateProgress = (function(progress) {
  646. this.offset.x = startOffset.x + progress * (targetOffset.x - startOffset.x);
  647. this.offset.y = startOffset.y + progress * (targetOffset.y - startOffset.y);
  648. this.update();
  649. }).bind(this);
  650. this.animate(
  651. this.options.animationDuration,
  652. updateProgress,
  653. this.swing
  654. );
  655. },
  656. zoomOutAnimation: function() {
  657. var startZoomFactor = this.zoomFactor,
  658. zoomFactor = 1,
  659. center = this.getCurrentZoomCenter(),
  660. updateProgress = (function(progress) {
  661. this.scaleTo(startZoomFactor + progress * (zoomFactor - startZoomFactor), center);
  662. }).bind(this);
  663. this.animate(
  664. this.options.animationDuration,
  665. updateProgress,
  666. this.swing
  667. );
  668. },
  669. updateAspectRatio: function() {
  670. this.setContainerY(this.getContainerX() / this.getAspectRatio());
  671. },
  672. getInitialZoomFactor: function() {
  673. if (this.container[0] && this.el[0]) {
  674. return this.container[0].offsetWidth / this.el[0].offsetWidth;
  675. } else {
  676. return 0
  677. }
  678. },
  679. getAspectRatio: function() {
  680. if (this.el[0]) {
  681. var offsetHeight = this.el[0].offsetHeight;
  682. return this.container[0].offsetWidth / offsetHeight;
  683. } else {
  684. return 0
  685. }
  686. },
  687. getCurrentZoomCenter: function() {
  688. var length = this.container[0].offsetWidth * this.zoomFactor,
  689. offsetLeft = this.offset.x,
  690. offsetRight = length - offsetLeft - this.container[0].offsetWidth,
  691. widthOffsetRatio = offsetLeft / offsetRight,
  692. centerX = widthOffsetRatio * this.container[0].offsetWidth / (widthOffsetRatio + 1),
  693. height = this.container[0].offsetHeight * this.zoomFactor,
  694. offsetTop = this.offset.y,
  695. offsetBottom = height - offsetTop - this.container[0].offsetHeight,
  696. heightOffsetRatio = offsetTop / offsetBottom,
  697. centerY = heightOffsetRatio * this.container[0].offsetHeight / (heightOffsetRatio + 1);
  698. if (offsetRight === 0) {
  699. centerX = this.container[0].offsetWidth;
  700. }
  701. if (offsetBottom === 0) {
  702. centerY = this.container[0].offsetHeight;
  703. }
  704. return {
  705. x: centerX,
  706. y: centerY
  707. };
  708. },
  709. canDrag: function() {
  710. return !isCloseTo(this.zoomFactor, 1);
  711. },
  712. getTouches: function(event) {
  713. var position = this.container.offset();
  714. return Array.prototype.slice.call(event.touches).map(function(touch) {
  715. return {
  716. x: touch.pageX - position.left,
  717. y: touch.pageY - position.top
  718. };
  719. });
  720. },
  721. animate: function(duration, framefn, timefn, callback) {
  722. var startTime = new Date().getTime(),
  723. renderFrame = (function() {
  724. if (!this.inAnimation) {
  725. return;
  726. }
  727. var frameTime = new Date().getTime() - startTime,
  728. progress = frameTime / duration;
  729. if (frameTime >= duration) {
  730. framefn(1);
  731. if (callback) {
  732. callback();
  733. }
  734. this.update();
  735. this.stopAnimation();
  736. } else {
  737. if (timefn) {
  738. progress = timefn(progress);
  739. }
  740. framefn(progress);
  741. this.update();
  742. requestAnimationFrame(renderFrame);
  743. }
  744. }).bind(this);
  745. this.inAnimation = true;
  746. requestAnimationFrame(renderFrame);
  747. },
  748. stopAnimation: function() {
  749. this.inAnimation = false;
  750. },
  751. swing: function(p) {
  752. return -Math.cos(p * Math.PI) / 2 + 0.5;
  753. },
  754. getContainerX: function() {
  755. if (this.el[0]) {
  756. return this.el[0].offsetWidth;
  757. } else {
  758. return 0;
  759. }
  760. },
  761. getContainerY: function() {
  762. return this.el[0].offsetHeight;
  763. },
  764. setContainerY: function(y) {
  765. y = y.toFixed(2);
  766. return this.container.height(y);
  767. },
  768. setupMarkup: function() {
  769. this.container = $('<div class="pinch-zoom-container"></div>');
  770. this.el.before(this.container);
  771. this.container.append(this.el);
  772. this.container.css({
  773. 'position': 'relative',
  774. });
  775. this.el.css({
  776. '-webkit-transform-origin': '0% 0%',
  777. '-moz-transform-origin': '0% 0%',
  778. '-ms-transform-origin': '0% 0%',
  779. '-o-transform-origin': '0% 0%',
  780. 'transform-origin': '0% 0%',
  781. 'position': 'relative'
  782. });
  783. },
  784. end: function() {
  785. this.hasInteraction = false;
  786. this.sanitize();
  787. this.update();
  788. },
  789. bindEvents: function() {
  790. detectGestures(this.container.eq(0), this, this.viewerContainer);
  791. $(g).on('resize', this.update.bind(this));
  792. $(this.el).find('img').on('load', this.update.bind(this));
  793. },
  794. update: function() {
  795. if (this.updatePlaned) {
  796. return;
  797. }
  798. this.updatePlaned = true;
  799. setTimeout((function() {
  800. this.updatePlaned = false;
  801. this.updateAspectRatio();
  802. var zoomFactor = this.getInitialZoomFactor() * this.zoomFactor,
  803. offsetX = (-this.offset.x / zoomFactor).toFixed(3),
  804. offsetY = (-this.offset.y / zoomFactor).toFixed(3);
  805. this.lastclientY = offsetY;
  806. var transform3d = 'scale3d(' + zoomFactor + ', ' + zoomFactor + ',1) ' +
  807. 'translate3d(' + offsetX + 'px,' + offsetY + 'px,0px)',
  808. transform2d = 'scale(' + zoomFactor + ', ' + zoomFactor + ') ' +
  809. 'translate(' + offsetX + 'px,' + offsetY + 'px)',
  810. removeClone = (function() {
  811. if (this.clone) {
  812. this.clone.remove();
  813. delete this.clone;
  814. }
  815. }).bind(this);
  816. if (!this.options.use2d || this.hasInteraction || this.inAnimation) {
  817. this.is3d = true;
  818. this.el.css({
  819. '-webkit-transform': transform3d,
  820. '-o-transform': transform2d,
  821. '-ms-transform': transform2d,
  822. '-moz-transform': transform2d,
  823. 'transform': transform3d
  824. });
  825. } else {
  826. this.el.css({
  827. '-webkit-transform': transform2d,
  828. '-o-transform': transform2d,
  829. '-ms-transform': transform2d,
  830. '-moz-transform': transform2d,
  831. 'transform': transform2d
  832. });
  833. this.is3d = false;
  834. }
  835. }).bind(this), 0);
  836. },
  837. enable: function() {
  838. this.enabled = true;
  839. },
  840. disable: function() {
  841. this.enabled = false;
  842. },
  843. destroy: function() {
  844. var dom = this.el.clone();
  845. var p = this.container.parent();
  846. this.container.remove();
  847. dom.removeAttr('style');
  848. p.append(dom);
  849. }
  850. };
  851. var detectGestures = function(el, target, viewerContainer) {
  852. var interaction = null,
  853. fingers = 0,
  854. lastTouchStart = null,
  855. startTouches = null,
  856. lastTouchY = null,
  857. clientY = null,
  858. lastclientY = 0,
  859. lastTop = 0,
  860. setInteraction = function(newInteraction, event) {
  861. if (interaction !== newInteraction) {
  862. if (interaction && !newInteraction) {
  863. switch (interaction) {
  864. case "zoom":
  865. target.handleZoomEnd(event);
  866. break;
  867. case 'drag':
  868. target.handleDragEnd(event);
  869. break;
  870. }
  871. }
  872. switch (newInteraction) {
  873. case 'zoom':
  874. target.handleZoomStart(event);
  875. break;
  876. case 'drag':
  877. target.handleDragStart(event);
  878. break;
  879. }
  880. }
  881. interaction = newInteraction;
  882. },
  883. updateInteraction = function(event) {
  884. if (fingers === 2) {
  885. setInteraction('zoom');
  886. } else if (fingers === 1 && target.canDrag()) {
  887. setInteraction('drag', event);
  888. } else {
  889. setInteraction(null, event);
  890. }
  891. },
  892. targetTouches = function(touches) {
  893. return Array.prototype.slice.call(touches).map(function(touch) {
  894. return {
  895. x: touch.pageX,
  896. y: touch.pageY
  897. };
  898. });
  899. },
  900. getDistance = function(a, b) {
  901. var x, y;
  902. x = a.x - b.x;
  903. y = a.y - b.y;
  904. return Math.sqrt(x * x + y * y);
  905. },
  906. calculateScale = function(startTouches, endTouches) {
  907. var startDistance = getDistance(startTouches[0], startTouches[1]),
  908. endDistance = getDistance(endTouches[0], endTouches[1]);
  909. return endDistance / startDistance;
  910. },
  911. cancelEvent = function(event) {
  912. event.stopPropagation();
  913. event.preventDefault();
  914. },
  915. detectDoubleTap = function(event) {
  916. var time = (new Date()).getTime();
  917. var pageY = event.changedTouches[0].pageY;
  918. var top = parentNode.scrollTop || 0;
  919. if (fingers > 1) {
  920. lastTouchStart = null;
  921. lastTouchY = null;
  922. cancelEvent(event);
  923. }
  924. if (time - lastTouchStart < 300 && Math.abs(pageY - lastTouchY) < 10 && Math.abs(lastTop - top) < 10) {
  925. cancelEvent(event);
  926. target.handleDoubleTap(event);
  927. switch (interaction) {
  928. case "zoom":
  929. target.handleZoomEnd(event);
  930. break;
  931. case 'drag':
  932. target.handleDragEnd(event);
  933. break;
  934. }
  935. }
  936. if (fingers === 1) {
  937. lastTouchStart = time;
  938. lastTouchY = pageY;
  939. lastTop = top;
  940. }
  941. },
  942. firstMove = true;
  943. if (viewerContainer) {
  944. var parentNode = viewerContainer[0];
  945. }
  946. if (parentNode) {
  947. parentNode.addEventListener('touchstart', function(event) {
  948. if (target.enabled) {
  949. firstMove = true;
  950. fingers = event.touches.length;
  951. detectDoubleTap(event);
  952. clientY = event.changedTouches[0].clientY;
  953. if (fingers > 1) {
  954. cancelEvent(event);
  955. }
  956. }
  957. });
  958. parentNode.addEventListener('touchmove', function(event) {
  959. if (target.enabled) {
  960. lastclientY = event.changedTouches[0].clientY;
  961. if (firstMove) {
  962. updateInteraction(event);
  963. startTouches = targetTouches(event.touches);
  964. } else {
  965. switch (interaction) {
  966. case 'zoom':
  967. target.handleZoom(event, calculateScale(startTouches, targetTouches(event.touches)));
  968. break;
  969. case 'drag':
  970. target.handleDrag(event);
  971. break;
  972. }
  973. if (interaction) {
  974. target.update(lastclientY);
  975. }
  976. }
  977. if (fingers > 1) {
  978. cancelEvent(event);
  979. }
  980. firstMove = false;
  981. }
  982. });
  983. parentNode.addEventListener('touchend', function(event) {
  984. if (target.enabled) {
  985. fingers = event.touches.length;
  986. if (fingers > 1) {
  987. cancelEvent(event);
  988. }
  989. updateInteraction(event);
  990. }
  991. });
  992. }
  993. };
  994. return PinchZoom;
  995. };
  996. var PinchZoom = definePinchZoom($);
  997. var Pdfh5 = function(dom, options) {
  998. this.version = version;
  999. this.container = $(dom);
  1000. this.options = options;
  1001. this.thePDF = null;
  1002. this.totalNum = null;
  1003. this.pages = null;
  1004. this.initTime = 0;
  1005. this.scale = 1.5;
  1006. this.currentNum = 1;
  1007. this.loadedCount = 0;
  1008. this.endTime = 0;
  1009. this.pinchZoom = null;
  1010. this.timer = null;
  1011. this.docWidth = document.documentElement.clientWidth;
  1012. this.winWidth = $(window).width();
  1013. this.cache = {};
  1014. this.eventType = {};
  1015. this.cacheNum = 1;
  1016. this.resizeEvent = false;
  1017. this.cacheData = null;
  1018. this.pdfjsLibPromise = null;
  1019. this.init(options);
  1020. };
  1021. Pdfh5.prototype = {
  1022. init: function(options) {
  1023. var self = this;
  1024. if (this.container[0].pdfLoaded) {
  1025. this.destroy();
  1026. }
  1027. if (options.cMapUrl) {
  1028. pdfjsLib.cMapUrl = options.cMapUrl;
  1029. } else {
  1030. pdfjsLib.cMapUrl = 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/';
  1031. }
  1032. pdfjsLib.cMapPacked = true;
  1033. pdfjsLib.rangeChunkSize = 65536;
  1034. this.container[0].pdfLoaded = false;
  1035. this.container.addClass("pdfjs")
  1036. this.initTime = new Date().getTime();
  1037. setTimeout(function() {
  1038. var arr1 = self.eventType["scroll"];
  1039. if (arr1 && arr1 instanceof Array) {
  1040. for (var i = 0; i < arr1.length; i++) {
  1041. arr1[i] && arr1[i].call(self, self.initTime)
  1042. }
  1043. }
  1044. }, 0)
  1045. this.options = this.options ? this.options : {};
  1046. this.options.pdfurl = this.options.pdfurl ? this.options.pdfurl : null;
  1047. this.options.data = this.options.data ? this.options.data : null;
  1048. this.options.scale = this.options.scale ? this.options.scale : this.scale;
  1049. this.options.zoomEnable = this.options.zoomEnable === false ? false : true;
  1050. this.options.scrollEnable = this.options.scrollEnable === false ? false : true;
  1051. this.options.loadingBar = this.options.loadingBar === false ? false : true;
  1052. this.options.pageNum = this.options.pageNum === false ? false : true;
  1053. this.options.backTop = this.options.backTop === false ? false : true;
  1054. this.options.URIenable = this.options.URIenable === true ? true : false;
  1055. this.options.fullscreen = this.options.fullscreen === false ? false : true;
  1056. this.options.lazy = this.options.lazy === true ? true : false;
  1057. this.options.renderType = this.options.renderType === "svg" ? "svg" : "canvas";
  1058. this.options.resize = this.options.resize === false ? false : true;
  1059. this.options.textLayer = this.options.textLayer === true ? true : false;
  1060. this.options.goto = isNaN(this.options.goto) ? 0 : this.options.goto;
  1061. if (this.options.logo && Object.prototype.toString.call(this.options.logo) === '[object Object]' && this.options.logo
  1062. .src) {
  1063. this.options.logo.img = new Image();
  1064. this.options.logo.img.src = this.options.logo.src;
  1065. this.options.logo.img.style.display = "none";
  1066. document.body.appendChild(this.options.logo.img)
  1067. } else {
  1068. this.options.logo = false;
  1069. }
  1070. if(!(this.options.background && (this.options.background.color || this.options.background.image))){
  1071. this.options.background = false
  1072. }
  1073. if (this.options.limit) {
  1074. var n = parseFloat(this.options.limit)
  1075. this.options.limit = isNaN(n) ? 0 : n < 0 ? 0 : n;
  1076. } else {
  1077. this.options.limit = 0
  1078. }
  1079. this.options.type = this.options.type === "fetch" ? "fetch" : "ajax";
  1080. var html = '<div class="loadingBar">' +
  1081. '<div class="progress">' +
  1082. ' <div class="glimmer">' +
  1083. '</div>' +
  1084. ' </div>' +
  1085. '</div>' +
  1086. '<div class="pageNum">' +
  1087. '<div class="pageNum-bg"></div>' +
  1088. ' <div class="pageNum-num">' +
  1089. ' <span class="pageNow">1</span>/' +
  1090. '<span class="pageTotal">1</span>' +
  1091. '</div>' +
  1092. ' </div>' +
  1093. '<div class="backTop">' +
  1094. '</div>' +
  1095. '<div class="loadEffect loading"></div>';
  1096. if (!this.container.find('.pageNum')[0]) {
  1097. this.container.append(html);
  1098. }
  1099. var viewer = document.createElement("div");
  1100. viewer.className = 'pdfViewer';
  1101. var viewerContainer = document.createElement("div");
  1102. viewerContainer.className = 'viewerContainer';
  1103. viewerContainer.appendChild(viewer);
  1104. this.container.append(viewerContainer);
  1105. this.viewer = $(viewer);
  1106. this.viewerContainer = $(viewerContainer);
  1107. this.pageNum = this.container.find('.pageNum');
  1108. this.pageNow = this.pageNum.find('.pageNow');
  1109. this.pageTotal = this.pageNum.find('.pageTotal');
  1110. this.loadingBar = this.container.find('.loadingBar');
  1111. this.progress = this.loadingBar.find('.progress');
  1112. this.progressDom = this.progress[0];
  1113. this.backTop = this.container.find('.backTop');
  1114. this.loading = this.container.find('.loading');
  1115. if (!this.options.loadingBar) {
  1116. this.loadingBar.hide()
  1117. }
  1118. var containerH = this.container.height(),
  1119. height = containerH * (1 / 3);
  1120. if (!this.options.scrollEnable) {
  1121. this.viewerContainer.css({
  1122. "overflow": "hidden"
  1123. })
  1124. } else {
  1125. this.viewerContainer.css({
  1126. "overflow": "auto"
  1127. })
  1128. }
  1129. viewerContainer.addEventListener('scroll', function() {
  1130. var scrollTop = viewerContainer.scrollTop;
  1131. if (scrollTop >= 150) {
  1132. if (self.options.backTop) {
  1133. self.backTop.show();
  1134. }
  1135. } else {
  1136. if (self.options.backTop) {
  1137. self.backTop.fadeOut(200);
  1138. }
  1139. }
  1140. if (self.viewerContainer) {
  1141. self.pages = self.viewerContainer.find('.pageContainer');
  1142. }
  1143. clearTimeout(self.timer);
  1144. if (self.options.pageNum && self.pageNum) {
  1145. self.pageNum.show();
  1146. }
  1147. var h = containerH;
  1148. if (self.pages) {
  1149. self.pages.each(function(index, obj) {
  1150. var top = obj.getBoundingClientRect().top;
  1151. var bottom = obj.getBoundingClientRect().bottom;
  1152. if (top <= height && bottom > height) {
  1153. if (self.options.pageNum) {
  1154. self.pageNow.text(index + 1)
  1155. }
  1156. self.currentNum = index + 1;
  1157. }
  1158. if (top <= h && bottom > h) {
  1159. self.cacheNum = index + 1;
  1160. }
  1161. })
  1162. }
  1163. if (scrollTop + self.container.height() >= self.viewer[0].offsetHeight) {
  1164. self.pageNow.text(self.totalNum)
  1165. }
  1166. if (scrollTop === 0) {
  1167. self.pageNow.text(1)
  1168. }
  1169. self.timer = setTimeout(function() {
  1170. if (self.options.pageNum && self.pageNum) {
  1171. self.pageNum.fadeOut(200);
  1172. }
  1173. }, 1500)
  1174. if (self.options.lazy) {
  1175. var num = Math.floor(100 / self.totalNum).toFixed(2);
  1176. if (self.cache[self.cacheNum + ""] && !self.cache[self.cacheNum + ""].loaded) {
  1177. var page = self.cache[self.cacheNum + ""].page;
  1178. var container = self.cache[self.cacheNum + ""].container;
  1179. var pageNum = self.cacheNum;
  1180. self.cache[pageNum + ""].loaded = true;
  1181. var scaledViewport = self.cache[pageNum + ""].scaledViewport;
  1182. if (self.options.renderType === "svg") {
  1183. self.renderSvg(page, scaledViewport, pageNum, num, container, self.options)
  1184. } else {
  1185. self.renderCanvas(page, scaledViewport, pageNum, num, container, self.options)
  1186. }
  1187. }
  1188. if (self.cache[(self.totalNum - 1) + ""] && self.cache[(self.totalNum - 1) + ""].loaded && !self.cache[self.totalNum +
  1189. ""].loaded) {
  1190. var page = self.cache[self.totalNum + ""].page;
  1191. var container = self.cache[self.totalNum + ""].container;
  1192. var pageNum = self.totalNum;
  1193. self.cache[pageNum + ""].loaded = true;
  1194. var scaledViewport = self.cache[pageNum + ""].scaledViewport;
  1195. if (self.options.renderType === "svg") {
  1196. self.renderSvg(page, scaledViewport, pageNum, num, container, self.options)
  1197. } else {
  1198. self.renderCanvas(page, scaledViewport, pageNum, num, container, self.options)
  1199. }
  1200. }
  1201. }
  1202. var arr1 = self.eventType["scroll"];
  1203. if (arr1 && arr1 instanceof Array) {
  1204. for (var i = 0; i < arr1.length; i++) {
  1205. arr1[i] && arr1[i].call(self, scrollTop)
  1206. }
  1207. }
  1208. })
  1209. this.backTop.on('click tap', function() {
  1210. var mart = self.viewer.css('transform');
  1211. var arr = mart.replace(/[a-z\(\)\s]/g, '').split(',');
  1212. var s1 = arr[0];
  1213. var s2 = arr[3];
  1214. var x = arr[4] / 2;
  1215. var left = self.viewer[0].getBoundingClientRect().left;
  1216. if (left <= -self.docWidth * 2) {
  1217. x = -self.docWidth / 2
  1218. }
  1219. self.viewer.css({
  1220. transform: 'scale(' + s1 + ', ' + s2 + ') translate(' + x + 'px, 0px)'
  1221. })
  1222. if (self.pinchZoom) {
  1223. self.pinchZoom.offset.y = 0;
  1224. self.pinchZoom.lastclientY = 0;
  1225. }
  1226. self.viewerContainer.animate({
  1227. scrollTop: 0
  1228. }, 300)
  1229. var arr1 = self.eventType["backTop"];
  1230. if (arr1 && arr1 instanceof Array) {
  1231. for (var i = 0; i < arr1.length; i++) {
  1232. arr1[i] && arr1[i].call(self)
  1233. }
  1234. }
  1235. })
  1236. function GetQueryString(name) {
  1237. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  1238. var r = g.location.search.substr(1).match(reg);
  1239. if (r != null) return decodeURIComponent(r[2]);
  1240. return "";
  1241. }
  1242. var pdfurl = GetQueryString("file"),
  1243. url = "";
  1244. if (pdfurl && self.options.URIenable) {
  1245. url = pdfurl
  1246. } else if (self.options.pdfurl) {
  1247. url = self.options.pdfurl
  1248. }
  1249. if (self.options.loadingBar) {
  1250. self.loadingBar.show();
  1251. self.progress.css({
  1252. width: "3%"
  1253. })
  1254. }
  1255. if (url) {
  1256. if (self.options.type === "ajax") {
  1257. $.ajax({
  1258. type: "get",
  1259. mimeType: 'text/plain; charset=x-user-defined',
  1260. url: url,
  1261. success: function(data) {
  1262. var rawLength = data.length;
  1263. var array = [];
  1264. for (i = 0; i < rawLength; i++) {
  1265. array.push(data.charCodeAt(i) & 0xff);
  1266. }
  1267. self.cacheData = array
  1268. self.renderPdf(self.options, {
  1269. data: array
  1270. })
  1271. },
  1272. error: function(err) {
  1273. self.loading.hide()
  1274. var time = new Date().getTime();
  1275. self.endTime = time - self.initTime;
  1276. var arr1 = self.eventType["complete"];
  1277. if (arr1 && arr1 instanceof Array) {
  1278. for (var i = 0; i < arr1.length; i++) {
  1279. arr1[i] && arr1[i].call(self, "error", err.statusText, self.endTime)
  1280. }
  1281. }
  1282. var arr2 = self.eventType["error"];
  1283. if (arr2 && arr2 instanceof Array) {
  1284. for (var i = 0; i < arr2.length; i++) {
  1285. arr2[i] && arr2[i].call(self, err.statusText, self.endTime)
  1286. }
  1287. }
  1288. throw Error(err.statusText)
  1289. }
  1290. });
  1291. } else {
  1292. self.renderPdf(self.options, {
  1293. url: url
  1294. })
  1295. }
  1296. } else if (self.options.data) {
  1297. var data = self.options.data;
  1298. var rawLength = data.length;
  1299. if (typeof data === "string" && data != "") {
  1300. var array = [];
  1301. for (i = 0; i < rawLength; i++) {
  1302. array.push(data.charCodeAt(i) & 0xff);
  1303. }
  1304. self.cacheData = array
  1305. self.renderPdf(self.options, {
  1306. data: array
  1307. })
  1308. } else if (typeof data === "object") {
  1309. if (data.length == 0) {
  1310. var time = new Date().getTime();
  1311. self.endTime = time - self.initTime;
  1312. var arr1 = self.eventType["complete"];
  1313. if (arr1 && arr1 instanceof Array) {
  1314. for (var i = 0; i < arr1.length; i++) {
  1315. arr1[i] && arr1[i].call(self, "error", "options.data is empty Array", self.endTime)
  1316. }
  1317. }
  1318. var arr2 = self.eventType["error"];
  1319. if (arr2 && arr2 instanceof Array) {
  1320. for (var i = 0; i < arr2.length; i++) {
  1321. arr2[i] && arr2[i].call(self, "options.data is empty Array", self.endTime)
  1322. }
  1323. }
  1324. throw Error("options.data is empty Array")
  1325. } else {
  1326. self.cacheData = data
  1327. self.renderPdf(self.options, {
  1328. data: data
  1329. })
  1330. }
  1331. }
  1332. } else {
  1333. var time = new Date().getTime();
  1334. self.endTime = time - self.initTime;
  1335. var arr1 = self.eventType["complete"];
  1336. if (arr1 && arr1 instanceof Array) {
  1337. for (var i = 0; i < arr1.length; i++) {
  1338. arr1[i] && arr1[i].call(self, "error", "Expect options.pdfurl or options.data!", self.endTime)
  1339. }
  1340. }
  1341. var arr2 = self.eventType["error"];
  1342. if (arr2 && arr2 instanceof Array) {
  1343. for (var i = 0; i < arr2.length; i++) {
  1344. arr2[i] && arr2[i].call(self, "Expect options.pdfurl or options.data!", self.endTime)
  1345. }
  1346. }
  1347. throw Error("Expect options.pdfurl or options.data!")
  1348. }
  1349. },
  1350. renderPdf: function(options, obj) {
  1351. this.container[0].pdfLoaded = true;
  1352. var self = this;
  1353. if (options.cMapUrl) {
  1354. obj.cMapUrl = options.cMapUrl;
  1355. } else {
  1356. obj.cMapUrl = 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/';
  1357. }
  1358. if(options.httpHeaders){
  1359. obj.httpHeaders = options.httpHeaders;
  1360. }
  1361. if(options.withCredentials){
  1362. obj.withCredentials = true;
  1363. }
  1364. if(options.password){
  1365. obj.password = options.password;
  1366. console.log(obj.password)
  1367. }
  1368. if(options.stopAtErrors){
  1369. obj.stopAtErrors = true;
  1370. }
  1371. if(options.disableFontFace){
  1372. obj.disableFontFace = true;
  1373. }
  1374. if(options.disableRange){
  1375. obj.disableRange = true;
  1376. }
  1377. if(options.disableStream){
  1378. obj.disableStream = true;
  1379. }
  1380. if(options.disableAutoFetch){
  1381. obj.disableAutoFetch = true;
  1382. }
  1383. obj.cMapPacked = true;
  1384. obj.rangeChunkSize = 65536;
  1385. this.pdfjsLibPromise = pdfjsLib.getDocument(obj).then(function(pdf) {
  1386. self.loading.hide()
  1387. self.thePDF = pdf;
  1388. self.totalNum = pdf.numPages;
  1389. if (options.limit > 0) {
  1390. self.totalNum = options.limit
  1391. }
  1392. self.pageTotal.text(self.totalNum)
  1393. if (!self.pinchZoom) {
  1394. var arr1 = self.eventType["ready"];
  1395. if (arr1 && arr1 instanceof Array) {
  1396. for (var i = 0; i < arr1.length; i++) {
  1397. arr1[i] && arr1[i].call(self)
  1398. }
  1399. }
  1400. self.pinchZoom = new PinchZoom(self.viewer, {
  1401. tapZoomFactor: options.tapZoomFactor,
  1402. zoomOutFactor: options.zoomOutFactor,
  1403. animationDuration: options.animationDuration,
  1404. maxZoom: options.maxZoom,
  1405. minZoom: options.minZoom
  1406. }, self.viewerContainer);
  1407. var timeout, firstZoom = true;
  1408. self.pinchZoom.done = function(scale) {
  1409. clearTimeout(timeout)
  1410. timeout = setTimeout(function() {
  1411. if (self.options.renderType === "svg") {
  1412. return
  1413. }
  1414. if (scale <= 1 || self.options.scale == 5) {
  1415. return
  1416. }
  1417. if (self.thePDF) {
  1418. self.thePDF.destroy();
  1419. self.thePDF = null;
  1420. }
  1421. self.options.scale = scale;
  1422. self.renderPdf(self.options, {
  1423. data: self.cacheData
  1424. })
  1425. }, 310)
  1426. if (scale == 1) {
  1427. if (self.viewerContainer) {
  1428. self.viewerContainer.css({
  1429. '-webkit-overflow-scrolling': 'touch'
  1430. })
  1431. }
  1432. } else {
  1433. if (self.viewerContainer) {
  1434. self.viewerContainer.css({
  1435. '-webkit-overflow-scrolling': 'auto'
  1436. })
  1437. }
  1438. }
  1439. var arr1 = self.eventType["zoom"];
  1440. if (arr1 && arr1 instanceof Array) {
  1441. for (var i = 0; i < arr1.length; i++) {
  1442. arr1[i] && arr1[i].call(self, scale)
  1443. }
  1444. }
  1445. }
  1446. if (options.zoomEnable) {
  1447. self.pinchZoom.enable()
  1448. } else {
  1449. self.pinchZoom.disable()
  1450. }
  1451. }
  1452. var promise = Promise.resolve();
  1453. var num = Math.floor(100 / self.totalNum).toFixed(2);
  1454. var i = 1;
  1455. for (i = 1; i <= self.totalNum; i++) {
  1456. self.cache[i + ""] = {
  1457. page: null,
  1458. loaded: false,
  1459. container: null,
  1460. scaledViewport: null
  1461. };
  1462. promise = promise.then(function(pageNum) {
  1463. return self.thePDF.getPage(pageNum).then(function(page) {
  1464. setTimeout(function() {
  1465. if (self.options.goto) {
  1466. if (pageNum == self.options.goto) {
  1467. self.goto(pageNum)
  1468. }
  1469. }
  1470. }, 0)
  1471. self.cache[pageNum + ""].page = page;
  1472. var viewport = page.getViewport(options.scale);
  1473. var scale = (self.docWidth / viewport.width).toFixed(2)
  1474. var scaledViewport = page.getViewport(parseFloat(scale))
  1475. var div = self.container.find('.pageContainer' + pageNum)[0];
  1476. var container;
  1477. if (!div) {
  1478. container = document.createElement('div');
  1479. container.className = 'pageContainer pageContainer' + pageNum;
  1480. container.setAttribute('name', 'page=' + pageNum);
  1481. container.setAttribute('title', 'Page ' + pageNum);
  1482. var loadEffect = document.createElement('div');
  1483. loadEffect.className = 'loadEffect';
  1484. container.appendChild(loadEffect);
  1485. self.viewer[0].appendChild(container);
  1486. if (window.ActiveXObject || "ActiveXObject" in window) {
  1487. $(container).css({
  1488. 'width': viewport.width + 'px',
  1489. "height": viewport.height + 'px'
  1490. }).attr("data-scale", viewport.width / viewport.height)
  1491. } else {
  1492. var h = $(container).width() / (viewport.viewBox[2] / viewport.viewBox[3]);
  1493. if (h > viewport.height) {
  1494. h = viewport.height
  1495. }
  1496. $(container).css({
  1497. 'max-width': viewport.width,
  1498. "max-height": viewport.height,
  1499. "min-height": h + 'px'
  1500. }).attr("data-scale", viewport.width / viewport.height)
  1501. }
  1502. } else {
  1503. container = div
  1504. }
  1505. if(options.background){
  1506. /*背颜色*/
  1507. if(options.background.color){
  1508. container.style["background-color"] = options.background.color
  1509. }
  1510. /*背景图片*/
  1511. if(options.background.image){
  1512. container.style["background-image"] = options.background.image
  1513. }
  1514. /*平铺与否*/
  1515. if(options.background.repeat){
  1516. container.style["background-repeat"] = options.background.repeat
  1517. }
  1518. /*背景图片位置*/
  1519. if(options.background.position){
  1520. container.style["background-position"] = options.background.position
  1521. }
  1522. /*背景图像的尺寸*/
  1523. if(options.background.size){
  1524. container.style["background-size"] = options.background.size
  1525. }
  1526. }
  1527. self.cache[pageNum + ""].container = container;
  1528. self.cache[pageNum + ""].scaledViewport = scaledViewport;
  1529. var sum = 0,
  1530. containerH = self.container.height();
  1531. self.pages = self.viewerContainer.find('.pageContainer');
  1532. if (options.resize) {
  1533. self.resize()
  1534. }
  1535. if (self.pages && options.lazy) {
  1536. self.pages.each(function(index, obj) {
  1537. var top = obj.offsetTop;
  1538. if (top <= containerH) {
  1539. sum = index + 1;
  1540. self.cache[sum + ""].loaded = true;
  1541. }
  1542. })
  1543. }
  1544. if (pageNum > sum && options.lazy) {
  1545. return
  1546. }
  1547. if (options.renderType === "svg") {
  1548. return self.renderSvg(page, scaledViewport, pageNum, num, container, options, viewport)
  1549. }
  1550. return self.renderCanvas(page, scaledViewport, pageNum, num, container, options)
  1551. });
  1552. }.bind(null, i));
  1553. }
  1554. }).catch(function(err) {
  1555. self.loading.hide();
  1556. var time = new Date().getTime();
  1557. self.endTime = time - self.initTime;
  1558. var arr1 = self.eventType["complete"];
  1559. if (arr1 && arr1 instanceof Array) {
  1560. for (var i = 0; i < arr1.length; i++) {
  1561. arr1[i] && arr1[i].call(self, "error", err.message, self.endTime)
  1562. }
  1563. }
  1564. var arr2 = self.eventType["error"];
  1565. if (arr2 && arr2 instanceof Array) {
  1566. for (var i = 0; i < arr2.length; i++) {
  1567. arr2[i] && arr2[i].call(self, err.message, self.endTime)
  1568. }
  1569. }
  1570. })
  1571. },
  1572. renderSvg: function(page, scaledViewport, pageNum, num, container, options, viewport) {
  1573. var self = this;
  1574. return page.getOperatorList().then(function(opList) {
  1575. var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
  1576. return svgGfx.getSVG(opList, scaledViewport).then(function(svg) {
  1577. self.loadedCount++;
  1578. container.children[0].style.display = "none";
  1579. container.appendChild(svg);
  1580. svg.style.width = "100%";
  1581. svg.style.height = "100%";
  1582. if (self.options.loadingBar) {
  1583. self.progress.css({
  1584. width: num * self.loadedCount + "%"
  1585. })
  1586. }
  1587. var time = new Date().getTime();
  1588. var arr1 = self.eventType["render"];
  1589. if (arr1 && arr1 instanceof Array) {
  1590. for (var i = 0; i < arr1.length; i++) {
  1591. arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container)
  1592. }
  1593. }
  1594. if (self.loadedCount === self.totalNum) {
  1595. self.finalRender(options)
  1596. }
  1597. });
  1598. });
  1599. },
  1600. renderCanvas: function(page, viewport, pageNum, num, container, options) {
  1601. var self = this;
  1602. var viewport = page.getViewport(options.scale);
  1603. var scale = (self.docWidth / viewport.width).toFixed(2)
  1604. var canvas = document.createElement("canvas");
  1605. var obj2 = {
  1606. 'Cheight': viewport.height * scale,
  1607. 'width': viewport.width,
  1608. 'height': viewport.height,
  1609. 'canvas': canvas,
  1610. 'index': self.loadedCount
  1611. }
  1612. var context = canvas.getContext('2d');
  1613. if (options.logo) {
  1614. context.drawImage(self.options.logo.img, self.options.logo.x,
  1615. self.options.logo.y, self.options.logo.width * self.options.scale, self.options.logo.height * self.options.scale
  1616. );
  1617. }
  1618. canvas.height = viewport.height;
  1619. canvas.width = viewport.width;
  1620. if (self.options.loadingBar) {
  1621. self.progress.css({
  1622. width: num * self.loadedCount + "%"
  1623. })
  1624. }
  1625. obj2.src = obj2.canvas.toDataURL("image/png");
  1626. var renderObj = {
  1627. canvasContext: context,
  1628. viewport: viewport
  1629. }
  1630. if(options.background){
  1631. renderObj.background = "rgba(255, 255, 255, 0)"
  1632. }
  1633. return page.render(renderObj).then(function() {
  1634. if (options.logo) {
  1635. context.drawImage(self.options.logo.img, self.options.logo.x,
  1636. self.options.logo.y, self.options.logo.width * self.options.scale, self.options.logo.height * self.options.scale
  1637. );
  1638. }
  1639. self.loadedCount++;
  1640. var img = new Image();
  1641. var time = new Date().getTime();
  1642. var time2 = 0;
  1643. if (self.renderTime == 0) {
  1644. time2 = time - self.startTime
  1645. } else {
  1646. time2 = time - self.renderTime
  1647. }
  1648. obj2.src = obj2.canvas.toDataURL("image/png");
  1649. img.src = obj2.src;
  1650. img.className = "canvasImg" + pageNum;
  1651. var img0 = self.container.find(".pageContainer" + pageNum).find(".canvasImg" + pageNum)[0];
  1652. if (container && !img0) {
  1653. container.appendChild(img);
  1654. } else if (img0) {
  1655. img0.src = obj2.src
  1656. }
  1657. container.children[0].style.display = "none";
  1658. var time = new Date().getTime();
  1659. var arr1 = self.eventType["render"];
  1660. if (arr1 && arr1 instanceof Array) {
  1661. for (var i = 0; i < arr1.length; i++) {
  1662. arr1[i] && arr1[i].call(self, pageNum, time - self.initTime, container)
  1663. }
  1664. }
  1665. if (self.loadedCount === self.totalNum) {
  1666. self.finalRender(options)
  1667. }
  1668. }).then(function() {
  1669. return page.getTextContent();
  1670. }).then(function(textContent) {
  1671. if (!self.options.textLayer) {
  1672. return
  1673. }
  1674. if ($(container).find(".textLayer")[0]) {
  1675. return
  1676. }
  1677. var textLayerDiv = document.createElement('div');
  1678. textLayerDiv.setAttribute('class', 'textLayer');
  1679. container.appendChild(textLayerDiv);
  1680. viewport.width = viewport.width * scale;
  1681. viewport.height = viewport.height * scale;
  1682. var textLayer = new TextLayerBuilder({
  1683. textLayerDiv: textLayerDiv,
  1684. pageIndex: page.pageIndex,
  1685. viewport: viewport
  1686. });
  1687. textLayer.setTextContent(textContent);
  1688. textLayer.render();
  1689. });
  1690. },
  1691. finalRender: function(options) {
  1692. var time = new Date().getTime();
  1693. var self = this;
  1694. if (self.options.loadingBar) {
  1695. self.progress.css({
  1696. width: "100%"
  1697. });
  1698. }
  1699. setTimeout(function() {
  1700. self.loadingBar.hide();
  1701. }, 300)
  1702. self.endTime = time - self.initTime;
  1703. if (options.renderType === "svg") {
  1704. if (self.totalNum !== 1) {
  1705. self.cache[(self.totalNum - 1) + ""].loaded = true;
  1706. } else {
  1707. self.cache["1"].loaded = true;
  1708. }
  1709. }
  1710. if (options.zoomEnable) {
  1711. if (self.pinchZoom) {
  1712. self.pinchZoom.enable()
  1713. }
  1714. } else {
  1715. if (self.pinchZoom) {
  1716. self.pinchZoom.disable()
  1717. }
  1718. }
  1719. var arr1 = self.eventType["complete"];
  1720. if (arr1 && arr1 instanceof Array) {
  1721. for (var i = 0; i < arr1.length; i++) {
  1722. arr1[i] && arr1[i].call(self, "success", "pdf加载完成", self.endTime)
  1723. }
  1724. }
  1725. var arr2 = self.eventType["success"];
  1726. if (arr2 && arr2 instanceof Array) {
  1727. for (var i = 0; i < arr2.length; i++) {
  1728. arr2[i] && arr2[i].call(self, self.endTime)
  1729. }
  1730. }
  1731. },
  1732. resize: function() {
  1733. var self = this;
  1734. if (self.resizeEvent) {
  1735. return
  1736. }
  1737. self.resizeEvent = true;
  1738. var timer;
  1739. if (self.pages) {
  1740. $(window).on("resize", function() {
  1741. self.pages.each(function(i, item) {
  1742. $(item).css("min-height", "auto")
  1743. })
  1744. })
  1745. }
  1746. },
  1747. show: function(callback) {
  1748. this.container.show();
  1749. callback && callback.call(this)
  1750. var arr = this.eventType["show"];
  1751. if (arr && arr instanceof Array) {
  1752. for (var i = 0; i < arr.length; i++) {
  1753. arr[i] && arr[i].call(this)
  1754. }
  1755. }
  1756. },
  1757. hide: function(callback) {
  1758. this.container.hide()
  1759. callback && callback.call(this)
  1760. var arr = this.eventType["hide"];
  1761. if (arr && arr instanceof Array) {
  1762. for (var i = 0; i < arr.length; i++) {
  1763. arr[i] && arr[i].call(this)
  1764. }
  1765. }
  1766. },
  1767. on: function(type, callback) {
  1768. if (this.eventType[type] && this.eventType[type] instanceof Array) {
  1769. this.eventType[type].push(callback)
  1770. }
  1771. this.eventType[type] = [callback]
  1772. },
  1773. off: function(type) {
  1774. if (type !== undefined) {
  1775. this.eventType[type] = [null]
  1776. } else {
  1777. for (var i in this.eventType) {
  1778. this.eventType[i] = [null]
  1779. }
  1780. }
  1781. },
  1782. goto: function(num) {
  1783. var self = this;
  1784. if (!isNaN(num)) {
  1785. if (self.viewerContainer) {
  1786. self.pages = self.viewerContainer.find('.pageContainer');
  1787. if (self.pages) {
  1788. var h = 0;
  1789. if (num - 1 > 0) {
  1790. self.pages.each(function(index, obj) {
  1791. var top = obj.getBoundingClientRect().top;
  1792. if (index === num - 1) {
  1793. h = top;
  1794. }
  1795. })
  1796. }
  1797. self.viewerContainer.animate({
  1798. scrollTop: h
  1799. }, 300)
  1800. }
  1801. }
  1802. }
  1803. },
  1804. scrollEnable: function(flag) {
  1805. if (flag === false) {
  1806. this.viewerContainer.css({
  1807. "overflow": "hidden"
  1808. })
  1809. } else {
  1810. this.viewerContainer.css({
  1811. "overflow": "auto"
  1812. })
  1813. }
  1814. var arr = this.eventType["scrollEnable"];
  1815. if (arr && arr instanceof Array) {
  1816. for (var i = 0; i < arr.length; i++) {
  1817. arr[i] && arr[i].call(this, flag)
  1818. }
  1819. }
  1820. },
  1821. zoomEnable: function(flag) {
  1822. if (!this.pinchZoom) {
  1823. return
  1824. }
  1825. if (flag === false) {
  1826. this.pinchZoom.disable()
  1827. } else {
  1828. this.pinchZoom.enable()
  1829. }
  1830. var arr = this.eventType["zoomEnable"];
  1831. if (arr && arr instanceof Array) {
  1832. for (var i = 0; i < arr.length; i++) {
  1833. arr[i] && arr[i].call(this, flag)
  1834. }
  1835. }
  1836. },
  1837. reset: function(callback) {
  1838. if (this.pinchZoom) {
  1839. this.pinchZoom.offset.y = 0;
  1840. this.pinchZoom.offset.x = 0;
  1841. this.pinchZoom.lastclientY = 0;
  1842. this.pinchZoom.zoomFactor = 1;
  1843. this.pinchZoom.update();
  1844. }
  1845. if (this.viewerContainer) {
  1846. this.viewerContainer.scrollTop(0);
  1847. }
  1848. callback && callback.call(this)
  1849. var arr = this.eventType["reset"];
  1850. if (arr && arr instanceof Array) {
  1851. for (var i = 0; i < arr.length; i++) {
  1852. arr[i] && arr[i].call(this)
  1853. }
  1854. }
  1855. },
  1856. destroy: function(callback) {
  1857. this.reset();
  1858. this.off();
  1859. if (this.thePDF) {
  1860. this.thePDF.destroy();
  1861. this.thePDF = null;
  1862. }
  1863. if (this.viewerContainer) {
  1864. this.viewerContainer.remove();
  1865. this.viewerContainer = null;
  1866. }
  1867. if (this.container) {
  1868. this.container.html('');
  1869. }
  1870. this.totalNum = null;
  1871. this.pages = null;
  1872. this.initTime = 0;
  1873. this.endTime = 0;
  1874. this.viewer = null;
  1875. this.pageNum = null;
  1876. this.pageNow = null;
  1877. this.pageTotal = null;
  1878. this.loadingBar = null;
  1879. this.progress = null;
  1880. this.loadedCount = 0;
  1881. this.timer = null;
  1882. callback && callback.call(this)
  1883. var arr = this.eventType["destroy"];
  1884. if (arr && arr instanceof Array) {
  1885. for (var i = 0; i < arr.length; i++) {
  1886. arr[i] && arr[i].call(this)
  1887. }
  1888. }
  1889. }
  1890. }
  1891. return Pdfh5;
  1892. });