node-item.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <template>
  2. <div
  3. class="node-item"
  4. ref="node"
  5. :class="[isActive || isSelected ? 'active' : '']"
  6. :style="flowNodeContainer"
  7. v-click-outside="setNotActive"
  8. @click="setActive"
  9. @mouseenter="showAnchor"
  10. @mouseleave="hideAnchor"
  11. @dblclick.prevent="editNode"
  12. @contextmenu.prevent="onContextmenu"
  13. >
  14. <div class="log-wrap">
  15. <img :src="node.logImg" alt="" />
  16. </div>
  17. <div class="nodeName">{{ node.nodeName }}</div>
  18. <!--连线用--//触发连线的区域-->
  19. <div class="node-anchor anchor-top" v-show="mouseEnter"></div>
  20. <div class="node-anchor anchor-right" v-show="mouseEnter"></div>
  21. <div class="node-anchor anchor-bottom" v-show="mouseEnter"></div>
  22. <div class="node-anchor anchor-left" v-show="mouseEnter"></div>
  23. <el-dialog
  24. :title="disabled ? '节点详情' : '节点编辑'"
  25. :visible.sync="dialog.visible"
  26. :modal="true"
  27. :append-to-body="true"
  28. width="30%"
  29. >
  30. <nodeEdit
  31. ref="nodeEdit"
  32. :data="dialog.data"
  33. :disabled="disabled"
  34. ></nodeEdit>
  35. <div slot="footer" class="dialog-footer">
  36. <el-button @click="dialog.visible = false">取 消</el-button>
  37. <el-button v-if="!disabled" type="primary" @click="dialogSubmit"
  38. >确 定</el-button
  39. >
  40. </div>
  41. </el-dialog>
  42. </div>
  43. </template>
  44. <script>
  45. import ClickOutside from "vue-click-outside";
  46. import nodeEdit from "./node-edit";
  47. export default {
  48. name: "nodeItem",
  49. components: {
  50. nodeEdit
  51. },
  52. props: {
  53. node: {
  54. type: Object,
  55. default: () => {}
  56. },
  57. disabled: {
  58. type: Boolean,
  59. default: false
  60. }
  61. },
  62. directives: {
  63. ClickOutside
  64. },
  65. computed: {
  66. // 节点容器样式
  67. flowNodeContainer: {
  68. get() {
  69. return {
  70. top: this.node.top,
  71. left: this.node.left
  72. };
  73. }
  74. }
  75. },
  76. data() {
  77. return {
  78. mouseEnter: false,
  79. isActive: false,
  80. isSelected: false,
  81. //对话框
  82. dialog: {
  83. visible: false,
  84. disabled: false,
  85. data: {}
  86. }
  87. };
  88. },
  89. methods: {
  90. showAnchor() {
  91. this.mouseEnter = true;
  92. },
  93. hideAnchor() {
  94. this.mouseEnter = false;
  95. },
  96. onContextmenu() {
  97. this.$contextmenu({
  98. items: [
  99. {
  100. label: "删除",
  101. disabled: false,
  102. icon: "",
  103. onClick: () => {
  104. this.deleteNode();
  105. }
  106. }
  107. ],
  108. event,
  109. customClass: "custom-class",
  110. zIndex: 9999,
  111. minWidth: 180
  112. });
  113. },
  114. setActive() {
  115. if (window.event.ctrlKey) {
  116. this.isSelected = !this.isSelected;
  117. return false;
  118. }
  119. this.isActive = true;
  120. this.isSelected = false;
  121. setTimeout(() => {
  122. this.$emit("changeLineState", this.node.id, true);
  123. }, 0);
  124. },
  125. setNotActive() {
  126. if (!window.event.ctrlKey) {
  127. this.isSelected = false;
  128. }
  129. if (!this.isActive) {
  130. return;
  131. }
  132. this.$emit("changeLineState", this.node.id, false);
  133. this.isActive = false;
  134. },
  135. editNode() {
  136. // 编辑节点
  137. this.newNodeName = this.node.nodeName;
  138. console.log("节点数据:");
  139. console.log(this.node);
  140. this.dialog.data = this.node;
  141. this.dialog.visible = true;
  142. },
  143. deleteNode() {
  144. this.$emit("deleteNode", this.node);
  145. },
  146. // 节点编辑提交
  147. dialogSubmit() {
  148. this.$refs.nodeEdit
  149. .validateFormData()
  150. .then(() => {
  151. let formData = this.$refs.nodeEdit.formData();
  152. console.log("表单数据: ");
  153. console.log(formData);
  154. this.$emit("setNode", this.node.id, formData);
  155. this.dialog.visible = false;
  156. })
  157. .catch(() => {});
  158. }
  159. }
  160. };
  161. </script>
  162. <style lang="less" scoped>
  163. @labelColor: #409eff;
  164. @nodeSize: 20px;
  165. @viewSize: 10px;
  166. .node-item {
  167. position: absolute;
  168. display: flex;
  169. height: 40px;
  170. width: 120px;
  171. justify-content: center;
  172. align-items: center;
  173. border: 1px solid #b7b6b6;
  174. border-radius: 4px;
  175. cursor: move;
  176. box-sizing: content-box;
  177. z-index: 9995;
  178. &:hover {
  179. z-index: 9998;
  180. .delete-btn {
  181. display: block;
  182. }
  183. }
  184. .log-wrap {
  185. width: 40px;
  186. height: 40px;
  187. border-right: 1px solid #b7b6b6;
  188. }
  189. .nodeName {
  190. flex-grow: 1;
  191. width: 0;
  192. overflow: hidden;
  193. text-overflow: ellipsis;
  194. white-space: nowrap;
  195. }
  196. .node-anchor {
  197. display: flex;
  198. position: absolute;
  199. width: @nodeSize;
  200. height: @nodeSize;
  201. align-items: center;
  202. justify-content: center;
  203. border-radius: 10px;
  204. cursor: crosshair;
  205. z-index: 9999;
  206. background: -webkit-radial-gradient(sandybrown 10%, white 30%, #9a54ff 60%);
  207. }
  208. .anchor-top {
  209. top: calc((@nodeSize / 2) * -1);
  210. left: 50%;
  211. margin-left: calc((@nodeSize / 2) * -1);
  212. }
  213. .anchor-right {
  214. top: 50%;
  215. right: calc((@nodeSize / 2) * -1);
  216. margin-top: calc((@nodeSize / 2) * -1);
  217. }
  218. .anchor-bottom {
  219. bottom: calc((@nodeSize / 2) * -1);
  220. left: 50%;
  221. margin-left: calc((@nodeSize / 2) * -1);
  222. }
  223. .anchor-left {
  224. top: 50%;
  225. left: calc((@nodeSize / 2) * -1);
  226. margin-top: calc((@nodeSize / 2) * -1);
  227. }
  228. }
  229. .active {
  230. border: 1px dashed @labelColor;
  231. box-shadow: 0px 5px 9px 0px rgba(0, 0, 0, 0.5);
  232. }
  233. </style>