BaseNode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { Node, ButtonInput, TitleElement, ContextMenu } from '../../libs/flow.module.js';
  2. export const onNodeValidElement = ( inputElement, outputElement ) => {
  3. const outputObject = outputElement.getObject();
  4. if ( ! outputObject || ! outputObject.isNode ) {
  5. return false;
  6. }
  7. };
  8. export class BaseNode extends Node {
  9. constructor( name, outputLength, value = null, width = 300 ) {
  10. super();
  11. const getObjectCallback = ( /*output = null*/ ) => {
  12. return this.value;
  13. };
  14. this.setWidth( width );
  15. const title = new TitleElement( name )
  16. .setObjectCallback( getObjectCallback )
  17. .setSerializable( false )
  18. .setOutput( outputLength );
  19. const closeButton = new ButtonInput().onClick( () => {
  20. context.open();
  21. } ).setIcon( 'ti ti-dots' );
  22. const context = new ContextMenu( this.dom );
  23. context.add( new ButtonInput( 'Remove' ).setIcon( 'ti ti-trash' ).onClick( () => {
  24. this.dispose();
  25. } ) );
  26. this.title = title;
  27. this.closeButton = closeButton;
  28. this.context = context;
  29. title.addButton( closeButton );
  30. this.add( title );
  31. this.setOutputColor( this.getColorValueFromValue( value ) );
  32. this.editor = null;
  33. this.value = value;
  34. this.onValidElement = onNodeValidElement;
  35. }
  36. getColor() {
  37. return ( this.getColorValueFromValue( this.value ) || '#777777' ) + 'BB';
  38. }
  39. serialize( data ) {
  40. super.serialize( data );
  41. delete data.width;
  42. }
  43. deserialize( data ) {
  44. delete data.width;
  45. super.deserialize( data );
  46. }
  47. setEditor( value ) {
  48. this.editor = value;
  49. return this;
  50. }
  51. getColorValueFromValue( value ) {
  52. if ( ! value ) return;
  53. if ( value.isMaterial === true ) {
  54. //return 'forestgreen';
  55. return '#228b22';
  56. } else if ( value.isObject3D === true ) {
  57. return '#ffa500';
  58. } else if ( value.isDataFile === true ) {
  59. return '#00ffff';
  60. }
  61. }
  62. add( element ) {
  63. element.onValid( ( source, target ) => this.onValidElement( source, target ) );
  64. return super.add( element );
  65. }
  66. setName( value ) {
  67. this.title.setTitle( value );
  68. return this;
  69. }
  70. getName() {
  71. return this.title.getTitle();
  72. }
  73. setObjectCallback( callback ) {
  74. this.title.setObjectCallback( callback );
  75. return this;
  76. }
  77. getObject( callback ) {
  78. return this.title.getObject( callback );
  79. }
  80. setColor( color ) {
  81. this.title.setColor( color );
  82. return this;
  83. }
  84. setOutputLength( length ) {
  85. this.title.setOutput( length );
  86. return this;
  87. }
  88. setOutputColor( color ) {
  89. this.title.setOutputColor( color );
  90. return this;
  91. }
  92. invalidate() {
  93. this.title.dispatchEvent( new Event( 'connect' ) );
  94. }
  95. dispose() {
  96. this.context.hide();
  97. super.dispose();
  98. }
  99. }