rpt_move_signature.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 orgTargetData = this.pageObj.items[0].signature_cells.find(item => item.signatureName === target.signatureName && item.signType === target.signType);
  78. orgTargetData.isMoving = true;
  79. this.render(me);
  80. const { top, left } = $("#rptCanvas").position();
  81. $("#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;"/>`);
  82. $("#templateSignature").unbind('mousemove').on('mousemove', (e) => { this.templateSignatureMove(e, this) });
  83. $("#templateSignature").unbind('click').on('click', (e) => { this.templateSignatureClick(e, this) });
  84. } else {
  85. me.selected = false;
  86. }
  87. }
  88. saveSignature(me) {
  89. //鼠标抬起,取消拖拽
  90. me.selected = false;
  91. me.render(me);
  92. const params = {};
  93. params.id = CURRENT_ROLE_REL_ID;
  94. params.tender_id = TENDER_ID;
  95. params.stage_id = getStageId();
  96. params.rpt_id = zTreeOprObj.currentNode.refId;
  97. params.business_id = BUSINESS_ID;
  98. // params.rel_content = ROLE_REL_LIST;
  99. params.rel_content = [];
  100. ROLE_REL_LIST.forEach((role) => {
  101. if (role.signature_name.indexOf('dummy_pic') < 0) {
  102. params.rel_content.push(role);
  103. }
  104. });
  105. const { x, y, width, height, signType, signatureName } = me.activeImg;
  106. const target = ROLE_REL_LIST.find(item => item.signature_name === signatureName);
  107. if (target) {
  108. if (!target.areaData) target.areaData = {};
  109. target.areaData[signType] = {
  110. Top: y,
  111. Bottom: y + height,
  112. Left: x,
  113. Right: x + width
  114. }
  115. }
  116. CommonAjax.postXsrfEx("/tender/report_api/updateRoleRelationship", params, 10000, true, getCookie('csrfToken_j'),
  117. async function (result) {
  118. // console.log(result);
  119. }
  120. )
  121. }
  122. render(me, isHideSignature = false) {
  123. let target;
  124. // 因为只有一页,所以就默认取是一个数据
  125. for (const signature of me.pageObj.items[0].signature_cells) {
  126. //找到要移动的签章
  127. if (signature.signature_name && me.activeImg && me.activeImg.signature_name.includes(signature.signature_name)) {
  128. target = signature;
  129. }
  130. }
  131. if (target) {
  132. Object.assign(target.area, {
  133. Top: me.activeImg.y,
  134. Bottom: me.activeImg.y + me.activeImg.height,
  135. Left: me.activeImg.x,
  136. Right: me.activeImg.x + me.activeImg.width,
  137. })
  138. me.jpcOutput.cleanCanvas(me.canvas);
  139. me.jpcOutput.drawPageBorder(me.pageObj, me.canvas, getScreenDPI())
  140. me.jpcOutput.drawToCanvas(me.pageObj, me.canvas, me.pageIdx, isHideSignature);
  141. }
  142. }
  143. domAddEventListener(canvas) {
  144. //点击事件
  145. $("#rptCanvas").unbind('mousedown').on('mousedown', (e) => { this.mousedownFn(e, this) });
  146. // $("#rptCanvas").unbind('mousemove').on('mousemove', (e) => { this.mouseMove(e, this) });
  147. }
  148. // 临时签章移动事件(控制签章只能在报表中移动)
  149. templateSignatureMove(event, me) {
  150. // 表格大小
  151. const size = me.jpcOutput.getReportSizeInPixel(me.pageObj, getScreenDPI());
  152. const reportMarginTop= me.pageObj.打印页面_信息.页边距.Top * 96/2;
  153. const reportMarginBottom= me.pageObj.打印页面_信息.页边距.Bottom * 96/2;
  154. const { top, left } = $("#rptCanvas").position();
  155. const signatureWidth = $("#templateSignature").width();
  156. const signatureHeight = $("#templateSignature").height();
  157. const maxTop = size[1] + top - signatureHeight + me.jpcOutput.offsetY - reportMarginBottom ; //签章的最大y坐标
  158. const maxLeft = me.pageObj.MergeBand.Right + left - signatureWidth + me.jpcOutput.offsetX ; //签章的最大x坐标
  159. const mousePos = me.getMousePos(event, me);
  160. let newTop = mousePos.y - me.subPos.y + top;
  161. let newLeft = mousePos.x - me.subPos.x + left;
  162. if ((mousePos.y - me.subPos.y) < reportMarginTop+top) newTop = reportMarginTop + top + me.jpcOutput.offsetY;
  163. if ((mousePos.x - me.subPos.x) < me.pageObj.MergeBand.Left) newLeft = me.pageObj.MergeBand.Left+left + me.jpcOutput.offsetX;
  164. if ((mousePos.y - me.subPos.y) > maxTop ) newTop = maxTop + me.jpcOutput.offsetY*2;
  165. if ((mousePos.x - me.subPos.x) > maxLeft - left) newLeft = maxLeft + me.jpcOutput.offsetX/2;
  166. event.currentTarget.style.top = `${newTop}px`;
  167. event.currentTarget.style.left = `${newLeft}px`;
  168. }
  169. // 临时签章点击事件
  170. templateSignatureClick(event, me) {
  171. const canvasOuterTop = $("#rptCanvas").position().top;
  172. const canvasOuterLeft = $("#rptCanvas").position().left;
  173. const { top, left } = $(event.currentTarget).position();
  174. me.activeImg.y = top - canvasOuterTop - this.jpcOutput.offsetY;
  175. me.activeImg.x = left - canvasOuterLeft - this.jpcOutput.offsetX;
  176. const orgTargetData = this.pageObj.items[0].signature_cells.find(item => item.signature_name === $(event.currentTarget).data('id'));
  177. orgTargetData.isMoving = false;
  178. this.saveSignature(me);
  179. if (this.pageObj.items.length > 1) {
  180. for (let idx = 0; idx < this.pageObj.items.length; idx++) {
  181. const targetCell = this.pageObj.items[idx].signature_cells.find(item => item.signatureName === orgTargetData.signatureName && item.signType === orgTargetData.signType);
  182. if (targetCell) {
  183. targetCell.area.Left = orgTargetData.area.Left;
  184. targetCell.area.Right = orgTargetData.area.Right;
  185. targetCell.area.Top = orgTargetData.area.Top;
  186. targetCell.area.Bottom = orgTargetData.area.Bottom;
  187. }
  188. }
  189. }
  190. $("#templateSignature").remove();
  191. }
  192. }