File size: 1,917 Bytes
6cd9596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * @author sunag / http://www.sunag.com.br/
 */

import { Node } from '../core/Node.js';

function SwitchNode( node, components ) {

	Node.call( this );

	this.node = node;
	this.components = components || 'x';

}

SwitchNode.prototype = Object.create( Node.prototype );
SwitchNode.prototype.constructor = SwitchNode;
SwitchNode.prototype.nodeType = "Switch";

SwitchNode.prototype.getType = function ( builder ) {

	return builder.getTypeFromLength( this.components.length );

};

SwitchNode.prototype.generate = function ( builder, output ) {

	var type = this.node.getType( builder ),
		node = this.node.build( builder, type ),
		inputLength = builder.getTypeLength( type ) - 1;

	if ( inputLength > 0 ) {

		// get max length

		var outputLength = 0,
			components = builder.colorToVectorProperties( this.components );

		var i, len = components.length;

		for ( i = 0; i < len; i ++ ) {

			outputLength = Math.max( outputLength, builder.getIndexByElement( components.charAt( i ) ) );

		}

		if ( outputLength > inputLength ) outputLength = inputLength;

		// split

		node += '.';

		for ( i = 0; i < len; i ++ ) {

			var elm = components.charAt( i );
			var idx = builder.getIndexByElement( components.charAt( i ) );

			if ( idx > outputLength ) idx = outputLength;

			node += builder.getElementByIndex( idx );

		}

		return builder.format( node, this.getType( builder ), output );

	} else {

		// join

		return builder.format( node, type, output );

	}

};

SwitchNode.prototype.copy = function ( source ) {

	Node.prototype.copy.call( this, source );

	this.node = source.node;
	this.components = source.components;

};

SwitchNode.prototype.toJSON = function ( meta ) {

	var data = this.getJSONNode( meta );

	if ( ! data ) {

		data = this.createJSONNode( meta );

		data.node = this.node.toJSON( meta ).uuid;
		data.components = this.components;

	}

	return data;

};

export { SwitchNode };