SwitchNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { Node } from '../core/Node.js';
  2. class SwitchNode extends Node {
  3. constructor( node, components ) {
  4. super();
  5. this.node = node;
  6. this.components = components || 'x';
  7. }
  8. getType( builder ) {
  9. return builder.getTypeFromLength( this.components.length );
  10. }
  11. generate( builder, output ) {
  12. const type = this.node.getType( builder ),
  13. inputLength = builder.getTypeLength( type ) - 1;
  14. let node = this.node.build( builder, type );
  15. if ( inputLength > 0 ) {
  16. // get max length
  17. let outputLength = 0;
  18. const components = builder.colorToVectorProperties( this.components );
  19. let i;
  20. const len = components.length;
  21. for ( i = 0; i < len; i ++ ) {
  22. outputLength = Math.max( outputLength, builder.getIndexByElement( components.charAt( i ) ) );
  23. }
  24. if ( outputLength > inputLength ) outputLength = inputLength;
  25. // split
  26. node += '.';
  27. for ( i = 0; i < len; i ++ ) {
  28. let idx = builder.getIndexByElement( components.charAt( i ) );
  29. if ( idx > outputLength ) idx = outputLength;
  30. node += builder.getElementByIndex( idx );
  31. }
  32. return builder.format( node, this.getType( builder ), output );
  33. } else {
  34. // join
  35. return builder.format( node, type, output );
  36. }
  37. }
  38. copy( source ) {
  39. super.copy( source );
  40. this.node = source.node;
  41. this.components = source.components;
  42. return this;
  43. }
  44. toJSON( meta ) {
  45. let data = this.getJSONNode( meta );
  46. if ( ! data ) {
  47. data = this.createJSONNode( meta );
  48. data.node = this.node.toJSON( meta ).uuid;
  49. data.components = this.components;
  50. }
  51. return data;
  52. }
  53. }
  54. SwitchNode.prototype.nodeType = 'Switch';
  55. export { SwitchNode };