cnofd-view.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
  2. $("#openFile").click(function() {
  3. openFile();
  4. });
  5. $("#file").change(function() {
  6. fileChanged();
  7. });
  8. $("#zoomIn").click(function() {
  9. zoomIn();
  10. });
  11. $("#zoomOut").click(function() {
  12. zoomOut();
  13. });
  14. $("#zoomValue").change(function() {
  15. zoomChange();
  16. });
  17. $("#firstPage").click(function() {
  18. firstPage();
  19. });
  20. $("#prePage").click(function() {
  21. prePage();
  22. });
  23. $("#nextPage").click(function() {
  24. nextPage();
  25. });
  26. $("#lastPage").click(function() {
  27. lastPage();
  28. });
  29. $("#print").click(function() {
  30. print();
  31. });
  32. $("#main").scroll(function() {
  33. scrool();
  34. });
  35. window.onresize = function() {
  36. return function() {
  37. var contentDiv = document.getElementById("content");
  38. var nowleft = 0;
  39. if (contentDiv.childNodes[0]) nowleft = contentDiv.childNodes[0].offsetLeft;
  40. } ();
  41. };
  42. // 手机端,隐藏缩放比例选择框,打开文件和打印按钮
  43. if (this.isMobile()) {
  44. if (document.getElementById("zoomSelect")) document.getElementById("zoomSelect").style.display = "none";
  45. if (document.getElementById("openFile")) document.getElementById("openFile").style.display = "none";
  46. if (document.getElementById("print")) document.getElementById("print").style.display = "none";
  47. if (document.getElementById("separator1")) document.getElementById("separator1").style.display = "none";
  48. if (document.getElementById("separator2")) document.getElementById("separator2").style.display = "none";
  49. }
  50. // 判断手机端还是PC端
  51. function isMobile() {
  52. var flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
  53. return flag;
  54. }
  55. // 判断是否IE浏览器
  56. function isIE() {
  57. var navigator = window.navigator.userAgent;
  58. if (navigator.indexOf("MSIE") > 0 || navigator.indexOf("Trident") > 0) {
  59. return true;
  60. }
  61. return false;
  62. }
  63. this.screenWidth = document.body.clientWidth;
  64. // cnofd提供浏览器端和服务端两种OFD版式文件的解析渲染模式。
  65. // 服务器渲染模式;浏览器端渲染时,请注释以下两行代码
  66. //Object(cnofd["setRenderMode"])("Server");
  67. //Object(cnofd["setServerUrl"])("http://localhost:8080/api/ofdrender/");
  68. data: function data() {
  69. return {
  70. leftMenuWidth: 0,
  71. ofdBase64: null,
  72. pageIndex: 1,
  73. pageCount: 0,
  74. title: null,
  75. value: null,
  76. ofdDoc: null,
  77. screenWidth: document.body.clientWidth,
  78. ofdDiv: null,
  79. isFont: false
  80. };
  81. }
  82. this.pageZoomScale = "1.0";
  83. cnofd["setScaleValue"](this.pageZoomScale);
  84. var scale = this.getQueryVariable("scale");
  85. if (scale && (scale == "width" || Number(scale))) {
  86. this.pageZoomScale = scale;
  87. cnofd["setScaleValue"](scale);
  88. }
  89. var file = this.getQueryVariable("file");
  90. file = decodeURIComponent(file);
  91. if (file) this.loadOfdFile(file);
  92. if (scale && (scale == "width" || Number(scale))) {
  93. var selectZoom = document.getElementById("zoomValue");
  94. selectZoom.selectedIndex = -1;
  95. if (this.pageZoomScale == "width") {
  96. selectZoom.selectedIndex = 0;
  97. } else {
  98. for (var i = 1; i < selectZoom.length; i++) {
  99. if (Math.abs(this.pageZoomScale - selectZoom.options[i].value) < 0.01) {
  100. selectZoom.selectedIndex = i;
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. function getQueryVariable(variable) {
  107. var query = window.location.search.substring(1);
  108. var vars = query.split("&");
  109. for (var i = 0; i < vars.length; i++) {
  110. var pair = vars[i].split("=");
  111. if (pair[0] == variable) {
  112. return pair[1];
  113. }
  114. }
  115. return false;
  116. }
  117. function loadOfdFile(ofdFile) {
  118. var that = this;
  119. JSZipUtils.getBinaryContent(ofdFile,
  120. function(err, data) {
  121. if (err) {
  122. console.log(err);
  123. } else {
  124. var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(data)));
  125. that.ofdBase64 = base64String;
  126. }
  127. });
  128. this.getOfdDocument(ofdFile, this.screenWidth, this.pageZoomScale);
  129. setPageInfo();
  130. }
  131. function openFile() {
  132. this.isFont = false;
  133. this.file = null;
  134. $("#file")[0].click();
  135. }
  136. function fileChanged() {
  137. this.file = $("#file")[0].files[0];
  138. if (this.file == null)
  139. return;
  140. var ext = this.file.name.replace(/.+\./, "");
  141. if (["ofd"].indexOf(ext) === -1) {
  142. window.alert("error,仅支持ofd文件类型");
  143. return;
  144. }
  145. if (this.file.size > 20 * 1024 * 1024) {
  146. window.alert("error,文件大小超过20MB");
  147. return;
  148. }
  149. Object(cnofd["setScaleValue"])(this.pageZoomScale);
  150. var selectZoom = document.getElementById("zoomValue");
  151. if (selectZoom)
  152. selectZoom.value = this.pageZoomScale;
  153. var that = this;
  154. var reader = new FileReader();
  155. reader.readAsDataURL(this.file);
  156. reader.onload = function(e) {
  157. that.ofdBase64 = e.target.result.split(",")[1];
  158. };
  159. this.getOfdDocument(this.file, this.screenWidth, this.pageZoomScale);
  160. }
  161. function getOfdDocument(file, screenWidth, pageZoomScale) {
  162. var that = this;
  163. $("#loading").show();
  164. var contentDiv = document.getElementById("content");
  165. contentDiv.innerHTML = "";
  166. Object(cnofd["ofdParse"])({
  167. ofd: file,
  168. success: function success(res) {
  169. that.ofdDoc = res;
  170. that.pageIndex = 1;
  171. that.pageCount = res.pageCount;
  172. if (pageZoomScale == "width") {
  173. var divs = Object(cnofd["ofdRender"])(res, screenWidth);
  174. that.displayOfdDiv(divs);
  175. } else {
  176. var divs = Object(cnofd["ofdRenderByScale"])(res, screenWidth, pageZoomScale);
  177. that.displayOfdDiv(divs);
  178. }
  179. $("#loading").hide();
  180. },
  181. fail: function fail(error) {
  182. $("#loading").hide();
  183. console.log('OFD打开失败');
  184. }
  185. });
  186. }
  187. function displayOfdDiv(divs) {
  188. var contentDiv = document.getElementById("content");
  189. contentDiv.innerHTML = "";
  190. var _iterator3 = _createForOfIteratorHelper(divs),
  191. _step3;
  192. try {
  193. for (_iterator3.s(); ! (_step3 = _iterator3.n()).done;) {
  194. var div = _step3.value;
  195. contentDiv.appendChild(div);
  196. }
  197. } catch(err) {
  198. _iterator3.e(err);
  199. } finally {
  200. _iterator3.f();
  201. }
  202. setPageInfo();
  203. }
  204. function zoomIn() {
  205. var selectZoom = document.getElementById("zoomValue");
  206. if (selectZoom.selectedIndex > 1) {
  207. selectZoom.selectedIndex = selectZoom.selectedIndex - 1;
  208. Object(cnofd["setScaleValue"])(selectZoom.options[selectZoom.selectedIndex].value);
  209. var divs = Object(cnofd["ofdRenderByScale"])(this.ofdDoc);
  210. if (divs) {
  211. this.displayOfdDiv(divs);
  212. } else {
  213. this.getOfdDocument(this.file, this.screenWidth);
  214. }
  215. }
  216. }
  217. function zoomOut() {
  218. var selectZoom = document.getElementById("zoomValue");
  219. if (selectZoom.selectedIndex < selectZoom.length-1) {
  220. selectZoom.selectedIndex = selectZoom.selectedIndex + 1;
  221. if (selectZoom.selectedIndex == 0) selectZoom.selectedIndex = 1;
  222. Object(cnofd["setScaleValue"])(selectZoom.options[selectZoom.selectedIndex].value);
  223. var divs = Object(cnofd["ofdRenderByScale"])(this.ofdDoc);
  224. if (divs) {
  225. this.displayOfdDiv(divs);
  226. } else {
  227. this.getOfdDocument(this.file, this.screenWidth);
  228. }
  229. }
  230. }
  231. function zoomChange() {
  232. var selectZoom = document.getElementById("zoomValue");
  233. if (selectZoom.options[selectZoom.selectedIndex].value == "width") {
  234. Object(cnofd["setScaleValue"])(selectZoom.options[selectZoom.selectedIndex].value);
  235. var divs = Object(cnofd["ofdRender"])(this.ofdDoc, this.screenWidth);
  236. if (divs) {
  237. this.displayOfdDiv(divs);
  238. } else {
  239. this.getOfdDocument(this.file, this.screenWidth);
  240. }
  241. } else {
  242. Object(cnofd["setScaleValue"])(selectZoom.options[selectZoom.selectedIndex].value);
  243. var divs = Object(cnofd["ofdRenderByScale"])(this.ofdDoc);
  244. if (divs) {
  245. this.displayOfdDiv(divs);
  246. } else {
  247. this.getOfdDocument(this.file, this.screenWidth);
  248. }
  249. }
  250. }
  251. function scrool() {
  252. var contentDiv = document.getElementById("content");
  253. var contentDiv1 = contentDiv.firstElementChild;
  254. var scrolled = (contentDiv1 === null || contentDiv1 === void 0 ? void 0 : contentDiv1.getBoundingClientRect() === null || contentDiv1.getBoundingClientRect() === void 0 ? void 0 : contentDiv1.getBoundingClientRect().top) - 60;
  255. var top = 0;
  256. var index = 0;
  257. for (var i = 0; i < contentDiv.childElementCount; i++) {
  258. var contentDiv2 = contentDiv.children.item(i);
  259. top += Math.abs(contentDiv2 === null || contentDiv2 === void 0 ? void 0 : contentDiv2.style.height.replace("px", "")) + Math.abs(contentDiv2 === null || contentDiv2 === void 0 ? void 0 : contentDiv2.style.marginBottom.replace("px", ""));
  260. if (Math.abs(scrolled) < top) {
  261. index = i;
  262. break;
  263. }
  264. }
  265. this.pageIndex = index + 1;
  266. setPageInfo();
  267. }
  268. function setPageInfo() {
  269. if (! (this.pageCount == null) && this.pageCount > 0) {
  270. $("#pageInfo")[0].innerText = this.pageIndex + "/" + this.pageCount;
  271. }
  272. }
  273. function prePage() {
  274. var contentDiv = document.getElementById("content");
  275. var ele = contentDiv.children.item(this.pageIndex - 2);
  276. ele === null || ele === void 0 ? void 0 : ele.scrollIntoView(true);
  277. ele ? this.pageIndex = this.pageIndex - 1 : "";
  278. setPageInfo();
  279. }
  280. function firstPage() {
  281. var contentDiv = document.getElementById("content");
  282. var ele = contentDiv.firstElementChild;
  283. ele === null || ele === void 0 ? void 0 : ele.scrollIntoView(true);
  284. ele ? this.pageIndex = 1 : "";
  285. setPageInfo();
  286. }
  287. function nextPage() {
  288. var contentDiv = document.getElementById("content");
  289. var ele = contentDiv.children.item(this.pageIndex);
  290. ele === null || ele === void 0 ? void 0 : ele.scrollIntoView(true);
  291. ele ? ++this.pageIndex: "";
  292. setPageInfo();
  293. }
  294. function lastPage() {
  295. var contentDiv = document.getElementById("content");
  296. var ele = contentDiv.children.item(contentDiv.childElementCount - 1);
  297. ele === null || ele === void 0 ? void 0 : ele.scrollIntoView(true);
  298. ele ? this.pageIndex = contentDiv.childElementCount : "";
  299. setPageInfo();
  300. }
  301. function print() {
  302. var contentDiv = document.getElementById("content");
  303. var contentDivChilds = contentDiv.children;
  304. var list = [];
  305. for (var i = 0; i < contentDivChilds.length; i++) {
  306. var ele = contentDivChilds.item(i);
  307. list.push(ele.cloneNode(true));
  308. this.percentage = i / contentDivChilds.length;
  309. }
  310. if (list.length > 0) {
  311. if (!isIE()) {
  312. var mywindow = window.open("打印窗口", "_blank");
  313. mywindow.document.write('<!DOCTYPE html><html><head>'
  314. +'<style media="print">.page-break { page-break-inside: avoid; page-break-after: always; }</style>'
  315. +'</head><body></body</html>');
  316. var documentBody = mywindow.document.body;
  317. var _iterator2 = _createForOfIteratorHelper(list),
  318. _step2;
  319. try {
  320. for (_iterator2.s(); ! (_step2 = _iterator2.n()).done;) {
  321. var canvas = _step2.value;
  322. canvas.style.margin = "";
  323. canvas.style.boxShadow = "";
  324. documentBody.appendChild(canvas);
  325. }
  326. } catch(err) {
  327. _iterator2.e(err);
  328. } finally {
  329. _iterator2.f();
  330. }
  331. mywindow.focus();
  332. mywindow.print();
  333. mywindow.close();
  334. } else {
  335. var printhtml = "";
  336. for (var i=0; i < list.length; i++) {
  337. var canvas = list[i];
  338. canvas.style.margin = "";
  339. canvas.style.boxShadow = "";
  340. printhtml = printhtml + canvas.outerHTML;
  341. }
  342. printIE(printhtml);
  343. }
  344. }
  345. }
  346. function printIE(printhtml) {
  347. var iframe = document.createElement("iframe");
  348. iframe.id = "printf";
  349. iframe.style.width = "0";
  350. iframe.style.display = "none"
  351. iframe.style.height = "0";
  352. iframe.style.border = "none";
  353. document.body.appendChild(iframe);
  354. setTimeout(function () {
  355. iframe.contentDocument.write(" <script type='text/javascript'> window.onload = function() { document.execCommand('print'); } </script> " + printhtml);
  356. iframe.contentDocument.close();
  357. },
  358. 100)
  359. }