baozhuangscript.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //表现层的处理
  2. if (window.location.href.indexOf("backEndAddressServiceWrapper") >= 0) {
  3. throw "serviceGrid"; //如果是服务器网页页面,则不执行工具
  4. }
  5. //返回element相对node节点的xpath,默认的node节点是: /
  6. function readXPath(element, type = 1, node = document.body) {
  7. try {
  8. if (type == 0) //type=0代表默认可通过id生成xpath type=1代表只能从根节点生成xpath
  9. {
  10. if (element.id !== "") { //判断id属性,如果这个元素有id,则显示//*[@id="xPath"] 形式内容
  11. return '//*[@id=\"' + element.id + '\"]';
  12. }
  13. }
  14. //这里需要需要主要字符串转译问题,可参考js 动态生成html时字符串和变量转译(注意引号的作用)
  15. if (element == node) { //递归到body处,结束递归
  16. if (node == document.body) {
  17. return '/html/' + element.tagName.toLowerCase();
  18. } else {
  19. return "";
  20. }
  21. }
  22. var ix = 1, //在nodelist中的位置,且每次点击初始化
  23. siblings = element.parentNode.childNodes; //同级的子元素
  24. for (var i = 0, l = siblings.length; i < l; i++) {
  25. var sibling = siblings[i];
  26. //如果这个元素是siblings数组中的元素,则执行递归操作;arguments.callee代表当前函数的名称
  27. if (sibling == element) {
  28. return readXPath(element.parentNode, type, node) + '/' + element.tagName.toLowerCase() + '[' + (ix) + ']';
  29. //如果不符合,判断是否是element元素,并且是否是相同元素,如果是相同的就开始累加
  30. } else if (sibling.nodeType == 1 && sibling.tagName == element.tagName) {
  31. //注意此处,为了防止多计算了插入的操作台的3个div元素导致定位错误,这里需要屏蔽掉三个元素的索引号
  32. if(sibling.id != "wrapperDiv" && sibling.id != "wrapperTdiv" &&sibling.id != "wrapperToolkit"){
  33. ix++;
  34. }
  35. }
  36. }
  37. } catch {
  38. return "/"
  39. }
  40. };
  41. //创造div作为选中元素后的样式存在
  42. var div = document.createElement('div');
  43. div.style.zIndex = -2147483647;
  44. div.setAttribute("id", "wrapperDiv");
  45. div.style.position = "fixed";
  46. div.style.boxSizing = "border-box";
  47. div.style.border = "dotted";
  48. var tdiv = document.createElement('div');
  49. tdiv.style.zIndex = 2147483647;
  50. tdiv.style.position = "fixed";
  51. tdiv.setAttribute("id", "wrapperTdiv");
  52. tdiv.classList = "tdiv";
  53. tdiv.style.top = "0px";
  54. tdiv.style.width = "3000px";
  55. tdiv.style.height = "3000px";
  56. tdiv.style.pointerEvents = "none";
  57. var mousemovebind = false; //如果出现元素默认绑定了mousemove事件导致匹配不到元素的时候,开启第二种模式获得元素
  58. var toolkit = document.createElement("div")
  59. toolkit.classList = "tooltips"; //添加样式
  60. toolkit.setAttribute("id", "wrapperToolkit");
  61. var tooltips = false; //标记鼠标是否在提示框上
  62. var defaultbgColor = 'rgba(221,221,255,0.8)'; //移动到元素的背景颜色
  63. var selectedColor = "rgba(151,255,255, 0.6)"; //选中元素的背景颜色
  64. var boxShadowColor = "blue 0px 0px 5px"; //待选元素的边框属性
  65. //右键菜单屏蔽
  66. document.oncontextmenu = () => false;
  67. var nodeList = []; //已被选中的节点列表
  68. var readyList = []; //预备选中的list
  69. var outputParameters = []; //输出参数列表
  70. var outputParameterNodes = []; //输出参数节点列表
  71. NowNode = null;
  72. var xnode = null;
  73. var step = 0; //记录这是第几次点击操作
  74. var style = ""; //记录上个元素的颜色
  75. document.addEventListener("mousemove", function() {
  76. if (mousemovebind) {
  77. tdiv.style.pointerEvents = "none";
  78. }
  79. //如果鼠标在元素框内则点击和选中失效
  80. var x = event.clientX;
  81. var y = event.clientY;
  82. var divx1 = toolkit.offsetLeft;
  83. var divy1 = toolkit.offsetTop;
  84. var divx2 = toolkit.offsetLeft + toolkit.offsetWidth;
  85. var divy2 = toolkit.offsetTop + toolkit.offsetHeight;
  86. if (x >= divx1 && x <= divx2 && y >= divy1 && y <= divy2) {
  87. tooltips = true;
  88. return;
  89. }
  90. oe = document.elementFromPoint(event.x, event.y);
  91. if (oe == tdiv) {
  92. return;
  93. }
  94. tooltips = false;
  95. NowNode = oe;
  96. te = 0;
  97. exist = 0;
  98. exist2 = 0;
  99. for (o of nodeList) {
  100. if (o["node"] == oe) {
  101. exist = 1;
  102. break;
  103. }
  104. }
  105. for (o of nodeList) {
  106. if (o["node"] == xnode) {
  107. exist2 = 1;
  108. break;
  109. }
  110. }
  111. // console.log(oe);
  112. if (xnode == null) {
  113. xnode = oe;
  114. }
  115. if (xnode != oe) {
  116. if (exist2 == 0) { //如果上个元素不在数组里,改回上个元素的初始颜色
  117. try {
  118. xnode.style.backgroundColor = style; //上个元素改回原来元素的背景颜色
  119. } catch {
  120. xnode.style.backgroundColor = ""; //上个元素改回原来元素的背景颜色
  121. }
  122. }
  123. try {
  124. style = oe.style.backgroundColor;
  125. } catch {
  126. style = "";
  127. }
  128. if (exist == 1) {
  129. } else {
  130. try {
  131. oe.style.backgroundColor = defaultbgColor; //设置新元素的背景元素
  132. } catch {}
  133. }
  134. xnode = oe;
  135. div.style.display = "none";
  136. }
  137. if (mousemovebind) {
  138. tdiv.style.pointerEvents = "";
  139. }
  140. });
  141. //点击没反应时候的替代方案
  142. document.onkeydown = function(event) {
  143. // console.log("keydown");
  144. var e = event || window.event || arguments.callee.caller.arguments[0];
  145. if (e && e.keyCode == 118) { // 按 F7
  146. addEl();
  147. } else if (e && e.keyCode == 119) { //按F8
  148. clearEl();
  149. } else if (e && e.keyCode == 120) { //按F9
  150. NowNode.focus();
  151. NowNode.click();
  152. // console.log("click",NowNode);
  153. } else {
  154. return event.keyCode;
  155. }
  156. };
  157. //选中元素到列表中
  158. function addEl() {
  159. // if (tooltips) {
  160. // return;
  161. // }
  162. let exist = false;
  163. for (o of nodeList) {
  164. if (o["node"] == oe) {
  165. exist = true;
  166. break;
  167. }
  168. }
  169. //元素没有被添加过才去添加
  170. if (!exist) {
  171. step++;
  172. exist = false; //判断刚加入的元素是否在readyList中,如果在,则将所有readylist中的元素全部放入list中
  173. for (o of readyList) {
  174. if (o["node"] == oe) {
  175. exist = true;
  176. break;
  177. }
  178. }
  179. if (exist) { //存在在readylist就全选中
  180. readyToList(step);
  181. if (app._data.selectedDescendents) {
  182. handleDescendents(); //如果之前有选中子元素,新加入的节点又则这里也需要重新选择子元素
  183. }
  184. } else //不然只添加一个元素
  185. {
  186. clearReady(); //readylist清零重新算
  187. nodeList.push({ node: NowNode, "step": step, bgColor: style, "boxShadow": NowNode.style.boxShadow == "" || boxShadowColor ? "none" : NowNode.style.boxShadow, xpath: readXPath(NowNode, 1) });
  188. NowNode.style.backgroundColor = selectedColor;
  189. }
  190. handleElement(); //处理新状态
  191. //将虚线框显示在元素上方但屏蔽其鼠标操作
  192. var pos = NowNode.getBoundingClientRect();
  193. div.style.display = "block";
  194. div.style.height = NowNode.offsetHeight + "px";
  195. div.style.width = NowNode.offsetWidth + "px";
  196. div.style.left = pos.left + "px";
  197. div.style.top = pos.top + "px";
  198. div.style.zIndex = 2147483645;
  199. div.style.pointerEvents = "none";
  200. }
  201. // console.log("------");
  202. // for (i = 0; i < nodeList.length; i++) {
  203. // console.log(nodeList[i]["xpath"]);
  204. // }
  205. //对于可点击元素,屏蔽a标签默认点击事件
  206. event.stopImmediatePropagation();
  207. event.stopPropagation();
  208. event.preventDefault ? event.preventDefault() : event.returnValue = false;
  209. }
  210. document.addEventListener("mousedown", addEl);
  211. toolkit.addEventListener("mousedown", function(e) { e.stopPropagation(); }); //重新定义toolkit里的点击事件
  212. //清除选择项
  213. function clearEl() {
  214. //如果最后停留的元素被选中,则调整此元素的style为原始style,否则不进行调整
  215. for (node of nodeList) {
  216. node["node"].style.backgroundColor = node["bgColor"];
  217. node["node"].style.boxShadow = node["boxShadow"];
  218. if (NowNode == node["node"]) {
  219. style = node["bgColor"];
  220. }
  221. }
  222. step = 0;
  223. clearReady();
  224. clearParameters();
  225. nodeList.splice(0, nodeList.length); //清空数组
  226. app._data.option = 0; //选项重置
  227. app._data.page = 0; //恢复原始页面
  228. }
  229. //清除预备数组
  230. function clearReady() {
  231. for (node of readyList) //节点列表状态恢复原状
  232. {
  233. node["node"].style.boxShadow = node["boxShadow"];
  234. }
  235. readyList.splice(0, readyList.length); //清空数组
  236. }
  237. document.body.append(div); //默认如果toolkit不存在则div和tdiv也不存在
  238. document.body.append(tdiv);
  239. document.body.append(toolkit);
  240. var timer;