node.vue 10 KB

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