node.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.type == 'socket-in' && (inp.type == match.dtype || match.dtype == '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.type =='socket-out' && (output.type == match.dtype || match.dtype == '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. <!-- TRIGGER SOCKETS -->
  104. <rect
  105. class="flow-node__trigger flow-node__socket--trigger"
  106. :class="{'flow-node__trigger--match': match.type == 'trigger-out'}"
  107. :data-nodeid="id"
  108. data-dir="in"
  109. :x="-5"
  110. :y="-bodyProps.height/2-5"
  111. width="10"
  112. height="10"
  113. @mousedown.stop.prevent="triggerPointerDown($event, 'in')"
  114. />
  115. <rect
  116. class="flow-node__trigger flow-node__socket--trigger"
  117. :class="{'flow-node__trigger--match': match.type == 'trigger-in'}"
  118. :data-nodeid="id"
  119. data-dir="out"
  120. :x="-5"
  121. :y="bodyProps.height/2 -5"
  122. width="10"
  123. height="10"
  124. @mousedown.stop.prevent="triggerPointerDown($event, 'out')"
  125. />
  126. <flow-node-activity
  127. v-if="activity"
  128. :activity="activity"
  129. :transform="'translate('+bodyProps.width/2 +','+ -bodyProps.height/2 +')'"/>
  130. </g>
  131. </template>
  132. <script>
  133. import FlowNodeActivity from './node-activity'
  134. import utils from '@/utils/utils'
  135. const shapeOpts = {
  136. 'circle': {
  137. textWrap: 'any'
  138. },
  139. default: {
  140. textWrap: 'white-space'
  141. }
  142. }
  143. export default {
  144. name: 'FlowNode',
  145. components: {FlowNodeActivity},
  146. props: {
  147. 'id': {type: String, required: true},
  148. 'label': {type: String, default: ''},
  149. 'inputs': {type: Array, default: () => []},
  150. 'output': {type: Object, default: ''},
  151. 'match': {type: Object, default: () => {}},
  152. 'dragging': {type: Boolean, default: false},
  153. 'selected': {type: Boolean, default: false},
  154. 'activity': {type: Object, default: () => {}},
  155. 'nodeStyle': {type: Object, default: () => {}}
  156. },
  157. data () {
  158. return {
  159. // maintain reference here?
  160. labelRect: {x: 0, y: 0, width: 0, height: 0}
  161. // bodyRect: {x: 0, y: 0, width: 0, height: 0}
  162. }
  163. },
  164. computed: {
  165. style () {
  166. return this.nodeStyle || {}
  167. },
  168. status () {
  169. return this.activity && (this.activity.status || '')
  170. },
  171. labelWrap () {
  172. let wrapThreshold = 8 // initial wrap threshold
  173. const opt = shapeOpts[this.style.shape] || shapeOpts.default
  174. return utils.textWrap(this.label, wrapThreshold, opt.textWrap)
  175. },
  176. labelProps () {
  177. return {
  178. x: 0,
  179. y: 0,
  180. fill: this.textColor,
  181. // transform: `translate(${-this.labelRect.width / 2},${-this.labelRect.height / 2})`
  182. transform: `translate(0,${-this.labelRect.height / 2})`
  183. }
  184. },
  185. bodyProps () {
  186. let width = this.labelRect.width + 46
  187. let height = Math.max(this.labelRect.height + 20, 60, this.inputs.length * 25)
  188. if (this.style.shape === 'circle') {
  189. width = height = Math.max(width, height)
  190. }
  191. const rect = {
  192. x: -width / 2,
  193. y: -height / 2,
  194. width: width,
  195. height: height,
  196. stroke: this.style.color || '#777',
  197. fill: this.style.color || '#777'
  198. }
  199. if (this.style.shape === 'circle') {
  200. rect.r = Math.max(width / 2, height / 2)
  201. }
  202. return rect
  203. },
  204. inputProps () {
  205. return (i) => {
  206. const {x, y} = this.inputPos(i)
  207. return {
  208. transform: `translate(${x} ${y})`
  209. }
  210. }
  211. },
  212. outputProps () {
  213. return (i) => {
  214. const {x, y} = this.outputPos(i)
  215. return {
  216. transform: `translate(${x} ${y})`,
  217. r: 5
  218. }
  219. }
  220. },
  221. inputLabel () {
  222. return (i) => {
  223. let input = ''
  224. if (this.inputs[i].name) {
  225. input += this.inputs[i].name + ':'
  226. }
  227. input += this.inputs[i].type
  228. return input
  229. }
  230. },
  231. outputLabel () {
  232. return (i) => {
  233. var output = ''
  234. if (this.output.name) {
  235. output += this.output.name + ':'
  236. }
  237. output += this.output.type
  238. return output
  239. }
  240. }
  241. },
  242. watch: {
  243. // the only thing now that affects geometry
  244. 'label' () {
  245. this.$nextTick(() => {
  246. this.labelRect = this.$refs.label.getBBox()
  247. })
  248. }
  249. },
  250. mounted () {
  251. // After render
  252. this.$nextTick(() => { // after mount we reupdate with new values
  253. if (!this.$refs.label) return
  254. this.labelRect = this.$refs.label.getBBox()
  255. this.$forceUpdate()
  256. })
  257. },
  258. methods: {
  259. inputPos (i) {
  260. const ilen = this.inputs.length
  261. if (ilen === 0) return {}
  262. const d = this.bodyProps.height / (ilen * 2)
  263. return {
  264. x: this.bodyProps.x + 7,
  265. y: this.bodyProps.y + d + (i * 2 * d)
  266. }
  267. },
  268. outputPos (i) {
  269. const rect = this.bodyProps
  270. return {
  271. x: rect.x + rect.width - 7,
  272. y: 0
  273. }
  274. },
  275. socketPointerDown (ev, socket) {
  276. this.$emit('socketPointerDown', ev, socket)
  277. },
  278. triggerPointerDown (ev, dir) {
  279. this.$emit('triggerPointerDown', ev, dir)
  280. }
  281. }
  282. }
  283. </script>
  284. <style>
  285. .flow-view:not(.activity) .flow-node:hover,
  286. .flow-node--dragging {
  287. cursor:move;
  288. }
  289. .flow-node__body {
  290. opacity:0.9;
  291. transition: all var(--transition-speed);
  292. }
  293. .flow-node[status=running] .flow-node__body{
  294. stroke: yellow !important;
  295. }
  296. .flow-node[status=error] .flow-node__body{
  297. stroke: red !important;
  298. }
  299. .flow-node[status=finished] .flow-node__body{
  300. stroke: green !important;
  301. }
  302. /* sockets */
  303. .flow-node__socket {
  304. pointer-events: none;
  305. stroke-width:1;
  306. opacity:0;
  307. transition: all var(--transition-speed);
  308. }
  309. .flow-view:not(.activity) .flow-node__socket:hover {
  310. stroke-width:10;
  311. cursor:pointer;
  312. }
  313. .flow-node__socket-detail {
  314. opacity:1;
  315. stroke-width:0 !important;
  316. fill: #fff !default;
  317. transition: all var(--transition-speed);
  318. }
  319. .flow-node__socket--match {
  320. cursor:pointer;
  321. stroke-width:10;
  322. }
  323. .flow-linking .flow-node__socket {
  324. opacity:1;
  325. pointer-events: inherit;
  326. }
  327. .flow-node__socket--match {
  328. opacity:1;
  329. pointer-events: inherit;
  330. }
  331. /* triggers */
  332. .flow-node__trigger {
  333. pointer-events:none;
  334. opacity:0;
  335. transition: all var(--transition-speed);
  336. }
  337. .flow-triggers .flow-node__trigger {
  338. pointer-events:inherit;
  339. opacity:1;
  340. }
  341. .flow-view:not(.activity) .flow-node__trigger:hover {
  342. stroke-width:10;
  343. cursor:pointer;
  344. }
  345. .flow-node__trigger--match {
  346. stroke-width:10;
  347. opacity:1;
  348. pointer-events: inherit;
  349. }
  350. /*
  351. Override flow-node
  352. for hidden
  353. */
  354. .flow-node__label {
  355. stroke:none;
  356. pointer-events:none;
  357. user-select:none;
  358. fill:#333 !default;
  359. }
  360. .flow-node .flow-node__selection {
  361. opacity:0;
  362. stroke-width:2;
  363. stroke: var(--primary-lighter);
  364. pointer-events:none;
  365. transition: all var(--transition-speed);
  366. }
  367. .flow-node--selected .flow-node__selection {
  368. opacity:0.9;
  369. }
  370. </style>