node.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <g
  3. class="flow-node"
  4. :class="{'flow-node--dragging':dragging}"
  5. @mousedown="$emit('nodePointerDown',$event)"
  6. @dblclick="$emit('nodeDoubleClick',$event)"
  7. >
  8. <rect
  9. ref="body"
  10. class="flow-node__body"
  11. :class="{'flow-node__body--dragging':dragging}"
  12. v-bind="bodyProps"
  13. />
  14. <text
  15. ref="label"
  16. class="flow-node__label"
  17. v-bind="labelProps">
  18. <tspan x="0" dy="1em" v-for="s in labelWrap">
  19. {{ s }}
  20. </tspan>
  21. <!--{{ label }}-->
  22. </text>
  23. <!-- input -->
  24. <circle
  25. class="flow-node__socket flow-node__socket--inputs"
  26. v-for="(inp,i) in inputs"
  27. v-bind="inputProps(i)"
  28. :data-nodeid="id"
  29. :data-in="i"
  30. :class="{'flow-node__socket--match': match.in && (inp == match.in || match.in == 'any' || input=='any')}"
  31. :key="'in'+i"
  32. @mousedown.stop.prevent="socketPointerDown($event, {in:i})"
  33. />
  34. <!-- output -->
  35. <circle
  36. v-if="output"
  37. class="flow-node__socket flow-node__socket--outputs"
  38. :data-nodeid="id"
  39. :data-out="0"
  40. :key="'out'+0"
  41. :class="{ 'flow-node__socket--match': match.out && (output == match.out || match.out == 'any' || output == 'any')}"
  42. v-bind="outputProps(0)"
  43. @mousedown.stop.prevent="socketPointerDown($event, {out:0})"
  44. />
  45. <text
  46. class="flow-node__socket-detail"
  47. v-for="(inp,i) in inputs"
  48. text-anchor="end"
  49. :x="inputPos(i).x - 10"
  50. :y="inputPos(i).y+5"
  51. >{{ inp }}</text>
  52. <text
  53. class="flow-node__socket-detail"
  54. :x="outputPos(0).x + 10"
  55. :y="outputPos(0).y+5">
  56. {{ output }}
  57. </text>
  58. </g>
  59. </template>
  60. <script>
  61. export default {
  62. name: 'FlowNode',
  63. props: {
  64. 'id': {type: String, required: true},
  65. 'label': {type: String, default: ''},
  66. 'inputs': {type: Array, default: () => []},
  67. 'output': {type: String, default: ''},
  68. 'match': {type: Object, default: () => {}},
  69. 'dragging': {type: Boolean, default: false},
  70. 'nodeStyle': {type: Object, default: () => {}}
  71. /* 'type': {type: String, default: ''},
  72. 'color': {type: String, default: '#777'},
  73. 'textColor': {type: String, default: '#fff'} */
  74. // 'value': {type: Object, default: () => {}}
  75. },
  76. data () {
  77. return {
  78. // maintain reference here?
  79. labelRect: {x: 0, y: 0, width: 0, height: 0},
  80. bodyRect: {x: 0, y: 0, width: 0, height: 0}
  81. }
  82. },
  83. computed: {
  84. labelWrap () {
  85. var ret = []
  86. const parts = this.label.split(' ', -1)
  87. let wrapThreshold = 10 // initial wrap threshold
  88. parts.forEach(n => {
  89. wrapThreshold = Math.max(n.length + 1, wrapThreshold)
  90. })
  91. ret.push(parts[0])
  92. for (let i = 1; i < parts.length; i++) {
  93. let ri = ret.length - 1 // last
  94. if (ret[ret.length - 1].length + parts[i].length > wrapThreshold) {
  95. ret.push(parts[i])
  96. } else {
  97. // we can add to same
  98. ret[ri] += ' ' + parts[i]
  99. }
  100. }
  101. return ret
  102. },
  103. labelProps () {
  104. return {
  105. x: 0,
  106. y: -0.2 + 'em',
  107. fill: this.textColor,
  108. transform: `translate(${-this.labelRect.width / 2},${-this.labelRect.height / 2})`
  109. }
  110. },
  111. bodyProps () {
  112. let width = this.labelRect.width + 30
  113. let height = Math.max(this.labelRect.height + 20, 60)
  114. if (this.nodeStyle.type === 'circle') {
  115. height = Math.max(width, height)
  116. width = height
  117. }
  118. const rect = {
  119. x: -width / 2,
  120. y: -height / 2,
  121. width: width,
  122. height: height,
  123. stroke: this.nodeStyle.color,
  124. fill: this.nodeStyle.color
  125. }
  126. if (this.nodeStyle && this.nodeStyle.type === 'circle') {
  127. rect.rx = width
  128. rect.ry = width
  129. }
  130. return rect
  131. },
  132. inputProps () {
  133. return (i) => {
  134. const {x, y} = this.inputPos(i)
  135. return {
  136. transform: `translate(${x} ${y})`,
  137. r: 5
  138. }
  139. }
  140. },
  141. outputProps () {
  142. return (i) => {
  143. const {x, y} = this.outputPos(i)
  144. return {
  145. transform: `translate(${x} ${y})`,
  146. r: 5
  147. }
  148. }
  149. }
  150. },
  151. watch: {
  152. // the only thing now that affects geometry
  153. 'label' () {
  154. this.$nextTick(() => {
  155. this.labelRect = this.$refs.label.getBBox()
  156. })
  157. }
  158. },
  159. mounted () {
  160. // After render
  161. this.$nextTick(() => { // after mount we reupdate with new values
  162. if (!this.$refs.label) return
  163. this.labelRect = this.$refs.label.getBBox()
  164. this.$forceUpdate()
  165. })
  166. },
  167. methods: {
  168. inputPos (i) {
  169. const ilen = this.inputs.length
  170. if (ilen === 0) return {}
  171. const d = this.bodyProps.height / (ilen * 2)
  172. return {
  173. x: this.bodyProps.x + 7,
  174. y: this.bodyProps.y + d + (i * 2 * d)
  175. }
  176. },
  177. outputPos (i) {
  178. const rect = this.bodyProps
  179. return {
  180. x: rect.x + rect.width - 7,
  181. y: 0
  182. }
  183. },
  184. socketPointerDown (ev, socket) {
  185. this.$emit('socketPointerDown', ev, socket)
  186. }
  187. }
  188. }
  189. </script>
  190. <style>
  191. .flow-view:not(.activity) .flow-node:hover,
  192. .flow-node--dragging {
  193. cursor:move;
  194. }
  195. .flow-node__socket {
  196. pointer-events: none;
  197. stroke-width:1;
  198. opacity:0;
  199. transition: all .3s;
  200. }
  201. .flow-node__body {
  202. transition: all .3s;
  203. }
  204. .flow-view:not(.activity) .flow-node__socket:hover {
  205. stroke-width:10;
  206. cursor:pointer;
  207. }
  208. .flow-node__socket--match {
  209. cursor:pointer;
  210. stroke-width:10;
  211. }
  212. /*
  213. Override flow-node
  214. for hidden
  215. */
  216. .flow-linking .flow-node__socket {
  217. opacity:1;
  218. pointer-events: initial;
  219. }
  220. .flow-linking .flow-node__socket--match,
  221. .flow-node__socket--match {
  222. opacity:1;
  223. pointer-events: initial;
  224. }
  225. .flow-node__label {
  226. stroke:none;
  227. pointer-events:none;
  228. user-select:none;
  229. fill:#333;
  230. }
  231. .flow-node__socket-detail {
  232. pointer-events:none;
  233. opacity:0;
  234. }
  235. .flow-view.flow-detail .flow-node__socket-detail {
  236. opacity:1;
  237. filter: url(#solid) /* doubt */
  238. }
  239. </style>