SwitchNode.js 1.9 KB

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