SwitchNode.js 1.8 KB

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