node.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <g
  3. class="flow-node"
  4. :class="{
  5. 'flow-node--dragging':dragging,
  6. 'flow-node--selected': selected
  7. }"
  8. :status="status"
  9. @mousedown.stop.prevent="$emit('nodePointerDown',$event)"
  10. @contextmenu.capture.prevent="$emit('nodeRightClick', $event)"
  11. @dblclick="$emit('nodeDoubleClick',$event)"
  12. >
  13. <!-- shape -->
  14. <template>
  15. <svg
  16. v-if="style.shape == 'thing'"
  17. ref="body"
  18. viewBox="0 0 100 100"
  19. preserveAspectRatio="XMinYMin"
  20. class="flow-node__body"
  21. :class="{'flow-node__body--dragging':dragging}"
  22. v-bind="bodyProps"
  23. >
  24. <path d=" M 0 0 l 90 0 l 10 50 l -10 50 l -90 0 c 10 -20, 10 -80, 0 -100 Z " />
  25. </svg>
  26. <circle
  27. v-else-if="style.shape == 'circle'"
  28. ref="body"
  29. class="flow-node__body"
  30. :class="{'flow-node__body--dragging':dragging}"
  31. v-bind="bodyProps"
  32. />
  33. <rect
  34. v-else
  35. ref="body"
  36. class="flow-node__body"
  37. :class="{'flow-node__body--dragging':dragging}"
  38. v-bind="bodyProps"
  39. />
  40. </template>
  41. <!-- selection square -->
  42. <rect
  43. class="flow-node__selection"
  44. stroke-dasharray="7,3"
  45. :x="bodyProps.x-4"
  46. :y="bodyProps.y-4"
  47. :width="bodyProps.width+8"
  48. :height="bodyProps.height+8"
  49. />
  50. <!-- label -->
  51. <text
  52. ref="label"
  53. class="flow-node__label"
  54. v-bind="labelProps"
  55. text-anchor="middle">
  56. <tspan
  57. :key="'label-wrap' + i"
  58. x="0"
  59. dy="1em"
  60. v-for="(s,i) in labelWrap" >
  61. {{ s }}
  62. </tspan>
  63. </text>
  64. <!-- Sockets -->
  65. <!-- input -->
  66. <g
  67. v-for="(inp,i) in inputs"
  68. :key="'in'+i"
  69. :data-nodeid="id"
  70. :data-in="i"
  71. v-bind="inputProps(i)"
  72. @mousedown.stop.prevent="socketPointerDown($event, {in:i})"
  73. class="flow-node__socket flow-node__socket--inputs"
  74. :class="{'flow-node__socket--match': match.in && (inp.type == match.in || match.in == 'interface {}' || inp.type=='interface {}')}"
  75. >
  76. <circle r="5" />
  77. <text
  78. text-anchor="end"
  79. :x="-18"
  80. :y="4"
  81. class="flow-node__socket-detail"
  82. >{{ inputLabel(i) }}</text>
  83. </g>
  84. <!-- output -->
  85. <g
  86. v-if="output"
  87. v-bind="outputProps(0)"
  88. :data-nodeid="id"
  89. :data-out="0"
  90. :key="'out'+0"
  91. @mousedown.stop.prevent="socketPointerDown($event, {out:0})"
  92. class="flow-node__socket flow-node__socket--outputs"
  93. :class="{ 'flow-node__socket--match': match.out && (output.type == match.out || match.out == 'interface {}' || output.type == 'interface {}'), }"
  94. >
  95. <circle r="5" />
  96. <text
  97. class="flow-node__socket-detail"
  98. :x="18"
  99. :y="4">
  100. {{ outputLabel(0) }}
  101. </text>
  102. </g>
  103. <!-- socket labels -->
  104. <flow-node-activity
  105. v-if="activity"
  106. :activity="activity"
  107. :transform="'translate('+bodyProps.width/2 +','+ -bodyProps.height/2 +')'"/>
  108. </g>
  109. </template>
  110. <script>
  111. import FlowNodeActivity from './node-activity'
  112. import utils from '@/utils/utils'
  113. const shapeOpts = {
  114. 'circle': {
  115. textWrap: 'any'
  116. },
  117. default: {
  118. textWrap: 'white-space'
  119. }
  120. }
  121. export default {
  122. name: 'FlowNode',
  123. components: {FlowNodeActivity},
  124. props: {
  125. 'id': {type: String, required: true},
  126. 'label': {type: String, default: ''},
  127. 'inputs': {type: Array, default: () => []},
  128. 'output': {type: Object, default: ''},
  129. 'match': {type: Object, default: () => {}},
  130. 'dragging': {type: Boolean, default: false},
  131. 'selected': {type: Boolean, default: false},
  132. 'activity': {type: Object, default: () => {}},
  133. 'nodeStyle': {type: Object, default: () => {}}
  134. },
  135. data () {
  136. return {
  137. // maintain reference here?
  138. labelRect: {x: 0, y: 0, width: 0, height: 0},
  139. bodyRect: {x: 0, y: 0, width: 0, height: 0}
  140. }
  141. },
  142. computed: {
  143. style () {
  144. return this.nodeStyle || {}
  145. },
  146. status () {
  147. return this.activity && (this.activity.status || '')
  148. },
  149. labelWrap () {
  150. let wrapThreshold = 8 // initial wrap threshold
  151. const opt = shapeOpts[this.style.shape] || shapeOpts.default
  152. return utils.textWrap(this.label, wrapThreshold, opt.textWrap)
  153. },
  154. labelProps () {
  155. return {
  156. x: 0,
  157. y: 0,
  158. fill: this.textColor,
  159. // transform: `translate(${-this.labelRect.width / 2},${-this.labelRect.height / 2})`
  160. transform: `translate(0,${-this.labelRect.height / 2})`
  161. }
  162. },
  163. bodyProps () {
  164. let width = this.labelRect.width + 46
  165. let height = Math.max(this.labelRect.height + 20, 60, this.inputs.length * 25)
  166. if (this.style.shape === 'circle') {
  167. width = height = Math.max(width, height)
  168. }
  169. const rect = {
  170. x: -width / 2,
  171. y: -height / 2,
  172. width: width,
  173. height: height,
  174. stroke: this.style.color || '#777',
  175. fill: this.style.color || '#777'
  176. }
  177. if (this.style.shape === 'circle') {
  178. rect.r = Math.max(width / 2, height / 2)
  179. }
  180. return rect
  181. },
  182. inputProps () {
  183. return (i) => {
  184. const {x, y} = this.inputPos(i)
  185. return {
  186. transform: `translate(${x} ${y})`
  187. }
  188. }
  189. },
  190. outputProps () {
  191. return (i) => {
  192. const {x, y} = this.outputPos(i)
  193. return {
  194. transform: `translate(${x} ${y})`,
  195. r: 5
  196. }
  197. }
  198. },
  199. inputLabel () {
  200. return (i) => {
  201. let input = ''
  202. if (this.inputs[i].name) {
  203. input += this.inputs[i].name + ':'
  204. }
  205. input += this.inputs[i].type
  206. return input
  207. }
  208. },
  209. outputLabel () {
  210. return (i) => {
  211. var output = ''
  212. if (this.output.name) {
  213. output += this.output.name + ':'
  214. }
  215. output += this.output.type
  216. return output
  217. }
  218. }
  219. },
  220. watch: {
  221. // the only thing now that affects geometry
  222. 'label' () {
  223. this.$nextTick(() => {
  224. this.labelRect = this.$refs.label.getBBox()
  225. })
  226. }
  227. },
  228. mounted () {
  229. // After render
  230. this.$nextTick(() => { // after mount we reupdate with new values
  231. if (!this.$refs.label) return
  232. this.labelRect = this.$refs.label.getBBox()
  233. this.$forceUpdate()
  234. })
  235. },
  236. methods: {
  237. inputPos (i) {
  238. const ilen = this.inputs.length
  239. if (ilen === 0) return {}
  240. const d = this.bodyProps.height / (ilen * 2)
  241. return {
  242. x: this.bodyProps.x + 7,
  243. y: this.bodyProps.y + d + (i * 2 * d)
  244. }
  245. },
  246. outputPos (i) {
  247. const rect = this.bodyProps
  248. return {
  249. x: rect.x + rect.width - 7,
  250. y: 0
  251. }
  252. },
  253. socketPointerDown (ev, socket) {
  254. this.$emit('socketPointerDown', ev, socket)
  255. }
  256. }
  257. }
  258. </script>
  259. <style>
  260. .flow-view:not(.activity) .flow-node:hover,
  261. .flow-node--dragging {
  262. cursor:move;
  263. }
  264. .flow-node__socket {
  265. pointer-events: none;
  266. stroke-width:1;
  267. opacity:0;
  268. transition: all var(--transition-speed);
  269. }
  270. .flow-node__body {
  271. opacity:0.9;
  272. transition: all var(--transition-speed);
  273. }
  274. .flow-node[status=running] .flow-node__body{
  275. stroke: yellow !important;
  276. }
  277. .flow-node[status=error] .flow-node__body{
  278. stroke: red !important;
  279. }
  280. .flow-node[status=finished] .flow-node__body{
  281. stroke: green !important;
  282. }
  283. .flow-view:not(.activity) .flow-node__socket:hover {
  284. stroke-width:10;
  285. cursor:pointer;
  286. }
  287. .flow-node__socket-detail {
  288. opacity:1;
  289. stroke-width:0 !important;
  290. fill: #fff !default;
  291. transition: all var(--transition-speed);
  292. }
  293. .flow-node__socket--match {
  294. cursor:pointer;
  295. stroke-width:10;
  296. }
  297. /*
  298. Override flow-node
  299. for hidden
  300. */
  301. .flow-linking .flow-node__socket {
  302. opacity:1;
  303. pointer-events: inherit;
  304. }
  305. .flow-linking .flow-node__socket--match,
  306. .flow-node__socket--match {
  307. opacity:1;
  308. pointer-events: inherit;
  309. }
  310. .flow-node__label {
  311. stroke:none;
  312. pointer-events:none;
  313. user-select:none;
  314. fill:#333 !default;
  315. }
  316. .flow-node .flow-node__selection {
  317. opacity:0;
  318. stroke-width:2;
  319. stroke: var(--primary-lighter);
  320. pointer-events:none;
  321. transition: all var(--transition-speed);
  322. }
  323. .flow-node--selected .flow-node__selection {
  324. opacity:0.9;
  325. }
  326. </style>