rpt_main.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /**
  2. * Created by Tony on 2017/6/26.
  3. */
  4. 'use strict'
  5. const PRE_PAGE_OFFSET = 150;
  6. const NEXT_PAGE_OFFSET = 160;
  7. const FIRST_PAGE_OFFSET = 50;
  8. const LAST_PAGE_OFFSET = 60;
  9. let rptTplObj = {
  10. hasInitialized: false,
  11. iniPage: function() {
  12. let me = this;
  13. if (!me.hasInitialized) {
  14. zTreeOprObj.getCustomerCfg();
  15. zTreeOprObj.getReportTemplateTree();
  16. me.hasInitialized = true;
  17. let canvas = document.getElementById("rptCanvas");
  18. canvas.onclick = canvasOprObj.canvasOnClick;
  19. canvas.onmousemove = canvasOprObj.canvasOnMouseMove;
  20. }
  21. }
  22. }
  23. let zTreeOprObj = {
  24. treeObj: null,
  25. currentNode: null,
  26. checkedRptTplNodes: null,
  27. currentRptPageRst: null,
  28. reportPageCfg: null,
  29. currentPage: 1,
  30. maxPages: 0,
  31. countChkedRptTpl: function () {
  32. let me = zTreeOprObj;
  33. if (me.treeObj) {
  34. me.checkedRptTplNodes = [];
  35. let chkNodes = me.treeObj.getCheckedNodes(true), cnt = 0, hasCurrentNode = false;
  36. for (let node of chkNodes) {
  37. if (node.nodeType === TPL_TYPE_TEMPLATE) {
  38. cnt++;
  39. me.checkedRptTplNodes.push(node);
  40. if (me.currentNode === node) hasCurrentNode = true;
  41. }
  42. }
  43. if (!hasCurrentNode && cnt === 0 && me.currentNode !== null) {
  44. //这里根据实际需求再做处理
  45. cnt++;
  46. me.checkedRptTplNodes.push(me.currentNode);
  47. }
  48. $("#export_div").find("span").each(function(cIdx,elementSpan){
  49. elementSpan.innerText = cnt;
  50. });
  51. $("#print_div").find("span").each(function(cIdx,elementSpan){
  52. elementSpan.innerText = cnt;
  53. });
  54. }
  55. },
  56. getReportTemplateTree: function() {
  57. let me = zTreeOprObj, params = {};
  58. params.engineerId = projectInfoObj.projectInfo.property.engineering;
  59. CommonAjax.postEx("report_tpl_api/getRptTplTree", params, 20000, true, function(result){
  60. let private_remove_hide_item = function (items) {
  61. if (items && items.length > 0) {
  62. for (let i = items.length - 1; i >= 0; i--) {
  63. if (!(items[i].released) && items[i].nodeType === 2) {
  64. items.splice(i, 1);
  65. } else {
  66. if (items[i].items && items[i].items.length > 0) {
  67. private_remove_hide_item(items[i].items);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. private_remove_hide_item(result);
  74. for (let topNode of result) {
  75. if (topNode.userId === "-100") {
  76. topNode.name = topNode.name + " - 标准报表";
  77. } else {
  78. topNode.name = topNode.name + " - 定制报表";
  79. }
  80. }
  81. zTreeHelper.createTreeDirectly(result, rpt_tpl_setting, "rptTplTree", me);
  82. me.refreshNodes();
  83. }, null, null);
  84. },
  85. getCustomerCfg: function() {
  86. let me = zTreeOprObj, params = {};
  87. params.engineerId = projectInfoObj.projectInfo.property.engineering;
  88. CommonAjax.postEx("report_tpl_api/getCustomizeCfg", params, 20000, true, function(result){
  89. if (result) {
  90. me.reportPageCfg = result;
  91. $("#elementMargin_Left").get(0).value = result.margins.Left;
  92. $("#elementMargin_Right").get(0).value = result.margins.Right;
  93. $("#elementMargin_Top").get(0).value = result.margins.Top;
  94. $("#elementMargin_Bottom").get(0).value = result.margins.Bottom;
  95. for (let font of result.fonts) {
  96. let fontPropSuffix = "title";
  97. if (font.CfgDispName === "表标题") {
  98. fontPropSuffix = "title";
  99. } else if (font.CfgDispName === "列标题") {
  100. fontPropSuffix = "column";
  101. } else if (font.CfgDispName === "正文内容") {
  102. fontPropSuffix = "content";
  103. } else if (font.CfgDispName === "合计") {
  104. fontPropSuffix = "summary";
  105. } else if (font.CfgDispName === "表眉/表脚") {
  106. fontPropSuffix = "header_footer";
  107. }
  108. document.getElementById("fontName_" + fontPropSuffix).value = font.Name;
  109. document.getElementById("fontHeight_" + fontPropSuffix).value = font.FontHeight;
  110. document.getElementById("font_bold_" + fontPropSuffix).className = (font.FontBold === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
  111. document.getElementById("font_italic_" + fontPropSuffix).className = (font.FontItalic === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
  112. document.getElementById("font_underline_" + fontPropSuffix).className = (font.FontUnderline === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
  113. }
  114. document.getElementById("rpt_vertical_line").checked = result.showVerticalLine;
  115. document.getElementById("rpt_narrow").checked = result.isNarrow;
  116. document.getElementById("rpt_fill_zero").checked = result.fillZero;
  117. } else {
  118. me.reportPageCfg = null;
  119. }
  120. }, null, null
  121. );
  122. },
  123. refreshNodes: function() {
  124. let me = this;
  125. let private_setupIsParent = function(node){
  126. node.isParent = (node.nodeType === RT.NodeType.NODE || node.level === 0);
  127. if (node.items && node.items.length) {
  128. for (let i = 0; i < node.items.length; i++) {
  129. private_setupIsParent(node.items[i]);
  130. }
  131. }
  132. };
  133. let topNodes = me.treeObj.getNodes();
  134. for (let i = 0; i < topNodes.length; i++) {
  135. private_setupIsParent(topNodes[i]);
  136. }
  137. me.treeObj.refresh();
  138. },
  139. onCheck: function(event, treeId, treeNode) {
  140. zTreeOprObj.countChkedRptTpl();
  141. },
  142. onClick: function(event,treeId,treeNode) {
  143. let me = zTreeOprObj;
  144. if (treeNode.nodeType === TPL_TYPE_TEMPLATE && treeNode.refId > 0) {
  145. let params = {};
  146. let pageSize = rptControlObj.getCurrentPageSize();
  147. params.pageSize = pageSize;
  148. params.rpt_tpl_id = treeNode.refId;
  149. params.prj_id = projectInfoObj.projectInfo.ID;
  150. params.custCfg = me.reportPageCfg;
  151. me.currentNode = treeNode;
  152. me.requestReport(params);
  153. me.countChkedRptTpl();
  154. }
  155. },
  156. changePageSize: function(dom) {
  157. let me = zTreeOprObj,
  158. targetDom = document.getElementById("btnRptPageSize");
  159. let tmpStr = targetDom.innerHTML.trim();
  160. targetDom.innerHTML = dom.innerHTML.trim();
  161. dom.innerHTML = tmpStr;
  162. me.changeCfg();
  163. },
  164. changeOrientation: function(dom) {
  165. let me = zTreeOprObj,
  166. targetDom = document.getElementById("btnRptOrientation");
  167. let tmpStr = targetDom.innerHTML.trim();
  168. targetDom.innerHTML = dom.innerHTML.trim();
  169. dom.innerHTML = tmpStr;
  170. me.changeCfg();
  171. },
  172. changeCfg: function() {
  173. let me = zTreeOprObj;
  174. let params = {};
  175. params.pageSize = rptControlObj.getCurrentPageSize();
  176. params.orientation = rptControlObj.getCurrentOrientation();
  177. params.rpt_tpl_id = me.currentNode.refId;
  178. params.prj_id = projectInfoObj.projectInfo.ID;
  179. params.custCfg = me.reportPageCfg;
  180. me.requestReport(params);
  181. },
  182. resetAfter: function (pageRst) {
  183. let size = pageRst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE].slice(0);
  184. if (size[0] > size[1]) {
  185. document.getElementById("btnRptOrientation").innerHTML = "横向";
  186. document.getElementById("hrefRptOrientation").innerHTML = "纵向";
  187. } else {
  188. document.getElementById("btnRptOrientation").innerHTML = "纵向";
  189. document.getElementById("hrefRptOrientation").innerHTML = "横向";
  190. }
  191. },
  192. requestReport: function (params) {
  193. let me = zTreeOprObj;
  194. CommonAjax.postEx("report_api/getReport", params, 5000, true,
  195. function(result){
  196. let pageRst = result;
  197. if (pageRst) {
  198. me.resetAfter(pageRst);
  199. let canvas = document.getElementById("rptCanvas");
  200. me.currentRptPageRst = pageRst;
  201. me.maxPages = pageRst.items.length;
  202. me.currentPage = 1;
  203. me.displayPageValue();
  204. let size = JpcCanvasOutput.getReportSizeInPixel(me.currentRptPageRst, getScreenDPI());
  205. canvas.width = size[0] + 20;
  206. if (size[1] > size[0]) {
  207. canvas.height = size[1] + 100;
  208. } else {
  209. canvas.height = size[1] + 50;
  210. }
  211. me.showPage(1, canvas);
  212. }
  213. }, null, null
  214. );
  215. },
  216. showPage: function (pageNum, canvas) {
  217. let me = zTreeOprObj;
  218. if (pageNum >= 1 && pageNum <= me.maxPages) {
  219. me.currentPage = pageNum;
  220. JpcCanvasOutput.cleanCanvas(canvas);
  221. JpcCanvasOutput.drawPageBorder(me.currentRptPageRst, canvas, getScreenDPI());
  222. JpcCanvasOutput.drawToCanvas(me.currentRptPageRst, canvas, me.currentPage);
  223. }
  224. me.displayPageValue();
  225. },
  226. displayPageValue: function() {
  227. let me = zTreeOprObj;
  228. $("#rpt_page_num").get(0).value = me.currentPage + "/" + me.maxPages;
  229. }
  230. };
  231. let canvasOprObj = {
  232. canvasOnMouseMove: function (event) {
  233. if (zTreeOprObj.currentNode) {
  234. let x = event.offsetX - JpcCanvasOutput.offsetX, canvas = event.originalTarget;
  235. if (!(canvas)) canvas = event.target; //chrome浏览器不认event.originalTarget,只认event.target或event.currentTarget
  236. if (x < FIRST_PAGE_OFFSET) {
  237. canvas.style.cursor = "url(/web/building_saas/img/FirstPageSimple.cur), auto";
  238. } else if (x < PRE_PAGE_OFFSET) {
  239. canvas.style.cursor = "url(/web/building_saas/img/PreviousPageSimple.cur), auto";
  240. } else if ((canvas.width - x) < LAST_PAGE_OFFSET) {
  241. canvas.style.cursor = "url(/web/building_saas/img/LastPageSimple.cur), auto";
  242. } else if ((canvas.width - x) < NEXT_PAGE_OFFSET) {
  243. canvas.style.cursor = "url(/web/building_saas/img/NextPageSimple.cur), auto";
  244. } else {
  245. canvas.style.cursor = "";
  246. }
  247. }
  248. },
  249. canvasOnClick: function(event){
  250. if (zTreeOprObj.currentNode) {
  251. let x = event.offsetX - JpcCanvasOutput.offsetX, canvas = event.originalTarget;
  252. if (!(canvas)) canvas = event.target; //chrome浏览器不认event.originalTarget,只认event.target或event.currentTarget
  253. if (x < FIRST_PAGE_OFFSET) {
  254. zTreeOprObj.showPage(1, canvas);
  255. } else if (x < PRE_PAGE_OFFSET) {
  256. zTreeOprObj.showPage(zTreeOprObj.currentPage - 1, canvas);
  257. } else if ((canvas.width - x) < LAST_PAGE_OFFSET) {
  258. zTreeOprObj.showPage(zTreeOprObj.maxPages, canvas);
  259. } else if ((canvas.width - x) < NEXT_PAGE_OFFSET) {
  260. zTreeOprObj.showPage(zTreeOprObj.currentPage + 1, canvas);
  261. }
  262. }
  263. }
  264. };
  265. let rptControlObj = {
  266. currentOutputType: "Excel",
  267. currentDownloadUrl: null,
  268. currentDownloadIdx: 0,
  269. getCurrentPageSize: function() {
  270. // let rst = "A4";
  271. let rst = document.getElementById("btnRptPageSize").innerHTML.trim();
  272. //btnRptPageSize
  273. return rst;
  274. },
  275. getCurrentOrientation: function() {
  276. // let rst = "横向";
  277. let rst = document.getElementById("btnRptOrientation").innerHTML.trim();
  278. return rst;
  279. },
  280. getCurrentReportOption: function() {
  281. //
  282. },
  283. changeType: function(newType) {
  284. let me = rptControlObj;
  285. let excelDom = document.getElementById("EXCEL_TYPE");
  286. let pdfDom = document.getElementById("PDF_TYPE");
  287. if (newType === "Excel") {
  288. excelDom.className = "btn btn-block btn-primary";
  289. pdfDom.className = "btn btn-block btn-outline-secondary";
  290. me.currentOutputType = newType;
  291. } else if (newType === "PDF") {
  292. excelDom.className = "btn btn-block btn-outline-secondary";
  293. pdfDom.className = "btn btn-block btn-primary";
  294. me.currentOutputType = newType;
  295. } else {
  296. //me.currentOutputType = newType;
  297. }
  298. },
  299. outputRpt: function() {
  300. let me = rptControlObj;
  301. if (me.currentOutputType === "Excel") {
  302. me.getExcel();
  303. } else if (me.currentOutputType === "PDF") {
  304. me.getPDF();
  305. } else {
  306. //other types if needed.
  307. }
  308. },
  309. getAllInOneBook: function () {
  310. let me = rptControlObj;
  311. if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
  312. let orgRptName = zTreeOprObj.checkedRptTplNodes[0].name;
  313. let refRptTplIds = [];
  314. for (let node of zTreeOprObj.checkedRptTplNodes) {
  315. refRptTplIds.push(node.refId);
  316. }
  317. let url = "/report_api/getExcelInOneBook/" + projectInfoObj.projectInfo.ID + "/" + refRptTplIds.join(",") + "/" +
  318. me.getCurrentPageSize() + "/" + orgRptName + "/" + 'normal';
  319. window.location = url;
  320. }
  321. },
  322. getExcel: function () {
  323. let me = rptControlObj;
  324. if ($("#excelExportType_AllInOneBook").get(0).checked) {
  325. me.getAllInOneBook();
  326. } else if ($("#excelExportType_IndividualBook").get(0).checked) {
  327. if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
  328. let urls = [];
  329. let orientation = ((zTreeOprObj.checkedRptTplNodes.length > 1)?null:me.getCurrentOrientation());
  330. for (let tplNode of zTreeOprObj.checkedRptTplNodes) {
  331. let orgRptName = tplNode.name;
  332. let url = "/report_api/getExcel/" + projectInfoObj.projectInfo.ID + "/" + tplNode.refId + "/" +
  333. me.getCurrentPageSize() + "/" + orientation + "/" + orgRptName + "/" + true + "/" + 'normal';
  334. urls.push(url);
  335. }
  336. downloadReport(urls);
  337. }
  338. }
  339. },
  340. getPDF: function() {
  341. let me = rptControlObj;
  342. if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
  343. let urls = [];
  344. let orientation = ((zTreeOprObj.checkedRptTplNodes.length > 1)?null:me.getCurrentOrientation());
  345. for (let tplNode of zTreeOprObj.checkedRptTplNodes) {
  346. let orgRptName = tplNode.name;
  347. let url = "/report_api/getPDF/" + projectInfoObj.projectInfo.ID + "/" + tplNode.refId + "/" +
  348. me.getCurrentPageSize() + "/" + orientation + "/" + orgRptName;
  349. urls.push(url);
  350. }
  351. downloadReport(urls);
  352. }
  353. },
  354. firstPage: function(dom) {
  355. let canvas = document.getElementById("rptCanvas");
  356. zTreeOprObj.showPage(1, canvas);
  357. },
  358. prePage: function(dom) {
  359. let canvas = document.getElementById("rptCanvas");
  360. zTreeOprObj.showPage(zTreeOprObj.currentPage - 1, canvas);
  361. },
  362. nextPage: function(dom) {
  363. let canvas = document.getElementById("rptCanvas");
  364. zTreeOprObj.showPage(zTreeOprObj.currentPage + 1, canvas);
  365. },
  366. lastPage: function(dom) {
  367. let me = zTreeOprObj;
  368. let canvas = document.getElementById("rptCanvas");
  369. zTreeOprObj.showPage(me.maxPages, canvas);
  370. },
  371. onKeydown: function (event, dom) {
  372. let me = zTreeOprObj, keyPressed = null;
  373. if (window.event) {
  374. keyPressed = window.event.keyCode; // IE/Chrome
  375. } else {
  376. keyPressed = event.which; // Firefox
  377. }
  378. if (keyPressed === 13) {
  379. let pageNum = 1;
  380. try {
  381. pageNum = parseInt(dom.value);
  382. } catch (e) {
  383. pageNum = 1;
  384. }
  385. let canvas = document.getElementById("rptCanvas");
  386. if (pageNum < 1) {
  387. pageNum = 1;
  388. } else if (pageNum > me.maxPages) {
  389. pageNum = me.maxPages;
  390. }
  391. zTreeOprObj.showPage(pageNum, canvas);
  392. return false;
  393. }
  394. },
  395. changeMargin: function(marginPropStr, marginDom) {
  396. zTreeOprObj.reportPageCfg.margins[marginPropStr] = marginDom.value;
  397. },
  398. changeFontMain: function(CfgDispName, fontProperty, fontDom) {
  399. for (let font of zTreeOprObj.reportPageCfg.fonts) {
  400. if (font["CfgDispName"] === CfgDispName) {
  401. font[fontProperty] = fontDom.value;
  402. break;
  403. }
  404. }
  405. },
  406. changeCfgOption: function (optStr, dom) {
  407. zTreeOprObj.reportPageCfg[optStr] = dom.checked;
  408. },
  409. changeFontAdhoc: function(CfgDispName, fontProperty, fontDom) {
  410. for (let font of zTreeOprObj.reportPageCfg.fonts) {
  411. if (font["CfgDispName"] === CfgDispName) {
  412. if (font[fontProperty] === 'T') {
  413. font[fontProperty] = 'F';
  414. fontDom.className = "btn btn-sm btn-outline-secondary";
  415. } else {
  416. font[fontProperty] = 'T';
  417. fontDom.className = "btn btn-sm btn-outline-secondary active";
  418. }
  419. break;
  420. }
  421. }
  422. },
  423. saveCustCfg: function() {
  424. let params = {};
  425. params.custCfg = zTreeOprObj.reportPageCfg;
  426. CommonAjax.postEx("report_tpl_api/saveCustomerCfg", params, 20000, true, function(result){
  427. alert("Save successfully!");
  428. }, null, null
  429. );
  430. },
  431. confirmCfgChange: function() {
  432. let treeNode = zTreeOprObj.currentNode;
  433. if (treeNode.nodeType === TPL_TYPE_TEMPLATE && treeNode.refId > 0) {
  434. let params = {};
  435. params.pageSize = rptControlObj.getCurrentPageSize();
  436. params.rpt_tpl_id = treeNode.refId;
  437. params.prj_id = projectInfoObj.projectInfo.ID;
  438. params.custCfg = zTreeOprObj.reportPageCfg;
  439. zTreeOprObj.requestReport(params);
  440. }
  441. }
  442. };
  443. function downloadReport(urls) {
  444. //考虑到多个报表下载,一些浏览器(如chrome)不允许一下子下载多个文件,得缓缓处理,统一在这处理
  445. rptControlObj.currentDownloadUrl = null;
  446. rptControlObj.currentDownloadIdx = 0;
  447. let private_download = function() {
  448. if (rptControlObj.currentDownloadIdx >= 0 && rptControlObj.currentDownloadIdx < urls.length) {
  449. rptControlObj.currentDownloadUrl = urls[rptControlObj.currentDownloadIdx];
  450. rptControlObj.currentDownloadIdx++;
  451. window.location = rptControlObj.currentDownloadUrl;
  452. setTimeout(private_download, 2000);
  453. }
  454. }
  455. private_download();
  456. }