Project

General

Profile

1 42179 michele.ar
var wfGraphViewer = angular.module("wfGraphViewer", []);
2
3
wfGraphViewer.directive("wfGraphModal", function ($http) {
4
	return {
5
		restrict: 'E',
6
		replace:true,
7
		scope: {
8
	        'wfId'    : '=',
9
	        'visible' : '='
10
		},
11 46508 michele.ar
		templateUrl: '/html/wf/wf-graph-modal.html',
12 42179 michele.ar
		link: function (scope, element, attrs, ctrl) {
13 42192 michele.ar
			scope.width = 600;
14
			scope.height = 400;
15
16 42179 michele.ar
			scope.graph = {	nodes: [], arcs: [] };
17 42290 michele.ar
			scope.currentNode = {}
18
19 42179 michele.ar
			$(element).modal({'show': false});
20
21
			scope.$watch(function() {
22
				return scope.visible;
23
			}, function(value) {
24
				if (value == true && scope.wfId) {
25
					scope.graph = {	nodes: [], arcs: [] };
26
27
					$(element).modal('show');
28
					scope.visible = false;
29
30
					showSpinner();
31
					$http.get('wf/getGraph.do?wfId=' + scope.wfId).success(function (data) {
32
						scope.rearrangeNodes(data);
33
						scope.graph = data;
34 42300 michele.ar
						scope.currentNode = {};
35 42179 michele.ar
						hideSpinner();
36
					}).error(function (e) {
37
						show_notification('error', 'Error fetching graph: ' + e);
38
						hideSpinner();
39
					});
40
				}
41
			});
42
43 42290 michele.ar
			scope.showNodeInfo = function(node) {
44
				scope.currentNode = node;
45 42179 michele.ar
			};
46
47 42290 michele.ar
			scope.hideNodeInfo = function() {
48
				scope.currentNode = {};
49
			};
50
51 42179 michele.ar
			scope.rearrangeNodes = function(graph) {
52
				var nodeLevelSizes = [];
53 42192 michele.ar
				scope.width = 600;
54
				scope.height = 400;
55 42179 michele.ar
56
				angular.forEach(graph.nodes, function (node) {
57
					node.x = 0;
58
					node.y = 0;
59
				});
60
				angular.forEach(graph.nodes, function (node) {
61
					if (node.type == 'start') {
62
						scope.updatePosition(node, 0, graph, nodeLevelSizes);
63
					}
64
				});
65 42192 michele.ar
				angular.forEach(graph.nodes, function (node) {
66
					scope.width = Math.max(scope.width, node.x + 250);
67
					scope.height = Math.max(scope.height, node.y + 60);
68
				});
69 42179 michele.ar
			};
70
71
			scope.updatePosition = function(node, level, graph, nodeLevelSizes) {
72
73
				if (node.x == 0 && node.y == 0) {
74
					while (nodeLevelSizes.length <= level) {
75
						nodeLevelSizes.push(0);
76
					}
77
78
					node.x = (nodeLevelSizes[level] * 230) + 10;
79
					node.y = (level * 110) + 10;
80
81
					nodeLevelSizes[level]++;
82
83
					angular.forEach(graph.arcs, function (arc) {
84
							if (arc.from == node.name) {
85
86
								if (arc.from == arc.to) {
87
									arc.x1 = arc.x2 = node.x + 190;
88
									arc.y1 = arc.y2 = node.y + 25
89
								} else {
90
									angular.forEach(graph.nodes, function (other) {
91
										if (arc.to == other.name) {
92
											scope.updatePosition(other, level + 1, graph, nodeLevelSizes);
93
94
											var dx = node.x - other.x;
95
											var dy = node.y - other.y;
96
											// TODO : linee verticali che si sovrappongono (nodi che distano piu di un livello), linee che passano sotto ai nodi, linee che risalgono,
97
98
											if (dy < 0) {
99
												// linee con frecce in basso
100
101
												arc.x1 = node.x + 90;
102
												arc.y1 = node.y + 55;
103
104
												arc.x2 = other.x + 90;
105
												arc.y2 = other.y - 15;
106
107
												var step;
108
												if (dx > 0) {
109
													step = Math.min(80, 2 * Math.sqrt(dx));
110
												} else if (dx < 0) {
111
													step = Math.max(-80, -2 * Math.sqrt(-dx));
112
												} else {
113
													step = 0;
114
												}
115
												arc.x1 -= step;
116
												arc.x2 += step;
117
											} else if (dy > 0) {
118
												arc.x1 = node.x + 90;
119
												arc.y1 = node.y - 15;
120
121
												arc.x2 = other.x + 90;
122
												arc.y2 = other.y + 55;
123
124
												var step;
125
												if (dx > 0) {
126
													step = Math.min(80, 2 * Math.sqrt(dx));
127
												} else if (dx < 0) {
128
													step = Math.max(-80, -2 * Math.sqrt(-dx));
129
												} else {
130
													step = 0;
131
												}
132
												arc.x1 -= step;
133
												arc.x2 += step;
134
135
											} else {
136
												// linee orizzonatali
137
												arc.x1 = node.x;
138
												arc.x2 = other.x;
139
140
												arc.y1 = arc.y2 = node.y + 25 - (Math.min(20, Math.abs(dx / 230) - 1)) * 5;
141
142
												if (dx > 0) {
143
													arc.x1 -= 5;
144
													arc.x2 += 190;
145
												} else {
146
													arc.x1 += 190;
147
													arc.x2 -= 5;
148
												}
149
											}
150
										}
151
									});
152
								}
153
							}
154
						}
155
					);
156
				}
157
			};
158
		}
159
	}
160
});
161
162
163
wfGraphViewer.directive('wfGraphNode', function () {
164
	return {
165
		restrict: 'E',
166
		replace: true,
167
		templateNamespace: 'svg',
168 46508 michele.ar
		templateUrl: '/html/wf/wf-graph-node.html',
169 42179 michele.ar
		scope: {
170 42289 michele.ar
			name:      '@',
171
			type:      '@',
172
			classType: '@',
173
			x:         '@',
174
			y:         '@',
175
			funct:     '&'
176 42179 michele.ar
		}
177
	}
178
});
179
180
wfGraphViewer.directive('wfGraphArc', function () {
181
	return {
182
		restrict: 'E',
183
		replace: true,
184
		templateNamespace: 'svg',
185 46508 michele.ar
		templateUrl: '/html/wf/wf-graph-arc.html',
186 42179 michele.ar
		scope: {
187
			name: '@',
188
			from: '@',
189 42289 michele.ar
			to:   '@',
190
			x1:   '@',
191
			y1:   '@',
192
			x2:   '@',
193
			y2:   '@'
194 42179 michele.ar
		}
195
	}
196
});