rpt_move_signature.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // 思路是点击签章的时候,原来的签章不显示,生成一个img用于拖动,实现拖动效果,再点击新生成的img就保存数据,干掉临时的img
  2. class MoveSignatureTool {
  3. constructor(ctx, canvas, pageObj, pageIdx, jpcOutput) {
  4. this.signatureList = [];
  5. this.ctx = ctx;
  6. // 被选中的签章
  7. this.activeImg = undefined;
  8. //鼠标位减图形位
  9. this.subPos = null;
  10. //图形是否被选择
  11. this.selected = false;
  12. // 上一个被选中的签章属性
  13. this.lastPosition = { x: 0, y: 0, width: 0, height: 0 };
  14. //canvas的dom对象
  15. this.canvas = canvas;
  16. this.pageObj = pageObj;
  17. this.pageIdx = pageIdx;
  18. this.jpcOutput = jpcOutput;
  19. this.signatureCanvas = document.getElementById("rptSignatureCanvas");
  20. }
  21. setSignature(obj, x, y, width, height, signature_name, signatureName, signType) {
  22. // 只有签章才能移动
  23. if (signType) {
  24. this.signatureList.push({
  25. obj,
  26. x,
  27. y,
  28. width,
  29. height,
  30. crossOrigin: 'Anonymous',
  31. signature_name,
  32. signatureName,
  33. signType
  34. })
  35. }
  36. }
  37. //获取鼠标在canvas中的位置
  38. getMousePos(event, me) {
  39. //获取鼠标位置
  40. const { clientX, clientY } = event;
  41. //获取canvas 边界位置
  42. const { top, left } = me.canvas.getBoundingClientRect();
  43. //计算鼠标在canvas 中的位置
  44. const x = clientX - left;
  45. const y = clientY - top;
  46. return { x, y };
  47. }
  48. //判断点是否在图形中
  49. containPoint(data, mousePos) {
  50. //解构图形位置和尺寸
  51. const { x, y, width, height } = data;
  52. //解构鼠标位置
  53. const { x: mx, y: my } = mousePos;
  54. //计算鼠标和图形上、下、左、右边界的关系
  55. const l = mx > x;
  56. const r = mx < x + width;
  57. const t = my > y;
  58. const b = my < y + height;
  59. return l && r && t && b;
  60. }
  61. mousedownFn(event, me) {
  62. //鼠标位置
  63. const mousePos = me.getMousePos(event, me);
  64. // 找到相应的对象
  65. const target = me.signatureList.find((item) => me.containPoint(item, mousePos));
  66. if ($('#templateSignature').length === 0 && target) {
  67. //先把上一次的数据记录下来
  68. const { x, y, width, height } = target;
  69. Object.assign(me.lastPosition, { x, y, width, height });
  70. me.selected = true;
  71. me.activeImg = target;
  72. //鼠标位减图形位
  73. me.subPos = {
  74. x: mousePos.x - me.activeImg.x,
  75. y: mousePos.y - me.activeImg.y,
  76. };
  77. const curPage = zTreeOprObj.currentPage || 1;
  78. const orgTargetData = this.pageObj.items[curPage - 1].signature_cells.find(item => item.signatureName === target.signatureName && item.signType === target.signType);
  79. if (orgTargetData) {
  80. orgTargetData.isMoving = true;
  81. this.render(me);
  82. const { top, left } = $("#rptCanvas").position();
  83. $("#rptCanvas").after(`<div id='templateSignature' data-ID='${orgTargetData.signature_name}' style="background-image:url('${orgTargetData.path}');background-size: contain;cursor: move;position: absolute;top: ${y + top + 10}px;left:${x + left + 10}px;width:${width}px;height:${height}px;"/>`);
  84. $("#templateSignature").unbind('mousemove').on('mousemove', (e) => { this.templateSignatureMove(e, this) });
  85. $("#templateSignature").unbind('click').on('click', (e) => { this.templateSignatureClick(e, this) });
  86. }
  87. } else {
  88. me.selected = false;
  89. }
  90. }
  91. saveSignature(me) {
  92. //鼠标抬起,取消拖拽
  93. me.selected = false;
  94. me.render(me);
  95. const params = {};
  96. params.id = CURRENT_ROLE_REL_ID;
  97. params.tender_id = TENDER_ID;
  98. params.stage_id = getStageId();
  99. params.rpt_id = zTreeOprObj.currentNode.refId;
  100. params.business_id = BUSINESS_ID;
  101. // params.rel_content = ROLE_REL_LIST;
  102. params.rel_content = [];
  103. ROLE_REL_LIST.forEach((role) => {
  104. if (role.signature_name.indexOf('dummy_pic') < 0) {
  105. params.rel_content.push(role);
  106. }
  107. });
  108. const { x, y, width, height, signType, signatureName } = me.activeImg;
  109. const target = ROLE_REL_LIST.find(item => item.signature_name === signatureName);
  110. if (target) {
  111. if (!target.areaData) target.areaData = {};
  112. target.areaData[signType] = {
  113. Top: y,
  114. Bottom: y + height,
  115. Left: x,
  116. Right: x + width
  117. }
  118. }
  119. CommonAjax.postXsrfEx("/tender/report_api/updateRoleRelationship", params, 10000, true, getCookie('csrfToken_j'),
  120. async function (result) {
  121. // console.log(result);
  122. }
  123. )
  124. }
  125. render(me, isHideSignature = false) {
  126. let target;
  127. // 因为只有一页,所以就默认取是一个数据
  128. const curPage = zTreeOprObj.currentPage || 1;
  129. for (const signature of me.pageObj.items[curPage - 1].signature_cells) {
  130. //找到要移动的签章
  131. if (signature.signature_name && me.activeImg && me.activeImg.signature_name.includes(signature.signature_name)) {
  132. target = signature;
  133. }
  134. }
  135. if (target) {
  136. Object.assign(target.area, {
  137. Top: me.activeImg.y,
  138. Bottom: me.activeImg.y + me.activeImg.height,
  139. Left: me.activeImg.x,
  140. Right: me.activeImg.x + me.activeImg.width,
  141. })
  142. me.jpcOutput.cleanCanvas(me.canvas);
  143. me.jpcOutput.drawPageBorder(me.pageObj, me.canvas, getScreenDPI())
  144. me.jpcOutput.drawToCanvas(me.pageObj, me.canvas, me.pageIdx, isHideSignature);
  145. }
  146. }
  147. domAddEventListener(canvas) {
  148. //点击事件
  149. $("#rptCanvas").unbind('mousedown').on('mousedown', (e) => { this.mousedownFn(e, this) });
  150. // $("#rptCanvas").unbind('mousemove').on('mousemove', (e) => { this.mouseMove(e, this) });
  151. }
  152. // 临时签章移动事件(控制签章只能在报表中移动)
  153. templateSignatureMove(event, me) {
  154. // 表格大小
  155. const size = me.jpcOutput.getReportSizeInPixel(me.pageObj, getScreenDPI());
  156. const reportMarginTop= me.pageObj.打印页面_信息.页边距.Top * 96/2;
  157. const reportMarginBottom= me.pageObj.打印页面_信息.页边距.Bottom * 96/2;
  158. const { top, left } = $("#rptCanvas").position();
  159. const signatureWidth = $("#templateSignature").width();
  160. const signatureHeight = $("#templateSignature").height();
  161. const maxTop = size[1] + top - signatureHeight + me.jpcOutput.offsetY - reportMarginBottom ; //签章的最大y坐标
  162. const maxLeft = me.pageObj.MergeBand.Right + left - signatureWidth + me.jpcOutput.offsetX ; //签章的最大x坐标
  163. const mousePos = me.getMousePos(event, me);
  164. let newTop = mousePos.y - me.subPos.y + top;
  165. let newLeft = mousePos.x - me.subPos.x + left;
  166. if ((mousePos.y - me.subPos.y) < reportMarginTop+top) newTop = reportMarginTop + top + me.jpcOutput.offsetY;
  167. if ((mousePos.x - me.subPos.x) < me.pageObj.MergeBand.Left) newLeft = me.pageObj.MergeBand.Left+left + me.jpcOutput.offsetX;
  168. if ((mousePos.y - me.subPos.y) > maxTop ) newTop = maxTop + me.jpcOutput.offsetY*2;
  169. if ((mousePos.x - me.subPos.x) > maxLeft - left) newLeft = maxLeft + me.jpcOutput.offsetX/2;
  170. event.currentTarget.style.top = `${newTop}px`;
  171. event.currentTarget.style.left = `${newLeft}px`;
  172. }
  173. // 临时签章点击事件
  174. templateSignatureClick(event, me) {
  175. const canvasOuterTop = $("#rptCanvas").position().top;
  176. const canvasOuterLeft = $("#rptCanvas").position().left;
  177. const { top, left } = $(event.currentTarget).position();
  178. me.activeImg.y = top - canvasOuterTop - this.jpcOutput.offsetY;
  179. me.activeImg.x = left - canvasOuterLeft - this.jpcOutput.offsetX;
  180. const curPage = zTreeOprObj.currentPage || 1;
  181. const orgTargetData = this.pageObj.items[curPage - 1].signature_cells.find(item => item.signature_name === $(event.currentTarget).data('id'));
  182. if (orgTargetData) {
  183. orgTargetData.isMoving = false;
  184. this.saveSignature(me);
  185. if (this.pageObj.items.length > 1) {
  186. for (let idx = 0; idx < this.pageObj.items.length; idx++) {
  187. const targetCell = this.pageObj.items[idx].signature_cells.find(item => item.signatureName === orgTargetData.signatureName && item.signType === orgTargetData.signType);
  188. if (targetCell) {
  189. targetCell.area.Left = orgTargetData.area.Left;
  190. targetCell.area.Right = orgTargetData.area.Right;
  191. targetCell.area.Top = orgTargetData.area.Top;
  192. targetCell.area.Bottom = orgTargetData.area.Bottom;
  193. }
  194. }
  195. }
  196. $("#templateSignature").remove();
  197. }
  198. }
  199. }