rpt_move_signature.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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.rel_content = ROLE_REL_LIST;
  98. const { x, y, width, height, signType, signatureName } = me.activeImg;
  99. const target = ROLE_REL_LIST.find(item => item.signature_name === signatureName);
  100. if (target) {
  101. if (!target.areaData) target.areaData = {};
  102. target.areaData[signType] = {
  103. Top: y,
  104. Bottom: y + height,
  105. Left: x,
  106. Right: x + width
  107. }
  108. }
  109. CommonAjax.postXsrfEx("/tender/report_api/updateRoleRelationship", params, 10000, true, getCookie('csrfToken_j'),
  110. async function (result) {
  111. // console.log(result);
  112. }
  113. )
  114. }
  115. render(me, isHideSignature = false) {
  116. let target;
  117. // 因为只有一页,所以就默认取是一个数据
  118. for (const signature of me.pageObj.items[0].signature_cells) {
  119. //找到要移动的签章
  120. if (signature.signature_name && me.activeImg && me.activeImg.signature_name.includes(signature.signature_name)) {
  121. target = signature;
  122. }
  123. }
  124. if (target) {
  125. Object.assign(target.area, {
  126. Top: me.activeImg.y,
  127. Bottom: me.activeImg.y + me.activeImg.height,
  128. Left: me.activeImg.x,
  129. Right: me.activeImg.x + me.activeImg.width,
  130. })
  131. me.jpcOutput.cleanCanvas(me.canvas);
  132. me.jpcOutput.drawPageBorder(me.pageObj, me.canvas, getScreenDPI())
  133. me.jpcOutput.drawToCanvas(me.pageObj, me.canvas, me.pageIdx, isHideSignature);
  134. }
  135. }
  136. domAddEventListener(canvas) {
  137. //点击事件
  138. $("#rptCanvas").unbind('mousedown').on('mousedown', (e) => { this.mousedownFn(e, this) });
  139. // $("#rptCanvas").unbind('mousemove').on('mousemove', (e) => { this.mouseMove(e, this) });
  140. }
  141. // 临时签章移动事件(控制签章只能在报表中移动)
  142. templateSignatureMove(event, me) {
  143. const { top, left } = $("#rptCanvas").position();
  144. const canvasWidth = $("#rptCanvas").width();
  145. const canvasHeight = $("#rptCanvas").height();
  146. const signatureWidth = $("#templateSignature").width();
  147. const signatureHeight = $("#templateSignature").height();
  148. const maxTop = canvasHeight + top - signatureHeight; //签章的最大y坐标
  149. const maxLeft = canvasWidth + left - signatureWidth; //签章的最大x坐标
  150. const mousePos = me.getMousePos(event, me);
  151. let newTop = mousePos.y - me.subPos.y + top;
  152. let newLeft = mousePos.x - me.subPos.x + left;
  153. if ((mousePos.y - me.subPos.y) < 0) newTop = top + me.jpcOutput.offsetY;
  154. if ((mousePos.x - me.subPos.x) < 0) newLeft = left + me.jpcOutput.offsetX;
  155. if ((mousePos.y - me.subPos.y) > maxTop - top) newTop = maxTop - me.jpcOutput.offsetXY;
  156. if ((mousePos.x - me.subPos.x) > maxLeft - left) newLeft = maxLeft - me.jpcOutput.offsetX;
  157. event.currentTarget.style.top = `${newTop}px`;
  158. event.currentTarget.style.left = `${newLeft}px`;
  159. }
  160. // 临时签章点击事件
  161. templateSignatureClick(event, me) {
  162. const canvasOuterTop = $("#rptCanvas").position().top;
  163. const canvasOuterLeft = $("#rptCanvas").position().left;
  164. const { top, left } = $(event.currentTarget).position();
  165. me.activeImg.y = top - canvasOuterTop - this.jpcOutput.offsetY;
  166. me.activeImg.x = left - canvasOuterLeft - this.jpcOutput.offsetX;
  167. const orgTargetData = this.pageObj.items[0].signature_cells.find(item => item.signature_name === $(event.currentTarget).data('id'));
  168. orgTargetData.isMoving = false;
  169. this.saveSignature(me);
  170. $("#templateSignature").remove();
  171. }
  172. }